mv with symbolic links fix

David L. Stevens dls at j.cc.purdue.edu
Sun Oct 26 04:44:37 AEST 1986


Index:	/usr/src/bin/mv.c  2.9BSD

Description:
	The distributed mv copies when the source is a symbolic link,
	resulting in 2 instances of the file.
Repeat-By:
	ln -s /etc/passwd /tmp/hose
	mv /tmp/hose /tmp/hosedmore
	ls -l /tmp/hosedmore	(not a link => WRONG)
Fix:
	The fix is to use symlink(), instead of link(), when the source
	is a symbolic link. Diffs follow:

*** OLD mv.c	Sat Oct 25 12:34:16 1986
--- mv.c	Sat Oct 18 23:47:09 1986
***************
*** 97,103
  	int	status;
  	char	buf[MAXN];
  
! 	if (stat(source, &s1) < 0) {
  		fprintf(stderr, "mv: cannot access %s\n", source);
  		return(1);
  	}

--- 97,103 -----
  	int	status;
  	char	buf[MAXN];
  
! 	if (lstat(source, &s1) < 0) {
  		fprintf(stderr, "mv: cannot access %s\n", source);
  		return(1);
  	}
***************
*** 142,148
  			}
  		}
  	}
! 	if (link(source, target) < 0) {
  		i = fork();
  		if (i == -1) {
  			fprintf(stderr, "mv: try again\n");

--- 142,161 -----
  			}
  		}
  	}
! 	if ((s1.st_mode&S_IFMT) == S_IFLNK) {
! 		char lbuf[MAXN];
! 
! 		i = readlink(source, lbuf, sizeof(lbuf));
! 		if (i <= 0) {
! 			fprintf(stderr, "mv: readlink() failed\n");
! 			return(1);
! 		}
! 		lbuf[i] = '\0';
! 		if (symlink(lbuf, target) < 0) {
! 			fprintf(stderr, "mv: symlink failed\n");
! 			return(1);
! 		}
! 	} else if (link(source, target) < 0) {
  		i = fork();
  		if (i == -1) {
  			fprintf(stderr, "mv: try again\n");
-- 
					+-DLS  (dls at j.cc.purdue.edu)



More information about the Comp.bugs.2bsd mailing list