[Midnightbsd-cvs] src [8178] trunk/bin/mv: Add a -h flag similar to the -h flag for ln to force mv(1) to treat a

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Fri Sep 16 22:48:38 EDT 2016


Revision: 8178
          http://svnweb.midnightbsd.org/src/?rev=8178
Author:   laffer1
Date:     2016-09-16 22:48:38 -0400 (Fri, 16 Sep 2016)
Log Message:
-----------
Add a -h flag similar to the -h flag for ln to force mv(1) to treat a
symbolic link to a directory for the target as a symbolic link instead of
a directory.  This makes it possible to atomically update a symbolic
link using rename().

Modified Paths:
--------------
    trunk/bin/mv/mv.1
    trunk/bin/mv/mv.c

Modified: trunk/bin/mv/mv.1
===================================================================
--- trunk/bin/mv/mv.1	2016-09-17 02:47:46 UTC (rev 8177)
+++ trunk/bin/mv/mv.1	2016-09-17 02:48:38 UTC (rev 8178)
@@ -33,7 +33,7 @@
 .\" $FreeBSD: src/bin/mv/mv.1,v 1.28 2005/01/16 16:41:58 ru Exp $
 .\" $MidnightBSD: src/bin/mv/mv.1,v 1.3 2008/06/30 02:36:17 laffer1 Exp $
 .\"
-.Dd May 12, 2007
+.Dd August 28, 2012
 .Dt MV 1
 .Os
 .Sh NAME
@@ -42,7 +42,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl f | i | n
-.Op Fl v
+.Op Fl hv
 .Ar source target
 .Nm
 .Op Fl f | i | n
@@ -82,6 +82,21 @@
 or
 .Fl n
 options.)
+.It Fl h
+If the
+.Ar target
+operand is a symbolic link to a directory,
+do not follow it.
+This causes the
+.Nm
+utility to rename the file
+.Ar source
+to the destination path
+.Ar target
+rather than moving
+.Ar source
+into the directory referenced by
+.Ar target .
 .It Fl i
 Cause
 .Nm
@@ -143,7 +158,8 @@
 .Ex -std
 .Sh COMPATIBILITY
 The
-.Fl n
+.Fl h ,
+.Fl n ,
 and
 .Fl v
 options are non-standard and their use in scripts is not recommended.

Modified: trunk/bin/mv/mv.c
===================================================================
--- trunk/bin/mv/mv.c	2016-09-17 02:47:46 UTC (rev 8177)
+++ trunk/bin/mv/mv.c	2016-09-17 02:48:38 UTC (rev 8178)
@@ -43,7 +43,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/bin/mv/mv.c,v 1.3 2006/11/30 04:09:58 laffer1 Exp $");
+__MBSDID("$MidnightBSD$");
 
 #include <sys/types.h>
 #include <sys/acl.h>
@@ -69,7 +69,7 @@
 /* Exit code for a failed exec. */
 #define EXEC_FAILED 127
 
-int fflg, iflg, nflg, vflg;
+static int	fflg, hflg, iflg, nflg, vflg;
 
 static int	copy(const char *, const char *);
 static int	do_move(const char *, const char *);
@@ -88,8 +88,11 @@
 	int ch;
 	char path[PATH_MAX];
 
-	while ((ch = getopt(argc, argv, "finv")) != -1)
+	while ((ch = getopt(argc, argv, "fhinv")) != -1)
 		switch (ch) {
+		case 'h':
+			hflg = 1;
+			break;
 		case 'i':
 			iflg = 1;
 			fflg = nflg = 0;
@@ -124,6 +127,17 @@
 		exit(do_move(argv[0], argv[1]));
 	}
 
+	/*
+	 * If -h was specified, treat the target as a symlink instead of
+	 * directory.
+	 */
+	if (hflg) {
+		if (argc > 2)
+			usage();
+		if (lstat(argv[1], &sb) == 0 && S_ISLNK(sb.st_mode))
+			exit(do_move(argv[0], argv[1]));
+	}
+
 	/* It's a directory, move each file into it. */
 	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
 		errx(1, "%s: destination pathname too long", *argv);
@@ -490,7 +504,7 @@
 {
 
 	(void)fprintf(stderr, "%s\n%s\n",
-		      "usage: mv [-f | -i | -n] [-v] source target",
+		      "usage: mv [-f | -i | -n] [-hv] source target",
 		      "       mv [-f | -i | -n] [-v] source ... directory");
 	exit(EX_USAGE);
 }



More information about the Midnightbsd-cvs mailing list