cp.c 7.2 KB

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