2 * Copyright (c) 2010, Frank Lahm <franklahm@googlemail.com>
3 * Copyright (c) 1988, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * David Hitz of Auspex Systems Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Cp copies source files to target files.
37 * The global PATH_T structure "to" always contains the path to the
38 * current target file. Since fts(3) does not change directories,
39 * this path can be either absolute or dot-relative.
41 * The basic algorithm is to initialize "to" and use fts(3) to traverse
42 * the file hierarchy rooted in the argument list. A trivial case is the
43 * case of 'cp file1 file2'. The more interesting case is the case of
44 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
45 * path (relative to the root of the traversal) is appended to dir (stored
46 * in "to") to form the final target path.
51 #endif /* HAVE_CONFIG_H */
53 #include <sys/types.h>
63 #include <atalk/ftw.h>
64 #include <atalk/adouble.h>
65 #include <atalk/vfs.h>
66 #include <atalk/util.h>
67 #include <atalk/unix.h>
68 #include <atalk/volume.h>
69 #include <atalk/volinfo.h>
70 #include <atalk/bstrlib.h>
71 #include <atalk/bstradd.h>
72 #include <atalk/queue.h>
76 #define STRIP_TRAILING_SLASH(p) { \
77 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
81 static char emptystring[] = "";
83 PATH_T to = { to.p_path, emptystring, "" };
85 int fflag, iflag, lflag, nflag, pflag, vflag;
87 struct volinfo svolinfo, dvolinfo;
88 struct vol svolume, dvolume;
89 cnid_t did = 0; /* current dir CNID */
93 volatile sig_atomic_t sigint;
94 static int badcp, rval;
95 static int ftw_options = FTW_MOUNT | FTW_PHYS | FTW_ACTIONRETVAL;
96 static q_t *cnidq; /* CNID dir stack */
98 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
100 static char *netatalk_dirs[] = {
107 static int copy(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf);
108 static void siginfo(int _U_);
111 Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
112 Returns pointer to name or NULL.
114 static const char *check_netatalk_dirs(const char *name)
118 for (c=0; netatalk_dirs[c]; c++) {
119 if ((strcmp(name, netatalk_dirs[c])) == 0)
120 return netatalk_dirs[c];
126 static void usage_cp(void)
129 "Usage: ad cp [-R [-P]] [-pvf] <source_file> <target_file>\n"
130 "Usage: ad cp [-R [-P]] [-pvfx] <source_file [source_file ...]> <target_directory>\n"
135 static void upfunc(void)
138 cnid_t *cnid = dequeue(cnidq);
146 int ad_cp(int argc, char *argv[])
148 struct stat to_stat, tmp_stat;
149 int r, ch, have_trailing_slash;
156 while ((ch = getopt(argc, argv, "Rafilnpvx")) != -1)
187 ftw_options |= FTW_MOUNT;
199 (void)signal(SIGINT, siginfo);
203 /* Save the target base in "to". */
204 target = argv[--argc];
205 if ((strlcpy(to.p_path, target, PATH_MAX)) >= PATH_MAX)
206 ERROR("%s: name too long", target);
208 to.p_end = to.p_path + strlen(to.p_path);
209 if (to.p_path == to.p_end) {
213 have_trailing_slash = (to.p_end[-1] == '/');
214 if (have_trailing_slash)
215 STRIP_TRAILING_SLASH(to);
216 to.target_end = to.p_end;
218 /* Set end of argument list */
222 * Cp has two distinct cases:
224 * cp [-R] source target
225 * cp [-R] source1 ... sourceN directory
227 * In both cases, source can be either a file or a directory.
229 * In (1), the target becomes a copy of the source. That is, if the
230 * source is a file, the target will be a file, and likewise for
233 * In (2), the real target is not directory, but "directory/source".
235 r = stat(to.p_path, &to_stat);
236 if (r == -1 && errno != ENOENT)
237 ERROR("%s", to.p_path);
238 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
240 * Case (1). Target is not a directory.
243 ERROR("%s is not a directory", to.p_path);
246 * Need to detect the case:
248 * Where dir is a directory and foo does not exist, where
249 * we want pathname concatenations turned on but not for
250 * the initial mkdir().
253 lstat(*argv, &tmp_stat);
255 if (S_ISDIR(tmp_stat.st_mode) && Rflag)
262 if (have_trailing_slash && type == FILE_TO_FILE) {
264 ERROR("directory %s does not exist", to.p_path);
266 ERROR("%s is not a directory", to.p_path);
270 * Case (2). Target is a directory.
275 * Keep an inverted copy of the umask, for use in correcting
276 * permissions on created directories when not using -p.
282 /* Inhereting perms in ad_mkdir etc requires this */
286 /* Load .volinfo file for destination*/
287 if (loadvolinfo(to.p_path, &dvolinfo) == 0) {
288 if (vol_load_charsets(&dvolinfo) == -1)
289 ERROR("Error loading charsets!");
290 /* Sanity checks to ensure we can touch this volume */
291 if (dvolinfo.v_vfs_ea != AFPVOL_EA_SYS)
292 ERROR("Unsupported Extended Attributes option: %u", dvolinfo.v_vfs_ea);
294 /* initialize sufficient struct vol and initialize CNID connection */
295 dvolume.v_adouble = AD_VERSION2;
296 dvolume.v_vfs_ea = AFPVOL_EA_SYS;
297 initvol_vfs(&dvolume);
299 if ((dvolinfo.v_flags & AFPVOL_NODEV))
300 flags |= CNID_FLAG_NODEV;
302 if ((dvolume.v_cdb = cnid_open(dvolinfo.v_dbpath,
307 dvolinfo.v_dbd_port)) == NULL)
308 ERROR("Cant initialize CNID database connection for %s", dvolinfo.v_path);
310 /* setup a list for storing the CNID stack of dirs in destination path */
311 if ((cnidq = queue_init()) == NULL)
312 ERROR("Cant initialize CNID stack");
315 for (int i = 0; argv[i] != NULL; i++) {
316 /* Load .volinfo file for source */
317 if (loadvolinfo(to.p_path, &svolinfo) == 0) {
318 if (vol_load_charsets(&svolinfo) == -1)
319 ERROR("Error loading charsets!");
320 /* Sanity checks to ensure we can touch this volume */
321 if (svolinfo.v_vfs_ea != AFPVOL_EA_SYS)
322 ERROR("Unsupported Extended Attributes option: %u", svolinfo.v_vfs_ea);
324 /* initialize sufficient struct vol and initialize CNID connection */
325 svolume.v_adouble = AD_VERSION2;
326 svolume.v_vfs_ea = AFPVOL_EA_SYS;
327 initvol_vfs(&svolume);
329 if ((svolinfo.v_flags & AFPVOL_NODEV))
330 flags |= CNID_FLAG_NODEV;
332 if ((svolume.v_cdb = cnid_open(svolinfo.v_dbpath,
337 svolinfo.v_dbd_port)) == NULL)
338 ERROR("Cant initialize CNID database connection for %s", svolinfo.v_path);
341 if (nftw(argv[i], copy, upfunc, 20, ftw_options) == -1) {
342 ERROR("%s: %s", argv[i], strerror(errno));
347 cnid_close(svolume.v_cdb);
348 svolume.v_cdb = NULL;
354 static int copy(const char *path,
355 const struct stat *statp,
365 const char *dir = strrchr(path, '/');
370 if (check_netatalk_dirs(dir) != NULL) {
371 SLOG("Skipping Netatalk dir %s", path);
372 return FTW_SKIP_SIBLINGS;
376 * If we are in case (2) above, we need to append the
377 * source name to the target name.
379 if (type != FILE_TO_FILE) {
381 * Need to remember the roots of traversals to create
382 * correct pathnames. If there's a directory being
383 * copied to a non-existent directory, e.g.
384 * cp -R a/dir noexist
385 * the resulting path name should be noexist/foo, not
386 * noexist/dir/foo (where foo is a file in dir), which
387 * is the case where the target exists.
389 * Also, check for "..". This is for correct path
390 * concatenation for paths ending in "..", e.g.
392 * Paths ending in ".." are changed to ".". This is
393 * tricky, but seems the easiest way to fix the problem.
396 * Since the first level MUST be FTS_ROOTLEVEL, base
397 * is always initialized.
399 if (ftw->level == 0) {
400 if (type != DIR_TO_DNE) {
403 if (strcmp(&path[base], "..") == 0)
410 nlen = strlen(path) - base;
411 target_mid = to.target_end;
412 if (*p != '/' && target_mid[-1] != '/')
415 if (target_mid - to.p_path + nlen >= PATH_MAX) {
416 SLOG("%s%s: name too long (not copied)", to.p_path, p);
420 (void)strncat(target_mid, p, nlen);
421 to.p_end = target_mid + nlen;
423 STRIP_TRAILING_SLASH(to);
426 /* Not an error but need to remember it happened */
427 if (stat(to.p_path, &to_stat) == -1)
430 if (to_stat.st_dev == statp->st_dev &&
431 to_stat.st_ino == statp->st_ino) {
432 SLOG("%s and %s are identical (not copied).",
435 if (S_ISDIR(statp->st_mode))
436 /* without using glibc extension FTW_ACTIONRETVAL cant handle this */
439 if (!S_ISDIR(statp->st_mode) &&
440 S_ISDIR(to_stat.st_mode)) {
441 SLOG("cannot overwrite directory %s with "
450 switch (statp->st_mode & S_IFMT) {
452 if (copy_link(ftw, path, statp, !dne))
457 SLOG("%s is a directory", path);
462 * If the directory doesn't exist, create the new
463 * one with the from file mode plus owner RWX bits,
464 * modified by the umask. Trade-off between being
465 * able to write the directory (if from directory is
466 * 555) and not causing a permissions race. If the
467 * umask blocks owner writes, we fail..
470 if (mkdir(to.p_path, statp->st_mode | S_IRWXU) < 0)
471 ERROR("%s", to.p_path);
472 } else if (!S_ISDIR(to_stat.st_mode)) {
474 ERROR("%s", to.p_path);
477 /* Create ad dir and copy ".Parent" */
478 if (svolinfo.v_path && svolinfo.v_adouble == AD_VERSION2 &&
479 dvolinfo.v_path && dvolinfo.v_adouble == AD_VERSION2) {
480 /* Create ".AppleDouble" dir */
481 mode_t omask = umask(0);
482 bstring addir = bfromcstr(to.p_path);
483 bcatcstr(addir, "/.AppleDouble");
484 mkdir(cfrombstr(addir), 02777);
486 /* copy ".Parent" file */
487 bcatcstr(addir, "/.Parent");
488 bstring sdir = bfromcstr(path);
489 bcatcstr(sdir, "/.AppleDouble/.Parent");
490 if (copy_file(-1, cfrombstr(sdir), cfrombstr(addir), 0666) != 0) {
491 SLOG("Error copying %s -> %s", cfrombstr(sdir), cfrombstr(addir));
497 /* Get CNID of Parent and add new childir to CNID database */
498 did = cnid_for_path(&dvolinfo, &dvolume, to.p_path);
499 SLOG("got CNID: %u for path: %s", ntohl(did), to.p_path);
503 if (setfile(statp, -1))
506 if (preserve_dir_acls(statp, curr->fts_accpath, to.p_path) != 0)
514 SLOG("%s is a device file (not copied).", path);
517 SLOG("%s is a socket (not copied).", path);
520 SLOG("%s is a FIFO (not copied).", path);
523 if (ftw_copy_file(ftw, path, statp, dne))
528 (void)printf("%s -> %s\n", path, to.p_path);
533 static void siginfo(int sig _U_)