/bin/mv/mv.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 503 lines · 386 code · 36 blank · 81 comment · 128 complexity · eb2b846d3d755428e6030fefecc9dc53 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1989, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Ken Smith of The State University of New York at Buffalo.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #if 0
  33. #ifndef lint
  34. static char const copyright[] =
  35. "@(#) Copyright (c) 1989, 1993, 1994\n\
  36. The Regents of the University of California. All rights reserved.\n";
  37. #endif /* not lint */
  38. #ifndef lint
  39. static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
  40. #endif /* not lint */
  41. #endif
  42. #include <sys/cdefs.h>
  43. __FBSDID("$FreeBSD$");
  44. #include <sys/types.h>
  45. #include <sys/acl.h>
  46. #include <sys/param.h>
  47. #include <sys/time.h>
  48. #include <sys/wait.h>
  49. #include <sys/stat.h>
  50. #include <sys/mount.h>
  51. #include <err.h>
  52. #include <errno.h>
  53. #include <fcntl.h>
  54. #include <grp.h>
  55. #include <limits.h>
  56. #include <paths.h>
  57. #include <pwd.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <sysexits.h>
  62. #include <unistd.h>
  63. /* Exit code for a failed exec. */
  64. #define EXEC_FAILED 127
  65. static int fflg, hflg, iflg, nflg, vflg;
  66. static int copy(const char *, const char *);
  67. static int do_move(const char *, const char *);
  68. static int fastcopy(const char *, const char *, struct stat *);
  69. static void usage(void);
  70. static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
  71. const char *dest_path);
  72. int
  73. main(int argc, char *argv[])
  74. {
  75. size_t baselen, len;
  76. int rval;
  77. char *p, *endp;
  78. struct stat sb;
  79. int ch;
  80. char path[PATH_MAX];
  81. while ((ch = getopt(argc, argv, "fhinv")) != -1)
  82. switch (ch) {
  83. case 'h':
  84. hflg = 1;
  85. break;
  86. case 'i':
  87. iflg = 1;
  88. fflg = nflg = 0;
  89. break;
  90. case 'f':
  91. fflg = 1;
  92. iflg = nflg = 0;
  93. break;
  94. case 'n':
  95. nflg = 1;
  96. fflg = iflg = 0;
  97. break;
  98. case 'v':
  99. vflg = 1;
  100. break;
  101. default:
  102. usage();
  103. }
  104. argc -= optind;
  105. argv += optind;
  106. if (argc < 2)
  107. usage();
  108. /*
  109. * If the stat on the target fails or the target isn't a directory,
  110. * try the move. More than 2 arguments is an error in this case.
  111. */
  112. if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
  113. if (argc > 2)
  114. usage();
  115. exit(do_move(argv[0], argv[1]));
  116. }
  117. /*
  118. * If -h was specified, treat the target as a symlink instead of
  119. * directory.
  120. */
  121. if (hflg) {
  122. if (argc > 2)
  123. usage();
  124. if (lstat(argv[1], &sb) == 0 && S_ISLNK(sb.st_mode))
  125. exit(do_move(argv[0], argv[1]));
  126. }
  127. /* It's a directory, move each file into it. */
  128. if (strlen(argv[argc - 1]) > sizeof(path) - 1)
  129. errx(1, "%s: destination pathname too long", *argv);
  130. (void)strcpy(path, argv[argc - 1]);
  131. baselen = strlen(path);
  132. endp = &path[baselen];
  133. if (!baselen || *(endp - 1) != '/') {
  134. *endp++ = '/';
  135. ++baselen;
  136. }
  137. for (rval = 0; --argc; ++argv) {
  138. /*
  139. * Find the last component of the source pathname. It
  140. * may have trailing slashes.
  141. */
  142. p = *argv + strlen(*argv);
  143. while (p != *argv && p[-1] == '/')
  144. --p;
  145. while (p != *argv && p[-1] != '/')
  146. --p;
  147. if ((baselen + (len = strlen(p))) >= PATH_MAX) {
  148. warnx("%s: destination pathname too long", *argv);
  149. rval = 1;
  150. } else {
  151. memmove(endp, p, (size_t)len + 1);
  152. if (do_move(*argv, path))
  153. rval = 1;
  154. }
  155. }
  156. exit(rval);
  157. }
  158. static int
  159. do_move(const char *from, const char *to)
  160. {
  161. struct stat sb;
  162. int ask, ch, first;
  163. char modep[15];
  164. /*
  165. * Check access. If interactive and file exists, ask user if it
  166. * should be replaced. Otherwise if file exists but isn't writable
  167. * make sure the user wants to clobber it.
  168. */
  169. if (!fflg && !access(to, F_OK)) {
  170. /* prompt only if source exist */
  171. if (lstat(from, &sb) == -1) {
  172. warn("%s", from);
  173. return (1);
  174. }
  175. #define YESNO "(y/n [n]) "
  176. ask = 0;
  177. if (nflg) {
  178. if (vflg)
  179. printf("%s not overwritten\n", to);
  180. return (0);
  181. } else if (iflg) {
  182. (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
  183. ask = 1;
  184. } else if (access(to, W_OK) && !stat(to, &sb)) {
  185. strmode(sb.st_mode, modep);
  186. (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
  187. modep + 1, modep[9] == ' ' ? "" : " ",
  188. user_from_uid((unsigned long)sb.st_uid, 0),
  189. group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
  190. ask = 1;
  191. }
  192. if (ask) {
  193. first = ch = getchar();
  194. while (ch != '\n' && ch != EOF)
  195. ch = getchar();
  196. if (first != 'y' && first != 'Y') {
  197. (void)fprintf(stderr, "not overwritten\n");
  198. return (0);
  199. }
  200. }
  201. }
  202. /*
  203. * Rename on FreeBSD will fail with EISDIR and ENOTDIR, before failing
  204. * with EXDEV. Therefore, copy() doesn't have to perform the checks
  205. * specified in the Step 3 of the POSIX mv specification.
  206. */
  207. if (!rename(from, to)) {
  208. if (vflg)
  209. printf("%s -> %s\n", from, to);
  210. return (0);
  211. }
  212. if (errno == EXDEV) {
  213. struct statfs sfs;
  214. char path[PATH_MAX];
  215. /*
  216. * If the source is a symbolic link and is on another
  217. * filesystem, it can be recreated at the destination.
  218. */
  219. if (lstat(from, &sb) == -1) {
  220. warn("%s", from);
  221. return (1);
  222. }
  223. if (!S_ISLNK(sb.st_mode)) {
  224. /* Can't mv(1) a mount point. */
  225. if (realpath(from, path) == NULL) {
  226. warn("cannot resolve %s: %s", from, path);
  227. return (1);
  228. }
  229. if (!statfs(path, &sfs) &&
  230. !strcmp(path, sfs.f_mntonname)) {
  231. warnx("cannot rename a mount point");
  232. return (1);
  233. }
  234. }
  235. } else {
  236. warn("rename %s to %s", from, to);
  237. return (1);
  238. }
  239. /*
  240. * If rename fails because we're trying to cross devices, and
  241. * it's a regular file, do the copy internally; otherwise, use
  242. * cp and rm.
  243. */
  244. if (lstat(from, &sb)) {
  245. warn("%s", from);
  246. return (1);
  247. }
  248. return (S_ISREG(sb.st_mode) ?
  249. fastcopy(from, to, &sb) : copy(from, to));
  250. }
  251. static int
  252. fastcopy(const char *from, const char *to, struct stat *sbp)
  253. {
  254. struct timeval tval[2];
  255. static u_int blen = MAXPHYS;
  256. static char *bp = NULL;
  257. mode_t oldmode;
  258. int nread, from_fd, to_fd;
  259. if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
  260. warn("fastcopy: open() failed (from): %s", from);
  261. return (1);
  262. }
  263. if (bp == NULL && (bp = malloc((size_t)blen)) == NULL) {
  264. warnx("malloc(%u) failed", blen);
  265. return (1);
  266. }
  267. while ((to_fd =
  268. open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
  269. if (errno == EEXIST && unlink(to) == 0)
  270. continue;
  271. warn("fastcopy: open() failed (to): %s", to);
  272. (void)close(from_fd);
  273. return (1);
  274. }
  275. while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
  276. if (write(to_fd, bp, (size_t)nread) != nread) {
  277. warn("fastcopy: write() failed: %s", to);
  278. goto err;
  279. }
  280. if (nread < 0) {
  281. warn("fastcopy: read() failed: %s", from);
  282. err: if (unlink(to))
  283. warn("%s: remove", to);
  284. (void)close(from_fd);
  285. (void)close(to_fd);
  286. return (1);
  287. }
  288. oldmode = sbp->st_mode & ALLPERMS;
  289. if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
  290. warn("%s: set owner/group (was: %lu/%lu)", to,
  291. (u_long)sbp->st_uid, (u_long)sbp->st_gid);
  292. if (oldmode & (S_ISUID | S_ISGID)) {
  293. warnx(
  294. "%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
  295. to, oldmode);
  296. sbp->st_mode &= ~(S_ISUID | S_ISGID);
  297. }
  298. }
  299. if (fchmod(to_fd, sbp->st_mode))
  300. warn("%s: set mode (was: 0%03o)", to, oldmode);
  301. /*
  302. * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
  303. * for dest_file, then its ACLs shall reflect the ACLs of the
  304. * source_file.
  305. */
  306. preserve_fd_acls(from_fd, to_fd, from, to);
  307. (void)close(from_fd);
  308. /*
  309. * XXX
  310. * NFS doesn't support chflags; ignore errors unless there's reason
  311. * to believe we're losing bits. (Note, this still won't be right
  312. * if the server supports flags and we were trying to *remove* flags
  313. * on a file that we copied, i.e., that we didn't create.)
  314. */
  315. errno = 0;
  316. if (fchflags(to_fd, (u_long)sbp->st_flags))
  317. if (errno != EOPNOTSUPP || sbp->st_flags != 0)
  318. warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
  319. tval[0].tv_sec = sbp->st_atime;
  320. tval[1].tv_sec = sbp->st_mtime;
  321. tval[0].tv_usec = tval[1].tv_usec = 0;
  322. if (utimes(to, tval))
  323. warn("%s: set times", to);
  324. if (close(to_fd)) {
  325. warn("%s", to);
  326. return (1);
  327. }
  328. if (unlink(from)) {
  329. warn("%s: remove", from);
  330. return (1);
  331. }
  332. if (vflg)
  333. printf("%s -> %s\n", from, to);
  334. return (0);
  335. }
  336. static int
  337. copy(const char *from, const char *to)
  338. {
  339. struct stat sb;
  340. int pid, status;
  341. if (lstat(to, &sb) == 0) {
  342. /* Destination path exists. */
  343. if (S_ISDIR(sb.st_mode)) {
  344. if (rmdir(to) != 0) {
  345. warn("rmdir %s", to);
  346. return (1);
  347. }
  348. } else {
  349. if (unlink(to) != 0) {
  350. warn("unlink %s", to);
  351. return (1);
  352. }
  353. }
  354. } else if (errno != ENOENT) {
  355. warn("%s", to);
  356. return (1);
  357. }
  358. /* Copy source to destination. */
  359. if (!(pid = vfork())) {
  360. execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
  361. (char *)NULL);
  362. _exit(EXEC_FAILED);
  363. }
  364. if (waitpid(pid, &status, 0) == -1) {
  365. warn("%s %s %s: waitpid", _PATH_CP, from, to);
  366. return (1);
  367. }
  368. if (!WIFEXITED(status)) {
  369. warnx("%s %s %s: did not terminate normally",
  370. _PATH_CP, from, to);
  371. return (1);
  372. }
  373. switch (WEXITSTATUS(status)) {
  374. case 0:
  375. break;
  376. case EXEC_FAILED:
  377. warnx("%s %s %s: exec failed", _PATH_CP, from, to);
  378. return (1);
  379. default:
  380. warnx("%s %s %s: terminated with %d (non-zero) status",
  381. _PATH_CP, from, to, WEXITSTATUS(status));
  382. return (1);
  383. }
  384. /* Delete the source. */
  385. if (!(pid = vfork())) {
  386. execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
  387. _exit(EXEC_FAILED);
  388. }
  389. if (waitpid(pid, &status, 0) == -1) {
  390. warn("%s %s: waitpid", _PATH_RM, from);
  391. return (1);
  392. }
  393. if (!WIFEXITED(status)) {
  394. warnx("%s %s: did not terminate normally", _PATH_RM, from);
  395. return (1);
  396. }
  397. switch (WEXITSTATUS(status)) {
  398. case 0:
  399. break;
  400. case EXEC_FAILED:
  401. warnx("%s %s: exec failed", _PATH_RM, from);
  402. return (1);
  403. default:
  404. warnx("%s %s: terminated with %d (non-zero) status",
  405. _PATH_RM, from, WEXITSTATUS(status));
  406. return (1);
  407. }
  408. return (0);
  409. }
  410. static void
  411. preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
  412. const char *dest_path)
  413. {
  414. acl_t acl;
  415. acl_type_t acl_type;
  416. int acl_supported = 0, ret, trivial;
  417. ret = fpathconf(source_fd, _PC_ACL_NFS4);
  418. if (ret > 0 ) {
  419. acl_supported = 1;
  420. acl_type = ACL_TYPE_NFS4;
  421. } else if (ret < 0 && errno != EINVAL) {
  422. warn("fpathconf(..., _PC_ACL_NFS4) failed for %s",
  423. source_path);
  424. return;
  425. }
  426. if (acl_supported == 0) {
  427. ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
  428. if (ret > 0 ) {
  429. acl_supported = 1;
  430. acl_type = ACL_TYPE_ACCESS;
  431. } else if (ret < 0 && errno != EINVAL) {
  432. warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
  433. source_path);
  434. return;
  435. }
  436. }
  437. if (acl_supported == 0)
  438. return;
  439. acl = acl_get_fd_np(source_fd, acl_type);
  440. if (acl == NULL) {
  441. warn("failed to get acl entries for %s", source_path);
  442. return;
  443. }
  444. if (acl_is_trivial_np(acl, &trivial)) {
  445. warn("acl_is_trivial() failed for %s", source_path);
  446. acl_free(acl);
  447. return;
  448. }
  449. if (trivial) {
  450. acl_free(acl);
  451. return;
  452. }
  453. if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
  454. warn("failed to set acl entries for %s", dest_path);
  455. acl_free(acl);
  456. return;
  457. }
  458. acl_free(acl);
  459. }
  460. static void
  461. usage(void)
  462. {
  463. (void)fprintf(stderr, "%s\n%s\n",
  464. "usage: mv [-f | -i | -n] [-hv] source target",
  465. " mv [-f | -i | -n] [-v] source ... directory");
  466. exit(EX_USAGE);
  467. }