-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add squashfuse_extract tool #14
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks nice, thanks for the contribution. I commented on a lot of minor issues, please feel free to fix them as you see fit, but the buffer overflow definitely should be addressed before landing.
die("sqfs_traverse_open error"); | ||
while (sqfs_traverse_next(&trv, &err)) { | ||
if (!trv.dir_end) { | ||
if((startsWith(path_to_extract, trv.path) != 0) || (strcmp("-a", path_to_extract) == 0)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could factor the second strcmp out into a one-time check, and then make this code a little clearer. also it might be nice to split this very long while/nested if block into separate functions or other factorings to make it a little easier to digest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to change as you see fit; for me it is OK as it is
fprintf(stderr, "inode.base.inode_type: %i\n", inode.base.inode_type); | ||
fprintf(stderr, "inode.xtra.reg.file_size: %lu\n", inode.xtra.reg.file_size); | ||
strcpy(prefixed_path_to_extract, ""); | ||
strcat(strcat(prefixed_path_to_extract, prefix), trv.path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a potential buffer overflow; rather than a 1024 limit, you should allocate it dynamically once you know each trv.path (variable length array)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out; fixed in b4618fc
strcpy(prefixed_path_to_extract, ""); | ||
strcat(strcat(prefixed_path_to_extract, prefix), trv.path); | ||
if(inode.base.inode_type == SQUASHFS_DIR_TYPE){ | ||
fprintf(stderr, "inode.xtra.dir.parent_inode: %ui\n", inode.xtra.dir.parent_inode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider putting these fprintf's into a debug mode and disable by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could but then I would have to implement fancier arg parsing - do you know how to do that without bloating the code?
die("fopen error"); | ||
while (bytes_already_read < inode.xtra.reg.file_size) | ||
{ | ||
char *buf = malloc(bytes_at_a_time); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for explicit malloc/free -- you can use a VLA here, too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 8276a81
fprintf(stderr, "Extract to: %s\n", prefixed_path_to_extract); | ||
// Read the file in chunks | ||
off_t bytes_already_read = 0; | ||
size_t bytes_at_a_time = 1024; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd go with at least 64k, or up to 1mb; 1024 is pretty small and less efficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in b4618fc
} | ||
fclose(f); | ||
} else if(inode.base.inode_type == SQUASHFS_SYMLINK_TYPE){ | ||
size_t size = 1024; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 8276a81
} else if(inode.base.inode_type == SQUASHFS_SYMLINK_TYPE){ | ||
size_t size = 1024; | ||
char *buf = malloc(size); | ||
sqfs_readlink(&fs, &inode, buf, &size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there an error code you can check here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 9e83e5d
#define ERR_OPEN (-3) | ||
|
||
static void usage() { | ||
fprintf(stderr, "Usage: %s ARCHIVE PATH_TO_EXTRACT\n", PROGNAME); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a nice future enhancement would be to support extracting multiple files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but that's for a later version
|
||
prefix = "squashfs-root/"; | ||
|
||
if(access(prefix, F_OK ) == -1 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: space before the open paren of if statements; similar issue in other places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in d743c1f
|
||
#define PROGNAME "squashfuse_extract" | ||
|
||
#define ERR_MISC (-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exit codes should be positive, not negative
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 59ae332
Thanks for your thorough review @chipturner. I have addressed your points. Now I would like to get rid of the following compiler warnings, can you give me hints on how to do that?
|
Looks good, thanks! |
To fix that warning, you probably need to switch from long int to a size_t (size_t is unsigned) |
The squashfuse_extract can extract files, directories, and symlinks from a squashfs image. Closes #13 and partly #9.