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; only version 2 of the License is applicable.
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 #define _DEFAULT_SOURCE
22 #include <sys/types.h>
26 #include <sys/xattr.h>
32 #include "metastore.h"
35 #include "metaentry.h"
37 /* metastore settings */
38 static struct metasettings settings
= {
41 .do_emptydirs
= false,
42 .do_removeemptydirs
= false,
46 /* Used to create lists of dirs / other files which are missing in the fs */
47 static struct metaentry
*missingdirs
= NULL
;
48 static struct metaentry
*missingothers
= NULL
;
50 /* Used to create lists of dirs / other files which are missing in metadata */
51 static struct metaentry
*extradirs
= NULL
;
54 * Inserts an entry in a linked list ordered by pathlen
57 insert_entry_plist(struct metaentry
**list
, struct metaentry
*entry
)
59 struct metaentry
**parent
;
61 for (parent
= list
; *parent
; parent
= &((*parent
)->list
)) {
62 if ((*parent
)->pathlen
> entry
->pathlen
)
66 entry
->list
= *parent
;
71 * Inserts an entry in a linked list ordered by pathlen descendingly
74 insert_entry_pdlist(struct metaentry
**list
, struct metaentry
*entry
)
76 struct metaentry
**parent
;
78 for (parent
= list
; *parent
; parent
= &((*parent
)->list
)) {
79 if ((*parent
)->pathlen
< entry
->pathlen
)
83 entry
->list
= *parent
;
88 * Prints differences between real and stored actual metadata
89 * - for use in mentries_compare
92 compare_print(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
94 if (!real
&& (!stored
|| (cmp
== DIFF_NONE
|| cmp
& DIFF_ADDED
))) {
95 msg(MSG_ERROR
, "%s called with incorrect arguments\n", __func__
);
99 if (cmp
== DIFF_NONE
) {
100 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
104 msg(MSG_QUIET
, "%s:\t", real
? real
->path
: stored
->path
);
106 if (cmp
& DIFF_ADDED
)
107 msg(MSG_QUIET
, "added ", real
->path
);
109 msg(MSG_QUIET
, "removed ", stored
->path
);
110 if (cmp
& DIFF_OWNER
)
111 msg(MSG_QUIET
, "owner ");
112 if (cmp
& DIFF_GROUP
)
113 msg(MSG_QUIET
, "group ");
115 msg(MSG_QUIET
, "mode ");
117 msg(MSG_QUIET
, "type ");
118 if (cmp
& DIFF_MTIME
)
119 msg(MSG_QUIET
, "mtime ");
120 if (cmp
& DIFF_XATTR
)
121 msg(MSG_QUIET
, "xattr ");
122 msg(MSG_QUIET
, "\n");
126 * Tries to change the real metadata to match the stored one
127 * - for use in mentries_compare
130 compare_fix(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
133 struct passwd
*owner
;
139 if (!real
&& !stored
) {
140 msg(MSG_ERROR
, "%s called with incorrect arguments\n", __func__
);
145 if (S_ISDIR(stored
->mode
))
146 insert_entry_plist(&missingdirs
, stored
);
148 insert_entry_plist(&missingothers
, stored
);
150 msg(MSG_NORMAL
, "%s:\tremoved\n", stored
->path
);
155 if (S_ISDIR(real
->mode
))
156 insert_entry_pdlist(&extradirs
, real
);
157 msg(MSG_NORMAL
, "%s:\tadded\n", real
->path
);
161 if (cmp
== DIFF_NONE
) {
162 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
166 if (cmp
& DIFF_TYPE
) {
167 msg(MSG_NORMAL
, "%s:\tnew type, will not change metadata\n",
172 msg(MSG_QUIET
, "%s:\tchanging metadata\n", real
->path
);
174 while (cmp
& (DIFF_OWNER
| DIFF_GROUP
)) {
175 if (cmp
& DIFF_OWNER
) {
176 msg(MSG_NORMAL
, "%s:\tchanging owner from %s to %s\n",
177 real
->path
, real
->group
, stored
->group
);
178 owner
= xgetpwnam(stored
->owner
);
180 msg(MSG_DEBUG
, "\tgetpwnam failed: %s\n",
187 if (cmp
& DIFF_GROUP
) {
188 msg(MSG_NORMAL
, "%s:\tchanging group from %s to %s\n",
189 real
->path
, real
->group
, stored
->group
);
190 group
= xgetgrnam(stored
->group
);
192 msg(MSG_DEBUG
, "\tgetgrnam failed: %s\n",
199 if (lchown(real
->path
, uid
, gid
)) {
200 msg(MSG_DEBUG
, "\tlchown failed: %s\n",
207 if (cmp
& DIFF_MODE
) {
208 msg(MSG_NORMAL
, "%s:\tchanging mode from 0%o to 0%o\n",
209 real
->path
, real
->mode
& 07777, stored
->mode
& 07777);
210 if (chmod(real
->path
, stored
->mode
& 07777))
211 msg(MSG_DEBUG
, "\tchmod failed: %s\n", strerror(errno
));
214 /* FIXME: Use utimensat here, or even better - lutimensat */
215 if ((cmp
& DIFF_MTIME
) && S_ISLNK(real
->mode
)) {
216 msg(MSG_NORMAL
, "%s:\tsymlink, not changing mtime\n", real
->path
);
217 } else if (cmp
& DIFF_MTIME
) {
218 msg(MSG_NORMAL
, "%s:\tchanging mtime from %ld to %ld\n",
219 real
->path
, real
->mtime
, stored
->mtime
);
220 tbuf
.actime
= stored
->mtime
;
221 tbuf
.modtime
= stored
->mtime
;
222 if (utime(real
->path
, &tbuf
)) {
223 msg(MSG_DEBUG
, "\tutime failed: %s\n", strerror(errno
));
228 if (cmp
& DIFF_XATTR
) {
229 for (i
= 0; i
< real
->xattrs
; i
++) {
230 /* Any attrs to remove? */
231 if (mentry_find_xattr(stored
, real
, i
) >= 0)
234 msg(MSG_NORMAL
, "%s:\tremoving xattr %s\n",
235 real
->path
, real
->xattr_names
[i
]);
236 if (lremovexattr(real
->path
, real
->xattr_names
[i
]))
237 msg(MSG_DEBUG
, "\tlremovexattr failed: %s\n",
241 for (i
= 0; i
< stored
->xattrs
; i
++) {
242 /* Any xattrs to add? (on change they are removed above) */
243 if (mentry_find_xattr(real
, stored
, i
) >= 0)
246 msg(MSG_NORMAL
, "%s:\tadding xattr %s\n",
247 stored
->path
, stored
->xattr_names
[i
]);
248 if (lsetxattr(stored
->path
, stored
->xattr_names
[i
],
249 stored
->xattr_values
[i
],
250 stored
->xattr_lvalues
[i
], XATTR_CREATE
)
252 msg(MSG_DEBUG
, "\tlsetxattr failed: %s\n",
259 * Tries to fix any empty dirs which are missing from the filesystem by
263 fixup_emptydirs(void)
265 struct metaentry
*entry
;
266 struct metaentry
*cur
;
267 struct metaentry
**parent
;
271 struct metaentry
*new;
275 msg(MSG_DEBUG
, "\nAttempting to recreate missing dirs\n");
277 /* If directory x/y is missing, but file x/y/z is also missing,
278 * we should prune directory x/y from the list of directories to
279 * recreate since the deletition of x/y is likely to be genuine
280 * (as opposed to empty dir pruning like git/cvs does).
282 * Also, if file x/y/z is missing, any child directories of
283 * x/y should be pruned as they are probably also intentionally
287 msg(MSG_DEBUG
, "List of candidate dirs:\n");
288 for (cur
= missingdirs
; cur
; cur
= cur
->list
)
289 msg(MSG_DEBUG
, " %s\n", cur
->path
);
291 for (entry
= missingothers
; entry
; entry
= entry
->list
) {
292 msg(MSG_DEBUG
, "Pruning using file %s\n", entry
->path
);
293 bpath
= xstrdup(entry
->path
);
294 delim
= strrchr(bpath
, '/');
296 msg(MSG_NORMAL
, "No delimiter found in %s\n", bpath
);
302 parent
= &missingdirs
;
303 for (cur
= *parent
; cur
; cur
= cur
->list
) {
304 if (strcmp(cur
->path
, bpath
)) {
309 msg(MSG_DEBUG
, "Prune phase 1 - %s\n", cur
->path
);
313 /* Now also prune subdirs of the base dir */
316 blen
= strlen(bpath
);
318 parent
= &missingdirs
;
319 for (cur
= *parent
; cur
; cur
= cur
->list
) {
320 if (strncmp(cur
->path
, bpath
, blen
)) {
325 msg(MSG_DEBUG
, "Prune phase 2 - %s\n", cur
->path
);
331 msg(MSG_DEBUG
, "\n");
333 for (cur
= missingdirs
; cur
; cur
= cur
->list
) {
334 msg(MSG_QUIET
, "%s:\trecreating...", cur
->path
);
335 if (mkdir(cur
->path
, cur
->mode
)) {
336 msg(MSG_QUIET
, "failed (%s)\n", strerror(errno
));
339 msg(MSG_QUIET
, "ok\n");
341 new = mentry_create(cur
->path
);
343 msg(MSG_QUIET
, "Failed to get metadata for %s\n");
347 compare_fix(new, cur
, mentry_compare(new, cur
, &settings
));
352 * Deletes any empty dirs present in the filesystem that are missing
354 * An "empty" dir is one which either:
356 * - only contains empty dirs
359 fixup_newemptydirs(void)
361 struct metaentry
**cur
;
362 int removed_dirs
= 1;
367 /* This is a simpleminded algorithm that attempts to rmdir() all
368 * directories discovered missing from the metadata. Naturally, this will
369 * succeed only on the truly empty directories, but depending on the order,
370 * it may mean that parent directory removal are attempted to be removed
371 * *before* the children. To circumvent this, keep looping around all the
372 * directories until none have been successfully removed. This is a
373 * O(N**2) algorithm, so don't try to remove too many nested directories
374 * at once (e.g. thousands).
376 * Note that this will succeed only if each parent directory is writable.
378 while (removed_dirs
) {
380 msg(MSG_DEBUG
, "\nAttempting to delete empty dirs\n");
381 for (cur
= &extradirs
; *cur
;) {
382 msg(MSG_QUIET
, "%s:\tremoving...", (*cur
)->path
);
383 if (rmdir((*cur
)->path
)) {
384 msg(MSG_QUIET
, "failed (%s)\n", strerror(errno
));
388 /* No freeing, because OS will do the job at the end. */
391 msg(MSG_QUIET
, "ok\n");
396 /* Outputs version information and exits */
400 msg(MSG_QUIET
, "metastore %s\n", METASTORE_VER
);
405 /* Prints usage message and exits */
407 usage(const char *arg0
, const char *message
)
410 msg(MSG_CRITICAL
, "%s: %s\n\n", arg0
, message
);
412 "Usage: %s ACTION [OPTION...] [PATH...]\n",
416 "Where ACTION is one of:\n"
417 " -c, --compare Show differences between stored and real metadata\n"
418 " -s, --save Save current metadata\n"
419 " -a, --apply Apply stored metadata\n"
420 " -d, --dump Dump stored (if no PATH is given) or real metadata\n"
421 " (if PATH is present, e.g. ./) in human-readable form\n"
422 " -V, --version Output version information and exit\n"
423 " -h, --help Help message (this text)\n"
425 "Valid OPTIONS are:\n"
426 " -v, --verbose Print more verbose messages\n"
427 " -q, --quiet Print less verbose messages\n"
428 " -m, --mtime Also take mtime into account for diff or apply\n"
429 " -e, --empty-dirs Recreate missing empty directories\n"
430 " -E, --remove-empty-dirs Remove extra empty directories\n"
431 " -g, --git Do not omit .git directories\n"
432 " -f, --file=FILE Set metadata file (" METAFILE
" by default)\n"
435 exit(message
? EXIT_FAILURE
: EXIT_SUCCESS
);
439 static struct option long_options
[] = {
440 { "compare", no_argument
, NULL
, 'c' },
441 { "save", no_argument
, NULL
, 's' },
442 { "apply", no_argument
, NULL
, 'a' },
443 { "dump", no_argument
, NULL
, 'd' },
444 { "version", no_argument
, NULL
, 'V' },
445 { "help", no_argument
, NULL
, 'h' },
446 { "verbose", no_argument
, NULL
, 'v' },
447 { "quiet", no_argument
, NULL
, 'q' },
448 { "mtime", no_argument
, NULL
, 'm' },
449 { "empty-dirs", no_argument
, NULL
, 'e' },
450 { "remove-empty-dirs", no_argument
, NULL
, 'E' },
451 { "git", no_argument
, NULL
, 'g' },
452 { "file", required_argument
, NULL
, 'f' },
458 main(int argc
, char **argv
)
461 struct metahash
*real
= NULL
;
462 struct metahash
*stored
= NULL
;
468 int option_index
= 0;
469 c
= getopt_long(argc
, argv
, "csadVhvqmeEgf:",
470 long_options
, &option_index
);
474 case 'c': /* compare */ action
|= ACTION_DIFF
; i
++; break;
475 case 's': /* save */ action
|= ACTION_SAVE
; i
++; break;
476 case 'a': /* apply */ action
|= ACTION_APPLY
; i
++; break;
477 case 'd': /* dump */ action
|= ACTION_DUMP
; i
++; break;
478 case 'V': /* version */ action
|= ACTION_VER
; i
++; break;
479 case 'h': /* help */ action
|= ACTION_HELP
; i
++; break;
480 case 'v': /* verbose */ adjust_verbosity(1); break;
481 case 'q': /* quiet */ adjust_verbosity(-1); break;
482 case 'm': /* mtime */ settings
.do_mtime
= true; break;
483 case 'e': /* empty-dirs */ settings
.do_emptydirs
= true; break;
484 case 'E': /* remove-empty-dirs */ settings
.do_removeemptydirs
= true;
486 case 'g': /* git */ settings
.do_git
= true; break;
487 case 'f': /* file */ settings
.metafile
= optarg
; break;
489 usage(argv
[0], "unknown option");
493 /* Make sure only one action is specified */
495 usage(argv
[0], "incorrect option(s)");
497 /* Make sure --empty-dirs is only used with apply */
498 if (settings
.do_emptydirs
&& action
!= ACTION_APPLY
)
499 usage(argv
[0], "--empty-dirs is only valid with --apply");
501 /* Make sure --remove-empty-dirs is only used with apply */
502 if (settings
.do_removeemptydirs
&& action
!= ACTION_APPLY
)
503 usage(argv
[0], "--remove-empty-dirs is only valid with --apply");
505 if (action
== ACTION_VER
)
508 if (action
== ACTION_HELP
)
509 usage(argv
[0], NULL
);
512 if (action
& ACTIONS_READING
&& !(action
== ACTION_DUMP
&& optind
< argc
)) {
513 mentries_fromfile(&stored
, settings
.metafile
);
515 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
522 while (optind
< argc
)
523 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
524 } else if (action
!= ACTION_DUMP
) {
525 mentries_recurse_path(".", &real
, &settings
);
528 if (!real
&& (action
!= ACTION_DUMP
|| optind
< argc
)) {
530 "Failed to load metadata from file system\n");
536 mentries_compare(real
, stored
, compare_print
, &settings
);
539 mentries_tofile(real
, settings
.metafile
);
542 mentries_compare(real
, stored
, compare_fix
, &settings
);
543 if (settings
.do_emptydirs
)
545 if (settings
.do_removeemptydirs
)
546 fixup_newemptydirs();
549 mentries_dump(real
? real
: stored
);