source: rtems/cpukit/libmisc/shell/main_cp.c @ 308bb9b

4.115
Last change on this file since 308bb9b was 308bb9b, checked in by Joel Sherrill <joel.sherrill@…>, on 10/12/14 at 21:09:20

shell/main_cp.c: Delete unused -rtems_shell_cp_exit()

  • Property mode set to 100644
File size: 14.8 KB
Line 
1/* $NetBSD: cp.c,v 1.39 2005/10/24 12:59:07 kleink Exp $ */
2
3/*
4 * Copyright (c) 1988, 1993, 1994
5 *  The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * David Hitz of Auspex Systems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#if 0
40#ifndef lint
41__COPYRIGHT(
42"@(#) Copyright (c) 1988, 1993, 1994\n\
43  The Regents of the University of California.  All rights reserved.\n");
44#endif /* not lint */
45
46#ifndef lint
47#if 0
48static char sccsid[] = "@(#)cp.c  8.5 (Berkeley) 4/29/95";
49#else
50__RCSID("$NetBSD: cp.c,v 1.39 2005/10/24 12:59:07 kleink Exp $");
51#endif
52#endif /* not lint */
53#endif
54/*
55 * Cp copies source files to target files.
56 *
57 * The global PATH_T structure "to" always contains the path to the
58 * current target file.  Since fts(3) does not change directories,
59 * this path can be either absolute or dot-relative.
60 *
61 * The basic algorithm is to initialize "to" and use fts(3) to traverse
62 * the file hierarchy rooted in the argument list.  A trivial case is the
63 * case of 'cp file1 file2'.  The more interesting case is the case of
64 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
65 * path (relative to the root of the traversal) is appended to dir (stored
66 * in "to") to form the final target path.
67 */
68
69#include <rtems.h>
70#include <rtems/shell.h>
71#include <rtems/shellconfig.h>
72#define __need_getopt_newlib
73#include <getopt.h>
74
75#include <sys/param.h>
76#include <sys/stat.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fts.h>
81#include <limits.h>
82#include <signal.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <unistd.h>
87
88#include "internal.h"
89
90#include "extern-cp.h"
91
92#define S_ISTXT 0
93
94#define STRIP_TRAILING_SLASH(p) {                                       \
95        while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')      \
96                *--(p).p_end = 0;                                       \
97}
98
99enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
100
101static int Rflag, rflag;
102
103static int main_cp(rtems_shell_cp_globals* cp_globals, int, char *[]);
104static int copy(rtems_shell_cp_globals* cp_globals, char *[], enum op, int);
105static int mastercmp(const FTSENT **, const FTSENT **);
106
107int
108rtems_shell_main_cp(int argc, char *argv[])
109{
110  rtems_shell_cp_globals cp_globals;
111  memset (&cp_globals, 0, sizeof (cp_globals));
112  Rflag = rflag = 0;
113  if (setjmp (cp_globals.exit_jmp) == 0)
114    return main_cp (&cp_globals, argc, argv);
115  return 1;
116}
117
118int
119main_cp(rtems_shell_cp_globals* cp_globals, int argc, char *argv[])
120{
121        struct stat to_stat, tmp_stat;
122        enum op type;
123        int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
124        char *target;
125  struct getopt_data getopt_reent;
126
127        Hflag = Lflag = Pflag = 0;
128  memset(&getopt_reent, 0, sizeof(getopt_data));
129
130        while ((ch = getopt_r(argc, argv, "HLPRafilnprv", &getopt_reent)) != -1)
131                switch (ch) {
132                case 'H':
133                        Hflag = 1;
134                        Lflag = Pflag = 0;
135                        break;
136                case 'L':
137                        Lflag = 1;
138                        Hflag = Pflag = 0;
139                        break;
140                case 'P':
141                        Pflag = 1;
142                        Hflag = Lflag = 0;
143                        break;
144                case 'R':
145                        Rflag = 1;
146                        break;
147                case 'a':
148                        Pflag = 1;
149                        pflag = 1;
150                        Rflag = 1;
151                        Hflag = Lflag = 0;
152                        break;
153                case 'f':
154                        fflag = 1;
155                        iflag = nflag = 0;
156                        break;
157                case 'i':
158                        iflag = 1;
159                        fflag = nflag = 0;
160                        break;
161                case 'l':
162                        lflag = 1;
163                        break;
164                case 'n':
165                        nflag = 1;
166                        fflag = iflag = 0;
167                        break;
168                case 'p':
169                        pflag = 1;
170                        break;
171                case 'r':
172                        rflag = Lflag = 1;
173                        Hflag = Pflag = 0;
174                        break;
175                case 'v':
176                        vflag = 1;
177                        break;
178                default:
179                        usage(cp_globals);
180                        break;
181                }
182        argc -= getopt_reent.optind;
183        argv += getopt_reent.optind;
184
185        if (argc < 2)
186                usage(cp_globals);
187
188        fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
189        if (Rflag && rflag)
190                errx(exit_jump, 1, "the -R and -r options may not be specified together");
191        if (rflag)
192                Rflag = 1;
193        if (Rflag) {
194                if (Hflag)
195                        fts_options |= FTS_COMFOLLOW;
196                if (Lflag) {
197                        fts_options &= ~FTS_PHYSICAL;
198                        fts_options |= FTS_LOGICAL;
199                }
200        } else {
201                fts_options &= ~FTS_PHYSICAL;
202                fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
203        }
204#if 0
205        (void)signal(SIGINFO, siginfo);
206#endif
207
208        /* Save the target base in "to". */
209        target = argv[--argc];
210        if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
211                errx(exit_jump, 1, "%s: name too long", target);
212        to.p_end = to.p_path + strlen(to.p_path);
213        if (to.p_path == to.p_end) {
214                *to.p_end++ = '.';
215                *to.p_end = 0;
216        }
217        have_trailing_slash = (to.p_end[-1] == '/');
218        if (have_trailing_slash)
219                STRIP_TRAILING_SLASH(to);
220        to.target_end = to.p_end;
221
222        /* Set end of argument list for fts(3). */
223        argv[argc] = NULL;
224
225        /*
226         * Cp has two distinct cases:
227         *
228         * cp [-R] source target
229         * cp [-R] source1 ... sourceN directory
230         *
231         * In both cases, source can be either a file or a directory.
232         *
233         * In (1), the target becomes a copy of the source. That is, if the
234         * source is a file, the target will be a file, and likewise for
235         * directories.
236         *
237         * In (2), the real target is not directory, but "directory/source".
238         */
239        r = stat(to.p_path, &to_stat);
240        if (r == -1 && errno != ENOENT)
241                err(exit_jump, 1, "%s", to.p_path);
242        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
243                /*
244                 * Case (1).  Target is not a directory.
245                 */
246                if (argc > 1)
247                        errx(exit_jump, 1, "%s is not a directory", to.p_path);
248
249                /*
250                 * Need to detect the case:
251                 *      cp -R dir foo
252                 * Where dir is a directory and foo does not exist, where
253                 * we want pathname concatenations turned on but not for
254                 * the initial mkdir().
255                 */
256                if (r == -1) {
257                        if (Rflag && (Lflag || Hflag))
258                                stat(*argv, &tmp_stat);
259                        else
260                                lstat(*argv, &tmp_stat);
261
262                        if (S_ISDIR(tmp_stat.st_mode) && Rflag)
263                                type = DIR_TO_DNE;
264                        else
265                                type = FILE_TO_FILE;
266                } else
267                        type = FILE_TO_FILE;
268
269                if (have_trailing_slash && type == FILE_TO_FILE) {
270                        if (r == -1)
271                                errx(exit_jump, 1, "directory %s does not exist",
272                                     to.p_path);
273                        else
274                                errx(exit_jump, 1, "%s is not a directory", to.p_path);
275                }
276        } else
277                /*
278                 * Case (2).  Target is a directory.
279                 */
280                type = FILE_TO_DIR;
281
282  return copy(cp_globals, argv, type, fts_options);
283}
284
285int
286copy(rtems_shell_cp_globals* cp_globals,
287     char *argv[], enum op type, int fts_options)
288{
289        struct stat to_stat;
290        FTS *ftsp;
291        FTSENT *curr;
292        int base = 0, dne, badcp, rval;
293        size_t nlen;
294        char *p, *target_mid;
295        mode_t mask, mode;
296
297        /*
298         * Keep an inverted copy of the umask, for use in correcting
299         * permissions on created directories when not using -p.
300         */
301        mask = ~umask(0777);
302        umask(~mask);
303
304        if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
305                err(exit_jump, 1, "fts_open");
306        for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
307                switch (curr->fts_info) {
308                case FTS_NS:
309                case FTS_DNR:
310                case FTS_ERR:
311                        warnx("%s: %s",
312                            curr->fts_path, strerror(curr->fts_errno));
313                        badcp = rval = 1;
314                        continue;
315                case FTS_DC:                    /* Warn, continue. */
316                        warnx("%s: directory causes a cycle", curr->fts_path);
317                        badcp = rval = 1;
318                        continue;
319                default:
320                        ;
321                }
322
323                /*
324                 * If we are in case (2) or (3) above, we need to append the
325                 * source name to the target name.
326                 */
327                if (type != FILE_TO_FILE) {
328                        /*
329                         * Need to remember the roots of traversals to create
330                         * correct pathnames.  If there's a directory being
331                         * copied to a non-existent directory, e.g.
332                         *      cp -R a/dir noexist
333                         * the resulting path name should be noexist/foo, not
334                         * noexist/dir/foo (where foo is a file in dir), which
335                         * is the case where the target exists.
336                         *
337                         * Also, check for "..".  This is for correct path
338                         * concatenation for paths ending in "..", e.g.
339                         *      cp -R .. /tmp
340                         * Paths ending in ".." are changed to ".".  This is
341                         * tricky, but seems the easiest way to fix the problem.
342                         *
343                         * XXX
344                         * Since the first level MUST be FTS_ROOTLEVEL, base
345                         * is always initialized.
346                         */
347                        if (curr->fts_level == FTS_ROOTLEVEL) {
348                                if (type != DIR_TO_DNE) {
349                                        p = strrchr(curr->fts_path, '/');
350                                        base = (p == NULL) ? 0 :
351                                            (int)(p - curr->fts_path + 1);
352
353                                        if (!strcmp(&curr->fts_path[base],
354                                            ".."))
355                                                base += 1;
356                                } else
357                                        base = curr->fts_pathlen;
358                        }
359
360                        p = &curr->fts_path[base];
361                        nlen = curr->fts_pathlen - base;
362                        target_mid = to.target_end;
363                        if (*p != '/' && target_mid[-1] != '/')
364                                *target_mid++ = '/';
365                        *target_mid = 0;
366                        if (target_mid - to.p_path + nlen >= PATH_MAX) {
367                                warnx("%s%s: name too long (not copied)",
368                                    to.p_path, p);
369                                badcp = rval = 1;
370                                continue;
371                        }
372                        (void)strncat(target_mid, p, nlen);
373                        to.p_end = target_mid + nlen;
374                        *to.p_end = 0;
375                        STRIP_TRAILING_SLASH(to);
376                }
377
378                if (curr->fts_info == FTS_DP) {
379                        /*
380                         * We are nearly finished with this directory.  If we
381                         * didn't actually copy it, or otherwise don't need to
382                         * change its attributes, then we are done.
383                         */
384                        if (!curr->fts_number)
385                                continue;
386                        /*
387                         * If -p is in effect, set all the attributes.
388                         * Otherwise, set the correct permissions, limited
389                         * by the umask.  Optimise by avoiding a chmod()
390                         * if possible (which is usually the case if we
391                         * made the directory).  Note that mkdir() does not
392                         * honour setuid, setgid and sticky bits, but we
393                         * normally want to preserve them on directories.
394                         */
395                        if (pflag) {
396                                if (setfile(cp_globals, curr->fts_statp, -1))
397                                        rval = 1;
398                                if (preserve_dir_acls(curr->fts_statp,
399                                    curr->fts_accpath, to.p_path) != 0)
400                                        rval = 1;
401                        } else {
402                                mode = curr->fts_statp->st_mode;
403                                if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
404                                    ((mode | S_IRWXU) & mask) != (mode & mask))
405                                        if (chmod(to.p_path, mode & mask) != 0){
406                                                warn("chmod: %s", to.p_path);
407                                                rval = 1;
408                                        }
409                        }
410                        continue;
411                }
412
413                /* Not an error but need to remember it happened */
414                if (stat(to.p_path, &to_stat) == -1)
415                        dne = 1;
416                else {
417                        if (to_stat.st_dev == curr->fts_statp->st_dev &&
418                            to_stat.st_ino == curr->fts_statp->st_ino) {
419                                warnx("%s and %s are identical (not copied).",
420                                    to.p_path, curr->fts_path);
421                                badcp = rval = 1;
422                                if (S_ISDIR(curr->fts_statp->st_mode))
423                                        (void)fts_set(ftsp, curr, FTS_SKIP);
424                                continue;
425                        }
426                        if (!S_ISDIR(curr->fts_statp->st_mode) &&
427                            S_ISDIR(to_stat.st_mode)) {
428                                warnx("cannot overwrite directory %s with "
429                                    "non-directory %s",
430                                    to.p_path, curr->fts_path);
431                                badcp = rval = 1;
432                                continue;
433                        }
434                        dne = 0;
435                }
436
437                switch (curr->fts_statp->st_mode & S_IFMT) {
438                case S_IFLNK:
439                        /* Catch special case of a non-dangling symlink */
440                        if ((fts_options & FTS_LOGICAL) ||
441                            ((fts_options & FTS_COMFOLLOW) &&
442                            curr->fts_level == 0)) {
443                                if (copy_file(cp_globals, curr, dne))
444                                        badcp = rval = 1;
445                        } else {
446                                if (copy_link(cp_globals, curr, !dne))
447                                        badcp = rval = 1;
448                        }
449                        break;
450                case S_IFDIR:
451                        if (!Rflag) {
452                                warnx("%s is a directory (not copied).",
453                                    curr->fts_path);
454                                (void)fts_set(ftsp, curr, FTS_SKIP);
455                                badcp = rval = 1;
456                                break;
457                        }
458                        /*
459                         * If the directory doesn't exist, create the new
460                         * one with the from file mode plus owner RWX bits,
461                         * modified by the umask.  Trade-off between being
462                         * able to write the directory (if from directory is
463                         * 555) and not causing a permissions race.  If the
464                         * umask blocks owner writes, we fail..
465                         */
466                        if (dne) {
467                                if (mkdir(to.p_path,
468                                    curr->fts_statp->st_mode | S_IRWXU) < 0)
469                                        err(exit_jump, 1, "%s", to.p_path);
470                        } else if (!S_ISDIR(to_stat.st_mode)) {
471                                errno = ENOTDIR;
472                                err(exit_jump, 1, "%s", to.p_path);
473                        }
474                        /*
475                         * Arrange to correct directory attributes later
476                         * (in the post-order phase) if this is a new
477                         * directory, or if the -p flag is in effect.
478                         */
479                        curr->fts_number = pflag || dne;
480                        break;
481                case S_IFBLK:
482                case S_IFCHR:
483                        if (Rflag) {
484                                if (copy_special(cp_globals, curr->fts_statp, !dne))
485                                        badcp = rval = 1;
486                        } else {
487                                if (copy_file(cp_globals, curr, dne))
488                                        badcp = rval = 1;
489                        }
490                        break;
491                case S_IFSOCK:
492                        warnx("%s is a socket (not copied).",
493                                    curr->fts_path);
494                case S_IFIFO:
495                        if (Rflag) {
496                                if (copy_fifo(cp_globals, curr->fts_statp, !dne))
497                                        badcp = rval = 1;
498                        } else {
499                                if (copy_file(cp_globals, curr, dne))
500                                        badcp = rval = 1;
501                        }
502                        break;
503                default:
504                        if (copy_file(cp_globals, curr, dne))
505                                badcp = rval = 1;
506                        break;
507                }
508                if (vflag && !badcp)
509                        (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
510        }
511        if (errno)
512                err(exit_jump, 1, "fts_read");
513        fts_close(ftsp);
514        return (rval);
515}
516
517/*
518 * mastercmp --
519 *      The comparison function for the copy order.  The order is to copy
520 *      non-directory files before directory files.  The reason for this
521 *      is because files tend to be in the same cylinder group as their
522 *      parent directory, whereas directories tend not to be.  Copying the
523 *      files first reduces seeking.
524 */
525int
526mastercmp(const FTSENT **a, const FTSENT **b)
527{
528        int a_info, b_info;
529
530        a_info = (*a)->fts_info;
531        if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
532                return (0);
533        b_info = (*b)->fts_info;
534        if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
535                return (0);
536        if (a_info == FTS_D)
537                return (-1);
538        if (b_info == FTS_D)
539                return (1);
540        return (0);
541}
542
543rtems_shell_cmd_t rtems_shell_CP_Command = {
544  "cp",                                                /* name */
545  "cp [-R [-H | -L | -P]] [-f | -i] [-pv] src target", /* usage */
546  "files",                                             /* topic */
547  rtems_shell_main_cp,                                 /* command */
548  NULL,                                                /* alias */
549  NULL                                                 /* next */
550};
551
Note: See TracBrowser for help on using the repository browser.