cp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (9.7 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. //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. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  69. flags = getopt32long(argv, "^"
  70. FILEUTILS_CP_OPTSTR
  71. "\0"
  72. // Need at least two arguments
  73. // Soft- and hardlinking doesn't mix
  74. // -P and -d are the same (-P is POSIX, -d is GNU)
  75. // -r and -R are the same
  76. // -R (and therefore -r) turns on -d (coreutils does this)
  77. // -a = -pdR
  78. "-2:l--s:s--l:Pd:rRd:Rd:apdR",
  79. "archive\0" No_argument "a"
  80. "force\0" No_argument "f"
  81. "interactive\0" No_argument "i"
  82. "link\0" No_argument "l"
  83. "dereference\0" No_argument "L"
  84. "no-dereference\0" No_argument "P"
  85. "recursive\0" No_argument "R"
  86. "symbolic-link\0" No_argument "s"
  87. "verbose\0" No_argument "v"
  88. "update\0" No_argument "u"
  89. "remove-destination\0" No_argument "\xff"
  90. "parents\0" No_argument "\xfe"
  91. );
  92. #else
  93. flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
  94. #endif
  95. /* Options of cp from GNU coreutils 6.10:
  96. * -a, --archive
  97. * -f, --force
  98. * -i, --interactive
  99. * -l, --link
  100. * -L, --dereference
  101. * -P, --no-dereference
  102. * -R, -r, --recursive
  103. * -s, --symbolic-link
  104. * -v, --verbose
  105. * -H follow command-line symbolic links in SOURCE
  106. * -d same as --no-dereference --preserve=links
  107. * -p same as --preserve=mode,ownership,timestamps
  108. * -c same as --preserve=context
  109. * -u, --update
  110. * copy only when the SOURCE file is newer than the destination
  111. * file or when the destination file is missing
  112. * --remove-destination
  113. * remove each existing destination file before attempting to open
  114. * --parents
  115. * use full source file name under DIRECTORY
  116. * NOT SUPPORTED IN BBOX:
  117. * --backup[=CONTROL]
  118. * make a backup of each existing destination file
  119. * -b like --backup but does not accept an argument
  120. * --copy-contents
  121. * copy contents of special files when recursive
  122. * --preserve[=ATTR_LIST]
  123. * preserve attributes (default: mode,ownership,timestamps),
  124. * if possible additional attributes: security context,links,all
  125. * --no-preserve=ATTR_LIST
  126. * --sparse=WHEN
  127. * control creation of sparse files
  128. * --strip-trailing-slashes
  129. * remove any trailing slashes from each SOURCE argument
  130. * -S, --suffix=SUFFIX
  131. * override the usual backup suffix
  132. * -t, --target-directory=DIRECTORY
  133. * copy all SOURCE arguments into DIRECTORY
  134. * -T, --no-target-directory
  135. * treat DEST as a normal file
  136. * -x, --one-file-system
  137. * stay on this file system
  138. * -Z, --context=CONTEXT
  139. * (SELinux) set SELinux security context of copy to CONTEXT
  140. */
  141. argc -= optind;
  142. argv += optind;
  143. /* Reverse this bit. If there is -d, bit is not set: */
  144. flags ^= FILEUTILS_DEREFERENCE;
  145. /* coreutils 6.9 compat:
  146. * by default, "cp" derefs symlinks (creates regular dest files),
  147. * but "cp -R" does not. We switch off deref if -r or -R (see above).
  148. * However, "cp -RL" must still deref symlinks: */
  149. if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
  150. flags |= FILEUTILS_DEREFERENCE;
  151. #if ENABLE_SELINUX
  152. if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
  153. selinux_or_die();
  154. }
  155. #endif
  156. status = EXIT_SUCCESS;
  157. last = argv[argc - 1];
  158. /* If there are only two arguments and... */
  159. if (argc == 2) {
  160. s_flags = cp_mv_stat2(*argv, &source_stat,
  161. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  162. if (s_flags < 0)
  163. return EXIT_FAILURE;
  164. d_flags = cp_mv_stat(last, &dest_stat);
  165. if (d_flags < 0)
  166. return EXIT_FAILURE;
  167. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  168. //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
  169. // flags, FILEUTILS_RMDEST, OPT_parents);
  170. if (flags & OPT_parents) {
  171. if (!(d_flags & 2)) {
  172. bb_error_msg_and_die("with --parents, the destination must be a directory");
  173. }
  174. }
  175. if (flags & FILEUTILS_RMDEST) {
  176. flags |= FILEUTILS_FORCE;
  177. }
  178. #endif
  179. /* ...if neither is a directory... */
  180. if (!((s_flags | d_flags) & 2)
  181. /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
  182. || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
  183. ) {
  184. /* Do a simple copy */
  185. dest = last;
  186. goto DO_COPY; /* NB: argc==2 -> *++argv==last */
  187. }
  188. }
  189. while (1) {
  190. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  191. if (flags & OPT_parents) {
  192. char *dest_dup;
  193. char *dest_dir;
  194. dest = concat_path_file(last, *argv);
  195. dest_dup = xstrdup(dest);
  196. dest_dir = dirname(dest_dup);
  197. if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
  198. return EXIT_FAILURE;
  199. }
  200. free(dest_dup);
  201. goto DO_COPY;
  202. }
  203. #endif
  204. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  205. DO_COPY:
  206. if (copy_file(*argv, dest, flags) < 0) {
  207. status = EXIT_FAILURE;
  208. }
  209. if (*++argv == last) {
  210. /* possibly leaking dest... */
  211. break;
  212. }
  213. /* don't move up: dest may be == last and not malloced! */
  214. free((void*)dest);
  215. }
  216. /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
  217. return status;
  218. }