From lyonsm@cr280808-a.crdva1.bc.wave.home.com  Fri Jul  7 00:25:51 2000
Return-Path: <lyonsm@cr280808-a.crdva1.bc.wave.home.com>
Received: from cr280808-a.crdva1.bc.wave.home.com (cr280808-a.crdva1.bc.wave.home.com [24.113.241.196])
	by hub.freebsd.org (Postfix) with SMTP id F3D7137BDC9
	for <FreeBSD-gnats-submit@freebsd.org>; Fri,  7 Jul 2000 00:25:48 -0700 (PDT)
	(envelope-from lyonsm@cr280808-a.crdva1.bc.wave.home.com)
Received: (qmail 926 invoked by uid 0); 6 Jul 2000 23:44:32 -0000
Message-Id: <20000706234432.925.qmail@cr280808-a.crdva1.bc.wave.home.com>
Date: 6 Jul 2000 23:44:32 +0000
From: lyonsm@netbistro.com
Sender: lyonsm@cr280808-a.crdva1.bc.wave.home.com
Reply-To: ml-dyn-bsdpr@netbistro.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: fetch -o clobbers special files
X-Send-Pr-Version: 3.2

>Number:         19748
>Category:       bin
>Synopsis:       fetch -o will unlink special files
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 07 00:30:01 PDT 2000
>Closed-Date:    Sun Sep 17 13:00:10 PDT 2000
>Last-Modified:  Sun Sep 17 13:00:20 PDT 2000
>Originator:     M Lyons
>Release:        FreeBSD 4.0-RELEASE i386
>Organization:
>Environment:


>Description:

(This is arguably a change-request and not a sw-bug.)

In some circumstances, fetch -o will unlink the target file if a transfer
fails.  It's true that this behaviour can be suppressed with the -R
switch; however, fetch should probably never unlink special files or files
it didn't create (or other people who use fetch -o/dev/null in a script
might find themselves wondering why /dev/null keeps disappearing. :)

>How-To-Repeat:

As root, fetch an unreachable address to /dev/null, and then kill with
SIGINT before it times out:

  # cp -Rp /dev/null /tmp/mynull
  # fetch -o/tmp/mynull http://1.2.3.4/foo
  (wait a few sec, then hit CTRL-C)
  # ls -l /tmp/mynull
  (it's gone!)

>Fix:

The following patch to src/usr.bin/fetch/util.c makes fetch
more careful about unlinking special files.  A better fix might
be to have fetch decline to unlink files it didn't create.


*** util.c.orig	Thu Jul  6 23:08:10 2000
--- util.c	Thu Jul  6 23:23:53 2000
***************
*** 29,36 ****
--- 29,37 ----
   * $FreeBSD: src/usr.bin/fetch/util.c,v 1.7 1999/08/28 01:00:52 peter Exp $
   */
  
  #include <sys/types.h>
+ #include <sys/stat.h>
  
  #include <ctype.h>
  #include <err.h>
  #include <errno.h>
***************
*** 104,117 ****
  }
  
  /*
   * Delete the file when exiting on error, if it is not `precious'.
   */
  void
  rm(struct fetch_state *fs)
  {
  	if (!(fs->fs_outputfile[0] == '-' && fs->fs_outputfile[1] == '\0')) {
! 		if (!fs->fs_restart && !fs->fs_mirror && !fs->fs_precious)
  			unlink(fs->fs_outputfile);
  		else
  			adjmodtime(fs);
  	}
--- 105,126 ----
  }
  
  /*
   * Delete the file when exiting on error, if it is not `precious'.
+  * Files which exist and are not plain files are automatically considered
+  * precious.
   */
  void
  rm(struct fetch_state *fs)
  {
+ 	int precious = fs->fs_precious;
+ 	struct stat sb;
+ 
+ 	if(lstat(fs->fs_outputfile, &sb)) return;
+ 	if((sb.st_mode & S_IFMT) != S_IFREG) precious = 1;
+ 
  	if (!(fs->fs_outputfile[0] == '-' && fs->fs_outputfile[1] == '\0')) {
! 		if (!fs->fs_restart && !fs->fs_mirror && !precious)
  			unlink(fs->fs_outputfile);
  		else
  			adjmodtime(fs);
  	}


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Fri Jul 7 07:13:30 PDT 2000 
Responsible-Changed-Why:  
DES will be interested in this observation. 

Note to the originator: don't expect anything to happen in 
a hurry.  DES is re-implementing fetch(1) in the 
development branch of FreeBSD at a moment, so it may be a while 
before you see any such change (if any change is made). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=19748 
State-Changed-From-To: open->feedback 
State-Changed-By: des 
State-Changed-When: Thu Aug 31 09:06:12 PDT 2000 
State-Changed-Why:  
Fixed in -CURRENT, awaiting MFC. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=19748 
State-Changed-From-To: feedback->closed 
State-Changed-By: des 
State-Changed-When: Sun Sep 17 13:00:10 PDT 2000 
State-Changed-Why:  
MFCed. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=19748 
>Unformatted:
