libsquashfs: list_files.c

libsquashfs 1.3.2 A new set of tools and libraries for working with SquashFS images

Loading...
Searching...
No Matches
list_files.c
/* SPDX-License-Identifier: 0BSD */
/*
* list_files.c
*
* Copyright (C) 2020 David Oberhollenzer <goliath@infraroot.at>
*/
#include "sqfs/compressor.h"
#include "sqfs/dir_reader.h"
#include "sqfs/id_table.h"
#include "sqfs/inode.h"
#include "sqfs/super.h"
#include "sqfs/io.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
static void write_tree_dfs(const sqfs_tree_node_t *n)
{
const sqfs_tree_node_t *p;
unsigned int mask, level;
int i;
for (n = n->children; n != NULL; n = n->next) {
level = 0;
mask = 0;
for (p = n->parent; p->parent != NULL; p = p->parent) {
if (p->next != NULL)
mask |= 1 << level;
++level;
}
for (i = level - 1; i >= 0; --i)
fputs(mask & (1 << i) ? "│ " : " ", stdout);
fputs(n->next == NULL ? "└─ " : "├─ ", stdout);
fputs((const char *)n->name, stdout);
if (n->inode->base. type == SQFS_INODE_SLINK) {
printf(" ⭢ %.*s",
(int)n->inode->data.slink. target_size,
(const char *)n->inode->extra);
} else if (n->inode->base.type == SQFS_INODE_EXT_SLINK) {
printf(" ⭢ %.*s",
(int)n->inode->data.slink_ext. target_size,
(const char *)n->inode->extra);
}
fputc('\n', stdout);
write_tree_dfs(n);
}
}
int main(int argc, char **argv)
{
int ret, status = EXIT_FAILURE;
sqfs_compressor_config_t cfg;
sqfs_compressor_t *cmp;
sqfs_tree_node_t *root = NULL;
sqfs_id_table_t *idtbl;
sqfs_dir_reader_t *dr;
sqfs_file_t *file;
sqfs_super_t super;
/* open the SquashFS file we want to read */
if (argc != 2) {
fputs("Usage: list_files <squashfs-file>\n", stderr);
return EXIT_FAILURE;
}
file = sqfs_open_file(argv[1], SQFS_FILE_OPEN_READ_ONLY);
if (file == NULL) {
perror(argv[1]);
return EXIT_FAILURE;
}
/* read the super block, create a compressor and
process the compressor options */
if (sqfs_super_read(&super, file)) {
fprintf(stderr, "%s: error reading super block.\n", argv[1]);
goto out_fd;
}
sqfs_compressor_config_init(&cfg, super. compression_id,
super. block_size,
SQFS_COMP_FLAG_UNCOMPRESS);
ret = sqfs_compressor_create(&cfg, &cmp);
if (ret != 0) {
fprintf(stderr, "%s: error creating compressor: %d.\n",
argv[1], ret);
goto out_fd;
}
/* Create and read the UID/GID mapping table */
idtbl = sqfs_id_table_create(0);
if (idtbl == NULL) {
fputs("Error creating ID table.\n", stderr);
goto out_cmp;
}
if (sqfs_id_table_read(idtbl, file, &super, cmp)) {
fprintf(stderr, "%s: error loading ID table.\n", argv[1]);
goto out_id;
}
/* create a directory reader and scan the entire directory hiearchy */
dr = sqfs_dir_reader_create(&super, cmp, file, 0);
if (dr == NULL) {
fprintf(stderr, "%s: error creating directory reader.\n",
argv[1]);
goto out_id;
}
if (sqfs_dir_reader_get_full_hierarchy(dr, idtbl, NULL, 0, &root)) {
fprintf(stderr, "%s: error loading directory tree.\n",
argv[1]);
goto out;
}
/* fancy print the hierarchy */
printf("/\n");
write_tree_dfs(root);
/* cleanup */
status = EXIT_SUCCESS;
out:
if (root != NULL)
sqfs_dir_tree_destroy(root);
sqfs_destroy(dr);
out_id:
sqfs_destroy(idtbl);
out_cmp:
sqfs_destroy(cmp);
out_fd:
sqfs_destroy(file);
return status;
}
compressor.h
Contains declarations to everything related to data compression.
SQFS_COMP_FLAG_UNCOMPRESS
@ SQFS_COMP_FLAG_UNCOMPRESS
Set this if the compressor should actually extract instead of compress data.
Definition compressor.h:312
sqfs_compressor_config_init
SQFS_API int sqfs_compressor_config_init(sqfs_compressor_config_t *cfg, SQFS_COMPRESSOR id, size_t block_size, sqfs_u16 flags)
Initialize a compressor configuration.
sqfs_compressor_create
SQFS_API int sqfs_compressor_create(const sqfs_compressor_config_t *cfg, sqfs_compressor_t **out)
Create an instance of a compressor implementation.
dir_reader.h
Contains declarations for the sqfs_dir_reader_t.
sqfs_dir_tree_destroy
SQFS_API void sqfs_dir_tree_destroy(sqfs_tree_node_t *root)
Recursively destroy a tree of sqfs_tree_node_t nodes.
id_table.h
Contains declarations for the sqfs_id_table_t data structure.
inode.h
Contains on-disk data structures used for inodes.
io.h
Contains the sqfs_file_t interface for abstracting file I/O.
SQFS_FILE_OPEN_READ_ONLY
@ SQFS_FILE_OPEN_READ_ONLY
If set, access the file for reading only.
Definition io.h:46
sqfs_open_file
SQFS_API sqfs_file_t * sqfs_open_file(const char *filename, sqfs_u32 flags)
Open a file through the operating systems filesystem API.
sqfs_compressor_config_t
Configuration parameters for instantiating a compressor backend.
Definition compressor.h:106
sqfs_compressor_t
Encapsultes a compressor with a simple interface to compress or extract chunks of data.
Definition compressor.h:40
sqfs_dir_reader_t
Abstracts reading of directory entries.
sqfs_file_t
Abstracts file I/O to make it easy to embedd SquashFS.
Definition io.h:94
sqfs_id_table_t
A simple data structure that encapsulates ID to index mapping for user and group IDs.
sqfs_inode_generic_t::base
sqfs_inode_t base
The common fields for all inodes.
Definition inode.h:492
sqfs_inode_generic_t::data
union sqfs_inode_generic_t::@354352226225362213300170005174067011053350221245 data
Type specific inode data.
sqfs_inode_generic_t::extra
sqfs_u32 extra[]
Holds type specific extra data, such as symlink target.
Definition inode.h:535
sqfs_inode_slink_ext_t::target_size
sqfs_u32 target_size
Size of the symlink target in bytes.
Definition inode.h:285
sqfs_inode_slink_t::target_size
sqfs_u32 target_size
Size of the symlink target in bytes.
Definition inode.h:262
sqfs_inode_t::type
sqfs_u16 type
An SQFS_INODE_TYPE value.
Definition inode.h:137
sqfs_super_t
The SquashFS super block, located at the beginning of the file system to describe the layout of the f...
Definition super.h:47
sqfs_super_t::compression_id
sqfs_u16 compression_id
Identifies the compressor that has been used.
Definition super.h:84
sqfs_super_t::block_size
sqfs_u32 block_size
The data block size in bytes.
Definition super.h:72
sqfs_tree_node_t
Encapsulates a node in the filesystem tree read by sqfs_dir_reader_get_full_hierarchy.
Definition dir_reader.h:114
sqfs_tree_node_t::parent
sqfs_tree_node_t * parent
Pointer to parent, NULL for the root node.
Definition dir_reader.h:118
sqfs_tree_node_t::children
sqfs_tree_node_t * children
For directories, a linked list of children.
Definition dir_reader.h:123
sqfs_tree_node_t::inode
sqfs_inode_generic_t * inode
Inode representing this element in the tree.
Definition dir_reader.h:133
sqfs_tree_node_t::name
sqfs_u8 name[]
null-terminated entry name.
Definition dir_reader.h:148
sqfs_tree_node_t::next
sqfs_tree_node_t * next
Linked list next pointer for children list.
Definition dir_reader.h:128
super.h
Contains on-disk data structures, identifiers and functions for the SquashFS super block.