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/bstrlib.h>
38 #include <atalk/bstradd.h>
39 #include <atalk/queue.h>
43 #define STRIP_TRAILING_SLASH(p) { \
44 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
48 static int fflg, iflg, nflg, vflg;
50 static afpvol_t svolume, dvolume;
51 static cnid_t did, pdid;
52 static volatile sig_atomic_t alarmed;
54 static int copy(const char *, const char *);
55 static int do_move(const char *, const char *);
59 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
62 static void sig_handler(int signo)
68 static void set_signal(void)
72 sv.sa_handler = sig_handler;
73 sv.sa_flags = SA_RESTART;
74 sigemptyset(&sv.sa_mask);
75 if (sigaction(SIGTERM, &sv, NULL) < 0)
76 ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
78 if (sigaction(SIGINT, &sv, NULL) < 0)
79 ERROR("error in sigaction(SIGINT): %s", strerror(errno));
81 memset(&sv, 0, sizeof(struct sigaction));
82 sv.sa_handler = SIG_IGN;
83 sigemptyset(&sv.sa_mask);
85 if (sigaction(SIGABRT, &sv, NULL) < 0)
86 ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
88 if (sigaction(SIGHUP, &sv, NULL) < 0)
89 ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
91 if (sigaction(SIGQUIT, &sv, NULL) < 0)
92 ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
95 static void usage_mv(void)
98 "Usage: ad mv [-f | -i | -n] [-v] source target\n"
99 " ad mv [-f | -i | -n] [-v] source ... directory\n\n"
100 "Move files around within an AFP volume, updating the CNID\n"
101 "database as needed. If either:\n"
102 " - source or destination is not an AFP volume\n"
103 " - source volume != destinatio volume\n"
104 "the files are copied and removed from the source.\n\n"
105 "The following options are available:\n\n"
106 " -f Do not prompt for confirmation before overwriting the destination\n"
107 " path. (The -f option overrides any previous -i or -n options.)\n"
108 " -i Cause mv to write a prompt to standard error before moving a file\n"
109 " that would overwrite an existing file. If the response from the\n"
110 " standard input begins with the character `y' or `Y', the move is\n"
111 " attempted. (The -i option overrides any previous -f or -n\n"
113 " -n Do not overwrite an existing file. (The -n option overrides any\n"
114 " previous -f or -i options.)\n"
115 " -v Cause mv to be verbose, showing files after they are moved.\n"
120 int ad_mv(int argc, char *argv[], AFPObj *obj)
127 char path[MAXPATHLEN];
136 while ((ch = getopt(argc, argv, "finv")) != -1)
165 if (openvol(obj, argv[argc - 1], &dvolume) != 0) {
166 SLOG("Error opening CNID database for source \"%s\": ", argv[argc - 1]);
171 * If the stat on the target fails or the target isn't a directory,
172 * try the move. More than 2 arguments is an error in this case.
174 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
177 if (openvol(obj, argv[0], &svolume) != 0) {
178 SLOG("Error opening CNID database for destination \"%s\": ", argv[0]);
181 rval = do_move(argv[0], argv[1]);
187 /* It's a directory, move each file into it. */
188 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
189 ERROR("%s: destination pathname too long", *argv);
191 (void)strcpy(path, argv[argc - 1]);
192 baselen = strlen(path);
193 endp = &path[baselen];
194 if (!baselen || *(endp - 1) != '/') {
199 for (rval = 0; --argc; ++argv) {
201 * Find the last component of the source pathname. It
202 * may have trailing slashes.
204 p = *argv + strlen(*argv);
205 while (p != *argv && p[-1] == '/')
207 while (p != *argv && p[-1] != '/')
210 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
211 SLOG("%s: destination pathname too long", *argv);
214 memmove(endp, p, (size_t)len + 1);
215 openvol(obj, *argv, &svolume);
217 if (do_move(*argv, path))
227 static int do_move(const char *from, const char *to)
233 * Check access. If interactive and file exists, ask user if it
234 * should be replaced. Otherwise if file exists but isn't writable
235 * make sure the user wants to clobber it.
237 if (!fflg && !access(to, F_OK)) {
239 /* prompt only if source exist */
240 if (lstat(from, &sb) == -1) {
241 SLOG("%s: %s", from, strerror(errno));
248 printf("%s not overwritten\n", to);
251 (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
253 } else if (access(to, W_OK) && !stat(to, &sb)) {
254 (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
258 first = ch = getchar();
259 while (ch != '\n' && ch != EOF)
261 if (first != 'y' && first != 'Y') {
262 (void)fprintf(stderr, "not overwritten\n");
270 * Besides the usual EXDEV we copy instead of moving if
271 * 1) source AFP volume != dest AFP volume
272 * 2) either source or dest isn't even an AFP volume
274 if (!svolume.vol->v_path
275 || !dvolume.vol->v_path
276 || strcmp(svolume.vol->v_path, dvolume.vol->v_path) != 0)
281 if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) {
282 SLOG("Couldn't resolve CNID for %s", from);
286 if (stat(from, &sb) != 0) {
287 SLOG("Cant stat %s: %s", to, strerror(errno));
291 if (rename(from, to) != 0) {
292 if (errno == EXDEV) {
294 char path[MAXPATHLEN];
297 * If the source is a symbolic link and is on another
298 * filesystem, it can be recreated at the destination.
300 if (lstat(from, &sb) == -1) {
301 SLOG("%s: %s", from, strerror(errno));
304 if (!S_ISLNK(sb.st_mode)) {
305 /* Can't mv(1) a mount point. */
306 if (realpath(from, path) == NULL) {
307 SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
311 } else { /* != EXDEV */
312 SLOG("rename %s to %s: %s", from, to, strerror(errno));
317 switch (sb.st_mode & S_IFMT) {
319 if (dvolume.vol->vfs->vfs_renamefile(dvolume.vol, -1, from, to) != 0) {
320 SLOG("Error moving adouble file for %s", from);
327 SLOG("Not a file or dir: %s", from);
331 /* get CNID of new parent dir */
332 cnid_t newpdid, newdid;
333 if ((newdid = cnid_for_paths_parent(&dvolume, to, &newpdid)) == CNID_INVALID) {
334 SLOG("Couldn't resolve CNID for parent of %s", to);
338 if (stat(to, &sb) != 0) {
339 SLOG("Cant stat %s: %s", to, strerror(errno));
343 char *p = strdup(to);
344 char *name = basename(p);
345 if (cnid_update(dvolume.vol->v_cdb, cnid, &sb, newdid, name, strlen(name)) != 0) {
346 SLOG("Cant update CNID for: %s", to);
352 ad_init(&ad, dvolume.vol);
353 if (ad_open(&ad, to, S_ISDIR(sb.st_mode) ? (ADFLAGS_DIR | ADFLAGS_HF | ADFLAGS_RDWR) : ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
354 SLOG("Error opening adouble for: %s", to);
357 ad_setid(&ad, sb.st_dev, sb.st_ino, cnid, newdid, dvolume.db_stamp);
359 ad_close(&ad, ADFLAGS_HF);
362 printf("%s -> %s\n", from, to);
367 return copy(from, to);
369 /* If we get here it's an error */
373 static int copy(const char *from, const char *to)
378 if (lstat(to, &sb) == 0) {
379 /* Destination path exists. */
380 if (S_ISDIR(sb.st_mode)) {
381 if (rmdir(to) != 0) {
382 SLOG("rmdir %s: %s", to, strerror(errno));
386 if (unlink(to) != 0) {
387 SLOG("unlink %s: %s", to, strerror(errno));
391 } else if (errno != ENOENT) {
392 SLOG("%s: %s", to, strerror(errno));
396 /* Copy source to destination. */
397 if (!(pid = fork())) {
398 execl(_PATH_AD, "ad", "cp", vflg ? "-Rpv" : "-Rp", from, to, (char *)NULL);
401 while ((waitpid(pid, &status, 0)) == -1) {
404 SLOG("%s cp -R %s %s: waitpid: %s", _PATH_AD, from, to, strerror(errno));
407 if (!WIFEXITED(status)) {
408 SLOG("%s cp -R %s %s: did not terminate normally", _PATH_AD, from, to);
411 switch (WEXITSTATUS(status)) {
415 SLOG("%s cp -R %s %s: terminated with %d (non-zero) status",
416 _PATH_AD, from, to, WEXITSTATUS(status));
420 /* Delete the source. */
421 if (!(pid = fork())) {
422 execl(_PATH_AD, "ad", "rm", "-R", from, (char *)NULL);
425 while ((waitpid(pid, &status, 0)) == -1) {
428 SLOG("%s rm -R %s: waitpid: %s", _PATH_AD, from, strerror(errno));
431 if (!WIFEXITED(status)) {
432 SLOG("%s rm -R %s: did not terminate normally", _PATH_AD, from);
435 switch (WEXITSTATUS(status)) {
439 SLOG("%s rm -R %s: terminated with %d (non-zero) status",
440 _PATH_AD, from, WEXITSTATUS(status));