Move the uid-range parser to string.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 /* dir count. */
121 static uint64_t current_dir_num;
122
123 uint64_t this_dir_num = ++current_dir_num;
124
125 check_signals();
126 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)current_dir_num, dirname);
127 ret = adu_opendir(dirname, &dir, &cwd_fd);
128 if (ret < 0) {
129 if (ret != -ERRNO_TO_ERROR(EACCES))
130 return ret;
131 WARNING_LOG("permission denied for %s\n", dirname);
132 return 1;
133 }
134 while ((entry = readdir(dir))) {
135 mode_t m;
136 struct stat64 s;
137 uint32_t uid;
138 uint64_t size;
139 struct user_info *ui;
140
141 if (!strcmp(entry->d_name, "."))
142 continue;
143 if (!strcmp(entry->d_name, ".."))
144 continue;
145 if (lstat64(entry->d_name, &s) == -1) {
146 WARNING_LOG("lstat64 error for %s/%s (%s)\n",
147 dirname, entry->d_name, strerror(errno));
148 continue;
149 }
150 m = s.st_mode;
151 if (!S_ISREG(m) && !S_ISDIR(m))
152 continue;
153 if (S_ISDIR(m)) {
154 if (conf.one_file_system_given && s.st_dev != device_id)
155 continue;
156 ret = scan_dir(entry->d_name, &this_dir_num);
157 if (ret < 0)
158 goto out;
159 continue;
160 }
161 /* regular file */
162 size = s.st_size;
163 dir_size += size;
164 dir_files++;
165 uid = s.st_uid;
166 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
167 if (ret < 0)
168 goto out;
169 ui->bytes += size;
170 ui->files++;
171 ret = update_user_row(ui->table, this_dir_num, &size);
172 if (ret < 0)
173 goto out;
174 }
175 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
176 &dir_size, &dir_files);
177 out:
178 closedir(dir);
179 ret2 = adu_fchdir(cwd_fd);
180 if (ret2 < 0 && ret >= 0)
181 ret = ret2;
182 close(cwd_fd);
183 return ret;
184 }
185
186 int com_create()
187 {
188 uint64_t zero = 0ULL;
189 int ret;
190 struct stat statbuf;
191
192 if (lstat(conf.base_dir_arg, &statbuf) == -1)
193 return -ERRNO_TO_ERROR(errno);
194 if (!S_ISDIR(statbuf.st_mode))
195 return -ERRNO_TO_ERROR(ENOTDIR);
196 device_id = statbuf.st_dev;
197 create_hash_table(conf.hash_table_bits_arg);
198 ret = open_dir_table(1);
199 if (ret < 0)
200 return ret;
201 check_signals();
202 ret = scan_dir(conf.base_dir_arg, &zero);
203 if (ret < 0)
204 goto out;
205 ret = write_uid_list();
206 out:
207 close_all_tables();
208 return ret;
209 }