80-col align code, add license headers
[metastore.git] / metastore.c
blob9ebc8fc8c84e3b7797d3d1f308ec7333341b716c
1 /*
2 * Main functions of the program.
4 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <getopt.h>
26 #include <utime.h>
27 #include <attr/xattr.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "metastore.h"
33 #include "utils.h"
34 #include "metaentry.h"
36 /* Used to signal whether mtimes should be corrected */
37 int do_mtime = 0;
40 * Prints differences between stored and actual metadata
41 * - for use in mentries_compare
43 static void
44 compare_print(struct metaentry *left, struct metaentry *right, int cmp)
46 if (!left) {
47 msg(MSG_QUIET, "%s:\tremoved\n", right->path);
48 return;
51 if (!right) {
52 msg(MSG_QUIET, "%s:\tadded\n", left->path);
53 return;
56 if (cmp == DIFF_NONE) {
57 msg(MSG_DEBUG, "%s:\tno difference\n", left->path);
58 return;
61 msg(MSG_QUIET, "%s:\t", left->path);
62 if (cmp & DIFF_OWNER)
63 msg(MSG_QUIET, "owner ");
64 if (cmp & DIFF_GROUP)
65 msg(MSG_QUIET, "group ");
66 if (cmp & DIFF_MODE)
67 msg(MSG_QUIET, "mode ");
68 if (cmp & DIFF_TYPE)
69 msg(MSG_QUIET, "type ");
70 if (cmp & DIFF_MTIME)
71 msg(MSG_QUIET, "mtime ");
72 if (cmp & DIFF_XATTR)
73 msg(MSG_QUIET, "xattr ");
74 msg(MSG_QUIET, "\n");
78 * Tries to change the real metadata to match the stored one
79 * - for use in mentries_compare
81 static void
82 compare_fix(struct metaentry *left, struct metaentry *right, int cmp)
84 struct group *group;
85 struct passwd *owner;
86 gid_t gid = -1;
87 uid_t uid = -1;
88 struct utimbuf tbuf;
89 int i;
91 if (!left && !right) {
92 msg(MSG_ERROR, "%s called with incorrect arguments\n",
93 __FUNCTION__);
94 return;
97 if (!left) {
98 msg(MSG_NORMAL, "%s:\tremoved\n", right->path);
99 return;
102 if (!right) {
103 msg(MSG_NORMAL, "%s:\tadded\n", left->path);
104 return;
107 if (cmp == DIFF_NONE) {
108 msg(MSG_DEBUG, "%s:\tno difference\n", left->path);
109 return;
112 if (cmp & DIFF_TYPE) {
113 msg(MSG_NORMAL, "%s:\tnew type, will not change metadata\n",
114 left->path);
115 return;
118 msg(MSG_QUIET, "%s:\tchanging metadata\n", left->path);
120 while (cmp & (DIFF_OWNER | DIFF_GROUP)) {
121 if (cmp & DIFF_OWNER) {
122 msg(MSG_NORMAL, "\tchanging owner from %s to %s\n",
123 left->path, left->group, right->group);
124 owner = getpwnam(right->owner);
125 if (!owner) {
126 msg(MSG_DEBUG, "\tgetpwnam failed: %s\n",
127 strerror(errno));
128 break;
130 uid = owner->pw_uid;
133 if (cmp & DIFF_GROUP) {
134 msg(MSG_NORMAL, "\tchanging group from %s to %s\n",
135 left->path, left->group, right->group);
136 group = getgrnam(right->group);
137 if (!group) {
138 msg(MSG_DEBUG, "\tgetgrnam failed: %s\n",
139 strerror(errno));
140 break;
142 gid = group->gr_gid;
145 if (lchown(left->path, uid, gid)) {
146 msg(MSG_DEBUG, "\tlchown failed: %s\n",
147 strerror(errno));
148 break;
150 break;
153 if (cmp & DIFF_MODE) {
154 msg(MSG_NORMAL, "%s:\tchanging mode from 0%o to 0%o\n",
155 left->path, left->mode, right->mode);
156 if (chmod(left->path, left->mode))
157 msg(MSG_DEBUG, "\tchmod failed: %s\n", strerror(errno));
160 if (cmp & DIFF_MTIME) {
161 msg(MSG_NORMAL, "%s:\tchanging mtime from %ld to %ld\n",
162 left->path, left->mtime, right->mtime);
163 /* FIXME: Use utimensat here */
164 tbuf.actime = right->mtime;
165 tbuf.modtime = right->mtime;
166 if (utime(left->path, &tbuf)) {
167 msg(MSG_DEBUG, "\tutime failed: %s\n", strerror(errno));
168 return;
172 if (cmp & DIFF_XATTR) {
173 for (i = 0; i < left->xattrs; i++) {
174 /* Any attrs to remove? */
175 if (mentry_find_xattr(right, left, i) >= 0)
176 continue;
178 msg(MSG_NORMAL, "%s:\tremoving xattr %s\n",
179 left->path, left->xattr_names[i]);
180 if (lremovexattr(left->path, left->xattr_names[i]))
181 msg(MSG_DEBUG, "\tlremovexattr failed: %s\n",
182 strerror(errno));
185 for (i = 0; i < right->xattrs; i++) {
186 /* Any xattrs to add? (on change they are removed above) */
187 if (mentry_find_xattr(left, right, i) >= 0)
188 continue;
190 msg(MSG_NORMAL, "%s:\tadding xattr %s\n",
191 right->path, right->xattr_names[i]);
192 if (lsetxattr(right->path, right->xattr_names[i],
193 right->xattr_values[i],
194 right->xattr_lvalues[i], XATTR_CREATE))
195 msg(MSG_DEBUG, "\tlsetxattr failed: %s\n",
196 strerror(errno));
201 /* Prints usage message and exits */
202 static void
203 usage(const char *arg0, const char *message)
205 if (message)
206 msg(MSG_CRITICAL, "%s: %s\n\n", arg0, msg);
207 msg(MSG_CRITICAL, "Usage: %s ACTION [OPTIONS] [PATH]...\n\n", arg0);
208 msg(MSG_CRITICAL, "Where ACTION is one of:\n"
209 " -d, --diff\tShow differences between stored and real metadata\n"
210 " -s, --save\tSave current metadata\n"
211 " -a, --apply\tApply stored metadata\n"
212 " -h, --help\tHelp message (this text)\n\n"
213 "Valid OPTIONS are (can be given more than once):\n"
214 " -v, --verbose\tPrint more verbose messages\n"
215 " -q, --quiet\tPrint less verbose messages\n");
217 exit(message ? EXIT_FAILURE : EXIT_SUCCESS);
220 /* Options */
221 static struct option long_options[] = {
222 {"compare", 0, 0, 0},
223 {"save", 0, 0, 0},
224 {"apply", 0, 0, 0},
225 {"help", 0, 0, 0},
226 {"verbose", 0, 0, 0},
227 {"quiet", 0, 0, 0},
228 {"mtime", 0, 0, 0},
229 {0, 0, 0, 0}
232 /* Main function */
234 main(int argc, char **argv, char **envp)
236 int i, c;
237 struct metaentry *mhead = NULL;
238 struct metaentry *mfhead = NULL;
239 int action = 0;
241 /* Parse options */
242 i = 0;
243 while (1) {
244 int option_index = 0;
245 c = getopt_long(argc, argv, "csahvqm",
246 long_options, &option_index);
247 if (c == -1)
248 break;
249 switch (c) {
250 case 0:
251 if (!strcmp("verbose", long_options[option_index].name)) {
252 verbosity++;
253 } else if (!strcmp("quiet",
254 long_options[option_index].name)) {
255 verbosity--;
256 } else if (!strcmp("mtime",
257 long_options[option_index].name)) {
258 do_mtime = 1;
259 } else {
260 action |= (1 << option_index);
261 i++;
263 break;
264 case 'c':
265 action |= ACTION_DIFF;
266 i++;
267 break;
268 case 's':
269 action |= ACTION_SAVE;
270 i++;
271 break;
272 case 'a':
273 action |= ACTION_APPLY;
274 i++;
275 break;
276 case 'h':
277 action |= ACTION_HELP;
278 i++;
279 break;
280 case 'v':
281 verbosity++;
282 break;
283 case 'q':
284 verbosity--;
285 break;
286 case 'm':
287 do_mtime = 1;
288 break;
289 default:
290 usage(argv[0], "unknown option");
294 /* Make sure only one action is specified */
295 if (i != 1)
296 usage(argv[0], "incorrect option(s)");
298 /* Perform action */
299 switch (action) {
300 case ACTION_DIFF:
301 mentries_fromfile(&mfhead, METAFILE);
302 if (!mfhead) {
303 msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
304 METAFILE);
305 exit(EXIT_FAILURE);
308 if (optind < argc) {
309 while (optind < argc)
310 mentries_recurse_path(argv[optind++], &mhead);
311 } else {
312 mentries_recurse_path(".", &mhead);
315 if (!mhead) {
316 msg(MSG_CRITICAL,
317 "Failed to load metadata from file system\n");
318 exit(EXIT_FAILURE);
321 mentries_compare(mhead, mfhead, compare_print);
322 break;
324 case ACTION_SAVE:
325 if (optind < argc) {
326 while (optind < argc)
327 mentries_recurse_path(argv[optind++], &mhead);
328 } else {
329 mentries_recurse_path(".", &mhead);
332 if (!mhead) {
333 msg(MSG_CRITICAL,
334 "Failed to load metadata from file system\n");
335 exit(EXIT_FAILURE);
338 mentries_tofile(mhead, METAFILE);
339 break;
341 case ACTION_APPLY:
342 mentries_fromfile(&mfhead, METAFILE);
343 if (!mfhead) {
344 msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
345 METAFILE);
346 exit(EXIT_FAILURE);
349 if (optind < argc) {
350 while (optind < argc)
351 mentries_recurse_path(argv[optind++], &mhead);
352 } else {
353 mentries_recurse_path(".", &mhead);
356 if (!mhead) {
357 msg(MSG_CRITICAL,
358 "Failed to load metadata from file system\n");
359 exit(EXIT_FAILURE);
362 mentries_compare(mhead, mfhead, compare_fix);
363 break;
365 case ACTION_HELP:
366 usage(argv[0], NULL);
369 exit(EXIT_SUCCESS);