summaryrefslogtreecommitdiff
path: root/lib/fstree/fstree_from_dir.c
blob: fb906f12dcd97aff1dd88ff9243315fe6498d34b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "fstree.h"
#include "util.h"

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

static size_t path_size(tree_node_t *n)
{
	size_t size = 0;

	while (n != NULL) {
		size += strlen(n->name) + 1;
		n = n->parent;
	}

	return size;
}

static char *print_path(char *ptr, tree_node_t *n)
{
	if (n->parent != NULL) {
		ptr = print_path(ptr, n->parent);
		*(ptr++) = '/';
		strcpy(ptr, n->name);
		return ptr + strlen(ptr);
	}

	return ptr;
}

static int populate_dir(tree_node_t *root, int fd, size_t blocksize,
			const char *rootdir)
{
	size_t size, blockcount;
	struct dirent *ent;
	struct stat sb;
	tree_node_t *n;
	ssize_t ret;
	void *ptr;
	DIR *dir;
	int cfd;

	dir = fdopendir(fd);
	if (dir == NULL) {
		perror("fdopendir");
		close(fd);
		return -1;
	}

	for (;;) {
		n = NULL;
		errno = 0;
		ent = readdir(dir);

		if (ent == NULL) {
			if (errno) {
				perror("readdir");
				goto fail;
			}
			break;
		}

		if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, "."))
			continue;

		if (fstatat(fd, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
			perror(ent->d_name);
			goto fail;
		}

		size = sizeof(tree_node_t) + strlen(ent->d_name) + 1;

		switch (sb.st_mode & S_IFMT) {
		case S_IFLNK:
			size += sb.st_size + 1;
			break;
		case S_IFREG:
			blockcount = sb.st_size / blocksize + 1;

			size += sizeof(file_info_t);
			size += sizeof(uint32_t) * blockcount;

			size += path_size(root) + strlen(ent->d_name) + 1;
			size += strlen(rootdir) + 2;
			break;
		case S_IFDIR:
			size += sizeof(dir_info_t);
			break;
		}

		n = calloc(1, size);
		if (n == NULL) {
			perror("allocating tree node");
			goto fail;
		}

		n->uid = sb.st_uid;
		n->gid = sb.st_gid;
		n->mode = sb.st_mode;
		n->parent = root;

		ptr = n->payload;

		switch (sb.st_mode & S_IFMT) {
		case S_IFLNK:
			ret = readlinkat(fd, ent->d_name, ptr, sb.st_size);
			if (ret < 0) {
				perror("readlink");
				goto fail;
			}

			n->data.slink_target = ptr;
			ptr = (char *)ptr + strlen(ptr) + 1;
			break;
		case S_IFBLK:
		case S_IFCHR:
			n->data.devno = sb.st_rdev;
			break;
		case S_IFDIR:
			n->data.dir = ptr;
			ptr = (char *)ptr + sizeof(dir_info_t);
			break;
		case S_IFREG:
			n->data.file = ptr;
			ptr = (char *)ptr + sizeof(file_info_t);
			ptr = (char *)ptr + sizeof(uint32_t) * blockcount;

			n->data.file->input_file = ptr;
			n->data.file->size = sb.st_size;

			strcpy(ptr, rootdir);
			ptr = (char *)ptr + strlen(ptr);
			ptr = print_path(ptr, root);

			*((char*)ptr) = '/';
			strcpy((char *)ptr + 1, ent->d_name);
			ptr = (char *)ptr + strlen(ptr) + 1;
			break;
		}

		n->name = ptr;
		strcpy(ptr, ent->d_name);

		n->next = root->data.dir->children;
		root->data.dir->children = n;
	}

	for (n = root->data.dir->children; n != NULL; n = n->next) {
		if (S_ISDIR(n->mode)) {
			cfd = openat(fd, n->name, O_RDONLY | O_DIRECTORY);
			if (cfd < 0) {
				perror(n->name);
				goto fail_dir;
			}

			if (populate_dir(n, cfd, blocksize, rootdir)) {
				close(cfd);
				goto fail_dir;
			}
		}
	}

	closedir(dir);
	return 0;
fail:
	free(n);
fail_dir:
	closedir(dir);
	return -1;
}

int fstree_from_dir(fstree_t *fs, const char *path)
{
	int fd = open(path, O_RDONLY | O_DIRECTORY);

	if (fd < 0) {
		perror(path);
		return -1;
	}

	return populate_dir(fs->root, fd, fs->block_size, path);
}