Add test for extracting soft links and fix errors; Fix error in extracting fifo nodes if file exists;

This commit is contained in:
Nathan Fisher 2023-11-20 19:05:11 -05:00
parent de23c4c4cb
commit a7a0f6542d
3 changed files with 52 additions and 2 deletions

View file

@ -675,7 +675,7 @@ haggis_node* haggis_create_node(
target = malloc(res + 1);
memcpy(target, pathbuf, (unsigned long)res);
haggis_filename_init(target, &node->filetype.f_type.target);
body.f_name = file;
body.f_name = strndup(file, PATH_MAX);
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
} else if (S_ISREG(st.st_mode)) {
@ -770,6 +770,8 @@ int haggis_extract_fifo(haggis_node *node, char *basedir) {
return 2;
}
mode = (mode_t)node->mode.val;
if (access(path, F_OK) == 0)
unlink(path);
ret = mkfifo(path, mode);
free(path);
if (ret !=0)
@ -790,6 +792,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);
ret = symlink(target, path);
free(path);
if (ret != 0)
@ -907,7 +911,8 @@ int haggis_extract_node(char *basedir, haggis_node *node, haggis_mq *mq) {
ret = haggis_extract_fifo(node, basedir);
break;
case softlink:
return haggis_extract_symlink(node, basedir);
ret = haggis_extract_symlink(node, basedir);
break;
case hardlink:
ret = haggis_extract_hardlink(node, basedir);
break;
@ -923,6 +928,7 @@ int haggis_extract_node(char *basedir, haggis_node *node, haggis_mq *mq) {
case eof:
return 0;
}
if (ret) return ret;
if (geteuid() == 0) {
ret = chown(path, (uid_t)node->uid.val, (gid_t)node->gid.val);
if (ret != 0) {
@ -931,6 +937,7 @@ int haggis_extract_node(char *basedir, haggis_node *node, haggis_mq *mq) {
}
}
ret = chmod(path, (mode_t)node->mode.val);
if (ret) return ret;
msg = calloc(1, sizeof(haggis_msg));
if (msg == NULL)
return 2;

View file

@ -75,6 +75,7 @@ tests += extract_dev_node
tests += extract_dir_node
tests += extract_fifo_node
tests += extract_file_node
tests += extract_symlink_node
total != echo $(tests) | wc -w | awk '{ print $$1 }'

View file

@ -0,0 +1,42 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "haggis.h"
#include "mq.h"
int main() {
haggis_node *node = NULL;
haggis_linkmap *map = NULL;
haggis_mq mq;
haggis_msg *msg = NULL;
char *tgt = "../Makefile", *lnk = "output/test.mk";
int ret = 0;
map = haggis_linkmap_init();
assert(map != NULL);
assert(haggis_mq_init(&mq) == 0);
unlink(lnk);
assert(symlink(tgt, lnk) == 0);
node = haggis_create_node(lnk, skip, map, &mq);
assert(node != NULL);
assert(node->filetype.tag == softlink);
assert(memcmp(node->name.name, lnk, 14) == 0);
assert(memcmp(node->filetype.f_type.target.name, tgt, 11) == 0);
ret = haggis_extract_node("output/extracted", node, &mq);
assert(ret == 0);
msg = haggis_mq_pop(&mq);
assert(msg->tag == NodeCreated);
assert(memcmp(msg->body.f_name, lnk, 14) == 0);
haggis_msg_deinit(msg);
msg = haggis_mq_pop(&mq);
assert(msg->tag == NodeExtracted);
assert(memcmp(msg->body.f_name, lnk, 14) == 0);
assert(mq.head == NULL);
assert(mq.tail == NULL);
haggis_msg_deinit(msg);
haggis_node_deinit(node);
haggis_linkmap_deinit(map);
return 0;
}