cp.c 6.3 KB

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