Understanding the arguments to archive_read_disk_entry_from_file

34 views
Skip to first unread message

omgal...@gmail.com

unread,
Aug 27, 2018, 4:02:19 PM8/27/18
to libarchive-discuss
Hi all,

I have a fairly simple use case where I want to create an archive whose contents come from files in a disk. I saw that the man page for archive_read_disk_entry_from_file shows the following:

void
file_to_archive
(struct archive *a, const char *name)
{
 
char buff[8192];
 size_t bytes_read
;
 
struct archive *ard;
 
struct archive_entry *entry;
 
int fd;

 ard
= archive_read_disk_new();
 archive_read_disk_set_standard_lookup
(ard);
 entry
= archive_entry_new();
 fd
= open(name, O_RDONLY);
 
if (fd < 0)
   
return;
 archive_entry_copy_pathname
(entry, name);
 archive_read_disk_entry_from_file
(ard, entry, fd, NULL);
 archive_write_header
(a, entry);
 
while ((bytes_read = read(fd, buff, sizeof(buff))) > 0)
   archive_write_data
(a, buff, bytes_read);
 archive_write_finish_entry
(a);
 archive_read_free
(ard);
 archive_entry_free
(entry);
}

What's the purpose of the first struct archive* argument of archive_read_disk_entry_from_file? It doesn't seem to act like a handle for the disk file, since we're manually copying its path to the entry's pathname.

Tim Kientzle

unread,
Sep 1, 2018, 7:21:40 PM9/1/18
to omgal...@gmail.com, libarchiv...@googlegroups.com
`archive_read_disk_new()` creates an object that knows how to read information from the file system.

You can use it in two ways:

* You can attach it to a directory and use it to iterate over all the items in the directory. In essence, this treats a directory tree as an archive in it's own right. For example, bsdtar makes heavy use of this.

* In this situation, it's being used merely as a helper to look up information about the files on disk. Generally, you would create one of these objects and reuse it for many entries -- it builds up some internal caches of filesystem details that can help speed up certain operations.

Tim
> --
> You received this message because you are subscribed to the Google Groups "libarchive-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to libarchive-disc...@googlegroups.com.
> To post to this group, send email to libarchiv...@googlegroups.com.
> Visit this group at https://groups.google.com/group/libarchive-discuss.
> For more options, visit https://groups.google.com/d/optout.

Martin Galvan

unread,
Sep 8, 2018, 11:17:01 PM9/8/18
to Tim Kientzle, libarchiv...@googlegroups.com
El sáb., 1 sept. 2018 a las 20:21, Tim Kientzle (<t...@kientzle.com>) escribió:
> * You can attach it to a directory and use it to iterate over all the items in the directory. In essence, this treats a directory tree as an archive in it's own right. For example, bsdtar makes heavy use of this.

Thanks for the answer. If I understood correctly, here the first
struct archive* acts as a sort of handle for a directory, right?
Reply all
Reply to author
Forward
0 new messages