Subject: bin/10569: Restore(8) with -u option failed to create hard link
To: None <gnats-bugs@gnats.netbsd.org>
From: Takahiro Kambe <taca@sky.yamashina.kyoto.jp>
List: netbsd-bugs
Date: 07/11/2000 08:16:13
>Number:         10569
>Category:       bin
>Synopsis:       Restore(8) with -u option failed to create hard link
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Jul 11 08:17:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Takahiro Kambe
>Release:        NetBSD 1.5_ALPHA (2000/7/9)
>Organization:
	
>Environment:
	
System: NetBSD edge.sky.yamashina.kyoto.jp 1.5_ALPHA NetBSD 1.5_ALPHA (CF-M33) #42: Sun Jul 9 01:03:24 JST 2000 root@edge.sky.yamashina.kyoto.jp:/usr/src/sys/arch/i386/compile/CF-M33 i386


>Description:
	Restore(8) with -u option removes file before extraction, but dosen't
	care much for creating hard link.  Restore(8) with -u option should
	be unlink a destination file (except it is a directory) before create
	a hard link.

>How-To-Repeat:
	Try restore(8) with -u option twice on the same directory,
	it dosen't preserve hard links.

>Fix:
	Sample fix, not tested much.

--- utilities.c.orig	Mon Mar 20 23:37:04 2000
+++ utilities.c	Wed Jul 12 00:13:31 2000
@@ -213,24 +213,35 @@
 	char *existing, *new;
 	int type;
 {
+	struct stat st;
 
-	if (type == SYMLINK) {
-		if (!Nflag && symlink(existing, new) < 0) {
+	if (type != SYMLINK && HARDLINK) {
+		panic("linkit: unknown type %d\n", type);
+		return (FAIL);
+	}
+
+	if (!Nflag) {
+		if (uflag && lstat(new, &st) == 0 &&
+		    (st.st_mode & S_IFMT) != S_IFDIR) {
+			if (unlink(new) < 0) {
+				fprintf(stderr,
+					"warning: cannot unlink %s: %s\n",
+					new, strerror(errno));
+				return (FAIL);
+			}
+		}
+
+		if (type == SYMLINK && symlink(existing, new) < 0) {
 			fprintf(stderr,
-			    "warning: cannot create symbolic link %s->%s: %s\n",
-			    new, existing, strerror(errno));
+				"warning: cannot create symbolic link %s->%s: %s\n",
+				new, existing, strerror(errno));
 			return (FAIL);
-		}
-	} else if (type == HARDLINK) {
-		if (!Nflag && link(existing, new) < 0) {
+		} else if (type == HARDLINK && link(existing, new) < 0) {
 			fprintf(stderr,
-			    "warning: cannot create hard link %s->%s: %s\n",
-			    new, existing, strerror(errno));
+				"warning: cannot create hard link %s->%s: %s\n",
+				new, existing, strerror(errno));
 			return (FAIL);
 		}
-	} else {
-		panic("linkit: unknown type %d\n", type);
-		return (FAIL);
 	}
 	vprintf(stdout, "Create %s link %s->%s\n",
 		type == SYMLINK ? "symbolic" : "hard", new, existing);


>Release-Note:
>Audit-Trail:
>Unformatted: