aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-15 23:03:33 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-15 23:04:15 +0200
commit69742b622a228babad255cbd64330d04c39c7e81 (patch)
tree44f4e58b22d2b4fb5138f02f801f039f7fbaddb2 /lib
parent108707d3466877c0919c9a5f35f4c7d06961e283 (diff)
libfstree.a: add function to get struct stat from tree node
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makemodule.am2
-rw-r--r--lib/fstree/node_stat.c44
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index a676291..3d0cf41 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -1,7 +1,7 @@
libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c
libfstree_a_SOURCES += lib/fstree/fstree_sort.c lib/fstree/fstree_from_dir.c
libfstree_a_SOURCES += lib/fstree/gen_inode_table.c lib/fstree/get_path.c
-libfstree_a_SOURCES += include/fstree.h
+libfstree_a_SOURCES += lib/fstree/node_stat.c include/fstree.h
libfstree_a_CFLAGS = $(AM_CFLAGS)
libfstree_a_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/lib/fstree/node_stat.c b/lib/fstree/node_stat.c
new file mode 100644
index 0000000..a6ec097
--- /dev/null
+++ b/lib/fstree/node_stat.c
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <string.h>
+
+void fstree_node_stat(fstree_t *fs, tree_node_t *node, struct stat *sb)
+{
+ tree_node_t *n;
+
+ memset(sb, 0, sizeof(*sb));
+
+ sb->st_ino = node->inode_num;
+ sb->st_mode = node->mode;
+ sb->st_nlink = 1;
+ sb->st_uid = node->uid;
+ sb->st_gid = node->gid;
+ sb->st_blksize = fs->block_size;
+ sb->st_mtime = fs->default_mtime;
+ sb->st_atime = sb->st_mtime;
+ sb->st_ctime = sb->st_mtime;
+
+ switch (node->mode & S_IFMT) {
+ case S_IFDIR:
+ sb->st_nlink = 2;
+
+ for (n = node->data.dir->children; n != NULL; n = n->next)
+ sb->st_nlink += 1;
+
+ sb->st_size = node->data.dir->size;
+ break;
+ case S_IFREG:
+ sb->st_size = node->data.file->size;
+ break;
+ case S_IFLNK:
+ sb->st_size = strlen(node->data.slink_target);
+ break;
+ case S_IFBLK:
+ case S_IFCHR:
+ sb->st_rdev = node->data.devno;
+ break;
+ }
+
+ sb->st_blocks = sb->st_size / fs->block_size;
+}