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;
55 static char *netatalk_dirs[] = {
62 static int copy(const char *, const char *);
63 static int do_move(const char *, const char *);
64 static int fastcopy(const char *, const char *, struct stat *);
65 static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
66 const char *dest_path);
68 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
69 Returns pointer to name or NULL.
71 static const char *check_netatalk_dirs(const char *name)
75 for (c=0; netatalk_dirs[c]; c++) {
76 if ((strcmp(name, netatalk_dirs[c])) == 0)
77 return netatalk_dirs[c];
84 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
87 static void sig_handler(int signo)
93 static void set_signal(void)
97 sv.sa_handler = sig_handler;
98 sv.sa_flags = SA_RESTART;
99 sigemptyset(&sv.sa_mask);
100 if (sigaction(SIGTERM, &sv, NULL) < 0)
101 ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
103 if (sigaction(SIGINT, &sv, NULL) < 0)
104 ERROR("error in sigaction(SIGINT): %s", strerror(errno));
106 memset(&sv, 0, sizeof(struct sigaction));
107 sv.sa_handler = SIG_IGN;
108 sigemptyset(&sv.sa_mask);
110 if (sigaction(SIGABRT, &sv, NULL) < 0)
111 ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
113 if (sigaction(SIGHUP, &sv, NULL) < 0)
114 ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
116 if (sigaction(SIGQUIT, &sv, NULL) < 0)
117 ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
120 static void usage_mv(void)
123 "Usage: ad mv [-f | -i | -n] [-v] source target\n"
124 " ad mv [-f | -i | -n] [-v] source ... directory\n\n"
125 "Move files around within an AFP volume, updating the CNID\n"
126 "database as needed. If either:\n"
127 " - source or destination is not an AFP volume\n"
128 " - source volume != destinatio volume\n"
129 "the files are copied and removed from the source.\n\n"
130 "The following options are available:\n\n"
131 " -f Do not prompt for confirmation before overwriting the destination\n"
132 " path. (The -f option overrides any previous -i or -n options.)\n"
133 " -i Cause mv to write a prompt to standard error before moving a file\n"
134 " that would overwrite an existing file. If the response from the\n"
135 " standard input begins with the character `y' or `Y', the move is\n"
136 " attempted. (The -i option overrides any previous -f or -n\n"
138 " -n Do not overwrite an existing file. (The -n option overrides any\n"
139 " previous -f or -i options.)\n"
140 " -v Cause mv to be verbose, showing files after they are moved.\n"
145 int ad_mv(int argc, char *argv[])
152 char path[MAXPATHLEN];
162 while ((ch = getopt(argc, argv, "finv")) != -1)
191 if (openvol(argv[argc - 1], &dvolume) != 0) {
192 SLOG("Error opening CNID database for %s: ", argv[argc - 1]);
197 * If the stat on the target fails or the target isn't a directory,
198 * try the move. More than 2 arguments is an error in this case.
200 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
203 if (openvol(argv[0], &svolume) != 0) {
204 SLOG("Error opening CNID database for %s: ", argv[0]);
207 rval = do_move(argv[0], argv[1]);
213 /* It's a directory, move each file into it. */
214 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
215 ERROR("%s: destination pathname too long", *argv);
217 (void)strcpy(path, argv[argc - 1]);
218 baselen = strlen(path);
219 endp = &path[baselen];
220 if (!baselen || *(endp - 1) != '/') {
225 for (rval = 0; --argc; ++argv) {
227 * Find the last component of the source pathname. It
228 * may have trailing slashes.
230 p = *argv + strlen(*argv);
231 while (p != *argv && p[-1] == '/')
233 while (p != *argv && p[-1] != '/')
236 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
237 SLOG("%s: destination pathname too long", *argv);
240 memmove(endp, p, (size_t)len + 1);
241 if (openvol(*argv, &svolume) != 0) {
242 SLOG("Error opening CNID database for %s: ", argv[0]);
246 if (do_move(*argv, path))
257 static int do_move(const char *from, const char *to)
263 * Check access. If interactive and file exists, ask user if it
264 * should be replaced. Otherwise if file exists but isn't writable
265 * make sure the user wants to clobber it.
267 if (!fflg && !access(to, F_OK)) {
269 /* prompt only if source exist */
270 if (lstat(from, &sb) == -1) {
271 SLOG("%s: %s", from, strerror(errno));
278 printf("%s not overwritten\n", to);
281 (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
283 } else if (access(to, W_OK) && !stat(to, &sb)) {
284 (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
288 first = ch = getchar();
289 while (ch != '\n' && ch != EOF)
291 if (first != 'y' && first != 'Y') {
292 (void)fprintf(stderr, "not overwritten\n");
300 * Besides the usual EXDEV we copy instead of moving if
301 * 1) source AFP volume != dest AFP volume
302 * 2) either source or dest isn't even an AFP volume
304 if (!svolume.volinfo.v_path
305 || !dvolume.volinfo.v_path
306 || strcmp(svolume.volinfo.v_path, dvolume.volinfo.v_path) != 0)
311 if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) {
312 SLOG("Couldn't resolve CNID for %s", from);
316 if (stat(from, &sb) != 0) {
317 SLOG("Cant stat %s: %s", to, strerror(errno));
321 if (rename(from, to) != 0) {
322 if (errno == EXDEV) {
324 char path[MAXPATHLEN];
327 * If the source is a symbolic link and is on another
328 * filesystem, it can be recreated at the destination.
330 if (lstat(from, &sb) == -1) {
331 SLOG("%s: %s", from, strerror(errno));
334 if (!S_ISLNK(sb.st_mode)) {
335 /* Can't mv(1) a mount point. */
336 if (realpath(from, path) == NULL) {
337 SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
341 } else { /* != EXDEV */
342 SLOG("rename %s to %s: %s", from, to, strerror(errno));
347 switch (sb.st_mode & S_IFMT) {
349 if (dvolume.volume.vfs->vfs_renamefile(&dvolume.volume, -1, from, to) != 0) {
350 SLOG("Error moving adouble file for %s", from);
357 SLOG("Not a file or dir: %s", from);
361 /* get CNID of new parent dir */
362 cnid_t newpdid, newdid;
363 if ((newdid = cnid_for_paths_parent(&dvolume, to, &newpdid)) == CNID_INVALID) {
364 SLOG("Couldn't resolve CNID for parent of %s", to);
368 if (stat(to, &sb) != 0) {
369 SLOG("Cant stat %s: %s", to, strerror(errno));
373 char *p = strdup(to);
374 char *name = basename(p);
375 if (cnid_update(dvolume.volume.v_cdb, cnid, &sb, newdid, name, strlen(name)) != 0) {
376 SLOG("Cant update CNID for: %s", to);
382 ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
383 if (ad_open_metadata(to, S_ISDIR(sb.st_mode) ? ADFLAGS_DIR : 0, O_RDWR, &ad) != 0) {
384 SLOG("Error opening adouble for: %s", to);
387 ad_setid(&ad, sb.st_dev, sb.st_ino, cnid, newdid, dvolume.db_stamp);
389 ad_close_metadata(&ad);
392 printf("%s -> %s\n", from, to);
398 * If rename fails because we're trying to cross devices, and
399 * it's a regular file, do the copy internally; otherwise, use
402 if (lstat(from, &sb)) {
403 SLOG("%s: %s", from, strerror(errno));
406 return (S_ISREG(sb.st_mode) ?
407 fastcopy(from, to, &sb) : copy(from, to));
412 static int fastcopy(const char *from, const char *to, struct stat *sbp)
414 struct timeval tval[2];
418 int nread, from_fd, to_fd;
420 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
421 SLOG("%s: %s", from, strerror(errno));
424 if (blen < sbp->st_blksize) {
427 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
429 SLOG("malloc failed");
432 blen = sbp->st_blksize;
434 while ((to_fd = open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
435 if (errno == EEXIST && unlink(to) == 0)
437 SLOG("%s: %s", to, strerror(errno));
438 (void)close(from_fd);
441 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
442 if (write(to_fd, bp, (size_t)nread) != nread) {
443 SLOG("%s: %s", to, strerror(errno));
447 SLOG("%s: %s", from, strerror(errno));
450 SLOG("%s: remove", to, strerror(errno));
451 (void)close(from_fd);
456 oldmode = sbp->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID);
457 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
458 SLOG("%s: set owner/group (was: %lu/%lu)", to,
459 (u_long)sbp->st_uid, (u_long)sbp->st_gid, strerror(errno));
460 if (oldmode & S_ISUID) {
461 SLOG("%s: owner/group changed; clearing suid (mode was 0%03o)",
463 sbp->st_mode &= ~S_ISUID;
466 if (fchmod(to_fd, sbp->st_mode))
467 SLOG("%s: set mode (was: 0%03o): %s", to, oldmode, strerror(errno));
469 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
470 * for dest_file, then its ACLs shall reflect the ACLs of the
473 preserve_fd_acls(from_fd, to_fd, from, to);
474 (void)close(from_fd);
476 tval[0].tv_sec = sbp->st_atime;
477 tval[1].tv_sec = sbp->st_mtime;
478 tval[0].tv_usec = tval[1].tv_usec = 0;
479 if (utimes(to, tval))
480 SLOG("%s: set times: %s", to, strerror(errno));
483 SLOG("%s: %s", to, strerror(errno));
488 SLOG("%s: remove: %s", from, strerror(errno));
492 printf("%s -> %s\n", from, to);
496 static int copy(const char *from, const char *to)
501 if (lstat(to, &sb) == 0) {
502 /* Destination path exists. */
503 if (S_ISDIR(sb.st_mode)) {
504 if (rmdir(to) != 0) {
505 SLOG("rmdir %s: %s", to, strerror(errno));
509 if (unlink(to) != 0) {
510 SLOG("unlink %s: %s", to, strerror(errno));
514 } else if (errno != ENOENT) {
515 SLOG("%s: %s", to, strerror(errno));
519 /* Copy source to destination. */
520 if (!(pid = fork())) {
521 execl(adexecp, "ad", "cp", vflg ? "-Rpv" : "-Rp", from, to, (char *)NULL);
524 while ((waitpid(pid, &status, 0)) == -1) {
527 SLOG("%s %s %s: waitpid: %s", adexecp, from, to, strerror(errno));
530 if (!WIFEXITED(status)) {
531 SLOG("%s %s %s: did not terminate normally", adexecp, from, to);
534 switch (WEXITSTATUS(status)) {
538 SLOG("%s cp %s %s: terminated with %d (non-zero) status",
539 adexecp, from, to, WEXITSTATUS(status));
543 /* Delete the source. */
544 if (!(pid = fork())) {
545 execl(adexecp, "ad", "rm", "-R", from, (char *)NULL);
548 while ((waitpid(pid, &status, 0)) == -1) {
551 SLOG("%s rm %s: waitpid: %s", adexecp, from, strerror(errno));
554 if (!WIFEXITED(status)) {
555 SLOG("%s rm %s: did not terminate normally", adexecp, from);
558 switch (WEXITSTATUS(status)) {
562 SLOG("%s rm %s: terminated with %d (non-zero) status",
563 adexecp, from, WEXITSTATUS(status));
570 preserve_fd_acls(int source_fd,
572 const char *source_path,
573 const char *dest_path)