summaryrefslogtreecommitdiff
path: root/lib/fstree/node_stat.c
blob: 4c3afe6debc6cac13184828c43a2ff42db6f7bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"

#include "fstree.h"

#include <string.h>

void fstree_node_stat(fstree_t *fs, tree_node_t *node, struct stat *sb)
{
	tree_node_t *n;

	*sb = fs->defaults;
	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_mtime = node->mod_time;
	sb->st_atime = node->mod_time;
	sb->st_ctime = node->mod_time;

	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;
}