3
0

cp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // -v (--verbose) is ignored
  76. flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
  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. * --parents
  92. * use full source file name under DIRECTORY
  93. * NOT SUPPORTED IN BBOX:
  94. * --backup[=CONTROL]
  95. * make a backup of each existing destination file
  96. * -b like --backup but does not accept an argument
  97. * --copy-contents
  98. * copy contents of special files when recursive
  99. * --preserve[=ATTR_LIST]
  100. * preserve attributes (default: mode,ownership,timestamps),
  101. * if possible additional attributes: security context,links,all
  102. * --no-preserve=ATTR_LIST
  103. * --remove-destination
  104. * remove each existing destination file before attempting to open
  105. * --sparse=WHEN
  106. * control creation of sparse files
  107. * --strip-trailing-slashes
  108. * remove any trailing slashes from each SOURCE argument
  109. * -S, --suffix=SUFFIX
  110. * override the usual backup suffix
  111. * -t, --target-directory=DIRECTORY
  112. * copy all SOURCE arguments into DIRECTORY
  113. * -T, --no-target-directory
  114. * treat DEST as a normal file
  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. * -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. if (flags & OPT_parents) {
  151. if (!(d_flags & 2)) {
  152. bb_error_msg_and_die("with --parents, the destination must be a directory");
  153. }
  154. }
  155. #endif
  156. /* ...if neither is a directory... */
  157. if (!((s_flags | d_flags) & 2)
  158. /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
  159. || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
  160. ) {
  161. /* Do a simple copy */
  162. dest = last;
  163. goto DO_COPY; /* NB: argc==2 -> *++argv==last */
  164. }
  165. }
  166. while (1) {
  167. #if ENABLE_FEATURE_CP_LONG_OPTIONS
  168. if (flags & OPT_parents) {
  169. char *dest_dup;
  170. char *dest_dir;
  171. dest = concat_path_file(last, *argv);
  172. dest_dup = xstrdup(dest);
  173. dest_dir = dirname(dest_dup);
  174. if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
  175. return EXIT_FAILURE;
  176. }
  177. free(dest_dup);
  178. goto DO_COPY;
  179. }
  180. #endif
  181. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  182. DO_COPY:
  183. if (copy_file(*argv, dest, flags) < 0) {
  184. status = EXIT_FAILURE;
  185. }
  186. if (*++argv == last) {
  187. /* possibly leaking dest... */
  188. break;
  189. }
  190. /* don't move up: dest may be == last and not malloced! */
  191. free((void*)dest);
  192. }
  193. /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
  194. return status;
  195. }