-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
Hello, thanks for this useful library.
I am trying to extract a file but I am getting This doesn't look like a squashfs image.
/*
Compile with:
gcc -o extract_diricon extract_diricon.c -lsquash -I../include -L. -lz
mkdir -p test
echo "Test" >> ./test/.DirIcon
mksquashfs ./test/ test.sfs -comp lzma
./extract_diricon -o 0 test.sfs
This doesn't look like a squashfs image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <squash.h>
void usage(char *progname) {
fprintf(stderr, "Usage: %s [-o offset] squashfs_file\n", progname);
exit(1);
}
int main(int argc, char **argv) {
int opt;
char *squashfs_file = NULL;
off_t offset = 0;
sqfs fs;
struct stat buf;
int vfd;
char *buf2;
size_t size;
while ((opt = getopt(argc, argv, "o:")) != -1) {
switch (opt) {
case 'o':
offset = strtoll(optarg, NULL, 0);
break;
default:
usage(argv[0]);
}
}
if (optind >= argc) {
usage(argv[0]);
}
squashfs_file = argv[optind];
memset(&fs, 0, sizeof(sqfs));
if (sqfs_open_image(&fs, squashfs_file, offset) != 0) {
fprintf(stderr, "Failed to open %s: %s\n", squashfs_file, strerror(errno));
return 1;
}
if (squash_lstat(&fs, "/.DirIcon", &buf) < 0) {
fprintf(stderr, "Failed to stat /.DirIcon: %s\n", strerror(errno));
sqfs_destroy(&fs);
return 1;
}
if (S_ISLNK(buf.st_mode)) {
char link[1024];
size_t link_len = squash_readlink(&fs, "/.DirIcon", link, sizeof(link) - 1);
if (link_len < 0) {
fprintf(stderr, "Failed to readlink /.DirIcon: %s\n", strerror(errno));
sqfs_destroy(&fs);
return 1;
}
link[link_len] = '\0';
if (squash_lstat(&fs, link, &buf) < 0) {
fprintf(stderr, "Failed to stat %s: %s\n", link, strerror(errno));
sqfs_destroy(&fs);
return 1;
}
}
vfd = squash_open(&fs, "/.DirIcon");
if (vfd < 0) {
fprintf(stderr, "Failed to open /.DirIcon: %s\n", strerror(errno));
sqfs_destroy(&fs);
return 1;
}
size = squash_lseek(vfd, 0, SQUASH_SEEK_END);
if (size <= 0) {
fprintf(stderr, "Invalid file size for /.DirIcon\n");
squash_close(vfd);
sqfs_destroy(&fs);
return 1;
}
if (squash_lseek(vfd, 0, SQUASH_SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to the beginning of /.DirIcon: %s\n", strerror(errno));
squash_close(vfd);
sqfs_destroy(&fs);
return 1;
}
buf2 = malloc(size);
if (!buf2) {
fprintf(stderr, "Failed to allocate memory for /.DirIcon\n");
squash_close(vfd);
sqfs_destroy(&fs);
return 1;
}
ssize_t bytes_read = 0;
size_t total_read = 0;
while (total_read < size) {
bytes_read = squash_read(vfd, buf2 + total_read, size - total_read);
if (bytes_read < 0) {
fprintf(stderr, "Failed to read /.DirIcon: %s\n", strerror(errno));
free(buf2);
squash_close(vfd);
sqfs_destroy(&fs);
return 1;
}
if (bytes_read == 0) {
break; // End of file
}
total_read += bytes_read;
}
if (total_read != size) {
fprintf(stderr, "Warning: Read %zu bytes, expected %zu bytes\n", total_read, size);
}
fwrite(buf2, 1, total_read, stdout);
free(buf2);
squash_close(vfd);
sqfs_destroy(&fs);
return 0;
}
What am I doing wrong?
Is there an example for how to extract a file (following symlinks) somewhere?
Thank you very much.
Metadata
Metadata
Assignees
Labels
No labels