summaryrefslogtreecommitdiff
path: root/lib/fstree/gen_inode_table.c
blob: 15ef57750b73c7d8c21df317a908940cd56ae43e (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"

#include "fstree.h"

#include <stdlib.h>
#include <stdio.h>

static size_t count_nodes(tree_node_t *root)
{
	tree_node_t *n = root->data.dir->children;
	size_t count = 1;

	while (n != NULL) {
		if (S_ISDIR(n->mode)) {
			count += count_nodes(n);
		} else {
			++count;
		}
		n = n->next;
	}

	return count;
}

static void map_child_nodes(fstree_t *fs, tree_node_t *root, size_t *counter)
{
	bool has_subdirs = false;
	tree_node_t *it;

	for (it = root->data.dir->children; it != NULL; it = it->next) {
		if (S_ISDIR(it->mode)) {
			has_subdirs = true;
			break;
		}
	}

	if (has_subdirs) {
		for (it = root->data.dir->children; it != NULL; it = it->next) {
			if (S_ISDIR(it->mode))
				map_child_nodes(fs, it, counter);
		}
	}

	for (it = root->data.dir->children; it != NULL; it = it->next) {
		it->inode_num = *counter;
		*counter += 1;

		fs->inode_table[it->inode_num] = it;
	}
}

int fstree_gen_inode_table(fstree_t *fs)
{
	size_t inum = 2;

	fs->inode_tbl_size = count_nodes(fs->root) + 2;
	fs->inode_table = calloc(sizeof(tree_node_t *), fs->inode_tbl_size);

	if (fs->inode_table == NULL) {
		perror("allocating inode table");
		return -1;
	}

	map_child_nodes(fs, fs->root, &inum);
	fs->root->inode_num = inum;
	fs->inode_table[inum] = fs->root;
	return 0;
}