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"
129 int ad_mv(int argc, char *argv[])
136 char path[MAXPATHLEN];
146 while ((ch = getopt(argc, argv, "finv")) != -1)
175 if (openvol(argv[argc - 1], &dvolume) != 0) {
176 SLOG("Error opening CNID database for %s: ", argv[argc - 1]);
181 * If the stat on the target fails or the target isn't a directory,
182 * try the move. More than 2 arguments is an error in this case.
184 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
187 if (openvol(argv[0], &svolume) != 0) {
188 SLOG("Error opening CNID database for %s: ", argv[0]);
191 rval = do_move(argv[0], argv[1]);
196 /* It's a directory, move each file into it. */
197 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
198 ERROR("%s: destination pathname too long", *argv);
200 (void)strcpy(path, argv[argc - 1]);
201 baselen = strlen(path);
202 endp = &path[baselen];
203 if (!baselen || *(endp - 1) != '/') {
208 for (rval = 0; --argc; ++argv) {
210 * Find the last component of the source pathname. It
211 * may have trailing slashes.
213 p = *argv + strlen(*argv);
214 while (p != *argv && p[-1] == '/')
216 while (p != *argv && p[-1] != '/')
219 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
220 SLOG("%s: destination pathname too long", *argv);
223 memmove(endp, p, (size_t)len + 1);
224 if (openvol(*argv, &svolume) != 0) {
225 SLOG("Error opening CNID database for %s: ", argv[0]);
229 if (do_move(*argv, path))
240 static int do_move(const char *from, const char *to)
246 * Check access. If interactive and file exists, ask user if it
247 * should be replaced. Otherwise if file exists but isn't writable
248 * make sure the user wants to clobber it.
250 if (!fflg && !access(to, F_OK)) {
252 /* prompt only if source exist */
253 if (lstat(from, &sb) == -1) {
254 SLOG("%s: %s", from, strerror(errno));
261 printf("%s not overwritten\n", to);
264 (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
266 } else if (access(to, W_OK) && !stat(to, &sb)) {
267 (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
271 first = ch = getchar();
272 while (ch != '\n' && ch != EOF)
274 if (first != 'y' && first != 'Y') {
275 (void)fprintf(stderr, "not overwritten\n");
283 * Besides the usual EXDEV we copy instead of moving if
284 * 1) source AFP volume != dest AFP volume
285 * 2) either source or dest isn't even an AFP volume
287 if (!svolume.volinfo.v_path
288 || !dvolume.volinfo.v_path
289 || strcmp(svolume.volinfo.v_path, dvolume.volinfo.v_path) != 0)
294 if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) {
295 SLOG("Couldn't resolve CNID for %s", from);
299 if (stat(from, &sb) != 0) {
300 SLOG("Cant stat %s: %s", to, strerror(errno));
304 if (rename(from, to) != 0) {
305 if (errno == EXDEV) {
307 char path[MAXPATHLEN];
310 * If the source is a symbolic link and is on another
311 * filesystem, it can be recreated at the destination.
313 if (lstat(from, &sb) == -1) {
314 SLOG("%s: %s", from, strerror(errno));
317 if (!S_ISLNK(sb.st_mode)) {
318 /* Can't mv(1) a mount point. */
319 if (realpath(from, path) == NULL) {
320 SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
324 } else { /* != EXDEV */
325 SLOG("rename %s to %s: %s", from, to, strerror(errno));
330 switch (sb.st_mode & S_IFMT) {
332 if (dvolume.volume.vfs->vfs_renamefile(&dvolume.volume, -1, from, to) != 0) {
333 SLOG("Error moving adouble file for %s", from);
340 SLOG("Not a file or dir: %s", from);
344 char *newdir = strdup(to);
346 if ((newdid = cnid_for_path(&svolume, dirname(newdir), &pdid)) == CNID_INVALID) {
347 SLOG("Couldn't resolve CNID for %s", to);
351 if (stat(to, &sb) != 0) {
352 SLOG("Cant stat %s: %s", to, strerror(errno));
355 char *name = strdup(to);
356 if (cnid_update(dvolume.volume.v_cdb, cnid, &sb, newdid, basename(to), strlen(basename(to))) != 0) {
357 SLOG("Cant update CNID for: %s", to);
363 printf("%s -> %s\n", from, to);
369 * If rename fails because we're trying to cross devices, and
370 * it's a regular file, do the copy internally; otherwise, use
373 if (lstat(from, &sb)) {
374 SLOG("%s: %s", from, strerror(errno));
377 return (S_ISREG(sb.st_mode) ?
378 fastcopy(from, to, &sb) : copy(from, to));
383 static int fastcopy(const char *from, const char *to, struct stat *sbp)
385 struct timeval tval[2];
389 int nread, from_fd, to_fd;
391 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
392 SLOG("%s: %s", from, strerror(errno));
395 if (blen < sbp->st_blksize) {
398 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
400 SLOG("malloc failed");
403 blen = sbp->st_blksize;
405 while ((to_fd = open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
406 if (errno == EEXIST && unlink(to) == 0)
408 SLOG("%s: %s", to, strerror(errno));
409 (void)close(from_fd);
412 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
413 if (write(to_fd, bp, (size_t)nread) != nread) {
414 SLOG("%s: %s", to, strerror(errno));
418 SLOG("%s: %s", from, strerror(errno));
421 SLOG("%s: remove", to, strerror(errno));
422 (void)close(from_fd);
427 oldmode = sbp->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID);
428 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
429 SLOG("%s: set owner/group (was: %lu/%lu)", to,
430 (u_long)sbp->st_uid, (u_long)sbp->st_gid, strerror(errno));
431 if (oldmode & S_ISUID) {
432 SLOG("%s: owner/group changed; clearing suid (mode was 0%03o)",
434 sbp->st_mode &= ~S_ISUID;
437 if (fchmod(to_fd, sbp->st_mode))
438 SLOG("%s: set mode (was: 0%03o): %s", to, oldmode, strerror(errno));
440 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
441 * for dest_file, then its ACLs shall reflect the ACLs of the
444 preserve_fd_acls(from_fd, to_fd, from, to);
445 (void)close(from_fd);
447 tval[0].tv_sec = sbp->st_atime;
448 tval[1].tv_sec = sbp->st_mtime;
449 tval[0].tv_usec = tval[1].tv_usec = 0;
450 if (utimes(to, tval))
451 SLOG("%s: set times: %s", to, strerror(errno));
454 SLOG("%s: %s", to, strerror(errno));
459 SLOG("%s: remove: %s", from, strerror(errno));
463 printf("%s -> %s\n", from, to);
467 static int copy(const char *from, const char *to)
472 if (lstat(to, &sb) == 0) {
473 /* Destination path exists. */
474 if (S_ISDIR(sb.st_mode)) {
475 if (rmdir(to) != 0) {
476 SLOG("rmdir %s: %s", to, strerror(errno));
480 if (unlink(to) != 0) {
481 SLOG("unlink %s: %s", to, strerror(errno));
485 } else if (errno != ENOENT) {
486 SLOG("%s: %s", to, strerror(errno));
490 /* Copy source to destination. */
491 if (!(pid = fork())) {
492 execl(adexecp, "cp", vflg ? "-Rpv" : "-Rp", "--", from, to, (char *)NULL);
495 if (waitpid(pid, &status, 0) == -1) {
496 SLOG("%s %s %s: waitpid: %s", adexecp, from, to, strerror(errno));
499 if (!WIFEXITED(status)) {
500 SLOG("%s %s %s: did not terminate normally", adexecp, from, to);
503 switch (WEXITSTATUS(status)) {
507 SLOG("%s %s %s: terminated with %d (non-zero) status",
508 adexecp, from, to, WEXITSTATUS(status));
512 /* Delete the source. */
513 if (!(pid = fork())) {
514 execl(adexecp, "rm", "-Rf", "--", from, (char *)NULL);
517 if (waitpid(pid, &status, 0) == -1) {
518 SLOG("%s %s: waitpid: %s", adexecp, from, strerror(errno));
521 if (!WIFEXITED(status)) {
522 SLOG("%s %s: did not terminate normally", adexecp, from);
525 switch (WEXITSTATUS(status)) {
529 SLOG("%s %s: terminated with %d (non-zero) status",
530 adexecp, from, WEXITSTATUS(status));
537 preserve_fd_acls(int source_fd,
539 const char *source_path,
540 const char *dest_path)