Read directory with libarchive

1.5k Views Asked by At

I've got a C++ program and I desire to read an archive of some description from the disk. I want to reconstruct it in a roughly tree-like form to mirror the structure on the disk (funky stuff like symlinks/hardlinks not supported).

When reading the next header, it's clear that you can check the entry type to see if it is a directory or a file. What's not clear is what happens next if it's a directory. Does archive_read_next_header automatically perform a full tree traversal in BFS/DFS fashion? Or am I supposed to recursively create new archive_entry structures and call some function I didn't find yet to traverse directory entries?

1

There are 1 best solutions below

0
On BEST ANSWER

Generally speaking archive files are a flat sequence of file and directory entries. The archive formats generally don't specify a required order. Some archive formats implicitly require that directory entries be present, but some don't require them at all. In the former case the implicit requirement comes from archive utilities that will complain when extracting a file if the directory a file is supposed to be in doesn't exist. That means there either needs to be an entry for that directory anywhere earlier in the archive to create it or it needs to exist before utility is run. Other archive utilities, like tar or zip, will simply create the missing directories.

In other words directory entries only represent the directory itself, not their contents. In the general case you should be prepared to create directory nodes in your tree as you discover them in the names of file and directory entries. You'll need to walk the tree to find the node each file should be attached to.