From sam@beeblebrox.rfc1149.net  Thu Mar 18 10:54:00 2004
Return-Path: <sam@beeblebrox.rfc1149.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 4063F16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 18 Mar 2004 10:54:00 -0800 (PST)
Received: from mail.rfc1149.net (marvin.enst.fr [137.194.161.2])
	by mx1.FreeBSD.org (Postfix) with ESMTP id B529243D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 18 Mar 2004 10:53:59 -0800 (PST)
	(envelope-from sam@beeblebrox.rfc1149.net)
Received: from beeblebrox.rfc1149.net (beeblebrox-tun.enst.fr [137.194.161.40])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client CN "beeblebrox.rfc1149.net", Issuer "Top-level signer" (verified OK))
	by mail.rfc1149.net (Postfix) with ESMTP id 5888CA8010
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 18 Mar 2004 19:53:58 +0100 (CET)
Received: by beeblebrox.rfc1149.net (Postfix, from userid 1000)
	id 554EB2F1; Thu, 18 Mar 2004 19:53:55 +0100 (CET)
Message-Id: <20040318185355.554EB2F1@beeblebrox.rfc1149.net>
Date: Thu, 18 Mar 2004 19:53:55 +0100 (CET)
From: Samuel Tardieu <sam@rfc1149.net>
Reply-To: Samuel Tardieu <sam@rfc1149.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Bug in mv(1)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         64430
>Category:       bin
>Synopsis:       Bug in mv(1)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    pjd
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Mar 18 11:00:28 PST 2004
>Closed-Date:    Sun Mar 21 05:47:51 PST 2004
>Last-Modified:  Sun Mar 21 08:40:02 PST 2004
>Originator:     Samuel Tardieu
>Release:        FreeBSD 4.9-STABLE i386
>Organization:
Avian Carrier & Friends
>Environment:
System: FreeBSD beeblebrox 4.9-STABLE FreeBSD 4.9-STABLE #6: Mon Mar 15 21:54:41 CET 2004 root@willow:/usr/obj/usr/src/sys/BEEBLEBROX i386


>Description:
	In some cases, mv is unable to move files. Four conditions must
	be met to have it fail as far as I can see:
	  1) The source must be a symlink
	  2) The target must be a symlink
	  3) The target must point onto a mounting point
	  4) The source and target must not be on the same filesystem

	In this case, mv will issue an inappropriate "cannot rename a
	mounting point" error.

>How-To-Repeat:
	If /tmp is a mounting point and you are not in the /tmp filesystem,
	the following sequence reproduces the bug:
	  % ln -s /tmp bbb1
	  % ln -s /tmp bbb2
	  % mv bbb1 bbb2
	  mv: cannot rename a mount point

	The expected behaviour would be to move the bbb1 symlink into /tmp,
	target of the bbb2 symlink, as is done for any other kind of source
	or destination.

>Fix:
The following patch fixes that.

--- mv-old/mv.c	2004-03-18 19:44:53.000000000 +0100
+++ mv-new/mv.c	2004-03-18 19:44:53.000000000 +0100
@@ -212,6 +212,30 @@
 		struct statfs sfs;
 		char path[PATH_MAX];
 
+		/* If the source is a symbolic link and is on another
+		 * filesystem, it can be recreated at the destination.
+		 */
+		if (lstat(from, &sb) == -1) {
+		  warn("%s",from);
+		  return (1);
+		}
+		if (S_ISLNK(sb.st_mode)) {
+		  bzero(path, PATH_MAX);
+		  if (readlink(from, path, PATH_MAX) == -1) {
+		    warn("%s", from);
+		    return (1);
+		  }
+		  if (symlink(path,to)==-1) {
+		    warn("%s", to);
+		    return (1);
+		  }
+		  if (unlink(from) == -1) {
+		    warn("%s", from);
+		    return (1);
+		  }
+		  return (0);
+		}
+
 		/* Can't mv(1) a mount point. */
 		if (realpath(from, path) == NULL) {
 			warnx("cannot resolve %s: %s", from, path);
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->pjd 
Responsible-Changed-By: pjd 
Responsible-Changed-When: Sun Mar 21 04:58:47 PST 2004 
Responsible-Changed-Why:  
I'll take this one. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=64430 
State-Changed-From-To: open->closed 
State-Changed-By: pjd 
State-Changed-When: Sun Mar 21 05:43:22 PST 2004 
State-Changed-Why:  
Fix commited to HEAD, will be MFCed after 3 days. 
I haven't used this patch, because it was incorrect in few 
places. Anyway, thank you for your report! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=64430 

From: Pawel Jakub Dawidek <pjd@FreeBSD.org>
To: Samuel Tardieu <sam@rfc1149.net>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/64430: Bug in mv(1)
Date: Sun, 21 Mar 2004 15:04:14 +0100

 --QxSStYAgvEtE+iQJ
 Content-Type: text/plain; charset=iso-8859-2
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 On Thu, Mar 18, 2004 at 07:53:55PM +0100, Samuel Tardieu wrote:
 +> >Description:
 +> 	In some cases, mv is unable to move files. Four conditions must
 +> 	be met to have it fail as far as I can see:
 +> 	  1) The source must be a symlink
 +> 	  2) The target must be a symlink
 +> 	  3) The target must point onto a mounting point
 +> 	  4) The source and target must not be on the same filesystem
 +>=20
 +> 	In this case, mv will issue an inappropriate "cannot rename a
 +> 	mounting point" error.
 
 Thanks. I commited a fix to -CURRENT, but haven't used your patch.
 The problems with your patch are:
 1. You should give "PATH_MAX - 1" to readlink() as it doesn't append
    NUL character.
 2. When symlink() succeed and unlink() failed you aren't removing created
    symlink.
 3. When target already exists, you don't remove it first, but you should.
 4. You're not bothering about setting correct permissions and owner, etc.
 
 My patch is much simpler, it just use fastcopy() function when rename(2)
 fails with EXDEV and 'from' is a symblic link.
 
 http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/mv/mv.c.diff?r1=3D1.41&r2=3D1=
 .42
 
 --=20
 Pawel Jakub Dawidek                       http://www.FreeBSD.org
 pjd@FreeBSD.org                           http://garage.freebsd.pl
 FreeBSD committer                         Am I Evil? Yes, I Am!
 
 --QxSStYAgvEtE+iQJ
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.4 (FreeBSD)
 
 iD8DBQFAXaDeForvXbEpPzQRAuz4AKCIoyzMmYJrRcmzIpH/7Xpo+q7jzwCffu/Y
 poasjQ9IYJ9fSepUgbhZyNc=
 =Aw9Y
 -----END PGP SIGNATURE-----
 
 --QxSStYAgvEtE+iQJ--

From: Samuel Tardieu <sam@rfc1149.net>
To: Pawel Jakub Dawidek <pjd@FreeBSD.org>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/64430: Bug in mv(1)
Date: Sun, 21 Mar 2004 17:38:20 +0100

 On 21/03, Pawel Jakub Dawidek wrote:
 
 | My patch is much simpler, it just use fastcopy() function when rename(2)
 | fails with EXDEV and 'from' is a symblic link.
 
 Yup, much simpler. I didn't realize that fastcopy() was able to copy
 symbolic links contents, I thought it could only copy file contents.
 
   Sam
 
>Unformatted:
