Add create_dir_node
test
This commit is contained in:
parent
37d0668a2f
commit
3cc22ed428
3 changed files with 40 additions and 22 deletions
44
haggis.c
44
haggis.c
|
@ -520,31 +520,31 @@ void haggis_node_deinit(haggis_node *node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *map) {
|
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *map) {
|
||||||
struct stat *st = NULL;
|
|
||||||
u16 mode;
|
u16 mode;
|
||||||
char *target;
|
char *target;
|
||||||
char pathbuf[PATH_MAX];
|
char pathbuf[PATH_MAX];
|
||||||
int res;
|
int res;
|
||||||
size_t namlen;
|
size_t namlen;
|
||||||
haggis_node *node;
|
haggis_node *node;
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
node = calloc(1, sizeof(haggis_node));
|
node = calloc(1, sizeof(haggis_node));
|
||||||
if (node == NULL) return NULL;
|
if (node == NULL) return NULL;
|
||||||
if (stat(file, st) != 0) {
|
if (stat(file, &st) != 0) {
|
||||||
free(node);
|
free(node);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (S_ISBLK(st->st_mode)) {
|
if (S_ISBLK(st.st_mode)) {
|
||||||
node->filetype.tag = block;
|
node->filetype.tag = block;
|
||||||
} else if (S_ISCHR(st->st_mode)) {
|
} else if (S_ISCHR(st.st_mode)) {
|
||||||
node->filetype.tag = character;
|
node->filetype.tag = character;
|
||||||
} else if (S_ISDIR(st->st_mode)) {
|
} else if (S_ISDIR(st.st_mode)) {
|
||||||
node->filetype.tag = directory;
|
node->filetype.tag = directory;
|
||||||
} else if (S_ISFIFO(st->st_mode)) {
|
} else if (S_ISFIFO(st.st_mode)) {
|
||||||
node->filetype.tag = fifo;
|
node->filetype.tag = fifo;
|
||||||
} else if (S_ISLNK(st->st_mode)) {
|
} else if (S_ISLNK(st.st_mode)) {
|
||||||
node->filetype.tag = softlink;
|
node->filetype.tag = softlink;
|
||||||
} else if (S_ISREG(st->st_mode)) {
|
} else if (S_ISREG(st.st_mode)) {
|
||||||
node->filetype.tag = normal;
|
node->filetype.tag = normal;
|
||||||
} else {
|
} else {
|
||||||
free(node);
|
free(node);
|
||||||
|
@ -563,15 +563,15 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
node->name.len.val = (uint16_t)namlen;
|
node->name.len.val = (uint16_t)namlen;
|
||||||
node->uid.val = (uint32_t)st->st_uid;
|
node->uid.val = (uint32_t)st.st_uid;
|
||||||
node->gid.val = (uint32_t)st->st_gid;
|
node->gid.val = (uint32_t)st.st_gid;
|
||||||
node->mtime.val = (uint64_t)st->st_mtim.tv_sec;
|
node->mtime.val = (uint64_t)st.st_mtim.tv_sec;
|
||||||
mode.val = (uint16_t)(st->st_mode & 07777);
|
mode.val = (uint16_t)(st.st_mode & 07777);
|
||||||
node->mode = mode;
|
node->mode = mode;
|
||||||
switch (node->filetype.tag) {
|
switch (node->filetype.tag) {
|
||||||
case normal:
|
case normal:
|
||||||
if (st->st_nlink > 1) {
|
if (st.st_nlink > 1) {
|
||||||
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st.st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
@ -585,30 +585,30 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case block:
|
case block:
|
||||||
if (st->st_nlink > 1) {
|
if (st.st_nlink > 1) {
|
||||||
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st.st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
haggis_device_init(st->st_rdev, &node->filetype.f_type.dev);
|
haggis_device_init(st.st_rdev, &node->filetype.f_type.dev);
|
||||||
break;
|
break;
|
||||||
case character:
|
case character:
|
||||||
if (st->st_nlink > 1) {
|
if (st.st_nlink > 1) {
|
||||||
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st.st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
haggis_device_init(st->st_rdev, &node->filetype.f_type.dev);
|
haggis_device_init(st.st_rdev, &node->filetype.f_type.dev);
|
||||||
break;
|
break;
|
||||||
case fifo:
|
case fifo:
|
||||||
if (st->st_nlink > 1) {
|
if (st.st_nlink > 1) {
|
||||||
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st.st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
|
|
@ -65,6 +65,7 @@ tests += fnv1a_hash_inode
|
||||||
tests += fnv1a_hash_str
|
tests += fnv1a_hash_str
|
||||||
tests += linkmap_init
|
tests += linkmap_init
|
||||||
tests += linkmap_put
|
tests += linkmap_put
|
||||||
|
tests += create_dir_node
|
||||||
|
|
||||||
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
||||||
|
|
||||||
|
|
17
test/create_dir_node.c
Normal file
17
test/create_dir_node.c
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "haggis.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
haggis_linkmap *map;
|
||||||
|
haggis_node *node;
|
||||||
|
char *path = "output";
|
||||||
|
|
||||||
|
map = haggis_linkmap_init();
|
||||||
|
assert(map != NULL);
|
||||||
|
node = haggis_create_node(path, sha256, map);
|
||||||
|
assert(node->filetype.tag == directory);
|
||||||
|
assert(memcmp(path, node->name.name, 6) == 0);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue