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.
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.
22 #include <sys/types.h>
26 #include <attr/xattr.h>
31 #include "metastore.h"
34 #include "metaentry.h"
36 /* metastore settings */
37 static struct metasettings settings
= {
40 .do_emptydirs
= false,
41 .do_removeemptydirs
= false,
45 /* Used to create lists of dirs / other files which are missing in the fs */
46 static struct metaentry
*missingdirs
= NULL
;
47 static struct metaentry
*missingothers
= NULL
;
49 /* Used to create lists of dirs / other files which are missing in metadata */
50 static struct metaentry
*extradirs
= NULL
;
53 * Inserts an entry in a linked list ordered by pathlen
56 insert_entry_plist(struct metaentry
**list
, struct metaentry
*entry
)
58 struct metaentry
**parent
;
60 for (parent
= list
; *parent
; parent
= &((*parent
)->list
)) {
61 if ((*parent
)->pathlen
> entry
->pathlen
)
65 entry
->list
= *parent
;
70 * Prints differences between real and stored actual metadata
71 * - for use in mentries_compare
74 compare_print(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
76 if (!real
&& !stored
) {
77 msg(MSG_ERROR
, "%s called with incorrect arguments\n", __FUNCTION__
);
81 if (cmp
== DIFF_NONE
) {
82 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
86 msg(MSG_QUIET
, "%s:\t", real
? real
->path
: stored
->path
);
89 msg(MSG_QUIET
, "added ", real
->path
);
91 msg(MSG_QUIET
, "removed ", stored
->path
);
93 msg(MSG_QUIET
, "owner ");
95 msg(MSG_QUIET
, "group ");
97 msg(MSG_QUIET
, "mode ");
99 msg(MSG_QUIET
, "type ");
100 if (cmp
& DIFF_MTIME
)
101 msg(MSG_QUIET
, "mtime ");
102 if (cmp
& DIFF_XATTR
)
103 msg(MSG_QUIET
, "xattr ");
104 msg(MSG_QUIET
, "\n");
108 * Tries to change the real metadata to match the stored one
109 * - for use in mentries_compare
112 compare_fix(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
115 struct passwd
*owner
;
121 if (!real
&& !stored
) {
122 msg(MSG_ERROR
, "%s called with incorrect arguments\n",
128 if (S_ISDIR(stored
->mode
))
129 insert_entry_plist(&missingdirs
, stored
);
131 insert_entry_plist(&missingothers
, stored
);
133 msg(MSG_NORMAL
, "%s:\tremoved\n", stored
->path
);
138 if (S_ISDIR(real
->mode
))
139 insert_entry_plist(&extradirs
, real
);
140 msg(MSG_NORMAL
, "%s:\tadded\n", real
->path
);
144 if (cmp
== DIFF_NONE
) {
145 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
149 if (cmp
& DIFF_TYPE
) {
150 msg(MSG_NORMAL
, "%s:\tnew type, will not change metadata\n",
155 msg(MSG_QUIET
, "%s:\tchanging metadata\n", real
->path
);
157 while (cmp
& (DIFF_OWNER
| DIFF_GROUP
)) {
158 if (cmp
& DIFF_OWNER
) {
159 msg(MSG_NORMAL
, "%s:\tchanging owner from %s to %s\n",
160 real
->path
, real
->group
, stored
->group
);
161 owner
= xgetpwnam(stored
->owner
);
163 msg(MSG_DEBUG
, "\tgetpwnam failed: %s\n",
170 if (cmp
& DIFF_GROUP
) {
171 msg(MSG_NORMAL
, "%s:\tchanging group from %s to %s\n",
172 real
->path
, real
->group
, stored
->group
);
173 group
= xgetgrnam(stored
->group
);
175 msg(MSG_DEBUG
, "\tgetgrnam failed: %s\n",
182 if (lchown(real
->path
, uid
, gid
)) {
183 msg(MSG_DEBUG
, "\tlchown failed: %s\n",
190 if (cmp
& DIFF_MODE
) {
191 msg(MSG_NORMAL
, "%s:\tchanging mode from 0%o to 0%o\n",
192 real
->path
, real
->mode
& 07777, stored
->mode
& 07777);
193 if (chmod(real
->path
, stored
->mode
& 07777))
194 msg(MSG_DEBUG
, "\tchmod failed: %s\n", strerror(errno
));
197 /* FIXME: Use utimensat here, or even better - lutimensat */
198 if ((cmp
& DIFF_MTIME
) && S_ISLNK(real
->mode
)) {
199 msg(MSG_NORMAL
, "%s:\tsymlink, not changing mtime\n", real
->path
);
200 } else if (cmp
& DIFF_MTIME
) {
201 msg(MSG_NORMAL
, "%s:\tchanging mtime from %ld to %ld\n",
202 real
->path
, real
->mtime
, stored
->mtime
);
203 tbuf
.actime
= stored
->mtime
;
204 tbuf
.modtime
= stored
->mtime
;
205 if (utime(real
->path
, &tbuf
)) {
206 msg(MSG_DEBUG
, "\tutime failed: %s\n", strerror(errno
));
211 if (cmp
& DIFF_XATTR
) {
212 for (i
= 0; i
< real
->xattrs
; i
++) {
213 /* Any attrs to remove? */
214 if (mentry_find_xattr(stored
, real
, i
) >= 0)
217 msg(MSG_NORMAL
, "%s:\tremoving xattr %s\n",
218 real
->path
, real
->xattr_names
[i
]);
219 if (lremovexattr(real
->path
, real
->xattr_names
[i
]))
220 msg(MSG_DEBUG
, "\tlremovexattr failed: %s\n",
224 for (i
= 0; i
< stored
->xattrs
; i
++) {
225 /* Any xattrs to add? (on change they are removed above) */
226 if (mentry_find_xattr(real
, stored
, i
) >= 0)
229 msg(MSG_NORMAL
, "%s:\tadding xattr %s\n",
230 stored
->path
, stored
->xattr_names
[i
]);
231 if (lsetxattr(stored
->path
, stored
->xattr_names
[i
],
232 stored
->xattr_values
[i
],
233 stored
->xattr_lvalues
[i
], XATTR_CREATE
))
234 msg(MSG_DEBUG
, "\tlsetxattr failed: %s\n",
241 * Tries to fix any empty dirs which are missing from the filesystem by
245 fixup_emptydirs(struct metahash
*real
, struct metahash
*stored
)
247 struct metaentry
*entry
;
248 struct metaentry
*cur
;
249 struct metaentry
**parent
;
253 struct metaentry
*new;
257 msg(MSG_DEBUG
, "\nAttempting to recreate missing dirs\n");
259 /* If directory x/y is missing, but file x/y/z is also missing,
260 * we should prune directory x/y from the list of directories to
261 * recreate since the deletition of x/y is likely to be genuine
262 * (as opposed to empty dir pruning like git/cvs does).
264 * Also, if file x/y/z is missing, any child directories of
265 * x/y should be pruned as they are probably also intentionally
269 msg(MSG_DEBUG
, "List of candidate dirs:\n");
270 for (cur
= missingdirs
; cur
; cur
= cur
->list
)
271 msg(MSG_DEBUG
, " %s\n", cur
->path
);
273 for (entry
= missingothers
; entry
; entry
= entry
->list
) {
274 msg(MSG_DEBUG
, "Pruning using file %s\n", entry
->path
);
275 bpath
= xstrdup(entry
->path
);
276 delim
= strrchr(bpath
, '/');
278 msg(MSG_NORMAL
, "No delimiter found in %s\n", bpath
);
284 parent
= &missingdirs
;
285 for (cur
= *parent
; cur
; cur
= cur
->list
) {
286 if (strcmp(cur
->path
, bpath
)) {
291 msg(MSG_DEBUG
, "Prune phase 1 - %s\n", cur
->path
);
295 /* Now also prune subdirs of the base dir */
298 blen
= strlen(bpath
);
300 parent
= &missingdirs
;
301 for (cur
= *parent
; cur
; cur
= cur
->list
) {
302 if (strncmp(cur
->path
, bpath
, blen
)) {
307 msg(MSG_DEBUG
, "Prune phase 2 - %s\n", cur
->path
);
313 msg(MSG_DEBUG
, "\n");
315 for (cur
= missingdirs
; cur
; cur
= cur
->list
) {
316 msg(MSG_QUIET
, "%s:\trecreating...", cur
->path
);
317 if (mkdir(cur
->path
, cur
->mode
)) {
318 msg(MSG_QUIET
, "failed (%s)\n", strerror(errno
));
321 msg(MSG_QUIET
, "ok\n");
323 new = mentry_create(cur
->path
);
325 msg(MSG_QUIET
, "Failed to get metadata for %s\n");
329 compare_fix(new, cur
, mentry_compare(new, cur
, &settings
));
334 * Deletes any empty dirs present in the filesystem that are missing
336 * An "empty" dir is one which either:
338 * - only contains empty dirs
341 fixup_newemptydirs(void)
343 struct metaentry
*cur
;
344 int removed_dirs
= 1;
349 /* This is a simpleminded algorithm that attempts to rmdir() all
350 * directories discovered missing from the metadata. Naturally, this will
351 * succeed only on the truly empty directories, but depending on the order,
352 * it may mean that parent directory removal are attempted to be removed
353 * *before* the children. To circumvent this, keep looping around all the
354 * directories until none have been successfully removed. This is a
355 * O(N**2) algorithm, so don't try to remove too many nested directories
356 * at once (e.g. thousands).
358 * Note that this will succeed only if each parent directory is writable.
360 while (removed_dirs
) {
362 msg(MSG_DEBUG
, "\nAttempting to delete empty dirs\n");
363 for (cur
= extradirs
; cur
; cur
= cur
->list
) {
364 msg(MSG_QUIET
, "%s:\tremoving...", cur
->path
);
365 if (rmdir(cur
->path
)) {
366 msg(MSG_QUIET
, "failed (%s)\n", strerror(errno
));
370 msg(MSG_QUIET
, "ok\n");
375 /* Prints usage message and exits */
377 usage(const char *arg0
, const char *message
)
380 msg(MSG_CRITICAL
, "%s: %s\n\n", arg0
, message
);
381 msg(MSG_CRITICAL
, "Usage: %s ACTION [OPTION...] [PATH...]\n\n", arg0
);
382 msg(MSG_CRITICAL
, "Where ACTION is one of:\n"
383 " -c, --compare\t\tShow differences between stored and real metadata\n"
384 " -s, --save\t\tSave current metadata\n"
385 " -a, --apply\t\tApply stored metadata\n"
386 " -h, --help\t\tHelp message (this text)\n\n"
387 "Valid OPTIONS are:\n"
388 " -v, --verbose\t\tPrint more verbose messages\n"
389 " -q, --quiet\t\tPrint less verbose messages\n"
390 " -m, --mtime\t\tAlso take mtime into account for diff or apply\n"
391 " -E\t\t\tRemove extra empty directories\n"
392 " -e, --empty-dirs\tRecreate missing empty directories\n"
393 " -g, --git\t\tDo not omit .git directories\n"
394 " -f, --file <file>\tSet metadata file\n"
397 exit(message
? EXIT_FAILURE
: EXIT_SUCCESS
);
401 static struct option long_options
[] = {
402 {"compare", 0, 0, 0},
406 {"verbose", 0, 0, 0},
409 {"empty-dirs", 0, 0, 0},
410 {"remove-empty-dirs", 0, 0, 0},
412 {"file", required_argument
, 0, 0},
418 main(int argc
, char **argv
, char **envp
)
421 struct metahash
*real
= NULL
;
422 struct metahash
*stored
= NULL
;
428 int option_index
= 0;
429 c
= getopt_long(argc
, argv
, "csahvqmeEgf:",
430 long_options
, &option_index
);
435 if (!strcmp("verbose",
436 long_options
[option_index
].name
)) {
438 } else if (!strcmp("quiet",
439 long_options
[option_index
].name
)) {
440 adjust_verbosity(-1);
441 } else if (!strcmp("mtime",
442 long_options
[option_index
].name
)) {
443 settings
.do_mtime
= true;
444 } else if (!strcmp("empty-dirs",
445 long_options
[option_index
].name
)) {
446 settings
.do_emptydirs
= true;
447 } else if (!strcmp("remove-empty-dirs",
448 long_options
[option_index
].name
)) {
449 settings
.do_removeemptydirs
= true;
450 } else if (!strcmp("git",
451 long_options
[option_index
].name
)) {
452 settings
.do_git
= true;
453 } else if (!strcmp("file",
454 long_options
[option_index
].name
)) {
455 settings
.metafile
= optarg
;
457 action
|= (1 << option_index
);
462 action
|= ACTION_DIFF
;
466 action
|= ACTION_SAVE
;
470 action
|= ACTION_APPLY
;
474 action
|= ACTION_HELP
;
481 adjust_verbosity(-1);
484 settings
.do_mtime
= true;
487 settings
.do_emptydirs
= true;
490 settings
.do_removeemptydirs
= true;
493 settings
.do_git
= true;
496 settings
.metafile
= optarg
;
499 usage(argv
[0], "unknown option");
503 /* Make sure only one action is specified */
505 usage(argv
[0], "incorrect option(s)");
507 /* Make sure --empty-dirs is only used with apply */
508 if (settings
.do_emptydirs
&& action
!= ACTION_APPLY
)
509 usage(argv
[0], "--empty-dirs is only valid with --apply");
511 /* Make sure --remove-empty-dirs is only used with apply */
512 if (settings
.do_removeemptydirs
&& action
!= ACTION_APPLY
)
513 usage(argv
[0], "--remove-empty-dirs is only valid with --apply");
518 mentries_fromfile(&stored
, settings
.metafile
);
520 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
526 while (optind
< argc
)
527 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
529 mentries_recurse_path(".", &real
, &settings
);
534 "Failed to load metadata from file system\n");
538 mentries_compare(real
, stored
, compare_print
, &settings
);
543 while (optind
< argc
)
544 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
546 mentries_recurse_path(".", &real
, &settings
);
551 "Failed to load metadata from file system\n");
555 mentries_tofile(real
, settings
.metafile
);
559 mentries_fromfile(&stored
, settings
.metafile
);
561 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
567 while (optind
< argc
)
568 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
570 mentries_recurse_path(".", &real
, &settings
);
575 "Failed to load metadata from file system\n");
579 mentries_compare(real
, stored
, compare_fix
, &settings
);
581 if (settings
.do_emptydirs
)
582 fixup_emptydirs(real
, stored
);
583 if (settings
.do_removeemptydirs
)
584 fixup_newemptydirs();
588 usage(argv
[0], NULL
);