libsqsh v1.5.2
Loading...
Searching...
No Matches
traverse_dir_ll.c
Go to the documentation of this file.
1
9#include <sqsh.h>
10#include <stdio.h>
11
12int
13main(int argc, char *argv[]) {
14 int error_code = 0;
15 if (argc != 2) {
16 printf("Usage: %s <sqsh-file>\n", argv[0]);
17 return 1;
18 }
19 struct SqshConfig config = {
20 // Read the header file to find documentation on these fields.
21 // It's safe to set them all to 0.
23 .source_size = 0,
24 .mapper_block_size = 0,
25 .mapper_lru_size = 0,
26 .metablock_lru_size = 0,
27 .data_lru_size = 0,
28 .archive_offset = 0,
29 .max_symlink_depth = 0,
30 };
31 struct SqshArchive *archive =
32 sqsh_archive_open(argv[1], &config, &error_code);
33 if (error_code != 0) {
34 sqsh_perror(error_code, "sqsh_archive_new");
35 return 1;
36 }
37 const struct SqshSuperblock *superblock = sqsh_archive_superblock(archive);
38 uint64_t inode_root_ref = sqsh_superblock_inode_root_ref(superblock);
39 struct SqshFile *file =
40 sqsh_open_by_ref(archive, inode_root_ref, &error_code);
41 if (error_code != 0) {
42 sqsh_perror(error_code, "sqsh_file_new");
43 return 1;
44 }
45
46 struct SqshTreeTraversal *traversal =
47 sqsh_tree_traversal_new(file, &error_code);
48 if (error_code != 0) {
49 sqsh_perror(error_code, "sqsh_directory_iterator_new");
50 return 1;
51 }
52
53 while (sqsh_tree_traversal_next(traversal, &error_code)) {
54 enum SqshTreeTraversalState state =
57 continue;
58 }
59 size_t depth = sqsh_tree_traversal_depth(traversal);
60 size_t segment_size;
61 const char *segment;
62 for (sqsh_index_t i = 0; i < depth; i++) {
64 traversal, &segment_size, i);
65 fputc('/', stdout);
66 fwrite(segment, 1, segment_size, stdout);
67 }
68 fputc('\n', stdout);
69 }
70 if (error_code < 0) {
71 sqsh_perror(error_code, "sqsh_directory_iterator_next");
72 return 1;
73 }
74
75 sqsh_tree_traversal_free(traversal);
76 sqsh_close(file);
77 sqsh_archive_close(archive);
78 return 0;
79}
size_t sqsh_index_t
typedef used for indexing
Definition sqsh_common.h:69
void sqsh_perror(int error_code, const char *msg)
Print the error message for the given error code.
const struct SqshMemoryMapperImpl *const sqsh_mapper_impl_mmap
a mapper that uses mmap to map the file into memory.
SqshTreeTraversalState
The state of the tree traversal.
Definition sqsh_tree.h:455
@ SQSH_TREE_TRAVERSAL_STATE_DIRECTORY_END
Definition sqsh_tree.h:478
SQSH_NO_UNUSED struct SqshArchive * sqsh_archive_open(const void *source, const struct SqshConfig *config, int *err)
initializes a archive context in heap.
The SqshConfig struct contains all the configuration options for a sqsh session.
const struct SqshMemoryMapperImpl * source_mapper
source_mapper is the memory mapper implementation that will be used to map the archive.
The Inode context.
SQSH_NO_UNUSED struct SqshFile * sqsh_open_by_ref(struct SqshArchive *archive, uint64_t inode_ref, int *err)
Initializes a file context in heap.
The superblock context is used to access the superblock of the archive.
uint64_t sqsh_superblock_inode_root_ref(const struct SqshSuperblock *context)
Retrieves the reference of the root inode in a superblock context.
A walker over the contents of a file.
enum SqshTreeTraversalState sqsh_tree_traversal_state(const struct SqshTreeTraversal *traversal)
returns the state of the traversal.
SQSH_NO_UNUSED bool sqsh_tree_traversal_next(struct SqshTreeTraversal *traversal, int *err)
Moves the traversal to the next entry int the current directory.
int sqsh_tree_traversal_free(struct SqshTreeTraversal *traversal)
struct SqshTreeTraversal * sqsh_tree_traversal_new(const struct SqshFile *file, int *err)
Creates a new SqshTreeTraversal object rooted at the specified inode.
const char * sqsh_tree_traversal_path_segment(const struct SqshTreeTraversal *traversal, size_t *len, sqsh_index_t index)
Get a segment of the path of the current entry.
size_t sqsh_tree_traversal_depth(const struct SqshTreeTraversal *traversal)
Returns the path segment at a given index.
int main(int argc, char *argv[])