1139969Simp/*-
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Ken Smith of The State University of New York at Buffalo.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
33114433Sobrien#if 0
341556Srgrimes#ifndef lint
3520420Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4136049Scharnierstatic char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
42114433Sobrien#endif /* not lint */
4336049Scharnier#endif
4492974Sobrien#include <sys/cdefs.h>
4592974Sobrien__FBSDID("$FreeBSD: releng/11.0/bin/mv/mv.c 300643 2016-05-25 02:51:15Z truckman $");
461556Srgrimes
47149790Scsjp#include <sys/types.h>
48149790Scsjp#include <sys/acl.h>
491556Srgrimes#include <sys/param.h>
501556Srgrimes#include <sys/time.h>
511556Srgrimes#include <sys/wait.h>
521556Srgrimes#include <sys/stat.h>
5331664Seivind#include <sys/mount.h>
541556Srgrimes
551556Srgrimes#include <err.h>
561556Srgrimes#include <errno.h>
571556Srgrimes#include <fcntl.h>
5890644Simp#include <grp.h>
5977409Simp#include <limits.h>
6096806Sjmallett#include <paths.h>
6190644Simp#include <pwd.h>
621556Srgrimes#include <stdio.h>
631556Srgrimes#include <stdlib.h>
641556Srgrimes#include <string.h>
6550544Smharo#include <sysexits.h>
661556Srgrimes#include <unistd.h>
671556Srgrimes
68174935Sdds/* Exit code for a failed exec. */
69174935Sdds#define EXEC_FAILED 127
70174935Sdds
71239951Sjhbstatic int	fflg, hflg, iflg, nflg, vflg;
721556Srgrimes
73180604Sdelphijstatic int	copy(const char *, const char *);
74180604Sdelphijstatic int	do_move(const char *, const char *);
75180604Sdelphijstatic int	fastcopy(const char *, const char *, struct stat *);
76180604Sdelphijstatic void	usage(void);
77196841Straszstatic void	preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
78196841Strasz		    const char *dest_path);
791556Srgrimes
801556Srgrimesint
8190110Simpmain(int argc, char *argv[])
821556Srgrimes{
8391085Smarkm	size_t baselen, len;
8491085Smarkm	int rval;
8590114Simp	char *p, *endp;
861556Srgrimes	struct stat sb;
871556Srgrimes	int ch;
8877409Simp	char path[PATH_MAX];
891556Srgrimes
90239951Sjhb	while ((ch = getopt(argc, argv, "fhinv")) != -1)
911556Srgrimes		switch (ch) {
92239951Sjhb		case 'h':
93239951Sjhb			hflg = 1;
94239951Sjhb			break;
951556Srgrimes		case 'i':
9614154Swosch			iflg = 1;
9792935Sobrien			fflg = nflg = 0;
981556Srgrimes			break;
991556Srgrimes		case 'f':
1001556Srgrimes			fflg = 1;
10192935Sobrien			iflg = nflg = 0;
1021556Srgrimes			break;
10392935Sobrien		case 'n':
10492935Sobrien			nflg = 1;
10592935Sobrien			fflg = iflg = 0;
10692935Sobrien			break;
10750544Smharo		case 'v':
10850544Smharo			vflg = 1;
10950544Smharo			break;
1101556Srgrimes		default:
1111556Srgrimes			usage();
1121556Srgrimes		}
11314305Swosch	argc -= optind;
1141556Srgrimes	argv += optind;
1151556Srgrimes
1161556Srgrimes	if (argc < 2)
1171556Srgrimes		usage();
1181556Srgrimes
1191556Srgrimes	/*
1201556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1211556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1221556Srgrimes	 */
1231556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1241556Srgrimes		if (argc > 2)
125284916Sjilles			errx(1, "%s is not a directory", argv[argc - 1]);
1261556Srgrimes		exit(do_move(argv[0], argv[1]));
1271556Srgrimes	}
1281556Srgrimes
129239951Sjhb	/*
130239951Sjhb	 * If -h was specified, treat the target as a symlink instead of
131239951Sjhb	 * directory.
132239951Sjhb	 */
133239951Sjhb	if (hflg) {
134239951Sjhb		if (argc > 2)
135239951Sjhb			usage();
136239951Sjhb		if (lstat(argv[1], &sb) == 0 && S_ISLNK(sb.st_mode))
137239951Sjhb			exit(do_move(argv[0], argv[1]));
138239951Sjhb	}
139239951Sjhb
1401556Srgrimes	/* It's a directory, move each file into it. */
14136785Simp	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
14236785Simp		errx(1, "%s: destination pathname too long", *argv);
1431556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1441556Srgrimes	baselen = strlen(path);
1451556Srgrimes	endp = &path[baselen];
14636383Ssteve	if (!baselen || *(endp - 1) != '/') {
14736383Ssteve		*endp++ = '/';
14836383Ssteve		++baselen;
14936383Ssteve	}
1501556Srgrimes	for (rval = 0; --argc; ++argv) {
15111298Sbde		/*
15211298Sbde		 * Find the last component of the source pathname.  It
15311298Sbde		 * may have trailing slashes.
15411298Sbde		 */
15511298Sbde		p = *argv + strlen(*argv);
15611298Sbde		while (p != *argv && p[-1] == '/')
15711298Sbde			--p;
15811298Sbde		while (p != *argv && p[-1] != '/')
15911298Sbde			--p;
16011298Sbde
16177409Simp		if ((baselen + (len = strlen(p))) >= PATH_MAX) {
1621556Srgrimes			warnx("%s: destination pathname too long", *argv);
1631556Srgrimes			rval = 1;
1641556Srgrimes		} else {
16576878Skris			memmove(endp, p, (size_t)len + 1);
1661556Srgrimes			if (do_move(*argv, path))
1671556Srgrimes				rval = 1;
1681556Srgrimes		}
1691556Srgrimes	}
1701556Srgrimes	exit(rval);
1711556Srgrimes}
1721556Srgrimes
173180604Sdelphijstatic int
174180604Sdelphijdo_move(const char *from, const char *to)
1751556Srgrimes{
1761556Srgrimes	struct stat sb;
17729933Swosch	int ask, ch, first;
1781556Srgrimes	char modep[15];
1791556Srgrimes
1801556Srgrimes	/*
1811556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1821556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1831556Srgrimes	 * make sure the user wants to clobber it.
1841556Srgrimes	 */
1851556Srgrimes	if (!fflg && !access(to, F_OK)) {
18614166Swosch
18714166Swosch		/* prompt only if source exist */
18814166Swosch	        if (lstat(from, &sb) == -1) {
18914305Swosch			warn("%s", from);
19014305Swosch			return (1);
19114166Swosch		}
19230106Swosch
19330106Swosch#define YESNO "(y/n [n]) "
1941556Srgrimes		ask = 0;
19592935Sobrien		if (nflg) {
19692935Sobrien			if (vflg)
19792935Sobrien				printf("%s not overwritten\n", to);
19892935Sobrien			return (0);
19992935Sobrien		} else if (iflg) {
20030106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
2011556Srgrimes			ask = 1;
202243072Seadler		} else if (access(to, W_OK) && !stat(to, &sb) && isatty(STDIN_FILENO)) {
2031556Srgrimes			strmode(sb.st_mode, modep);
20430106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
2051556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
20676878Skris			    user_from_uid((unsigned long)sb.st_uid, 0),
20776878Skris			    group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
2081556Srgrimes			ask = 1;
2091556Srgrimes		}
2101556Srgrimes		if (ask) {
21129933Swosch			first = ch = getchar();
21229933Swosch			while (ch != '\n' && ch != EOF)
21329933Swosch				ch = getchar();
21430106Swosch			if (first != 'y' && first != 'Y') {
21530106Swosch				(void)fprintf(stderr, "not overwritten\n");
2161556Srgrimes				return (0);
21730106Swosch			}
2181556Srgrimes		}
2191556Srgrimes	}
220174935Sdds	/*
221174935Sdds	 * Rename on FreeBSD will fail with EISDIR and ENOTDIR, before failing
222174935Sdds	 * with EXDEV.  Therefore, copy() doesn't have to perform the checks
223174935Sdds	 * specified in the Step 3 of the POSIX mv specification.
224174935Sdds	 */
22550544Smharo	if (!rename(from, to)) {
22650544Smharo		if (vflg)
22750544Smharo			printf("%s -> %s\n", from, to);
2281556Srgrimes		return (0);
22950544Smharo	}
2301556Srgrimes
23131664Seivind	if (errno == EXDEV) {
23231664Seivind		struct statfs sfs;
23377409Simp		char path[PATH_MAX];
23431664Seivind
235127272Spjd		/*
236127272Spjd		 * If the source is a symbolic link and is on another
237127272Spjd		 * filesystem, it can be recreated at the destination.
238127272Spjd		 */
239127272Spjd		if (lstat(from, &sb) == -1) {
240127272Spjd			warn("%s", from);
24131664Seivind			return (1);
24231664Seivind		}
243127272Spjd		if (!S_ISLNK(sb.st_mode)) {
244127272Spjd			/* Can't mv(1) a mount point. */
245127272Spjd			if (realpath(from, path) == NULL) {
246174935Sdds				warn("cannot resolve %s: %s", from, path);
247127272Spjd				return (1);
248127272Spjd			}
249127272Spjd			if (!statfs(path, &sfs) &&
250127272Spjd			    !strcmp(path, sfs.f_mntonname)) {
251127272Spjd				warnx("cannot rename a mount point");
252127272Spjd				return (1);
253127272Spjd			}
25431664Seivind		}
25531664Seivind	} else {
2561556Srgrimes		warn("rename %s to %s", from, to);
2571556Srgrimes		return (1);
2581556Srgrimes	}
2591556Srgrimes
2601556Srgrimes	/*
2611556Srgrimes	 * If rename fails because we're trying to cross devices, and
2621556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
2631556Srgrimes	 * cp and rm.
2641556Srgrimes	 */
26562963Sdwmalone	if (lstat(from, &sb)) {
2661556Srgrimes		warn("%s", from);
2671556Srgrimes		return (1);
2681556Srgrimes	}
2691556Srgrimes	return (S_ISREG(sb.st_mode) ?
2701556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2711556Srgrimes}
2721556Srgrimes
273180604Sdelphijstatic int
274180604Sdelphijfastcopy(const char *from, const char *to, struct stat *sbp)
2751556Srgrimes{
276277645Sjilles	struct timespec ts[2];
277225954Sivoras	static u_int blen = MAXPHYS;
278225954Sivoras	static char *bp = NULL;
27923525Sguido	mode_t oldmode;
28090114Simp	int nread, from_fd, to_fd;
281268129Sdelphij	struct stat tsb;
2821556Srgrimes
2831556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
284225954Sivoras		warn("fastcopy: open() failed (from): %s", from);
2851556Srgrimes		return (1);
2861556Srgrimes	}
287225954Sivoras	if (bp == NULL && (bp = malloc((size_t)blen)) == NULL) {
288225954Sivoras		warnx("malloc(%u) failed", blen);
289300643Struckman		(void)close(from_fd);
290225954Sivoras		return (1);
29123525Sguido	}
29223525Sguido	while ((to_fd =
29323525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
29423525Sguido		if (errno == EEXIST && unlink(to) == 0)
29523525Sguido			continue;
296225954Sivoras		warn("fastcopy: open() failed (to): %s", to);
2971556Srgrimes		(void)close(from_fd);
2981556Srgrimes		return (1);
2991556Srgrimes	}
30076878Skris	while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
30176878Skris		if (write(to_fd, bp, (size_t)nread) != nread) {
302225954Sivoras			warn("fastcopy: write() failed: %s", to);
3031556Srgrimes			goto err;
3041556Srgrimes		}
3051556Srgrimes	if (nread < 0) {
306225954Sivoras		warn("fastcopy: read() failed: %s", from);
3071556Srgrimeserr:		if (unlink(to))
3081556Srgrimes			warn("%s: remove", to);
3091556Srgrimes		(void)close(from_fd);
3101556Srgrimes		(void)close(to_fd);
3111556Srgrimes		return (1);
3121556Srgrimes	}
3131556Srgrimes
31423525Sguido	oldmode = sbp->st_mode & ALLPERMS;
31523525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
31637245Sbde		warn("%s: set owner/group (was: %lu/%lu)", to,
31737245Sbde		    (u_long)sbp->st_uid, (u_long)sbp->st_gid);
31823525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
31923525Sguido			warnx(
32023525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
32123525Sguido			    to, oldmode);
32223525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
32323525Sguido		}
32423525Sguido	}
325196841Strasz	if (fchmod(to_fd, sbp->st_mode))
326196841Strasz		warn("%s: set mode (was: 0%03o)", to, oldmode);
327149790Scsjp	/*
328149790Scsjp	 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
329174935Sdds	 * for dest_file, then its ACLs shall reflect the ACLs of the
330149790Scsjp	 * source_file.
331149790Scsjp	 */
332196841Strasz	preserve_fd_acls(from_fd, to_fd, from, to);
333149790Scsjp	(void)close(from_fd);
33463680Ssada	/*
33563680Ssada	 * XXX
33663680Ssada	 * NFS doesn't support chflags; ignore errors unless there's reason
33763680Ssada	 * to believe we're losing bits.  (Note, this still won't be right
33863680Ssada	 * if the server supports flags and we were trying to *remove* flags
33963680Ssada	 * on a file that we copied, i.e., that we didn't create.)
34063680Ssada	 */
341268129Sdelphij	if (fstat(to_fd, &tsb) == 0) {
342268129Sdelphij		if ((sbp->st_flags  & ~UF_ARCHIVE) !=
343268129Sdelphij		    (tsb.st_flags & ~UF_ARCHIVE)) {
344268129Sdelphij			if (fchflags(to_fd,
345268129Sdelphij			    sbp->st_flags | (tsb.st_flags & UF_ARCHIVE)))
346268129Sdelphij				if (errno != EOPNOTSUPP ||
347268129Sdelphij				    ((sbp->st_flags & ~UF_ARCHIVE) != 0))
348268129Sdelphij					warn("%s: set flags (was: 0%07o)",
349268129Sdelphij					    to, sbp->st_flags);
350268129Sdelphij		}
351268129Sdelphij	} else
352268129Sdelphij		warn("%s: cannot stat", to);
3531556Srgrimes
354277645Sjilles	ts[0] = sbp->st_atim;
355277645Sjilles	ts[1] = sbp->st_mtim;
356280386Sjilles	if (futimens(to_fd, ts))
3571556Srgrimes		warn("%s: set times", to);
3581556Srgrimes
3591556Srgrimes	if (close(to_fd)) {
3601556Srgrimes		warn("%s", to);
3611556Srgrimes		return (1);
3621556Srgrimes	}
3631556Srgrimes
3641556Srgrimes	if (unlink(from)) {
3651556Srgrimes		warn("%s: remove", from);
3661556Srgrimes		return (1);
3671556Srgrimes	}
36850544Smharo	if (vflg)
36950544Smharo		printf("%s -> %s\n", from, to);
3701556Srgrimes	return (0);
3711556Srgrimes}
3721556Srgrimes
373180604Sdelphijstatic int
374180604Sdelphijcopy(const char *from, const char *to)
3751556Srgrimes{
376174664Sdds	struct stat sb;
377174667Sdds	int pid, status;
3781556Srgrimes
379174935Sdds	if (lstat(to, &sb) == 0) {
380174935Sdds		/* Destination path exists. */
381174935Sdds		if (S_ISDIR(sb.st_mode)) {
382174935Sdds			if (rmdir(to) != 0) {
383174935Sdds				warn("rmdir %s", to);
384174935Sdds				return (1);
385174935Sdds			}
386174935Sdds		} else {
387174935Sdds			if (unlink(to) != 0) {
388174935Sdds				warn("unlink %s", to);
389174935Sdds				return (1);
390174935Sdds			}
391174664Sdds		}
392174935Sdds	} else if (errno != ENOENT) {
393174935Sdds		warn("%s", to);
394174935Sdds		return (1);
395174664Sdds	}
396174935Sdds
397174664Sdds	/* Copy source to destination. */
398174935Sdds	if (!(pid = vfork())) {
39998280Stjr		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
40079452Sbrian		    (char *)NULL);
401174935Sdds		_exit(EXEC_FAILED);
4021556Srgrimes	}
4031556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
404174935Sdds		warn("%s %s %s: waitpid", _PATH_CP, from, to);
405174935Sdds		return (1);
4061556Srgrimes	}
4071556Srgrimes	if (!WIFEXITED(status)) {
408174935Sdds		warnx("%s %s %s: did not terminate normally",
409174935Sdds		    _PATH_CP, from, to);
410174935Sdds		return (1);
4111556Srgrimes	}
412174935Sdds	switch (WEXITSTATUS(status)) {
413174935Sdds	case 0:
414174935Sdds		break;
415174935Sdds	case EXEC_FAILED:
416174935Sdds		warnx("%s %s %s: exec failed", _PATH_CP, from, to);
417174935Sdds		return (1);
418174935Sdds	default:
419174935Sdds		warnx("%s %s %s: terminated with %d (non-zero) status",
420174935Sdds		    _PATH_CP, from, to, WEXITSTATUS(status));
421174935Sdds		return (1);
4221556Srgrimes	}
423174935Sdds
424174935Sdds	/* Delete the source. */
425174935Sdds	if (!(pid = vfork())) {
426174935Sdds		execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
427174935Sdds		_exit(EXEC_FAILED);
4281556Srgrimes	}
429174935Sdds	if (waitpid(pid, &status, 0) == -1) {
430174935Sdds		warn("%s %s: waitpid", _PATH_RM, from);
431174935Sdds		return (1);
432174935Sdds	}
433174935Sdds	if (!WIFEXITED(status)) {
434174935Sdds		warnx("%s %s: did not terminate normally", _PATH_RM, from);
435174935Sdds		return (1);
436174935Sdds	}
437174935Sdds	switch (WEXITSTATUS(status)) {
438174935Sdds	case 0:
439174935Sdds		break;
440174935Sdds	case EXEC_FAILED:
441174935Sdds		warnx("%s %s: exec failed", _PATH_RM, from);
442174935Sdds		return (1);
443174935Sdds	default:
444174935Sdds		warnx("%s %s: terminated with %d (non-zero) status",
445174935Sdds		    _PATH_RM, from, WEXITSTATUS(status));
446174935Sdds		return (1);
447174935Sdds	}
448174935Sdds	return (0);
4491556Srgrimes}
4501556Srgrimes
451180604Sdelphijstatic void
452196841Straszpreserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
453196841Strasz    const char *dest_path)
454196841Strasz{
455196841Strasz	acl_t acl;
456196841Strasz	acl_type_t acl_type;
457196841Strasz	int acl_supported = 0, ret, trivial;
458196841Strasz
459196841Strasz	ret = fpathconf(source_fd, _PC_ACL_NFS4);
460196841Strasz	if (ret > 0 ) {
461196841Strasz		acl_supported = 1;
462196841Strasz		acl_type = ACL_TYPE_NFS4;
463196841Strasz	} else if (ret < 0 && errno != EINVAL) {
464196841Strasz		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s",
465196841Strasz		    source_path);
466196841Strasz		return;
467196841Strasz	}
468196841Strasz	if (acl_supported == 0) {
469196841Strasz		ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
470196841Strasz		if (ret > 0 ) {
471196841Strasz			acl_supported = 1;
472196841Strasz			acl_type = ACL_TYPE_ACCESS;
473196841Strasz		} else if (ret < 0 && errno != EINVAL) {
474196841Strasz			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
475196841Strasz			    source_path);
476196841Strasz			return;
477196841Strasz		}
478196841Strasz	}
479196841Strasz	if (acl_supported == 0)
480196841Strasz		return;
481196841Strasz
482196841Strasz	acl = acl_get_fd_np(source_fd, acl_type);
483196841Strasz	if (acl == NULL) {
484196841Strasz		warn("failed to get acl entries for %s", source_path);
485196841Strasz		return;
486196841Strasz	}
487196841Strasz	if (acl_is_trivial_np(acl, &trivial)) {
488196841Strasz		warn("acl_is_trivial() failed for %s", source_path);
489196841Strasz		acl_free(acl);
490196841Strasz		return;
491196841Strasz	}
492196841Strasz	if (trivial) {
493196841Strasz		acl_free(acl);
494196841Strasz		return;
495196841Strasz	}
496196841Strasz	if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
497196841Strasz		warn("failed to set acl entries for %s", dest_path);
498196841Strasz		acl_free(acl);
499196841Strasz		return;
500196841Strasz	}
501196841Strasz	acl_free(acl);
502196841Strasz}
503196841Strasz
504196841Straszstatic void
50590110Simpusage(void)
5061556Srgrimes{
50750544Smharo
50814305Swosch	(void)fprintf(stderr, "%s\n%s\n",
509239951Sjhb		      "usage: mv [-f | -i | -n] [-hv] source target",
51099678Sjohan		      "       mv [-f | -i | -n] [-v] source ... directory");
51150544Smharo	exit(EX_USAGE);
5121556Srgrimes}
513