copy_file.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini copy_file implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. *
  9. */
  10. #include "libbb.h"
  11. static int retry_overwrite(const char *dest, int flags)
  12. {
  13. if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
  14. fprintf(stderr, "'%s' exists\n", dest);
  15. return -1;
  16. }
  17. if (flags & FILEUTILS_INTERACTIVE) {
  18. fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
  19. if (!bb_ask_confirmation())
  20. return 0; // not allowed to overwrite
  21. }
  22. if (unlink(dest) < 0) {
  23. bb_perror_msg("cannot remove '%s'", dest);
  24. return -1; // error
  25. }
  26. return 1; // ok (to try again)
  27. }
  28. int copy_file(const char *source, const char *dest, int flags)
  29. {
  30. struct stat source_stat;
  31. struct stat dest_stat;
  32. int status = 0;
  33. signed char dest_exists = 0;
  34. signed char ovr;
  35. #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
  36. if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
  37. // This may be a dangling symlink.
  38. // Making [sym]links to dangling symlinks works, so...
  39. if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
  40. goto make_links;
  41. bb_perror_msg("cannot stat '%s'", source);
  42. return -1;
  43. }
  44. if (lstat(dest, &dest_stat) < 0) {
  45. if (errno != ENOENT) {
  46. bb_perror_msg("cannot stat '%s'", dest);
  47. return -1;
  48. }
  49. } else {
  50. if (source_stat.st_dev == dest_stat.st_dev
  51. && source_stat.st_ino == dest_stat.st_ino
  52. ) {
  53. bb_error_msg("'%s' and '%s' are the same file", source, dest);
  54. return -1;
  55. }
  56. dest_exists = 1;
  57. }
  58. if (S_ISDIR(source_stat.st_mode)) {
  59. DIR *dp;
  60. struct dirent *d;
  61. mode_t saved_umask = 0;
  62. if (!(flags & FILEUTILS_RECUR)) {
  63. bb_error_msg("omitting directory '%s'", source);
  64. return -1;
  65. }
  66. /* Create DEST. */
  67. if (dest_exists) {
  68. if (!S_ISDIR(dest_stat.st_mode)) {
  69. bb_error_msg("target '%s' is not a directory", dest);
  70. return -1;
  71. }
  72. } else {
  73. mode_t mode;
  74. saved_umask = umask(0);
  75. mode = source_stat.st_mode;
  76. if (!(flags & FILEUTILS_PRESERVE_STATUS))
  77. mode = source_stat.st_mode & ~saved_umask;
  78. mode |= S_IRWXU;
  79. if (mkdir(dest, mode) < 0) {
  80. umask(saved_umask);
  81. bb_perror_msg("cannot create directory '%s'", dest);
  82. return -1;
  83. }
  84. umask(saved_umask);
  85. }
  86. /* Recursively copy files in SOURCE. */
  87. dp = opendir(source);
  88. if (dp == NULL) {
  89. status = -1;
  90. goto preserve_status;
  91. }
  92. while ((d = readdir(dp)) != NULL) {
  93. char *new_source, *new_dest;
  94. new_source = concat_subpath_file(source, d->d_name);
  95. if (new_source == NULL)
  96. continue;
  97. new_dest = concat_path_file(dest, d->d_name);
  98. if (copy_file(new_source, new_dest, flags) < 0)
  99. status = -1;
  100. free(new_source);
  101. free(new_dest);
  102. }
  103. closedir(dp);
  104. if (!dest_exists
  105. && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
  106. ) {
  107. bb_perror_msg("cannot change permissions of '%s'", dest);
  108. status = -1;
  109. }
  110. } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
  111. int (*lf)(const char *oldpath, const char *newpath);
  112. make_links:
  113. // Hmm... maybe
  114. // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
  115. // (but realpath returns NULL on dangling symlinks...)
  116. lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
  117. if (lf(source, dest) < 0) {
  118. ovr = retry_overwrite(dest, flags);
  119. if (ovr <= 0)
  120. return ovr;
  121. if (lf(source, dest) < 0) {
  122. bb_perror_msg("cannot create link '%s'", dest);
  123. return -1;
  124. }
  125. }
  126. return 0;
  127. } else if (S_ISREG(source_stat.st_mode)
  128. // Huh? DEREF uses stat, which never returns links IIRC...
  129. || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode))
  130. ) {
  131. int src_fd;
  132. int dst_fd;
  133. if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
  134. char *link_name;
  135. if (!FLAGS_DEREF
  136. && is_in_ino_dev_hashtable(&source_stat, &link_name)
  137. ) {
  138. if (link(link_name, dest) < 0) {
  139. ovr = retry_overwrite(dest, flags);
  140. if (ovr <= 0)
  141. return ovr;
  142. if (link(link_name, dest) < 0) {
  143. bb_perror_msg("cannot create link '%s'", dest);
  144. return -1;
  145. }
  146. }
  147. return 0;
  148. }
  149. // TODO: probably is_in_.. and add_to_...
  150. // can be combined: find_or_add_...
  151. add_to_ino_dev_hashtable(&source_stat, dest);
  152. }
  153. src_fd = open(source, O_RDONLY);
  154. if (src_fd == -1) {
  155. bb_perror_msg("cannot open '%s'", source);
  156. return -1;
  157. }
  158. // POSIX: if exists and -i, ask (w/o -i assume yes).
  159. // Then open w/o EXCL.
  160. // If open still fails and -f, try unlink, then try open again.
  161. // Result: a mess:
  162. // If dest is a softlink, we overwrite softlink's destination!
  163. // (or fail, if it points to dir/nonexistent location/etc).
  164. // This is strange, but POSIX-correct.
  165. // coreutils cp has --remove-destination to override this...
  166. dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
  167. ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL
  168. : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
  169. if (dst_fd == -1) {
  170. // We would not do POSIX insanity. -i asks,
  171. // then _unlinks_ the offender. Presto.
  172. // Or else we will end up having 3 open()s!
  173. ovr = retry_overwrite(dest, flags);
  174. if (ovr <= 0) {
  175. close(src_fd);
  176. return ovr;
  177. }
  178. dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
  179. if (dst_fd == -1) {
  180. bb_perror_msg("cannot open '%s'", dest);
  181. close(src_fd);
  182. return -1;
  183. }
  184. }
  185. if (bb_copyfd_eof(src_fd, dst_fd) == -1)
  186. status = -1;
  187. if (close(dst_fd) < 0) {
  188. bb_perror_msg("cannot close '%s'", dest);
  189. status = -1;
  190. }
  191. if (close(src_fd) < 0) {
  192. bb_perror_msg("cannot close '%s'", source);
  193. status = -1;
  194. }
  195. } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
  196. || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
  197. || S_ISLNK(source_stat.st_mode)
  198. ) {
  199. // We are lazy here, a bit lax with races...
  200. if (dest_exists) {
  201. ovr = retry_overwrite(dest, flags);
  202. if (ovr <= 0)
  203. return ovr;
  204. }
  205. if (S_ISFIFO(source_stat.st_mode)) {
  206. if (mkfifo(dest, source_stat.st_mode) < 0) {
  207. bb_perror_msg("cannot create fifo '%s'", dest);
  208. return -1;
  209. }
  210. } else if (S_ISLNK(source_stat.st_mode)) {
  211. char *lpath;
  212. lpath = xreadlink(source);
  213. if (symlink(lpath, dest) < 0) {
  214. bb_perror_msg("cannot create symlink '%s'", dest);
  215. free(lpath);
  216. return -1;
  217. }
  218. free(lpath);
  219. if (flags & FILEUTILS_PRESERVE_STATUS)
  220. if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
  221. bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
  222. return 0;
  223. } else {
  224. if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
  225. bb_perror_msg("cannot create '%s'", dest);
  226. return -1;
  227. }
  228. }
  229. } else {
  230. bb_error_msg("internal error: unrecognized file type");
  231. return -1;
  232. }
  233. preserve_status:
  234. if (flags & FILEUTILS_PRESERVE_STATUS
  235. /* Cannot happen: */
  236. /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
  237. ) {
  238. struct utimbuf times;
  239. times.actime = source_stat.st_atime;
  240. times.modtime = source_stat.st_mtime;
  241. if (utime(dest, &times) < 0)
  242. bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
  243. if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
  244. source_stat.st_mode &= ~(S_ISUID | S_ISGID);
  245. bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
  246. }
  247. if (chmod(dest, source_stat.st_mode) < 0)
  248. bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
  249. }
  250. return status;
  251. }