aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/fstree.h2
-rw-r--r--lib/common/serialize_fstree.c16
-rw-r--r--lib/fstree/mknode.c7
-rw-r--r--tests/add_by_path.c19
-rw-r--r--tests/fstree_from_file.c10
-rw-r--r--tests/mknode_dir.c5
-rw-r--r--tests/mknode_reg.c1
-rw-r--r--tests/mknode_simple.c4
-rw-r--r--tests/mknode_slink.c2
9 files changed, 60 insertions, 6 deletions
diff --git a/include/fstree.h b/include/fstree.h
index 1ac3f8b..d4839a2 100644
--- a/include/fstree.h
+++ b/include/fstree.h
@@ -59,7 +59,7 @@ struct tree_node_t {
sqfs_u32 inode_num;
sqfs_u32 mod_time;
sqfs_u16 mode;
- sqfs_u16 pad0;
+ sqfs_u16 link_count;
/* SquashFS inode refernce number. 32 bit offset of the meta data
block start (relative to inode table start), shifted left by 16
diff --git a/lib/common/serialize_fstree.c b/lib/common/serialize_fstree.c
index a64d90f..39f8f94 100644
--- a/lib/common/serialize_fstree.c
+++ b/lib/common/serialize_fstree.c
@@ -28,27 +28,27 @@ static sqfs_inode_generic_t *tree_node_to_inode(tree_node_t *node)
switch (node->mode & S_IFMT) {
case S_IFSOCK:
inode->base.type = SQFS_INODE_SOCKET;
- inode->data.ipc.nlink = 1;
+ inode->data.ipc.nlink = node->link_count;
break;
case S_IFIFO:
inode->base.type = SQFS_INODE_FIFO;
- inode->data.ipc.nlink = 1;
+ inode->data.ipc.nlink = node->link_count;
break;
case S_IFLNK:
inode->base.type = SQFS_INODE_SLINK;
- inode->data.slink.nlink = 1;
+ inode->data.slink.nlink = node->link_count;
inode->data.slink.target_size = extra;
inode->slink_target = (char *)inode->extra;
memcpy(inode->extra, node->data.target, extra);
break;
case S_IFBLK:
inode->base.type = SQFS_INODE_BDEV;
- inode->data.dev.nlink = 1;
+ inode->data.dev.nlink = node->link_count;
inode->data.dev.devno = node->data.devno;
break;
case S_IFCHR:
inode->base.type = SQFS_INODE_CDEV;
- inode->data.dev.nlink = 1;
+ inode->data.dev.nlink = node->link_count;
inode->data.dev.devno = node->data.devno;
break;
default:
@@ -91,6 +91,12 @@ static sqfs_inode_generic_t *write_dir_entries(const char *filename,
goto fail;
}
+ if (inode->base.type == SQFS_INODE_DIR) {
+ inode->data.dir.nlink = node->link_count;
+ } else {
+ inode->data.dir_ext.nlink = node->link_count;
+ }
+
return inode;
fail:
sqfs_perror(filename, "recoding directory entries", ret);
diff --git a/lib/fstree/mknode.c b/lib/fstree/mknode.c
index 16cfa9c..4353b74 100644
--- a/lib/fstree/mknode.c
+++ b/lib/fstree/mknode.c
@@ -44,6 +44,7 @@ tree_node_t *fstree_mknode(tree_node_t *parent, const char *name,
n->gid = sb->st_gid;
n->mode = sb->st_mode;
n->mod_time = sb->st_mtime;
+ n->link_count = 1;
n->name = (char *)n->payload;
memcpy(n->name, name, name_len);
@@ -66,7 +67,13 @@ tree_node_t *fstree_mknode(tree_node_t *parent, const char *name,
case S_IFCHR:
n->data.devno = sb->st_rdev;
break;
+ case S_IFDIR:
+ n->link_count = 2;
+ break;
}
+ if (parent != NULL)
+ parent->link_count += 1;
+
return n;
}
diff --git a/tests/add_by_path.c b/tests/add_by_path.c
index af8dd88..6abf05f 100644
--- a/tests/add_by_path.c
+++ b/tests/add_by_path.c
@@ -29,6 +29,8 @@ int main(void)
sb.st_uid = 1000;
sb.st_gid = 100;
+ assert(fs.root->link_count == 2);
+
a = fstree_add_generic(&fs, "dir", &sb, NULL);
assert(a != NULL);
assert(strcmp(a->name, "dir") == 0);
@@ -36,8 +38,10 @@ int main(void)
assert(a->uid == sb.st_uid);
assert(a->gid == sb.st_gid);
assert(a->parent == fs.root);
+ assert(a->link_count == 2);
assert(a->next == NULL);
assert(fs.root->data.dir.children == a);
+ assert(fs.root->link_count == 3);
assert(!a->data.dir.created_implicitly);
memset(&sb, 0, sizeof(sb));
@@ -52,8 +56,10 @@ int main(void)
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
assert(b->parent == fs.root);
+ assert(b->link_count == 1);
assert(b->data.devno == sb.st_rdev);
assert(b->next == a);
+ assert(fs.root->link_count == 4);
assert(fs.root->data.dir.children == b);
assert(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL) == NULL);
@@ -74,21 +80,29 @@ int main(void)
assert(b->mode == sb.st_mode);
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
+ assert(b->link_count == 1);
assert(b->parent == a);
assert(b->data.devno == sb.st_rdev);
assert(b->next == NULL);
assert(a->data.dir.children == b);
+ assert(a->link_count == 3);
+ assert(fs.root->link_count == 4);
+
b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL);
assert(b != NULL);
assert(b->next == NULL);
assert(b->mode == sb.st_mode);
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
+ assert(b->link_count == 1);
assert(b->parent != a);
assert(b->parent->parent == a);
assert(b->data.devno == sb.st_rdev);
assert(b->next == NULL);
+
+ assert(a->link_count == 4);
+ assert(fs.root->link_count == 4);
assert(a->data.dir.children != b);
b = b->parent;
@@ -96,6 +110,7 @@ int main(void)
assert(b->mode == (S_IFDIR | 0755));
assert(b->uid == 21);
assert(b->gid == 42);
+ assert(b->link_count == 3);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0750;
@@ -109,6 +124,10 @@ int main(void)
assert(a->mode == sb.st_mode);
assert(a->uid == sb.st_uid);
assert(a->gid == sb.st_gid);
+ assert(a->link_count == 3);
+
+ assert(a->parent->link_count == 4);
+ assert(fs.root->link_count == 4);
assert(fstree_add_generic(&fs, "dir/foo", &sb, NULL) == NULL);
assert(errno == EEXIST);
diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c
index e9b56be..34215c9 100644
--- a/tests/fstree_from_file.c
+++ b/tests/fstree_from_file.c
@@ -45,9 +45,12 @@ int main(void)
fstree_post_process(&fs);
n = fs.root->data.dir.children;
+ assert(fs.root->link_count == 9);
+
assert(n->mode == (S_IFBLK | 0600));
assert(n->uid == 8);
assert(n->gid == 9);
+ assert(n->link_count == 1);
assert(strcmp(n->name, "blkdev") == 0);
assert(n->data.devno == makedev(42, 21));
@@ -55,6 +58,7 @@ int main(void)
assert(n->mode == (S_IFCHR | 0600));
assert(n->uid == 6);
assert(n->gid == 7);
+ assert(n->link_count == 1);
assert(strcmp(n->name, "chardev") == 0);
assert(n->data.devno == makedev(13, 37));
@@ -62,6 +66,7 @@ int main(void)
assert(n->mode == (S_IFDIR | 0755));
assert(n->uid == 4);
assert(n->gid == 5);
+ assert(n->link_count == 2);
assert(strcmp(n->name, "dir") == 0);
assert(n->data.dir.children == NULL);
@@ -69,6 +74,7 @@ int main(void)
assert(n->mode == (S_IFDIR | 0755));
assert(n->uid == 0);
assert(n->gid == 0);
+ assert(n->link_count == 3);
assert(strcmp(n->name, "foo bar") == 0);
assert(n->data.dir.children != NULL);
@@ -76,6 +82,7 @@ int main(void)
assert(n->data.dir.children->mode == (S_IFDIR | 0755));
assert(n->data.dir.children->uid == 0);
assert(n->data.dir.children->gid == 0);
+ assert(n->data.dir.children->link_count == 2);
assert(strcmp(n->data.dir.children->name, " test \"") == 0);
assert(n->data.dir.children->data.dir.children == NULL);
@@ -83,12 +90,14 @@ int main(void)
assert(n->mode == (S_IFIFO | 0644));
assert(n->uid == 10);
assert(n->gid == 11);
+ assert(n->link_count == 1);
assert(strcmp(n->name, "pipe") == 0);
n = n->next;
assert(n->mode == (S_IFLNK | 0777));
assert(n->uid == 2);
assert(n->gid == 3);
+ assert(n->link_count == 1);
assert(strcmp(n->name, "slink") == 0);
fprintf(stderr, "'%s'\n", n->data.target);
assert(strcmp(n->data.target, "slinktarget") == 0);
@@ -97,6 +106,7 @@ int main(void)
assert(n->mode == (S_IFSOCK | 0555));
assert(n->uid == 12);
assert(n->gid == 13);
+ assert(n->link_count == 1);
assert(strcmp(n->name, "sock") == 0);
assert(n->next == NULL);
diff --git a/tests/mknode_dir.c b/tests/mknode_dir.c
index 3b6aeda..8f2131c 100644
--- a/tests/mknode_dir.c
+++ b/tests/mknode_dir.c
@@ -30,6 +30,7 @@ int main(void)
assert(root->uid == sb.st_uid);
assert(root->gid == sb.st_gid);
assert(root->mode == sb.st_mode);
+ assert(root->link_count == 2);
assert((char *)root->name >= (char *)root->payload);
assert(root->name >= (char *)root->payload);
assert(strcmp(root->name, "rootdir") == 0);
@@ -40,6 +41,8 @@ int main(void)
a = fstree_mknode(root, "adir", 4, NULL, &sb);
assert(a->parent == root);
assert(a->next == NULL);
+ assert(a->link_count == 2);
+ assert(root->link_count == 3);
assert(root->data.dir.children == a);
assert(root->parent == NULL);
assert(root->next == NULL);
@@ -47,7 +50,9 @@ int main(void)
b = fstree_mknode(root, "bdir", 4, NULL, &sb);
assert(a->parent == root);
assert(b->parent == root);
+ assert(b->link_count == 2);
assert(root->data.dir.children == b);
+ assert(root->link_count == 4);
assert(b->next == a);
assert(a->next == NULL);
assert(root->parent == NULL);
diff --git a/tests/mknode_reg.c b/tests/mknode_reg.c
index 26e42b7..40966f8 100644
--- a/tests/mknode_reg.c
+++ b/tests/mknode_reg.c
@@ -32,6 +32,7 @@ int main(void)
assert(node->gid == sb.st_gid);
assert(node->mode == sb.st_mode);
assert(node->parent == NULL);
+ assert(node->link_count == 1);
assert((char *)node->name >= (char *)node->payload);
assert(node->data.file.input_file >= (char *)node->payload);
assert(node->data.file.input_file >= node->name + 8);
diff --git a/tests/mknode_simple.c b/tests/mknode_simple.c
index 4b4e4aa..ef2a466 100644
--- a/tests/mknode_simple.c
+++ b/tests/mknode_simple.c
@@ -32,6 +32,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == sb.st_mode);
+ assert(node->link_count == 1);
assert(node->parent == NULL);
assert(node->data.target == NULL);
assert(node->data.devno == 0);
@@ -51,6 +52,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == sb.st_mode);
+ assert(node->link_count == 1);
assert(node->parent == NULL);
assert(node->data.target == NULL);
assert(node->data.devno == 0);
@@ -70,6 +72,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == sb.st_mode);
+ assert(node->link_count == 1);
assert(node->data.devno == sb.st_rdev);
assert(node->parent == NULL);
free(node);
@@ -88,6 +91,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == sb.st_mode);
+ assert(node->link_count == 1);
assert(node->data.devno == sb.st_rdev);
assert(node->parent == NULL);
free(node);
diff --git a/tests/mknode_slink.c b/tests/mknode_slink.c
index a520e70..bcb28f0 100644
--- a/tests/mknode_slink.c
+++ b/tests/mknode_slink.c
@@ -30,6 +30,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == (S_IFLNK | 0777));
+ assert(node->link_count == 1);
assert(node->parent == NULL);
assert((char *)node->name >= (char *)node->payload);
assert(node->data.target >= (char *)node->payload);
@@ -42,6 +43,7 @@ int main(void)
assert(node->uid == sb.st_uid);
assert(node->gid == sb.st_gid);
assert(node->mode == (S_IFLNK | 0777));
+ assert(node->link_count == 1);
assert(node->parent == NULL);
assert((char *)node->name >= (char *)node->payload);
assert(node->data.target >= (char *)node->payload);