forked from przemoc/metastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metastore.c
533 lines (469 loc) · 12.9 KB
/
metastore.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* Main functions of the program.
*
* Copyright (C) 2007 David Härdeman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define _BSD_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <utime.h>
#include <attr/xattr.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "metastore.h"
#include "settings.h"
#include "utils.h"
#include "metaentry.h"
/* metastore settings */
static struct metasettings settings = {
.metafile = METAFILE,
.do_mtime = false,
.do_emptydirs = false,
.do_git = false,
};
/* Used to create lists of dirs / other files which are missing in the fs */
static struct metaentry *missingdirs = NULL;
static struct metaentry *missingothers = NULL;
/*
* Inserts an entry in a linked list ordered by pathlen
*/
static void
insert_entry_plist(struct metaentry **list, struct metaentry *entry)
{
struct metaentry **parent;
for (parent = list; *parent; parent = &((*parent)->list)) {
if ((*parent)->pathlen > entry->pathlen)
break;
}
entry->list = *parent;
*parent = entry;
}
/*
* Prints differences between real and stored actual metadata
* - for use in mentries_compare
*/
static void
compare_print(struct metaentry *real, struct metaentry *stored, int cmp)
{
if (!real && !stored) {
msg(MSG_ERROR, "%s called with incorrect arguments\n", __FUNCTION__);
return;
}
if (cmp == DIFF_NONE) {
msg(MSG_DEBUG, "%s:\tno difference\n", real->path);
return;
}
msg(MSG_QUIET, "%s:\t", real ? real->path : stored->path);
if (cmp & DIFF_ADDED)
msg(MSG_QUIET, "added ", real->path);
if (cmp & DIFF_DELE)
msg(MSG_QUIET, "removed ", stored->path);
if (cmp & DIFF_OWNER)
msg(MSG_QUIET, "owner ");
if (cmp & DIFF_GROUP)
msg(MSG_QUIET, "group ");
if (cmp & DIFF_MODE)
msg(MSG_QUIET, "mode ");
if (cmp & DIFF_TYPE)
msg(MSG_QUIET, "type ");
if (cmp & DIFF_MTIME)
msg(MSG_QUIET, "mtime ");
if (cmp & DIFF_XATTR)
msg(MSG_QUIET, "xattr ");
msg(MSG_QUIET, "\n");
}
/*
* Tries to change the real metadata to match the stored one
* - for use in mentries_compare
*/
static void
compare_fix(struct metaentry *real, struct metaentry *stored, int cmp)
{
struct group *group;
struct passwd *owner;
gid_t gid = -1;
uid_t uid = -1;
struct utimbuf tbuf;
unsigned i;
if (!real && !stored) {
msg(MSG_ERROR, "%s called with incorrect arguments\n",
__FUNCTION__);
return;
}
if (!real) {
if (S_ISDIR(stored->mode))
insert_entry_plist(&missingdirs, stored);
else
insert_entry_plist(&missingothers, stored);
msg(MSG_NORMAL, "%s:\tremoved\n", stored->path);
return;
}
if (!stored) {
msg(MSG_NORMAL, "%s:\tadded\n", real->path);
return;
}
if (cmp == DIFF_NONE) {
msg(MSG_DEBUG, "%s:\tno difference\n", real->path);
return;
}
if (cmp & DIFF_TYPE) {
msg(MSG_NORMAL, "%s:\tnew type, will not change metadata\n",
real->path);
return;
}
msg(MSG_QUIET, "%s:\tchanging metadata\n", real->path);
while (cmp & (DIFF_OWNER | DIFF_GROUP)) {
if (cmp & DIFF_OWNER) {
msg(MSG_NORMAL, "%s:\tchanging owner from %s to %s\n",
real->path, real->group, stored->group);
owner = xgetpwnam(stored->owner);
if (!owner) {
msg(MSG_DEBUG, "\tgetpwnam failed: %s\n",
strerror(errno));
break;
}
uid = owner->pw_uid;
}
if (cmp & DIFF_GROUP) {
msg(MSG_NORMAL, "%s:\tchanging group from %s to %s\n",
real->path, real->group, stored->group);
group = xgetgrnam(stored->group);
if (!group) {
msg(MSG_DEBUG, "\tgetgrnam failed: %s\n",
strerror(errno));
break;
}
gid = group->gr_gid;
}
if (lchown(real->path, uid, gid)) {
msg(MSG_DEBUG, "\tlchown failed: %s\n",
strerror(errno));
break;
}
break;
}
if (cmp & DIFF_MODE) {
msg(MSG_NORMAL, "%s:\tchanging mode from 0%o to 0%o\n",
real->path, real->mode & 07777, stored->mode & 07777);
if (chmod(real->path, stored->mode & 07777))
msg(MSG_DEBUG, "\tchmod failed: %s\n", strerror(errno));
}
/* FIXME: Use utimensat here, or even better - lutimensat */
if ((cmp & DIFF_MTIME) && S_ISLNK(real->mode)) {
msg(MSG_NORMAL, "%s:\tsymlink, not changing mtime\n", real->path);
} else if (cmp & DIFF_MTIME) {
msg(MSG_NORMAL, "%s:\tchanging mtime from %ld to %ld\n",
real->path, real->mtime, stored->mtime);
tbuf.actime = stored->mtime;
tbuf.modtime = stored->mtime;
if (utime(real->path, &tbuf)) {
msg(MSG_DEBUG, "\tutime failed: %s\n", strerror(errno));
return;
}
}
if (cmp & DIFF_XATTR) {
for (i = 0; i < real->xattrs; i++) {
/* Any attrs to remove? */
if (mentry_find_xattr(stored, real, i) >= 0)
continue;
msg(MSG_NORMAL, "%s:\tremoving xattr %s\n",
real->path, real->xattr_names[i]);
if (lremovexattr(real->path, real->xattr_names[i]))
msg(MSG_DEBUG, "\tlremovexattr failed: %s\n",
strerror(errno));
}
for (i = 0; i < stored->xattrs; i++) {
/* Any xattrs to add? (on change they are removed above) */
if (mentry_find_xattr(real, stored, i) >= 0)
continue;
msg(MSG_NORMAL, "%s:\tadding xattr %s\n",
stored->path, stored->xattr_names[i]);
if (lsetxattr(stored->path, stored->xattr_names[i],
stored->xattr_values[i],
stored->xattr_lvalues[i], XATTR_CREATE))
msg(MSG_DEBUG, "\tlsetxattr failed: %s\n",
strerror(errno));
}
}
}
/*
* Tries to fix any empty dirs which are missing by recreating them.
* An "empty" dir is one which either:
* - is empty; or
* - only contained empty dirs
*/
static void
fixup_emptydirs(struct metahash *real, struct metahash *stored)
{
struct metaentry *entry;
struct metaentry *cur;
struct metaentry **parent;
char *bpath;
char *delim;
size_t blen;
struct metaentry *new;
if (!missingdirs)
return;
msg(MSG_DEBUG, "\nAttempting to recreate missing dirs\n");
/* If directory x/y is missing, but file x/y/z is also missing,
* we should prune directory x/y from the list of directories to
* recreate since the deletition of x/y is likely to be genuine
* (as opposed to empty dir pruning like git/cvs does).
*
* Also, if file x/y/z is missing, any child directories of
* x/y should be pruned as they are probably also intentionally
* removed.
*/
msg(MSG_DEBUG, "List of candidate dirs:\n");
for (cur = missingdirs; cur; cur = cur->list)
msg(MSG_DEBUG, " %s\n", cur->path);
for (entry = missingothers; entry; entry = entry->list) {
msg(MSG_DEBUG, "Pruning using file %s\n", entry->path);
bpath = xstrdup(entry->path);
delim = strrchr(bpath, '/');
if (!delim) {
msg(MSG_NORMAL, "No delimiter found in %s\n", bpath);
free(bpath);
continue;
}
*delim = '\0';
parent = &missingdirs;
for (cur = *parent; cur; cur = cur->list) {
if (strcmp(cur->path, bpath)) {
parent = &cur->list;
continue;
}
msg(MSG_DEBUG, "Prune phase 1 - %s\n", cur->path);
*parent = cur->list;
}
/* Now also prune subdirs of the base dir */
*delim++ = '/';
*delim = '\0';
blen = strlen(bpath);
parent = &missingdirs;
for (cur = *parent; cur; cur = cur->list) {
if (strncmp(cur->path, bpath, blen)) {
parent = &cur->list;
continue;
}
msg(MSG_DEBUG, "Prune phase 2 - %s\n", cur->path);
*parent = cur->list;
}
free(bpath);
}
msg(MSG_DEBUG, "\n");
for (cur = missingdirs; cur; cur = cur->list) {
msg(MSG_QUIET, "%s:\trecreating...", cur->path);
if (mkdir(cur->path, cur->mode)) {
msg(MSG_QUIET, "failed (%s)\n", strerror(errno));
continue;
}
msg(MSG_QUIET, "ok\n");
new = mentry_create(cur->path);
if (!new) {
msg(MSG_QUIET, "Failed to get metadata for %s\n");
continue;
}
compare_fix(new, cur, mentry_compare(new, cur, &settings));
}
}
/* Prints usage message and exits */
static void
usage(const char *arg0, const char *message)
{
if (message)
msg(MSG_CRITICAL, "%s: %s\n\n", arg0, message);
msg(MSG_CRITICAL, "Usage: %s ACTION [OPTION...] [PATH...]\n\n", arg0);
msg(MSG_CRITICAL, "Where ACTION is one of:\n"
" -c, --compare\t\tShow differences between stored and real metadata\n"
" -s, --save\t\tSave current metadata\n"
" -a, --apply\t\tApply stored metadata\n"
" -h, --help\t\tHelp message (this text)\n\n"
"Valid OPTIONS are:\n"
" -v, --verbose\t\tPrint more verbose messages\n"
" -q, --quiet\t\tPrint less verbose messages\n"
" -m, --mtime\t\tAlso take mtime into account for diff or apply\n"
" -e, --empty-dirs\tRecreate missing empty directories (experimental)\n"
" -g, --git\t\tDo not omit .git directories\n"
" -f, --file <file>\tSet metadata file\n"
);
exit(message ? EXIT_FAILURE : EXIT_SUCCESS);
}
/* Options */
static struct option long_options[] = {
{"compare", 0, 0, 0},
{"save", 0, 0, 0},
{"apply", 0, 0, 0},
{"help", 0, 0, 0},
{"verbose", 0, 0, 0},
{"quiet", 0, 0, 0},
{"mtime", 0, 0, 0},
{"empty-dirs", 0, 0, 0},
{"git", 0, 0, 0},
{"file", required_argument, 0, 0},
{0, 0, 0, 0}
};
/* Main function */
int
main(int argc, char **argv, char **envp)
{
int i, c;
struct metahash *real = NULL;
struct metahash *stored = NULL;
int action = 0;
/* Parse options */
i = 0;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "csahvqmegf:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
if (!strcmp("verbose",
long_options[option_index].name)) {
adjust_verbosity(1);
} else if (!strcmp("quiet",
long_options[option_index].name)) {
adjust_verbosity(-1);
} else if (!strcmp("mtime",
long_options[option_index].name)) {
settings.do_mtime = true;
} else if (!strcmp("empty-dirs",
long_options[option_index].name)) {
settings.do_emptydirs = true;
} else if (!strcmp("git",
long_options[option_index].name)) {
settings.do_git = true;
} else if (!strcmp("file",
long_options[option_index].name)) {
settings.metafile = optarg;
} else {
action |= (1 << option_index);
i++;
}
break;
case 'c':
action |= ACTION_DIFF;
i++;
break;
case 's':
action |= ACTION_SAVE;
i++;
break;
case 'a':
action |= ACTION_APPLY;
i++;
break;
case 'h':
action |= ACTION_HELP;
i++;
break;
case 'v':
adjust_verbosity(1);
break;
case 'q':
adjust_verbosity(-1);
break;
case 'm':
settings.do_mtime = true;
break;
case 'e':
settings.do_emptydirs = true;
break;
case 'g':
settings.do_git = true;
break;
case 'f':
settings.metafile = optarg;
break;
default:
usage(argv[0], "unknown option");
}
}
/* Make sure only one action is specified */
if (i != 1)
usage(argv[0], "incorrect option(s)");
/* Make sure --empty-dirs is only used with apply */
if (settings.do_emptydirs && action != ACTION_APPLY)
usage(argv[0], "--empty-dirs is only valid with --apply");
/* Perform action */
switch (action) {
case ACTION_DIFF:
mentries_fromfile(&stored, settings.metafile);
if (!stored) {
msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
settings.metafile);
exit(EXIT_FAILURE);
}
if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &real, &settings);
} else {
mentries_recurse_path(".", &real, &settings);
}
if (!real) {
msg(MSG_CRITICAL,
"Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
mentries_compare(real, stored, compare_print, &settings);
break;
case ACTION_SAVE:
if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &real, &settings);
} else {
mentries_recurse_path(".", &real, &settings);
}
if (!real) {
msg(MSG_CRITICAL,
"Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
mentries_tofile(real, settings.metafile);
break;
case ACTION_APPLY:
mentries_fromfile(&stored, settings.metafile);
if (!stored) {
msg(MSG_CRITICAL, "Failed to load metadata from %s\n",
settings.metafile);
exit(EXIT_FAILURE);
}
if (optind < argc) {
while (optind < argc)
mentries_recurse_path(argv[optind++], &real, &settings);
} else {
mentries_recurse_path(".", &real, &settings);
}
if (!real) {
msg(MSG_CRITICAL,
"Failed to load metadata from file system\n");
exit(EXIT_FAILURE);
}
mentries_compare(real, stored, compare_fix, &settings);
if (settings.do_emptydirs)
fixup_emptydirs(real, stored);
break;
case ACTION_HELP:
usage(argv[0], NULL);
}
exit(EXIT_SUCCESS);
}