From owner-svn-src-stable-8@FreeBSD.ORG Tue Jan 25 23:02:26 2011 Return-Path: Delivered-To: svn-src-stable-8@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 48E2E106564A; Tue, 25 Jan 2011 23:02:26 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 1CCE58FC16; Tue, 25 Jan 2011 23:02:26 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p0PN2QV1055535; Tue, 25 Jan 2011 23:02:26 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p0PN2PKk055533; Tue, 25 Jan 2011 23:02:26 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201101252302.p0PN2PKk055533@svn.freebsd.org> From: Jilles Tjoelker Date: Tue, 25 Jan 2011 23:02:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r217863 - stable/8/usr.bin/sed X-BeenThere: svn-src-stable-8@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for only the 8-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Jan 2011 23:02:26 -0000 Author: jilles Date: Tue Jan 25 23:02:25 2011 New Revision: 217863 URL: http://svn.freebsd.org/changeset/base/217863 Log: MFC r217133: sed: Try hard links to make -i target available continually. When creating a backup file, sed renamed the original before renaming the changed copy into place, leading to a short time when no file with the original name was present (usually only visible on SMP systems). Try creating the backup file using a hard link instead, avoiding this problem. If creating the hard link fails for any reason, fall back to the old rename method. When not creating a backup file, sed already renamed the changed copy onto the original. This remains unchanged. PR: bin/153261 Submitted by: Pedro F. Giffuni Reviewed by: dds (older version) Obtained from: Illumos Modified: stable/8/usr.bin/sed/main.c Directory Properties: stable/8/usr.bin/sed/ (props changed) Modified: stable/8/usr.bin/sed/main.c ============================================================================== --- stable/8/usr.bin/sed/main.c Tue Jan 25 22:55:50 2011 (r217862) +++ stable/8/usr.bin/sed/main.c Tue Jan 25 23:02:25 2011 (r217863) @@ -338,18 +338,35 @@ mf_fgets(SPACE *sp, enum e_spflag spflag if (infile != NULL) { fclose(infile); if (*oldfname != '\0') { - if (rename(fname, oldfname) != 0) { + /* if there was a backup file, remove it */ + unlink(oldfname); + /* + * Backup the original. Note that hard links + * are not supported on all filesystems. + */ + if ((link(fname, oldfname) != 0) && + (rename(fname, oldfname) != 0)) { warn("rename()"); - unlink(tmpfname); + if (*tmpfname) + unlink(tmpfname); exit(1); } *oldfname = '\0'; } if (*tmpfname != '\0') { if (outfile != NULL && outfile != stdout) - fclose(outfile); + if (fclose(outfile) != 0) { + warn("fclose()"); + unlink(tmpfname); + exit(1); + } outfile = NULL; - rename(tmpfname, fname); + if (rename(tmpfname, fname) != 0) { + /* this should not happen really! */ + warn("rename()"); + unlink(tmpfname); + exit(1); + } *tmpfname = '\0'; } outfname = NULL;