cp.c 5.5 KB

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