cp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. //usage:#define cp_trivial_usage
  37. //usage: "[OPTIONS] SOURCE... DEST"
  38. //usage:#define cp_full_usage "\n\n"
  39. //usage: "Copy SOURCE(s) to DEST\n"
  40. //usage: "\n -a Same as -dpR"
  41. //usage: IF_SELINUX(
  42. //usage: "\n -c Preserve security context"
  43. //usage: )
  44. //usage: "\n -R,-r Recurse"
  45. //usage: "\n -d,-P Preserve symlinks (default if -R)"
  46. //usage: "\n -L Follow all symlinks"
  47. //usage: "\n -H Follow symlinks on command line"
  48. //usage: "\n -p Preserve file attributes if possible"
  49. //usage: "\n -f Overwrite"
  50. //usage: "\n -i Prompt before overwrite"
  51. //usage: "\n -l,-s Create (sym)links"
  52. //usage: "\n -T Treat DEST as a normal file"
  53. //usage: "\n -u Copy only newer files"
  54. #include "libbb.h"
  55. #include "libcoreutils/coreutils.h"
  56. /* This is a NOEXEC applet. Be very careful! */
  57. int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  58. int cp_main(int argc, char **argv)
  59. {
  60. struct stat source_stat;
  61. struct stat dest_stat;
  62. const char *last;
  63. const char *dest;
  64. int s_flags;
  65. int d_flags;
  66. int flags;
  67. int status;
  68. enum {
  69. FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
  70. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  71. /*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
  72. OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
  73. OPT_reflink = 1 << (FILEUTILS_CP_OPTNUM+2),
  74. #endif
  75. };
  76. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  77. # if ENABLE_FEATURE_CP_REFLINK
  78. char *reflink = NULL;
  79. # endif
  80. flags = getopt32long(argv, "^"
  81. FILEUTILS_CP_OPTSTR
  82. "\0"
  83. // Need at least two arguments
  84. // Soft- and hardlinking doesn't mix
  85. // -P and -d are the same (-P is POSIX, -d is GNU)
  86. // -r and -R are the same
  87. // -R (and therefore -r) turns on -d (coreutils does this)
  88. // -a = -pdR
  89. "-2:l--s:s--l:Pd:rRd:Rd:apdR",
  90. "archive\0" No_argument "a"
  91. "force\0" No_argument "f"
  92. "interactive\0" No_argument "i"
  93. "link\0" No_argument "l"
  94. "dereference\0" No_argument "L"
  95. "no-dereference\0" No_argument "P"
  96. "recursive\0" No_argument "R"
  97. "symbolic-link\0" No_argument "s"
  98. "no-target-directory\0" No_argument "T"
  99. "verbose\0" No_argument "v"
  100. "update\0" No_argument "u"
  101. "remove-destination\0" No_argument "\xff"
  102. "parents\0" No_argument "\xfe"
  103. # if ENABLE_FEATURE_CP_REFLINK
  104. "reflink\0" Optional_argument "\xfd"
  105. , &reflink
  106. # endif
  107. );
  108. # if ENABLE_FEATURE_CP_REFLINK
  109. BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
  110. if (flags & FILEUTILS_REFLINK) {
  111. if (!reflink)
  112. flags |= FILEUTILS_REFLINK_ALWAYS;
  113. else if (strcmp(reflink, "always") == 0)
  114. flags |= FILEUTILS_REFLINK_ALWAYS;
  115. else if (strcmp(reflink, "auto") != 0)
  116. bb_show_usage();
  117. }
  118. # endif
  119. #else
  120. flags = getopt32(argv, "^"
  121. FILEUTILS_CP_OPTSTR
  122. "\0"
  123. "-2:l--s:s--l:Pd:rRd:Rd:apdR"
  124. );
  125. #endif
  126. /* Options of cp from GNU coreutils 6.10:
  127. * -a, --archive
  128. * -f, --force
  129. * -i, --interactive
  130. * -l, --link
  131. * -L, --dereference
  132. * -P, --no-dereference
  133. * -R, -r, --recursive
  134. * -s, --symbolic-link
  135. * -v, --verbose
  136. * -H follow command-line symbolic links in SOURCE
  137. * -d same as --no-dereference --preserve=links
  138. * -p same as --preserve=mode,ownership,timestamps
  139. * -c same as --preserve=context
  140. * -u, --update
  141. * copy only when the SOURCE file is newer than the destination
  142. * file or when the destination file is missing
  143. * --remove-destination
  144. * remove each existing destination file before attempting to open
  145. * --parents
  146. * use full source file name under DIRECTORY
  147. * -T, --no-target-directory
  148. * treat DEST as a normal file
  149. * NOT SUPPORTED IN BBOX:
  150. * --backup[=CONTROL]
  151. * make a backup of each existing destination file
  152. * -b like --backup but does not accept an argument
  153. * --copy-contents
  154. * copy contents of special files when recursive
  155. * --preserve[=ATTR_LIST]
  156. * preserve attributes (default: mode,ownership,timestamps),
  157. * if possible additional attributes: security context,links,all
  158. * --no-preserve=ATTR_LIST
  159. * --sparse=WHEN
  160. * control creation of sparse files
  161. * --strip-trailing-slashes
  162. * remove any trailing slashes from each SOURCE argument
  163. * -S, --suffix=SUFFIX
  164. * override the usual backup suffix
  165. * -t, --target-directory=DIRECTORY
  166. * copy all SOURCE arguments into DIRECTORY
  167. * -x, --one-file-system
  168. * stay on this file system
  169. * -Z, --context=CONTEXT
  170. * (SELinux) set SELinux security context of copy to CONTEXT
  171. */
  172. argc -= optind;
  173. argv += optind;
  174. /* Reverse this bit. If there is -d, bit is not set: */
  175. flags ^= FILEUTILS_DEREFERENCE;
  176. /* coreutils 6.9 compat:
  177. * by default, "cp" derefs symlinks (creates regular dest files),
  178. * but "cp -R" does not. We switch off deref if -r or -R (see above).
  179. * However, "cp -RL" must still deref symlinks: */
  180. if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
  181. flags |= FILEUTILS_DEREFERENCE;
  182. #if ENABLE_SELINUX
  183. if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
  184. selinux_or_die();
  185. }
  186. #endif
  187. status = EXIT_SUCCESS;
  188. last = argv[argc - 1];
  189. /* If there are only two arguments and... */
  190. if (argc == 2) {
  191. s_flags = cp_mv_stat2(*argv, &source_stat,
  192. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  193. if (s_flags < 0)
  194. return EXIT_FAILURE;
  195. d_flags = cp_mv_stat(last, &dest_stat);
  196. if (d_flags < 0)
  197. return EXIT_FAILURE;
  198. if (flags & FILEUTILS_NO_TARGET_DIR) { /* -T */
  199. if (!(s_flags & 2) && (d_flags & 2))
  200. /* cp -T NOTDIR DIR */
  201. bb_error_msg_and_die("'%s' is a directory", last);
  202. }
  203. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  204. //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
  205. // flags, FILEUTILS_RMDEST, OPT_parents);
  206. if (flags & OPT_parents) {
  207. if (!(d_flags & 2)) {
  208. bb_simple_error_msg_and_die("with --parents, the destination must be a directory");
  209. }
  210. }
  211. if (flags & FILEUTILS_RMDEST) {
  212. flags |= FILEUTILS_FORCE;
  213. }
  214. #endif
  215. /* ...if neither is a directory... */
  216. if (!((s_flags | d_flags) & 2)
  217. /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
  218. || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
  219. || (flags & FILEUTILS_NO_TARGET_DIR)
  220. ) {
  221. /* Do a simple copy */
  222. dest = last;
  223. goto DO_COPY; /* NB: argc==2 -> *++argv==last */
  224. }
  225. } else if (flags & FILEUTILS_NO_TARGET_DIR) {
  226. bb_simple_error_msg_and_die("too many arguments");
  227. }
  228. while (1) {
  229. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  230. if (flags & OPT_parents) {
  231. char *dest_dup;
  232. char *dest_dir;
  233. dest = concat_path_file(last, *argv);
  234. dest_dup = xstrdup(dest);
  235. dest_dir = dirname(dest_dup);
  236. if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
  237. return EXIT_FAILURE;
  238. }
  239. free(dest_dup);
  240. goto DO_COPY;
  241. }
  242. #endif
  243. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  244. DO_COPY:
  245. if (copy_file(*argv, dest, flags) < 0) {
  246. status = EXIT_FAILURE;
  247. }
  248. if (*++argv == last) {
  249. /* possibly leaking dest... */
  250. break;
  251. }
  252. /* don't move up: dest may be == last and not malloced! */
  253. free((void*)dest);
  254. }
  255. /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
  256. return status;
  257. }