3
0

cp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cp implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  11. *
  12. * Size reduction.
  13. */
  14. //config:config CP
  15. //config: bool "cp (10 kb)"
  16. //config: default y
  17. //config: help
  18. //config: cp is used to copy files and directories.
  19. //config:
  20. //config:config FEATURE_CP_LONG_OPTIONS
  21. //config: bool "Enable long options"
  22. //config: default y
  23. //config: depends on CP && LONG_OPTS
  24. //config: help
  25. //config: Enable long options.
  26. //config: Also add support for --parents option.
  27. //config:
  28. //config:config FEATURE_CP_REFLINK
  29. //config: bool "Enable --reflink[=auto]"
  30. //config: default y
  31. //config: depends on FEATURE_CP_LONG_OPTIONS
  32. //applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp))
  33. /* NOEXEC despite cases when it can be a "runner" (cp -r LARGE_DIR NEW_DIR) */
  34. //kbuild:lib-$(CONFIG_CP) += cp.o
  35. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
  36. // Options of cp from GNU coreutils 6.10:
  37. // -a, --archive
  38. // -f, --force
  39. // -i, --interactive
  40. // -l, --link
  41. // -L, --dereference
  42. // -P, --no-dereference
  43. // -R, -r, --recursive
  44. // -s, --symbolic-link
  45. // -v, --verbose
  46. // -H follow command-line symbolic links in SOURCE
  47. // -d same as --no-dereference --preserve=links
  48. // -p same as --preserve=mode,ownership,timestamps
  49. // -c same as --preserve=context
  50. // -u, --update
  51. // copy only when the SOURCE file is newer than the destination
  52. // file or when the destination file is missing
  53. // --remove-destination
  54. // remove each existing destination file before attempting to open
  55. // --parents
  56. // use full source file name under DIRECTORY
  57. // -T, --no-target-directory
  58. // treat DEST as a normal file
  59. // NOT SUPPORTED IN BBOX:
  60. // --backup[=CONTROL]
  61. // make a backup of each existing destination file
  62. // -b like --backup but does not accept an argument
  63. // --copy-contents
  64. // copy contents of special files when recursive
  65. // --preserve[=ATTR_LIST]
  66. // preserve attributes (default: mode,ownership,timestamps),
  67. // if possible additional attributes: security context,links,all
  68. // --no-preserve=ATTR_LIST
  69. // --sparse=WHEN
  70. // control creation of sparse files
  71. // --strip-trailing-slashes
  72. // remove any trailing slashes from each SOURCE argument
  73. // -S, --suffix=SUFFIX
  74. // override the usual backup suffix
  75. // -t, --target-directory=DIRECTORY
  76. // copy all SOURCE arguments into DIRECTORY
  77. // -x, --one-file-system
  78. // stay on this file system
  79. // -Z, --context=CONTEXT
  80. // (SELinux) set SELinux security context of copy to CONTEXT
  81. //usage:#define cp_trivial_usage
  82. //usage: "[-arPLHpfinlsTu] SOURCE DEST\n"
  83. //usage: "or: cp [-arPLHpfinlsu] SOURCE... { -t DIRECTORY | DIRECTORY }"
  84. //usage:#define cp_full_usage "\n\n"
  85. //usage: "Copy SOURCEs to DEST\n"
  86. //usage: "\n -a Same as -dpR"
  87. //usage: IF_SELINUX(
  88. //usage: "\n -c Preserve security context"
  89. //usage: )
  90. //usage: "\n -R,-r Recurse"
  91. //usage: "\n -d,-P Preserve symlinks (default if -R)"
  92. //usage: "\n -L Follow all symlinks"
  93. //usage: "\n -H Follow symlinks on command line"
  94. //usage: "\n -p Preserve file attributes if possible"
  95. //usage: "\n -f Overwrite"
  96. //usage: "\n -i Prompt before overwrite"
  97. //usage: "\n -n Don't overwrite"
  98. //usage: "\n -l,-s Create (sym)links"
  99. //usage: "\n -T Refuse to copy if DEST is a directory"
  100. //usage: "\n -t DIR Copy all SOURCEs into DIR"
  101. //usage: "\n -u Copy only newer files"
  102. #include "libbb.h"
  103. #include "libcoreutils/coreutils.h"
  104. /* This is a NOEXEC applet. Be very careful! */
  105. int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  106. int cp_main(int argc, char **argv)
  107. {
  108. struct stat source_stat;
  109. struct stat dest_stat;
  110. const char *last;
  111. const char *dest;
  112. int s_flags;
  113. int d_flags;
  114. int flags;
  115. int status;
  116. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  117. enum {
  118. /*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTBITS */
  119. OPT_parents = 1 << (FILEUTILS_CP_OPTBITS+1),
  120. OPT_reflink = 1 << (FILEUTILS_CP_OPTBITS+2),
  121. };
  122. # if ENABLE_FEATURE_CP_REFLINK
  123. char *reflink = NULL;
  124. # endif
  125. flags = getopt32long(argv, "^"
  126. FILEUTILS_CP_OPTSTR
  127. "\0"
  128. // Need at least one argument. (Usually two+, but -t DIR can have only one)
  129. // Soft- and hardlinking doesn't mix
  130. // -P and -d are the same (-P is POSIX, -d is GNU)
  131. // -r and -R are the same
  132. // -R (and therefore -r) turns on -d (coreutils does this)
  133. // -a = -pdR
  134. // -i overrides -n and vice versa (last wins)
  135. "-1:l--s:s--l:Pd:rRd:Rd:apdR:i-n:n-i",
  136. "archive\0" No_argument "a"
  137. "force\0" No_argument "f"
  138. "interactive\0" No_argument "i"
  139. "no-clobber\0" No_argument "n"
  140. "link\0" No_argument "l"
  141. "dereference\0" No_argument "L"
  142. "no-dereference\0" No_argument "P"
  143. "recursive\0" No_argument "R"
  144. "symbolic-link\0" No_argument "s"
  145. "no-target-directory\0" No_argument "T"
  146. "target-directory\0" Required_argument "t"
  147. "verbose\0" No_argument "v"
  148. "update\0" No_argument "u"
  149. "remove-destination\0" No_argument "\xff"
  150. "parents\0" No_argument "\xfe"
  151. # if ENABLE_FEATURE_CP_REFLINK
  152. "reflink\0" Optional_argument "\xfd"
  153. # endif
  154. , &last
  155. # if ENABLE_FEATURE_CP_REFLINK
  156. , &reflink
  157. # endif
  158. );
  159. # if ENABLE_FEATURE_CP_REFLINK
  160. BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
  161. if (flags & FILEUTILS_REFLINK) {
  162. if (!reflink)
  163. flags |= FILEUTILS_REFLINK_ALWAYS;
  164. else if (strcmp(reflink, "always") == 0)
  165. flags |= FILEUTILS_REFLINK_ALWAYS;
  166. else if (strcmp(reflink, "auto") != 0)
  167. bb_show_usage();
  168. }
  169. # endif
  170. #else
  171. flags = getopt32(argv, "^"
  172. FILEUTILS_CP_OPTSTR
  173. "\0"
  174. "-1:l--s:s--l:Pd:rRd:Rd:apdR"
  175. , &last
  176. );
  177. #endif
  178. argc -= optind;
  179. argv += optind;
  180. /* Reverse this bit. If there is -d, bit is not set: */
  181. flags ^= FILEUTILS_DEREFERENCE;
  182. /* coreutils 6.9 compat:
  183. * by default, "cp" derefs symlinks (creates regular dest files),
  184. * but "cp -R" does not. We switch off deref if -r or -R (see above).
  185. * However, "cp -RL" must still deref symlinks: */
  186. if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
  187. flags |= FILEUTILS_DEREFERENCE;
  188. #if ENABLE_SELINUX
  189. if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
  190. selinux_or_die();
  191. }
  192. #endif
  193. status = EXIT_SUCCESS;
  194. if (!(flags & FILEUTILS_TARGET_DIR)) {
  195. last = argv[argc - 1];
  196. if (argc < 2)
  197. bb_show_usage();
  198. if (argc != 2) {
  199. if (flags & FILEUTILS_NO_TARGET_DIR)
  200. bb_show_usage();
  201. /* "cp A B C... DIR" - target must be dir */
  202. } else /* argc == 2 */ {
  203. /* "cp A B" - only case where target can be not a dir */
  204. s_flags = cp_mv_stat2(*argv, &source_stat,
  205. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  206. if (s_flags < 0) /* error other than ENOENT */
  207. return EXIT_FAILURE;
  208. d_flags = cp_mv_stat(last, &dest_stat);
  209. if (d_flags < 0) /* error other than ENOENT */
  210. return EXIT_FAILURE;
  211. if (flags & FILEUTILS_NO_TARGET_DIR) { /* -T */
  212. if (!(s_flags & 2) && (d_flags & 2))
  213. /* cp -T NOTDIR DIR */
  214. bb_error_msg_and_die("'%s' is a directory", last);
  215. }
  216. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  217. //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
  218. // flags, FILEUTILS_RMDEST, OPT_parents);
  219. if (flags & OPT_parents) {
  220. if (!(d_flags & 2)) {
  221. bb_simple_error_msg_and_die("with --parents, the destination must be a directory");
  222. }
  223. }
  224. if (flags & FILEUTILS_RMDEST) {
  225. flags |= FILEUTILS_FORCE;
  226. }
  227. #endif
  228. /* ...if neither is a directory... */
  229. if (!((s_flags | d_flags) & 2)
  230. /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
  231. || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
  232. || (flags & FILEUTILS_NO_TARGET_DIR)
  233. ) {
  234. /* Do a simple copy */
  235. dest = last;
  236. goto DO_COPY; /* NB: argc==2 -> *++argv==last */
  237. }
  238. }
  239. }
  240. /* else: last is DIR from "-t DIR" */
  241. while (1) {
  242. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  243. if (flags & OPT_parents) {
  244. char *dest_dup;
  245. char *dest_dir;
  246. dest = concat_path_file(last, *argv);
  247. dest_dup = xstrdup(dest);
  248. dest_dir = dirname(dest_dup);
  249. if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
  250. return EXIT_FAILURE;
  251. }
  252. free(dest_dup);
  253. goto DO_COPY;
  254. }
  255. #endif
  256. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  257. DO_COPY:
  258. if (copy_file(*argv, dest, flags) < 0) {
  259. status = EXIT_FAILURE;
  260. }
  261. if (!*++argv || *argv == last) {
  262. /* possibly leaking dest... */
  263. break;
  264. }
  265. /* don't move up: dest may be == last and not malloced! */
  266. free((void*)dest);
  267. }
  268. /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
  269. return status;
  270. }