3
0

cp.c 7.3 KB

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