Fix issues around extracting symlinks which only showed up when using

Musl libc
This commit is contained in:
Nathan Fisher 2024-02-04 00:02:59 -05:00
parent a988a7877e
commit 675a030f66

View file

@ -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)