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 <sys/xattr.h>
32 #include "metastore.h"
35 #include "metaentry.h"
37 /* metastore settings */
38 static struct metasettings settings
= {
41 .do_emptydirs
= 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
;
50 * Inserts an entry in a linked list ordered by pathlen
53 insert_entry_plist(struct metaentry
**list
, struct metaentry
*entry
)
55 struct metaentry
**parent
;
57 for (parent
= list
; *parent
; parent
= &((*parent
)->list
)) {
58 if ((*parent
)->pathlen
> entry
->pathlen
)
62 entry
->list
= *parent
;
67 * Prints differences between real and stored actual metadata
68 * - for use in mentries_compare
71 compare_print(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
73 if (!real
&& !stored
) {
74 msg(MSG_ERROR
, "%s called with incorrect arguments\n", __FUNCTION__
);
78 if (cmp
== DIFF_NONE
) {
79 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
83 msg(MSG_QUIET
, "%s:\t", real
? real
->path
: stored
->path
);
86 msg(MSG_QUIET
, "added ", real
->path
);
88 msg(MSG_QUIET
, "removed ", stored
->path
);
90 msg(MSG_QUIET
, "owner ");
92 msg(MSG_QUIET
, "group ");
94 msg(MSG_QUIET
, "mode ");
96 msg(MSG_QUIET
, "type ");
98 msg(MSG_QUIET
, "mtime ");
100 msg(MSG_QUIET
, "xattr ");
101 msg(MSG_QUIET
, "\n");
105 * Tries to change the real metadata to match the stored one
106 * - for use in mentries_compare
109 compare_fix(struct metaentry
*real
, struct metaentry
*stored
, int cmp
)
112 struct passwd
*owner
;
118 if (!real
&& !stored
) {
119 msg(MSG_ERROR
, "%s called with incorrect arguments\n",
125 if (S_ISDIR(stored
->mode
))
126 insert_entry_plist(&missingdirs
, stored
);
128 insert_entry_plist(&missingothers
, stored
);
130 msg(MSG_NORMAL
, "%s:\tremoved\n", stored
->path
);
135 msg(MSG_NORMAL
, "%s:\tadded\n", real
->path
);
139 if (cmp
== DIFF_NONE
) {
140 msg(MSG_DEBUG
, "%s:\tno difference\n", real
->path
);
144 if (cmp
& DIFF_TYPE
) {
145 msg(MSG_NORMAL
, "%s:\tnew type, will not change metadata\n",
150 msg(MSG_QUIET
, "%s:\tchanging metadata\n", real
->path
);
152 while (cmp
& (DIFF_OWNER
| DIFF_GROUP
)) {
153 if (cmp
& DIFF_OWNER
) {
154 msg(MSG_NORMAL
, "%s:\tchanging owner from %s to %s\n",
155 real
->path
, real
->group
, stored
->group
);
156 owner
= xgetpwnam(stored
->owner
);
158 msg(MSG_DEBUG
, "\tgetpwnam failed: %s\n",
165 if (cmp
& DIFF_GROUP
) {
166 msg(MSG_NORMAL
, "%s:\tchanging group from %s to %s\n",
167 real
->path
, real
->group
, stored
->group
);
168 group
= xgetgrnam(stored
->group
);
170 msg(MSG_DEBUG
, "\tgetgrnam failed: %s\n",
177 if (lchown(real
->path
, uid
, gid
)) {
178 msg(MSG_DEBUG
, "\tlchown failed: %s\n",
185 if (cmp
& DIFF_MODE
) {
186 msg(MSG_NORMAL
, "%s:\tchanging mode from 0%o to 0%o\n",
187 real
->path
, real
->mode
& 07777, stored
->mode
& 07777);
188 if (chmod(real
->path
, stored
->mode
& 07777))
189 msg(MSG_DEBUG
, "\tchmod failed: %s\n", strerror(errno
));
192 /* FIXME: Use utimensat here, or even better - lutimensat */
193 if ((cmp
& DIFF_MTIME
) && S_ISLNK(real
->mode
)) {
194 msg(MSG_NORMAL
, "%s:\tsymlink, not changing mtime\n", real
->path
);
195 } else if (cmp
& DIFF_MTIME
) {
196 msg(MSG_NORMAL
, "%s:\tchanging mtime from %ld to %ld\n",
197 real
->path
, real
->mtime
, stored
->mtime
);
198 tbuf
.actime
= stored
->mtime
;
199 tbuf
.modtime
= stored
->mtime
;
200 if (utime(real
->path
, &tbuf
)) {
201 msg(MSG_DEBUG
, "\tutime failed: %s\n", strerror(errno
));
206 if (cmp
& DIFF_XATTR
) {
207 for (i
= 0; i
< real
->xattrs
; i
++) {
208 /* Any attrs to remove? */
209 if (mentry_find_xattr(stored
, real
, i
) >= 0)
212 msg(MSG_NORMAL
, "%s:\tremoving xattr %s\n",
213 real
->path
, real
->xattr_names
[i
]);
214 if (lremovexattr(real
->path
, real
->xattr_names
[i
]))
215 msg(MSG_DEBUG
, "\tlremovexattr failed: %s\n",
219 for (i
= 0; i
< stored
->xattrs
; i
++) {
220 /* Any xattrs to add? (on change they are removed above) */
221 if (mentry_find_xattr(real
, stored
, i
) >= 0)
224 msg(MSG_NORMAL
, "%s:\tadding xattr %s\n",
225 stored
->path
, stored
->xattr_names
[i
]);
226 if (lsetxattr(stored
->path
, stored
->xattr_names
[i
],
227 stored
->xattr_values
[i
],
228 stored
->xattr_lvalues
[i
], XATTR_CREATE
))
229 msg(MSG_DEBUG
, "\tlsetxattr failed: %s\n",
236 * Tries to fix any empty dirs which are missing by recreating them.
237 * An "empty" dir is one which either:
239 * - only contained empty dirs
242 fixup_emptydirs(struct metahash
*real
, struct metahash
*stored
)
244 struct metaentry
*entry
;
245 struct metaentry
*cur
;
246 struct metaentry
**parent
;
250 struct metaentry
*new;
254 msg(MSG_DEBUG
, "\nAttempting to recreate missing dirs\n");
256 /* If directory x/y is missing, but file x/y/z is also missing,
257 * we should prune directory x/y from the list of directories to
258 * recreate since the deletition of x/y is likely to be genuine
259 * (as opposed to empty dir pruning like git/cvs does).
261 * Also, if file x/y/z is missing, any child directories of
262 * x/y should be pruned as they are probably also intentionally
266 msg(MSG_DEBUG
, "List of candidate dirs:\n");
267 for (cur
= missingdirs
; cur
; cur
= cur
->list
)
268 msg(MSG_DEBUG
, " %s\n", cur
->path
);
270 for (entry
= missingothers
; entry
; entry
= entry
->list
) {
271 msg(MSG_DEBUG
, "Pruning using file %s\n", entry
->path
);
272 bpath
= xstrdup(entry
->path
);
273 delim
= strrchr(bpath
, '/');
275 msg(MSG_NORMAL
, "No delimiter found in %s\n", bpath
);
281 parent
= &missingdirs
;
282 for (cur
= *parent
; cur
; cur
= cur
->list
) {
283 if (strcmp(cur
->path
, bpath
)) {
288 msg(MSG_DEBUG
, "Prune phase 1 - %s\n", cur
->path
);
292 /* Now also prune subdirs of the base dir */
295 blen
= strlen(bpath
);
297 parent
= &missingdirs
;
298 for (cur
= *parent
; cur
; cur
= cur
->list
) {
299 if (strncmp(cur
->path
, bpath
, blen
)) {
304 msg(MSG_DEBUG
, "Prune phase 2 - %s\n", cur
->path
);
310 msg(MSG_DEBUG
, "\n");
312 for (cur
= missingdirs
; cur
; cur
= cur
->list
) {
313 msg(MSG_QUIET
, "%s:\trecreating...", cur
->path
);
314 if (mkdir(cur
->path
, cur
->mode
)) {
315 msg(MSG_QUIET
, "failed (%s)\n", strerror(errno
));
318 msg(MSG_QUIET
, "ok\n");
320 new = mentry_create(cur
->path
);
322 msg(MSG_QUIET
, "Failed to get metadata for %s\n");
326 compare_fix(new, cur
, mentry_compare(new, cur
, &settings
));
330 /* Prints usage message and exits */
332 usage(const char *arg0
, const char *message
)
335 msg(MSG_CRITICAL
, "%s: %s\n\n", arg0
, message
);
336 msg(MSG_CRITICAL
, "Usage: %s ACTION [OPTION...] [PATH...]\n\n", arg0
);
337 msg(MSG_CRITICAL
, "Where ACTION is one of:\n"
338 " -c, --compare\t\tShow differences between stored and real metadata\n"
339 " -s, --save\t\tSave current metadata\n"
340 " -a, --apply\t\tApply stored metadata\n"
341 " -h, --help\t\tHelp message (this text)\n\n"
342 "Valid OPTIONS are:\n"
343 " -v, --verbose\t\tPrint more verbose messages\n"
344 " -q, --quiet\t\tPrint less verbose messages\n"
345 " -m, --mtime\t\tAlso take mtime into account for diff or apply\n"
346 " -e, --empty-dirs\tRecreate missing empty directories (experimental)\n"
347 " -g, --git\t\tDo not omit .git directories\n"
348 " -f, --file <file>\tSet metadata file\n"
351 exit(message
? EXIT_FAILURE
: EXIT_SUCCESS
);
355 static struct option long_options
[] = {
356 {"compare", 0, 0, 0},
360 {"verbose", 0, 0, 0},
363 {"empty-dirs", 0, 0, 0},
365 {"file", required_argument
, 0, 0},
371 main(int argc
, char **argv
, char **envp
)
374 struct metahash
*real
= NULL
;
375 struct metahash
*stored
= NULL
;
381 int option_index
= 0;
382 c
= getopt_long(argc
, argv
, "csahvqmegf:",
383 long_options
, &option_index
);
388 if (!strcmp("verbose",
389 long_options
[option_index
].name
)) {
391 } else if (!strcmp("quiet",
392 long_options
[option_index
].name
)) {
393 adjust_verbosity(-1);
394 } else if (!strcmp("mtime",
395 long_options
[option_index
].name
)) {
396 settings
.do_mtime
= true;
397 } else if (!strcmp("empty-dirs",
398 long_options
[option_index
].name
)) {
399 settings
.do_emptydirs
= true;
400 } else if (!strcmp("git",
401 long_options
[option_index
].name
)) {
402 settings
.do_git
= true;
403 } else if (!strcmp("file",
404 long_options
[option_index
].name
)) {
405 settings
.metafile
= optarg
;
407 action
|= (1 << option_index
);
412 action
|= ACTION_DIFF
;
416 action
|= ACTION_SAVE
;
420 action
|= ACTION_APPLY
;
424 action
|= ACTION_HELP
;
431 adjust_verbosity(-1);
434 settings
.do_mtime
= true;
437 settings
.do_emptydirs
= true;
440 settings
.do_git
= true;
443 settings
.metafile
= optarg
;
446 usage(argv
[0], "unknown option");
450 /* Make sure only one action is specified */
452 usage(argv
[0], "incorrect option(s)");
454 /* Make sure --empty-dirs is only used with apply */
455 if (settings
.do_emptydirs
&& action
!= ACTION_APPLY
)
456 usage(argv
[0], "--empty-dirs is only valid with --apply");
461 mentries_fromfile(&stored
, settings
.metafile
);
463 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
469 while (optind
< argc
)
470 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
472 mentries_recurse_path(".", &real
, &settings
);
477 "Failed to load metadata from file system\n");
481 mentries_compare(real
, stored
, compare_print
, &settings
);
486 while (optind
< argc
)
487 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
489 mentries_recurse_path(".", &real
, &settings
);
494 "Failed to load metadata from file system\n");
498 mentries_tofile(real
, settings
.metafile
);
502 mentries_fromfile(&stored
, settings
.metafile
);
504 msg(MSG_CRITICAL
, "Failed to load metadata from %s\n",
510 while (optind
< argc
)
511 mentries_recurse_path(argv
[optind
++], &real
, &settings
);
513 mentries_recurse_path(".", &real
, &settings
);
518 "Failed to load metadata from file system\n");
522 mentries_compare(real
, stored
, compare_fix
, &settings
);
524 if (settings
.do_emptydirs
)
525 fixup_emptydirs(real
, stored
);
529 usage(argv
[0], NULL
);