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>
30 #include <atalk/ftw.h>
31 #include <atalk/adouble.h>
32 #include <atalk/vfs.h>
33 #include <atalk/util.h>
34 #include <atalk/unix.h>
35 #include <atalk/volume.h>
36 #include <atalk/volinfo.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 char *netatalk_dirs[] = {
61 static int copy(const char *, const char *);
62 static int do_move(const char *, const char *);
63 static int fastcopy(const char *, const char *, struct stat *);
64 static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
65 const char *dest_path);
67 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
68 Returns pointer to name or NULL.
70 static const char *check_netatalk_dirs(const char *name)
74 for (c=0; netatalk_dirs[c]; c++) {
75 if ((strcmp(name, netatalk_dirs[c])) == 0)
76 return netatalk_dirs[c];
83 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
86 static void sig_handler(int signo)
92 static void set_signal(void)
96 sv.sa_handler = sig_handler;
97 sv.sa_flags = SA_RESTART;
98 sigemptyset(&sv.sa_mask);
99 if (sigaction(SIGTERM, &sv, NULL) < 0)
100 ERROR("error in sigaction(SIGTERM): %s", strerror(errno));
102 if (sigaction(SIGINT, &sv, NULL) < 0)
103 ERROR("error in sigaction(SIGINT): %s", strerror(errno));
105 memset(&sv, 0, sizeof(struct sigaction));
106 sv.sa_handler = SIG_IGN;
107 sigemptyset(&sv.sa_mask);
109 if (sigaction(SIGABRT, &sv, NULL) < 0)
110 ERROR("error in sigaction(SIGABRT): %s", strerror(errno));
112 if (sigaction(SIGHUP, &sv, NULL) < 0)
113 ERROR("error in sigaction(SIGHUP): %s", strerror(errno));
115 if (sigaction(SIGQUIT, &sv, NULL) < 0)
116 ERROR("error in sigaction(SIGQUIT): %s", strerror(errno));
119 static void usage_mv(void)
122 "Usage: ad mv [-f | -i | -n] [-v] source target\n"
123 " ad mv [-f | -i | -n] [-v] source ... directory\n"
128 int ad_mv(int argc, char *argv[])
135 char path[MAXPATHLEN];
145 while ((ch = getopt(argc, argv, "finv")) != -1)
174 if (openvol(argv[argc - 1], &dvolume) != 0) {
175 SLOG("Error opening CNID database for %s: ", argv[argc - 1]);
180 * If the stat on the target fails or the target isn't a directory,
181 * try the move. More than 2 arguments is an error in this case.
183 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
186 if (openvol(argv[0], &svolume) != 0) {
187 SLOG("Error opening CNID database for %s: ", argv[0]);
190 rval = do_move(argv[0], argv[1]);
195 /* It's a directory, move each file into it. */
196 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
197 ERROR("%s: destination pathname too long", *argv);
199 (void)strcpy(path, argv[argc - 1]);
200 baselen = strlen(path);
201 endp = &path[baselen];
202 if (!baselen || *(endp - 1) != '/') {
207 for (rval = 0; --argc; ++argv) {
209 * Find the last component of the source pathname. It
210 * may have trailing slashes.
212 p = *argv + strlen(*argv);
213 while (p != *argv && p[-1] == '/')
215 while (p != *argv && p[-1] != '/')
218 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
219 SLOG("%s: destination pathname too long", *argv);
222 memmove(endp, p, (size_t)len + 1);
223 if (openvol(*argv, &svolume) != 0) {
224 SLOG("Error opening CNID database for %s: ", argv[0]);
228 if (do_move(*argv, path))
239 static int do_move(const char *from, const char *to)
245 * Check access. If interactive and file exists, ask user if it
246 * should be replaced. Otherwise if file exists but isn't writable
247 * make sure the user wants to clobber it.
249 if (!fflg && !access(to, F_OK)) {
251 /* prompt only if source exist */
252 if (lstat(from, &sb) == -1) {
253 SLOG("%s: %s", from, strerror(errno));
260 printf("%s not overwritten\n", to);
263 (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to);
265 } else if (access(to, W_OK) && !stat(to, &sb)) {
266 (void)fprintf(stderr, "override for %s? (y/n [n]) ", to);
270 first = ch = getchar();
271 while (ch != '\n' && ch != EOF)
273 if (first != 'y' && first != 'Y') {
274 (void)fprintf(stderr, "not overwritten\n");
280 if (!rename(from, to)) {
282 printf("%s -> %s\n", from, to);
286 if (errno == EXDEV) {
287 char path[MAXPATHLEN];
290 * If the source is a symbolic link and is on another
291 * filesystem, it can be recreated at the destination.
293 if (lstat(from, &sb) == -1) {
294 SLOG("%s: %s", from, strerror(errno));
297 if (!S_ISLNK(sb.st_mode)) {
298 /* Can't mv(1) a mount point. */
299 if (realpath(from, path) == NULL) {
300 SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno));
305 SLOG("rename %s to %s: %s", from, to, strerror(errno));
310 * If rename fails because we're trying to cross devices, and
311 * it's a regular file, do the copy internally; otherwise, use
314 if (lstat(from, &sb)) {
315 SLOG("%s: %s", from, strerror(errno));
318 return (S_ISREG(sb.st_mode) ?
319 fastcopy(from, to, &sb) : copy(from, to));
322 static int fastcopy(const char *from, const char *to, struct stat *sbp)
324 struct timeval tval[2];
328 int nread, from_fd, to_fd;
330 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
331 SLOG("%s: %s", from, strerror(errno));
334 if (blen < sbp->st_blksize) {
337 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
339 SLOG("malloc failed");
342 blen = sbp->st_blksize;
344 while ((to_fd = open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
345 if (errno == EEXIST && unlink(to) == 0)
347 SLOG("%s: %s", to, strerror(errno));
348 (void)close(from_fd);
351 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
352 if (write(to_fd, bp, (size_t)nread) != nread) {
353 SLOG("%s: %s", to, strerror(errno));
357 SLOG("%s: %s", from, strerror(errno));
360 SLOG("%s: remove", to, strerror(errno));
361 (void)close(from_fd);
366 oldmode = sbp->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID);
367 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
368 SLOG("%s: set owner/group (was: %lu/%lu)", to,
369 (u_long)sbp->st_uid, (u_long)sbp->st_gid, strerror(errno));
370 if (oldmode & S_ISUID) {
371 SLOG("%s: owner/group changed; clearing suid (mode was 0%03o)",
373 sbp->st_mode &= ~S_ISUID;
376 if (fchmod(to_fd, sbp->st_mode))
377 SLOG("%s: set mode (was: 0%03o): %s", to, oldmode, strerror(errno));
379 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
380 * for dest_file, then its ACLs shall reflect the ACLs of the
383 preserve_fd_acls(from_fd, to_fd, from, to);
384 (void)close(from_fd);
386 tval[0].tv_sec = sbp->st_atime;
387 tval[1].tv_sec = sbp->st_mtime;
388 tval[0].tv_usec = tval[1].tv_usec = 0;
389 if (utimes(to, tval))
390 SLOG("%s: set times: %s", to, strerror(errno));
393 SLOG("%s: %s", to, strerror(errno));
398 SLOG("%s: remove: %s", from, strerror(errno));
402 printf("%s -> %s\n", from, to);
406 static int copy(const char *from, const char *to)
411 if (lstat(to, &sb) == 0) {
412 /* Destination path exists. */
413 if (S_ISDIR(sb.st_mode)) {
414 if (rmdir(to) != 0) {
415 SLOG("rmdir %s: %s", to, strerror(errno));
419 if (unlink(to) != 0) {
420 SLOG("unlink %s: %s", to, strerror(errno));
424 } else if (errno != ENOENT) {
425 SLOG("%s: %s", to, strerror(errno));
429 /* Copy source to destination. */
430 if (!(pid = fork())) {
431 execl(adexecp, "cp", vflg ? "-Rpv" : "-Rp", "--", from, to, (char *)NULL);
434 if (waitpid(pid, &status, 0) == -1) {
435 SLOG("%s %s %s: waitpid: %s", adexecp, from, to, strerror(errno));
438 if (!WIFEXITED(status)) {
439 SLOG("%s %s %s: did not terminate normally", adexecp, from, to);
442 switch (WEXITSTATUS(status)) {
446 SLOG("%s %s %s: terminated with %d (non-zero) status",
447 adexecp, from, to, WEXITSTATUS(status));
451 /* Delete the source. */
452 if (!(pid = fork())) {
453 execl(adexecp, "rm", "-Rf", "--", from, (char *)NULL);
456 if (waitpid(pid, &status, 0) == -1) {
457 SLOG("%s %s: waitpid: %s", adexecp, from, strerror(errno));
460 if (!WIFEXITED(status)) {
461 SLOG("%s %s: did not terminate normally", adexecp, from);
464 switch (WEXITSTATUS(status)) {
468 SLOG("%s %s: terminated with %d (non-zero) status",
469 adexecp, from, WEXITSTATUS(status));
476 preserve_fd_acls(int source_fd,
478 const char *source_path,
479 const char *dest_path)