-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add minimal examples for listing files
- Loading branch information
Showing
8 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @author Enno Boland (mail@eboland.de) | ||
* @file list_files.c | ||
* | ||
* This is an example program that lists the top level files in a squashfs | ||
* archive. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <sqsh.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int | ||
main(int argc, char *argv[]) { | ||
if (argc != 2) { | ||
printf("Usage: %s <sqsh-file>\n", argv[0]); | ||
return 1; | ||
} | ||
struct SqshArchive *archive = sqsh_archive_open(argv[1], NULL); | ||
assert(archive != NULL); | ||
struct SqshInode *inode = sqsh_open(archive, "/", NULL); | ||
assert(inode != NULL); | ||
struct SqshDirectoryIterator *iterator = | ||
sqsh_directory_iterator_new(inode, NULL); | ||
assert(iterator != NULL); | ||
|
||
while (sqsh_directory_iterator_next(iterator) > 0) { | ||
/* Use the _dup() variant here because the _name() variant is not | ||
* null-terminated. | ||
*/ | ||
char *name = sqsh_directory_iterator_name_dup(iterator); | ||
puts(name); | ||
free(name); | ||
} | ||
|
||
sqsh_directory_iterator_free(iterator); | ||
sqsh_close(inode); | ||
sqsh_archive_close(archive); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @author Enno Boland (mail@eboland.de) | ||
* @file list_files_ll.c | ||
* | ||
* This is an example program that lists the top level files in a squashfs | ||
* archive. It uses low level variants of the API. | ||
*/ | ||
|
||
#include <sqsh.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int | ||
main(int argc, char *argv[]) { | ||
int error_code = 0; | ||
if (argc != 2) { | ||
printf("Usage: %s <sqsh-file>\n", argv[0]); | ||
return 1; | ||
} | ||
struct SqshConfig config = { | ||
// Read the header file to find documentation on these fields. | ||
// It's safe to set them all to 0. | ||
.source_mapper = sqsh_mapper_impl_mmap, | ||
.source_size = 0, | ||
.mapper_block_size = 0, | ||
.mapper_lru_size = 0, | ||
.compression_lru_size = 0, | ||
}; | ||
struct SqshArchive *archive = | ||
sqsh_archive_new(argv[1], &config, &error_code); | ||
if (error_code != 0) { | ||
sqsh_perror(error_code, "sqsh_archive_new"); | ||
return 1; | ||
} | ||
const struct SqshSuperblock *superblock = sqsh_archive_superblock(archive); | ||
uint64_t inode_root_ref = sqsh_superblock_inode_root_ref(superblock); | ||
struct SqshInode *inode = | ||
sqsh_inode_new(archive, inode_root_ref, &error_code); | ||
if (error_code != 0) { | ||
sqsh_perror(error_code, "sqsh_inode_new"); | ||
return 1; | ||
} | ||
|
||
struct SqshDirectoryIterator *iterator = | ||
sqsh_directory_iterator_new(inode, &error_code); | ||
if (error_code != 0) { | ||
sqsh_perror(error_code, "sqsh_directory_iterator_new"); | ||
return 1; | ||
} | ||
|
||
while (sqsh_directory_iterator_next(iterator) > 0) { | ||
const char *name = sqsh_directory_iterator_name(iterator); | ||
size_t size = sqsh_directory_iterator_name_size(iterator); | ||
fwrite(name, size, 1, stdout); | ||
fputc('\n', stdout); | ||
} | ||
|
||
sqsh_directory_iterator_free(iterator); | ||
sqsh_inode_free(inode); | ||
sqsh_archive_close(archive); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
executable( | ||
'list_files', | ||
'list_files.c', | ||
install: false, | ||
dependencies: libsqsh_dep, | ||
) | ||
executable( | ||
'list_files_ll', | ||
'list_files_ll.c', | ||
install: false, | ||
dependencies: libsqsh_dep, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters