2 * Copyright (c) 2010, Frank Lahm <franklahm@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
19 #include <sys/types.h>
31 #include <atalk/ftw.h>
32 #include <atalk/adouble.h>
33 #include <atalk/vfs.h>
34 #include <atalk/util.h>
35 #include <atalk/unix.h>
36 #include <atalk/volume.h>
37 #include <atalk/volinfo.h>
38 #include <atalk/bstrlib.h>
39 #include <atalk/bstradd.h>
40 #include <atalk/queue.h>
44 #define STRIP_TRAILING_SLASH(p) { \
45 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
49 static int fflg, iflg, nflg, vflg;
51 static afpvol_t svolume, dvolume;
52 static cnid_t did, pdid;
53 static volatile sig_atomic_t alarmed;
54 static char *netatalk_dirs[] = {
61 static int copy(const char *, const char *);
62 static int do_move(const char *, const char *);
63 static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
64 const char *dest_path);
66 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
67 Returns pointer to name or NULL.
69 static const char *check_netatalk_dirs(const char *name)
73 for (c=0; netatalk_dirs[c]; c++) {
74 if ((strcmp(name, netatalk_dirs[c])) == 0)
75 return netatalk_dirs[c];
82 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
85 static void sig_handler(int signo)
91 static void set_signal(void)
95 sv.sa_handler = sig_handler;
96 sv.sa_flags = SA_RESTART;
97 sigemptyset(&sv.sa_mask);
98 if (sigaction(SIGTERM, &sv, NULL) < 0)
99 ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
101 if (sigaction(SIGINT, &sv, NULL) < 0)
102 ERROR("error in sigaction(SIGINT): %s", strerror(errno));
104 memset(&sv, 0, sizeof(struct sigaction));
105 sv.sa_handler = SIG_IGN;
106 sigemptyset(&sv.sa_mask);
108 if (sigaction(SIGABRT, &sv, NULL) < 0)
109 ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
111 if (sigaction(SIGHUP, &sv, NULL) < 0)
112 ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
114 if (sigaction(SIGQUIT, &sv, NULL) < 0)
115 ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
118 static void usage_mv(void)
121 "Usage: ad mv [-f | -i | -n] [-v] source target\n"
122 " ad mv [-f | -i | -n] [-v] source ... directory\n\n"
123 "Move files around within an AFP volume, updating the CNID\n"
124 "database as needed. If either:\n"
125 " - source or destination is not an AFP volume\n"
126 " - source volume != destinatio volume\n"
127 "the files are copied and removed from the source.\n\n"
128 "The following options are available:\n\n"
129 " -f Do not prompt for confirmation before overwriting the destination\n"
130 " path. (The -f option overrides any previous -i or -n options.)\n"
131 " -i Cause mv to write a prompt to standard error before moving a file\n"
132 " that would overwrite an existing file. If the response from the\n"
133 " standard input begins with the character `y' or `Y', the move is\n"
134 " attempted. (The -i option overrides any previous -f or -n\n"
136 " -n Do not overwrite an existing file. (The -n option overrides any\n"
137 " previous -f or -i options.)\n"
138 " -v Cause mv to be verbose, showing files after they are moved.\n"
143 int ad_mv(int argc, char *argv[])
150 char path[MAXPATHLEN];
159 while ((ch = getopt(argc, argv, "finv")) != -1)
188 if (openvol(argv[argc - 1], &dvolume) != 0) {
189 SLOG("Error opening CNID database for source \"%s\": ", argv[argc - 1]);
194 * If the stat on the target fails or the target isn't a directory,
195 * try the move. More than 2 arguments is an error in this case.
197 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
200 if (openvol(argv[0], &svolume) != 0) {
201 SLOG("Error opening CNID database for destination \"%s\": ", argv[0]);
204 rval = do_move(argv[0], argv[1]);
210 /* It's a directory, move each file into it. */
211 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
212 ERROR("%s: destination pathname too long", *argv);
214 (void)strcpy(path, argv[argc - 1]);
215 baselen = strlen(path);
216 endp = &path[baselen];
217 if (!baselen || *(endp - 1) != '/') {
222 for (rval = 0; --argc; ++argv) {
224 * Find the last component of the source pathname. It
225 * may have trailing slashes.
227 p = *argv + strlen(*argv);
228 while (p != *argv && p[-1] == '/')
230 while (p != *argv && p[-1] != '/')
233 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
234 SLOG("%s: destination pathname too long", *argv);
237 memmove(endp, p, (size_t)len + 1);
238 openvol(*argv, &svolume);
240 if (do_move(*argv, path))
251 static int do_move(const char *from, const char *to)
257 * Check access. If interactive and file exists, ask user if it
258 * should be replaced. Otherwise if file exists but isn't writable
259 * make sure the user wants to clobber it.
261 if (!fflg && !access(to, F_OK)) {
263 /* prompt only if source exist */
264 if (lstat(from, &sb) == -1) {
265 SLOG("%s: %s", from, strerror(errno));
272 printf("%s not overwritten\n", to);
275 (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
277 } else if (access(to, W_OK) && !stat(to, &sb)) {
278 (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
282 first = ch = getchar();
283 while (ch != '\n' && ch != EOF)
285 if (first != 'y' && first != 'Y') {
286 (void)fprintf(stderr, "not overwritten\n");
294 * Besides the usual EXDEV we copy instead of moving if
295 * 1) source AFP volume != dest AFP volume
296 * 2) either source or dest isn't even an AFP volume
298 if (!svolume.volinfo.v_path
299 || !dvolume.volinfo.v_path
300 || strcmp(svolume.volinfo.v_path, dvolume.volinfo.v_path) != 0)
305 if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) {
306 SLOG("Couldn't resolve CNID for %s", from);
310 if (stat(from, &sb) != 0) {
311 SLOG("Cant stat %s: %s", to, strerror(errno));
315 if (rename(from, to) != 0) {
316 if (errno == EXDEV) {
318 char path[MAXPATHLEN];
321 * If the source is a symbolic link and is on another
322 * filesystem, it can be recreated at the destination.
324 if (lstat(from, &sb) == -1) {
325 SLOG("%s: %s", from, strerror(errno));
328 if (!S_ISLNK(sb.st_mode)) {
329 /* Can't mv(1) a mount point. */
330 if (realpath(from, path) == NULL) {
331 SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
335 } else { /* != EXDEV */
336 SLOG("rename %s to %s: %s", from, to, strerror(errno));
341 switch (sb.st_mode & S_IFMT) {
343 if (dvolume.volume.vfs->vfs_renamefile(&dvolume.volume, -1, from, to) != 0) {
344 SLOG("Error moving adouble file for %s", from);
351 SLOG("Not a file or dir: %s", from);
355 /* get CNID of new parent dir */
356 cnid_t newpdid, newdid;
357 if ((newdid = cnid_for_paths_parent(&dvolume, to, &newpdid)) == CNID_INVALID) {
358 SLOG("Couldn't resolve CNID for parent of %s", to);
362 if (stat(to, &sb) != 0) {
363 SLOG("Cant stat %s: %s", to, strerror(errno));
367 char *p = strdup(to);
368 char *name = basename(p);
369 if (cnid_update(dvolume.volume.v_cdb, cnid, &sb, newdid, name, strlen(name)) != 0) {
370 SLOG("Cant update CNID for: %s", to);
376 ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
377 if (ad_open(&ad, to, S_ISDIR(sb.st_mode) ? (ADFLAGS_DIR | ADFLAGS_HF | ADFLAGS_RDWR) : ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
378 SLOG("Error opening adouble for: %s", to);
381 ad_setid(&ad, sb.st_dev, sb.st_ino, cnid, newdid, dvolume.db_stamp);
383 ad_close(&ad, ADFLAGS_HF);
386 printf("%s -> %s\n", from, to);
391 return copy(from, to);
393 /* If we get here it's an error */
397 static int copy(const char *from, const char *to)
402 if (lstat(to, &sb) == 0) {
403 /* Destination path exists. */
404 if (S_ISDIR(sb.st_mode)) {
405 if (rmdir(to) != 0) {
406 SLOG("rmdir %s: %s", to, strerror(errno));
410 if (unlink(to) != 0) {
411 SLOG("unlink %s: %s", to, strerror(errno));
415 } else if (errno != ENOENT) {
416 SLOG("%s: %s", to, strerror(errno));
420 /* Copy source to destination. */
421 if (!(pid = fork())) {
422 execl(_PATH_AD, "ad", "cp", vflg ? "-Rpv" : "-Rp", from, to, (char *)NULL);
425 while ((waitpid(pid, &status, 0)) == -1) {
428 SLOG("%s cp -R %s %s: waitpid: %s", _PATH_AD, from, to, strerror(errno));
431 if (!WIFEXITED(status)) {
432 SLOG("%s cp -R %s %s: did not terminate normally", _PATH_AD, from, to);
435 switch (WEXITSTATUS(status)) {
439 SLOG("%s cp -R %s %s: terminated with %d (non-zero) status",
440 _PATH_AD, from, to, WEXITSTATUS(status));
444 /* Delete the source. */
445 if (!(pid = fork())) {
446 execl(_PATH_AD, "ad", "rm", "-R", from, (char *)NULL);
449 while ((waitpid(pid, &status, 0)) == -1) {
452 SLOG("%s rm -R %s: waitpid: %s", _PATH_AD, from, strerror(errno));
455 if (!WIFEXITED(status)) {
456 SLOG("%s rm -R %s: did not terminate normally", _PATH_AD, from);
459 switch (WEXITSTATUS(status)) {
463 SLOG("%s rm -R %s: terminated with %d (non-zero) status",
464 _PATH_AD, from, WEXITSTATUS(status));
471 preserve_fd_acls(int source_fd,
473 const char *source_path,
474 const char *dest_path)