Bug 64430

Summary: Bug in mv(1)
Product: Base System Reporter: Samuel Tardieu <sam>
Component: binAssignee: Pawel Jakub Dawidek <pjd>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.9-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Samuel Tardieu 2004-03-18 19:00:28 UTC
	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.

Fix: The following patch fixes that.
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.
Comment 1 Pawel Jakub Dawidek freebsd_committer freebsd_triage 2004-03-21 12:58:47 UTC
Responsible Changed
From-To: freebsd-bugs->pjd

I'll take this one.
Comment 2 Pawel Jakub Dawidek freebsd_committer freebsd_triage 2004-03-21 13:43:22 UTC
State Changed
From-To: open->closed

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!
Comment 3 Pawel Jakub Dawidek freebsd_committer freebsd_triage 2004-03-21 14:04:14 UTC
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
+> 
+> 	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=1.41&r2=1.42

-- 
Pawel Jakub Dawidek                       http://www.FreeBSD.org
pjd@FreeBSD.org                           http://garage.freebsd.pl
FreeBSD committer                         Am I Evil? Yes, I Am!
Comment 4 Samuel Tardieu 2004-03-21 16:38:20 UTC
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