cp.c 6.1 KB

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