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
);
382 "Usage: %s ACTION [OPTION...] [PATH...]\n",
386 "Where ACTION is one of:\n"
387 " -c, --compare Show differences between stored and real metadata\n"
388 " -s, --save Save current metadata\n"
389 " -a, --apply Apply stored metadata\n"
390 " -h, --help Help message (this text)\n"
392 "Valid OPTIONS are:\n"
393 " -v, --verbose Print more verbose messages\n"
394 " -q, --quiet Print less verbose messages\n"
395 " -m, --mtime Also take mtime into account for diff or apply\n"
396 " -e, --empty-dirs Recreate missing empty directories\n"
397 " -E, --remove-empty-dirs Remove extra empty directories\n"
398 " -g, --git Do not omit .git directories\n"
399 " -f, --file=FILE Set metadata file to FILE\n"
402 exit(message
? EXIT_FAILURE
: EXIT_SUCCESS
);
406 static struct option long_options
[] = {
407 {"compare", 0, 0, 0},
411 {"verbose", 0, 0, 0},
414 {"empty-dirs", 0, 0, 0},
415 {"remove-empty-dirs", 0, 0, 0},
417 {"file", required_argument
, 0, 0},
423 main(int argc
, char **argv
, char **envp
)
426 struct metahash
*real
= NULL
;
427 struct metahash
*stored
= NULL
;
433 int option_index
= 0;
434 c
= getopt_long(argc
, argv
, "csahvqmeEgf:",
435 long_options
, &option_index
);
440 if (!strcmp("verbose",
441 long_options
[option_index
].name
)) {
443 } else if (!strcmp("quiet",
444 long_options
[option_index
].name
)) {
445 adjust_verbosity(-1);
446 } else if (!strcmp("mtime",
447 long_options
[option_index
].name
)) {
448 settings
.do_mtime
= true;
449 } else if (!strcmp("empty-dirs",
450 long_options
[option_index
].name
)) {
451 settings
.do_emptydirs
= true;
452 } else if (!strcmp("remove-empty-dirs",
453 long_options
[option_index
].name
)) {
454 settings
.do_removeemptydirs
= true;
455 } else if (!strcmp("git",
456 long_options
[option_index
].name
)) {
457 settings
.do_git
= true;
458 } else if (!strcmp("file",
459 long_options
[option_index
].name
)) {
460 settings
.metafile
= optarg
;
462 action
|= (1 << option_index
);
467 action
|= ACTION_DIFF
;
471 action
|= ACTION_SAVE
;
475 action
|= ACTION_APPLY
;
479 action
|= ACTION_HELP
;
486 adjust_verbosity(-1);
489 settings
.do_mtime
= true;
492 settings
.do_emptydirs
= true;
495 settings
.do_removeemptydirs
= true;
498 settings
.do_git
= true;
501 settings
.metafile
= optarg
;
504 usage(argv
[0], "unknown option");
508 /* Make sure only one action is specified */
510 usage(argv
[0], "incorrect option(s)");
512 /* Make sure --empty-dirs is only used with apply */
513 if (settings
.do_emptydirs
&& action
!= ACTION_APPLY
)
514 usage(argv
[0], "--empty-dirs is only valid with --apply");
516 /* Make sure --remove-empty-dirs is only used with apply */
517 if (settings
.do_removeemptydirs
&& action
!= ACTION_APPLY
)
518 usage(argv
[0], "--remove-empty-dirs is only valid with --apply");
523 mentries_fromfile(&stored
, settings
.metafile
);
525 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
531 while (optind
< argc
)
532 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
534 mentries_recurse_path(".", &real
, &settings
);
539 "Failed to load metadata from file system\n");
543 mentries_compare(real
, stored
, compare_print
, &settings
);
548 while (optind
< argc
)
549 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
551 mentries_recurse_path(".", &real
, &settings
);
556 "Failed to load metadata from file system\n");
560 mentries_tofile(real
, settings
.metafile
);
564 mentries_fromfile(&stored
, settings
.metafile
);
566 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
572 while (optind
< argc
)
573 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
575 mentries_recurse_path(".", &real
, &settings
);
580 "Failed to load metadata from file system\n");
584 mentries_compare(real
, stored
, compare_fix
, &settings
);
586 if (settings
.do_emptydirs
)
587 fixup_emptydirs(real
, stored
);
588 if (settings
.do_removeemptydirs
)
589 fixup_newemptydirs();
593 usage(argv
[0], NULL
);