2 * Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #endif /* HAVE_CONFIG_H */
35 #include <sys/types.h>
37 #include <sys/param.h>
51 #include <atalk/cnid.h>
52 #include <atalk/volinfo.h>
55 #define cp_pct(x, y)((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
57 /* Memory strategy threshold, in pages: if physmem is larger then this, use a
59 #define PHYSPAGES_THRESHOLD (32*1024)
61 /* Maximum buffer size in bytes - do not allow it to grow larger than this */
62 #define BUFSIZE_MAX (2*1024*1024)
64 /* Small (default) buffer size in bytes. It's inefficient for this to be
65 * smaller than MAXPHYS */
66 #define MAXPHYS (64 * 1024)
67 #define BUFSIZE_SMALL (MAXPHYS)
69 int log_verbose; /* Logging flag */
71 void _log(enum logtype lt, char *fmt, ...)
74 static char logbuffer[1024];
77 if ( (lt == STD) || (log_verbose == 1)) {
79 len = vsnprintf(logbuffer, 1023, fmt, args);
83 printf("%s\n", logbuffer);
87 int newvol(const char *path, afpvol_t *vol)
91 memset(vol, 0, sizeof(afpvol_t));
93 // pathdup = strdup(path);
94 // vol->dirname = strdup(dirname(pathdup));
97 // pathdup = strdup(path);
98 // vol->basename = strdup(basename(pathdup));
101 loadvolinfo((char *)path, &vol->volinfo);
106 void freevol(afpvol_t *vol)
115 vol->basename = NULL;
121 Taken form afpd/desktop.c
123 char *utompath(const struct volinfo *volinfo, char *upath)
125 static char mpath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
127 uint16_t flags = CONV_IGNORE | CONV_UNESCAPEHEX;
135 outlen = strlen(upath);
137 if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
138 flags |= CONV_TOUPPER;
139 else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
140 flags |= CONV_TOLOWER;
142 if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
143 flags |= CONV__EILSEQ;
146 /* convert charsets */
147 if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
149 volinfo->v_maccharset,
150 u, outlen, mpath, MAXPATHLEN, &flags)) ) {
151 SLOG("Conversion from %s to %s for %s failed.",
152 volinfo->v_volcodepage, volinfo->v_maccodepage, u);
160 * Resolves CNID of a given paths parent directory
164 * "dir/subdir" with cwd: "/afp_volume/topdir"
166 * "/afp_volume/dir/subdir"
168 * 1) in case a) concatenate both paths
169 * 2) strip last element
170 * 3) strip volume root
171 * 4) start recursive CNID search with
174 * 5) ...until we have the CNID for
175 * a) "/afp_volume/topdir/dir"
176 * b) "/afp_volume/dir" (no recursion required)
178 cnid_t get_parent_cnid_for_path(const struct volinfo *vi,
179 const struct vol *vol,
186 int ftw_copy_file(const struct FTW *entp,
188 const struct stat *sp,
191 static char *buf = NULL;
192 static size_t bufsize;
196 int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
200 if ((from_fd = open(spath, O_RDONLY, 0)) == -1) {
201 SLOG("%s: %s", spath, strerror(errno));
206 * If the file exists and we're interactive, verify with the user.
207 * If the file DNE, set the mode to be the from file, minus setuid
208 * bits, modified by the umask; arguably wrong, but it makes copying
209 * executables work right and it's been that way forever. (The
210 * other choice is 666 or'ed with the execute bits on the from file
211 * modified by the umask.)
214 #define YESNO "(y/n [n]) "
217 printf("%s not overwritten\n", to.p_path);
218 (void)close(from_fd);
221 (void)fprintf(stderr, "overwrite %s? %s",
223 checkch = ch = getchar();
224 while (ch != '\n' && ch != EOF)
226 if (checkch != 'y' && checkch != 'Y') {
227 (void)close(from_fd);
228 (void)fprintf(stderr, "not overwritten\n");
234 /* remove existing destination file name,
235 * create a new file */
236 (void)unlink(to.p_path);
238 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
239 sp->st_mode & ~(S_ISUID | S_ISGID));
242 /* overwrite existing destination file name */
243 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
247 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
248 sp->st_mode & ~(S_ISUID | S_ISGID));
252 SLOG("%s: %s", to.p_path, strerror(errno));
253 (void)close(from_fd);
261 * Mmap and write if less than 8M (the limit is so we don't totally
262 * trash memory on big files. This is really a minor hack, but it
263 * wins some CPU back.
264 * Some filesystems, such as smbnetfs, don't support mmap,
265 * so this is a best-effort attempt.
268 if (S_ISREG(sp->st_mode) && sp->st_size > 0 &&
269 sp->st_size <= 8 * 1024 * 1024 &&
270 (p = mmap(NULL, (size_t)sp->st_size, PROT_READ,
271 MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
273 for (bufp = p, wresid = sp->st_size; ;
274 bufp += wcount, wresid -= (size_t)wcount) {
275 wcount = write(to_fd, bufp, wresid);
279 if (wcount >= (ssize_t)wresid)
282 if (wcount != (ssize_t)wresid) {
283 SLOG("%s: %s", to.p_path, strerror(errno));
286 /* Some systems don't unmap on close(2). */
287 if (munmap(p, sp->st_size) < 0) {
288 SLOG("%s: %s", spath, strerror(errno));
294 * Note that buf and bufsize are static. If
295 * malloc() fails, it will fail at the start
296 * and not copy only some files.
298 if (sysconf(_SC_PHYS_PAGES) >
300 bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
302 bufsize = BUFSIZE_SMALL;
303 buf = malloc(bufsize);
305 ERROR("Not enough memory");
309 while ((rcount = read(from_fd, buf, bufsize)) > 0) {
310 for (bufp = buf, wresid = rcount; ;
311 bufp += wcount, wresid -= wcount) {
312 wcount = write(to_fd, bufp, wresid);
316 if (wcount >= (ssize_t)wresid)
319 if (wcount != (ssize_t)wresid) {
320 SLOG("%s: %s", to.p_path, strerror(errno));
326 SLOG("%s: %s", spath, strerror(errno));
331 if (link(spath, to.p_path)) {
332 SLOG("%s", to.p_path);
338 * Don't remove the target even after an error. The target might
339 * not be a regular file, or its attributes might be important,
340 * or its contents might be irreplaceable. It would only be safe
341 * to remove it if we created it and its length is 0.
345 if (pflag && setfile(sp, to_fd))
347 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
350 SLOG("%s: %s", to.p_path, strerror(errno));
355 (void)close(from_fd);
360 int copy_link(const struct FTW *p,
362 const struct stat *sstp,
366 char llink[PATH_MAX];
368 if ((len = readlink(spath, llink, sizeof(llink) - 1)) == -1) {
369 SLOG("readlink: %s: %s", spath, strerror(errno));
373 if (exists && unlink(to.p_path)) {
374 SLOG("unlink: %s: %s", to.p_path, strerror(errno));
377 if (symlink(llink, to.p_path)) {
378 SLOG("symlink: %s: %s", llink, strerror(errno));
381 return (pflag ? setfile(sstp, -1) : 0);
384 int setfile(const struct stat *fs, int fd)
386 static struct timeval tv[2];
388 int rval, gotstat, islink, fdval;
393 islink = !fdval && S_ISLNK(fs->st_mode);
394 mode = fs->st_mode & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
396 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atim);
397 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtim);
398 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
399 SLOG("%sutimes: %s", islink ? "l" : "", to.p_path);
402 if (fdval ? fstat(fd, &ts) :
403 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
407 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
408 S_IRWXU | S_IRWXG | S_IRWXO;
411 * Changing the ownership probably won't succeed, unless we're root
412 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
413 * the mode; current BSD behavior is to remove all setuid bits on
414 * chown. If chown fails, lose setuid/setgid bits.
416 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
417 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
418 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
419 chown(to.p_path, fs->st_uid, fs->st_gid))) {
420 if (errno != EPERM) {
421 SLOG("chown: %s: %s", to.p_path, strerror(errno));
424 mode &= ~(S_ISUID | S_ISGID);
427 if (!gotstat || mode != ts.st_mode)
428 if (fdval ? fchmod(fd, mode) : chmod(to.p_path, mode)) {
429 SLOG("chmod: %s: %s", to.p_path, strerror(errno));
434 if (!gotstat || fs->st_flags != ts.st_flags)
436 fchflags(fd, fs->st_flags) :
437 (islink ? lchflags(to.p_path, fs->st_flags) :
438 chflags(to.p_path, fs->st_flags))) {
439 SLOG("chflags: %s: %s", to.p_path, strerror(errno));
447 int preserve_fd_acls(int source_fd, int dest_fd)
452 int acl_supported = 0, ret, trivial;
454 ret = fpathconf(source_fd, _PC_ACL_NFS4);
457 acl_type = ACL_TYPE_NFS4;
458 } else if (ret < 0 && errno != EINVAL) {
459 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
462 if (acl_supported == 0) {
463 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
466 acl_type = ACL_TYPE_ACCESS;
467 } else if (ret < 0 && errno != EINVAL) {
468 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
473 if (acl_supported == 0)
476 acl = acl_get_fd_np(source_fd, acl_type);
478 warn("failed to get acl entries while setting %s", to.p_path);
481 if (acl_is_trivial_np(acl, &trivial)) {
482 warn("acl_is_trivial() failed for %s", to.p_path);
490 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
491 warn("failed to set acl entries for %s", to.p_path);
500 int preserve_dir_acls(const struct stat *fs, char *source_dir, char *dest_dir)
503 acl_t (*aclgetf)(const char *, acl_type_t);
504 int (*aclsetf)(const char *, acl_type_t, acl_t);
508 int acl_supported = 0, ret, trivial;
510 ret = pathconf(source_dir, _PC_ACL_NFS4);
513 acl_type = ACL_TYPE_NFS4;
514 } else if (ret < 0 && errno != EINVAL) {
515 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
518 if (acl_supported == 0) {
519 ret = pathconf(source_dir, _PC_ACL_EXTENDED);
522 acl_type = ACL_TYPE_ACCESS;
523 } else if (ret < 0 && errno != EINVAL) {
524 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
529 if (acl_supported == 0)
533 * If the file is a link we will not follow it
535 if (S_ISLNK(fs->st_mode)) {
536 aclgetf = acl_get_link_np;
537 aclsetf = acl_set_link_np;
539 aclgetf = acl_get_file;
540 aclsetf = acl_set_file;
542 if (acl_type == ACL_TYPE_ACCESS) {
544 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
545 * size ACL will be returned. So it is not safe to simply
546 * check the pointer to see if the default ACL is present.
548 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
550 warn("failed to get default acl entries on %s",
554 aclp = &acl->ats_acl;
555 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
556 ACL_TYPE_DEFAULT, acl) < 0) {
557 warn("failed to set default acl entries on %s",
564 acl = aclgetf(source_dir, acl_type);
566 warn("failed to get acl entries on %s", source_dir);
569 if (acl_is_trivial_np(acl, &trivial)) {
570 warn("acl_is_trivial() failed on %s", source_dir);
578 if (aclsetf(dest_dir, acl_type, acl) < 0) {
579 warn("failed to set acl entries on %s", dest_dir);