Add create.c.
[adu.git] / create.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file create.c The create mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include "adu.h"
11 #include "gcc-compat.h"
12 #include "cmdline.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "error.h"
16 #include "portable_io.h"
17
18 /* Id of the device containing the base dir. */
19 static dev_t device_id;
20
21 static int write_uid(struct user_info *ui, void *data)
22 {
23 char **p = data;
24
25 write_u32(*p, ui->uid);
26 *p += sizeof(uint32_t);
27 return 1;
28 }
29
30 static int write_uid_list(void)
31 {
32 char *buf, *p, *filename;
33 uint32_t count = 0;
34 struct user_info *ui;
35 size_t size = num_uids * sizeof(uint32_t);
36 int ret;
37
38 if (!num_uids)
39 return 0;
40 buf = p = adu_malloc(size);
41 ret = for_each_admissible_user(write_uid, &p);
42 if (ret < 0)
43 goto out;
44 filename = get_uid_list_name();
45 ret = adu_write_file(filename, buf, size);
46 free(filename);
47 out:
48 free(buf);
49 return ret;
50 }
51
52 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
53 uint64_t *dir_size, uint64_t *dir_files)
54 {
55 struct osl_object dir_objects[NUM_DT_COLUMNS];
56
57 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
58 dir_objects[DT_NAME].data = dirname;
59 dir_objects[DT_NAME].size = strlen(dirname) + 1;
60 dir_objects[DT_NUM].data = dir_num;
61 dir_objects[DT_NUM].size = sizeof(*dir_num);
62 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
63 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
64 dir_objects[DT_BYTES].data = dir_size;
65 dir_objects[DT_BYTES].size = sizeof(*dir_size);
66 dir_objects[DT_FILES].data = dir_files;
67 dir_objects[DT_FILES].size = sizeof(*dir_files);
68 return osl(osl_add_row(dir_table, dir_objects));
69 }
70
71 static int update_user_row(struct osl_table *t, uint64_t dir_num,
72 uint64_t *add)
73 {
74 struct osl_row *row;
75 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
76
77 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
78
79 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
80 return ret;
81 if (ret < 0) { /* this is the first file we add */
82 struct osl_object objects[NUM_UT_COLUMNS];
83 uint64_t num_files = 1;
84
85 objects[UT_DIR_NUM].data = &dir_num;
86 objects[UT_DIR_NUM].size = sizeof(dir_num);
87 objects[UT_BYTES].data = add;
88 objects[UT_BYTES].size = sizeof(*add);
89 objects[UT_FILES].data = &num_files;
90 objects[UT_FILES].size = sizeof(num_files);
91 INFO_LOG("######################### ret: %d\n", ret);
92 ret = osl(osl_add_row(t, objects));
93 INFO_LOG("######################### ret: %d\n", ret);
94 return ret;
95 } else { /* add size and increment file count */
96 uint64_t num;
97 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
98
99 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
100 if (ret < 0)
101 return ret;
102 num = *(uint64_t *)obj1.data + *add;
103 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
104 if (ret < 0)
105 return ret;
106 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
107 if (ret < 0)
108 return ret;
109 num = *(uint64_t *)obj1.data + 1;
110 return osl(osl_update_object(t, row, UT_FILES, &obj2));
111 }
112 }
113
114 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
115 {
116 DIR *dir;
117 struct dirent *entry;
118 int ret, cwd_fd, ret2;
119 uint64_t dir_size = 0, dir_files = 0;
120 uint64_t this_dir_num = ++num_dirs;
121
122 check_signals();
123 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
124 ret = adu_opendir(dirname, &dir, &cwd_fd);
125 if (ret < 0) {
126 if (ret != -ERRNO_TO_ERROR(EACCES))
127 return ret;
128 WARNING_LOG("permission denied for %s\n", dirname);
129 return 1;
130 }
131 while ((entry = readdir(dir))) {
132 mode_t m;
133 struct stat s;
134 uint32_t uid;
135 uint64_t size;
136 struct user_info *ui;
137
138 if (!strcmp(entry->d_name, "."))
139 continue;
140 if (!strcmp(entry->d_name, ".."))
141 continue;
142 if (lstat(entry->d_name, &s) == -1) {
143 WARNING_LOG("lstat error for %s/%s\n", dirname,
144 entry->d_name);
145 continue;
146 }
147 m = s.st_mode;
148 if (!S_ISREG(m) && !S_ISDIR(m))
149 continue;
150 if (S_ISDIR(m)) {
151 if (conf.one_file_system_given && s.st_dev != device_id)
152 continue;
153 ret = scan_dir(entry->d_name, &this_dir_num);
154 if (ret < 0)
155 goto out;
156 continue;
157 }
158 /* regular file */
159 size = s.st_size;
160 dir_size += size;
161 num_bytes += size;
162 dir_files++;
163 num_files++;
164 uid = s.st_uid;
165 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
166 if (ret < 0)
167 goto out;
168 ui->bytes += size;
169 ui->files++;
170 ret = update_user_row(ui->table, this_dir_num, &size);
171 if (ret < 0)
172 goto out;
173 }
174 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
175 &dir_size, &dir_files);
176 out:
177 closedir(dir);
178 ret2 = adu_fchdir(cwd_fd);
179 if (ret2 < 0 && ret >= 0)
180 ret = ret2;
181 close(cwd_fd);
182 return ret;
183 }
184
185 int com_create()
186 {
187 uint64_t zero = 0ULL;
188 int ret;
189 struct stat statbuf;
190
191 if (lstat(conf.base_dir_arg, &statbuf) == -1)
192 return -ERRNO_TO_ERROR(errno);
193 if (!S_ISDIR(statbuf.st_mode))
194 return -ERRNO_TO_ERROR(ENOTDIR);
195 device_id = statbuf.st_dev;
196 create_hash_table(conf.hash_table_bits_arg);
197 ret = open_dir_table(1);
198 if (ret < 0)
199 return ret;
200 check_signals();
201 ret = scan_dir(conf.base_dir_arg, &zero);
202 if (ret < 0)
203 goto out;
204 ret = write_uid_list();
205 out:
206 close_all_tables();
207 return ret;
208 }