cp.c 6.8 KB

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