From 675a030f661ccce240d0f380bcd5a5ca105914ad Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 4 Feb 2024 00:02:59 -0500 Subject: [PATCH] Fix issues around extracting symlinks which only showed up when using Musl libc --- haggis.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/haggis.c b/haggis.c index 820a194..f7bc1a0 100644 --- a/haggis.c +++ b/haggis.c @@ -355,8 +355,6 @@ int haggis_load_file(FILE *stream, haggis_file *f) { return -1; int res = fread(f->data, 1, (size_t)f->len.val, stream); if (res != (size_t)f->len.val) { - fprintf(stderr, "Error reading file:\n length: %i\n expected: %lu\n", - res, f->len.val); free(f->data); return 1; } @@ -751,6 +749,8 @@ int haggis_extract_dev(haggis_node *node, char *basedir) { } else if (node->filetype.tag == character) { mode = (mode_t)node->mode.val | S_IFCHR; } + if (access(path, F_OK) == 0) + unlink(path); ret = mknod(path, mode, dev); free(path); return ret; @@ -782,6 +782,7 @@ int haggis_extract_fifo(haggis_node *node, char *basedir) { int haggis_extract_symlink(haggis_node *node, char *basedir) { char *path, *target; int ret; + struct stat st; assert(node->filetype.tag == softlink); path = get_full_path(&node->name, basedir); @@ -792,8 +793,8 @@ int haggis_extract_symlink(haggis_node *node, char *basedir) { return 2; } target = node->filetype.f_type.target.name; - if (access(path, F_OK) == 0) - unlink(path); + if (lstat(path, &st) == 0) + unlink(path); ret = symlink(target, path); free(path); if (ret != 0) @@ -870,7 +871,7 @@ char* haggis_extract_file(haggis_node *node, char *basedir) { free(path); return NULL; } - fd = fopen(path, "w"); + fd = fopen(path, "w+"); if (fd == NULL) { free(path); return NULL; @@ -938,7 +939,8 @@ int haggis_extract_node(char *basedir, haggis_node *node, haggis_mq *mq) { return errno; } } - ret = chmod(path, (mode_t)node->mode.val); + if (node->filetype.tag != softlink) + ret = chmod(path, (mode_t)node->mode.val); if (ret) return ret; msg = calloc(1, sizeof(haggis_msg)); if (msg == NULL)