Add "--no-abs-symlink" to not store absolute paths

This commit is contained in:
Stephen Seo 2024-07-24 13:32:39 +09:00
parent d939939723
commit e9c327a62f
3 changed files with 112 additions and 94 deletions

View file

@ -673,13 +673,17 @@ int write_files_fn(void *data, void *ud) {
// Get absolute path. // Get absolute path.
__attribute__((cleanup(free_malloced_memory))) void *abs_path = __attribute__((cleanup(free_malloced_memory))) void *abs_path =
realpath(file_info->filename, NULL); realpath(file_info->filename, NULL);
__attribute__((cleanup(free_malloced_memory))) void *rel_path = NULL;
if (!abs_path) { if (!abs_path) {
fprintf(stderr, fprintf(stderr,
"WARNING: Failed to get absolute path of link destination!\n"); "WARNING: Failed to get absolute path of link destination!\n");
} temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(2);
temp_to_write->size = 2;
memset(temp_to_write->buf, 0, 2);
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
} else {
// Get relative path. // Get relative path.
__attribute__((cleanup(free_malloced_memory))) void *rel_path = NULL;
// First get absolute path of link. // First get absolute path of link.
// Get abs path to dirname of link. // Get abs path to dirname of link.
unsigned int link_dir_path_len = strlen(file_info->filename) + 1; unsigned int link_dir_path_len = strlen(file_info->filename) + 1;
@ -750,7 +754,7 @@ int write_files_fn(void *data, void *ud) {
} while (has_slash); } while (has_slash);
} }
if (abs_path) { if ((state->parsed->flags & 0x20) == 0) {
// Write absolute path length. // Write absolute path length.
u16 = strlen(abs_path); u16 = strlen(abs_path);
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -759,7 +763,8 @@ int write_files_fn(void *data, void *ud) {
temp_to_write->buf = malloc(2); temp_to_write->buf = malloc(2);
temp_to_write->size = 2; temp_to_write->size = 2;
memcpy(temp_to_write->buf, &u16, 2); memcpy(temp_to_write->buf, &u16, 2);
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write,
free_internal_to_write);
// Write absolute path. // Write absolute path.
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -767,13 +772,19 @@ int write_files_fn(void *data, void *ud) {
temp_to_write->buf = malloc(u16 + 1); temp_to_write->buf = malloc(u16 + 1);
temp_to_write->size = u16 + 1; temp_to_write->size = u16 + 1;
strncpy(temp_to_write->buf, abs_path, u16 + 1); strncpy(temp_to_write->buf, abs_path, u16 + 1);
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write,
free_internal_to_write);
} else { } else {
fprintf(stderr,
"NOTICE: Not saving absolute path since \"--no-abs-symlink\" "
"was specified.\n");
temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite));
temp_to_write->buf = malloc(2); temp_to_write->buf = malloc(2);
temp_to_write->size = 2; temp_to_write->size = 2;
memset(temp_to_write->buf, 0, 2); memset(temp_to_write->buf, 0, 2);
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write,
free_internal_to_write);
}
} }
if (rel_path) { if (rel_path) {
@ -803,7 +814,9 @@ int write_files_fn(void *data, void *ud) {
// Write all previously set data. // Write all previously set data.
fprintf(stderr, "Writing symlink info: %s\n", file_info->filename); fprintf(stderr, "Writing symlink info: %s\n", file_info->filename);
if ((state->parsed->flags & 0x20) == 0) {
fprintf(stderr, " abs path: %s\n", (char *)abs_path); fprintf(stderr, " abs path: %s\n", (char *)abs_path);
}
fprintf(stderr, " rel path: %s\n", (char *)rel_path); fprintf(stderr, " rel path: %s\n", (char *)rel_path);
simple_archiver_list_get(to_write, write_list_datas_fn, state->out_f); simple_archiver_list_get(to_write, write_list_datas_fn, state->out_f);
simple_archiver_list_free(&to_write); simple_archiver_list_free(&to_write);

View file

@ -151,6 +151,8 @@ void simple_archiver_print_usage(void) {
"file's stored decompressor\n"); "file's stored decompressor\n");
fprintf(stderr, "--overwrite-create : allows overwriting an archive file\n"); fprintf(stderr, "--overwrite-create : allows overwriting an archive file\n");
fprintf(stderr, "--overwrite-extract : allows overwriting when extracting\n"); fprintf(stderr, "--overwrite-extract : allows overwriting when extracting\n");
fprintf(stderr,
"--no-abs-symlink : do not store absolute paths for symlinks\n");
fprintf(stderr, fprintf(stderr,
"-- : specifies remaining arguments are files to archive/extract\n"); "-- : specifies remaining arguments are files to archive/extract\n");
fprintf( fprintf(
@ -243,6 +245,8 @@ int simple_archiver_parse_args(int argc, const char **argv,
out->flags |= 0x4; out->flags |= 0x4;
} else if (strcmp(argv[0], "--overwrite-extract") == 0) { } else if (strcmp(argv[0], "--overwrite-extract") == 0) {
out->flags |= 0x8; out->flags |= 0x8;
} else if (strcmp(argv[0], "--no-abs-symlink") == 0) {
out->flags |= 0x20;
} else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) { } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) {
is_remaining_args = 1; is_remaining_args = 1;
} else if (argv[0][0] != '-') { } else if (argv[0][0] != '-') {

View file

@ -30,6 +30,7 @@ typedef struct SDArchiverParsed {
/// 0b xxxx x1xx - Allow create archive overwrite. /// 0b xxxx x1xx - Allow create archive overwrite.
/// 0b xxxx 1xxx - Allow extract overwrite. /// 0b xxxx 1xxx - Allow extract overwrite.
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin. /// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
/// 0b xx1x xxxx - Do not save absolute paths for symlinks.
unsigned int flags; unsigned int flags;
/// Null-terminated string. /// Null-terminated string.
char *filename; char *filename;