Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/cp.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/cp.c
r1765 r2725 6 6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp> 7 7 * 8 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.8 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 9 9 */ 10 10 … … 21 21 /* This is a NOEXEC applet. Be very careful! */ 22 22 23 24 int cp_main(int argc, char **argv); 23 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 25 24 int cp_main(int argc, char **argv) 26 25 { … … 32 31 int d_flags; 33 32 int flags; 34 int status = 0;33 int status; 35 34 enum { 36 35 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), 37 36 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)), 38 37 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1), 39 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), 40 OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), 38 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), 39 #if ENABLE_FEATURE_CP_LONG_OPTIONS 40 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), 41 #endif 41 42 }; 42 43 43 // Soft- and hardlinking don't mix 44 // Need at least two arguments 45 // Soft- and hardlinking doesn't mix 44 46 // -P and -d are the same (-P is POSIX, -d is GNU) 45 47 // -r and -R are the same 48 // -R (and therefore -r) turns on -d (coreutils does this) 46 49 // -a = -pdR 47 opt_complementary = "l--s:s--l:Pd:rR:apdR"; 48 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHL"); 49 /* Default behavior of cp is to dereference, so we don't have to do 50 * anything special when we are given -L. 51 * The behavior of -H is *almost* like -L, but not quite, so let's 52 * just ignore it too for fun. 53 if (flags & OPT_L) ... 54 if (flags & OPT_H) ... // deref command-line params only 55 */ 50 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR"; 51 #if ENABLE_FEATURE_CP_LONG_OPTIONS 52 applet_long_options = 53 "archive\0" No_argument "a" 54 "force\0" No_argument "f" 55 "interactive\0" No_argument "i" 56 "link\0" No_argument "l" 57 "dereference\0" No_argument "L" 58 "no-dereference\0" No_argument "P" 59 "recursive\0" No_argument "R" 60 "symbolic-link\0" No_argument "s" 61 "verbose\0" No_argument "v" 62 "parents\0" No_argument "\xff" 63 ; 64 #endif 65 // -v (--verbose) is ignored 66 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv"); 67 /* Options of cp from GNU coreutils 6.10: 68 * -a, --archive 69 * -f, --force 70 * -i, --interactive 71 * -l, --link 72 * -L, --dereference 73 * -P, --no-dereference 74 * -R, -r, --recursive 75 * -s, --symbolic-link 76 * -v, --verbose 77 * -H follow command-line symbolic links in SOURCE 78 * -d same as --no-dereference --preserve=links 79 * -p same as --preserve=mode,ownership,timestamps 80 * -c same as --preserve=context 81 * --parents 82 * use full source file name under DIRECTORY 83 * NOT SUPPORTED IN BBOX: 84 * --backup[=CONTROL] 85 * make a backup of each existing destination file 86 * -b like --backup but does not accept an argument 87 * --copy-contents 88 * copy contents of special files when recursive 89 * --preserve[=ATTR_LIST] 90 * preserve attributes (default: mode,ownership,timestamps), 91 * if possible additional attributes: security context,links,all 92 * --no-preserve=ATTR_LIST 93 * --remove-destination 94 * remove each existing destination file before attempting to open 95 * --sparse=WHEN 96 * control creation of sparse files 97 * --strip-trailing-slashes 98 * remove any trailing slashes from each SOURCE argument 99 * -S, --suffix=SUFFIX 100 * override the usual backup suffix 101 * -t, --target-directory=DIRECTORY 102 * copy all SOURCE arguments into DIRECTORY 103 * -T, --no-target-directory 104 * treat DEST as a normal file 105 * -u, --update 106 * copy only when the SOURCE file is newer than the destination 107 * file or when the destination file is missing 108 * -x, --one-file-system 109 * stay on this file system 110 * -Z, --context=CONTEXT 111 * (SELinux) set SELinux security context of copy to CONTEXT 112 */ 113 argc -= optind; 114 argv += optind; 115 /* Reverse this bit. If there is -d, bit is not set: */ 116 flags ^= FILEUTILS_DEREFERENCE; 117 /* coreutils 6.9 compat: 118 * by default, "cp" derefs symlinks (creates regular dest files), 119 * but "cp -R" does not. We switch off deref if -r or -R (see above). 120 * However, "cp -RL" must still deref symlinks: */ 121 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */ 122 flags |= FILEUTILS_DEREFERENCE; 56 123 57 124 #if ENABLE_SELINUX … … 61 128 #endif 62 129 63 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ 64 65 if (optind + 2 > argc) { 66 bb_show_usage(); 67 } 68 130 status = EXIT_SUCCESS; 69 131 last = argv[argc - 1]; 70 argv += optind;71 72 132 /* If there are only two arguments and... */ 73 if ( optind + 2 == argc) {133 if (argc == 2) { 74 134 s_flags = cp_mv_stat2(*argv, &source_stat, 75 135 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); 76 136 if (s_flags < 0) 77 137 return EXIT_FAILURE; … … 80 140 return EXIT_FAILURE; 81 141 82 /* ...if neither is a directory or... */ 83 if ( !((s_flags | d_flags) & 2) || 84 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ 85 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) 142 #if ENABLE_FEATURE_CP_LONG_OPTIONS 143 if (flags & OPT_parents) { 144 if (!(d_flags & 2)) { 145 bb_error_msg_and_die("with --parents, the destination must be a directory"); 146 } 147 } 148 #endif 149 150 /* ...if neither is a directory... */ 151 if (!((s_flags | d_flags) & 2) 152 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */ 153 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) 86 154 ) { 87 /* ...do a simple copy.*/88 dest = xstrdup(last);89 goto DO_COPY; /* N ote: optind+2==argc implies argv[1]==last below.*/155 /* Do a simple copy */ 156 dest = last; 157 goto DO_COPY; /* NB: argc==2 -> *++argv==last */ 90 158 } 91 159 } 92 160 93 do { 94 dest = concat_path_file(last, bb_get_last_path_component(*argv)); 161 while (1) { 162 #if ENABLE_FEATURE_CP_LONG_OPTIONS 163 if (flags & OPT_parents) { 164 char *dest_dup; 165 char *dest_dir; 166 dest = concat_path_file(last, *argv); 167 dest_dup = xstrdup(dest); 168 dest_dir = dirname(dest_dup); 169 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) { 170 return EXIT_FAILURE; 171 } 172 free(dest_dup); 173 goto DO_COPY; 174 } 175 #endif 176 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); 95 177 DO_COPY: 96 178 if (copy_file(*argv, dest, flags) < 0) { 97 status = 1;179 status = EXIT_FAILURE; 98 180 } 181 if (*++argv == last) { 182 /* possibly leaking dest... */ 183 break; 184 } 185 /* don't move up: dest may be == last and not malloced! */ 99 186 free((void*)dest); 100 } while (*++argv != last);187 } 101 188 189 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */ 102 190 return status; 103 191 }
Note:
See TracChangeset
for help on using the changeset viewer.