zsh-workers
 help / color / mirror / code / Atom feed
From: Wayne Davison <wayned@users.sourceforge.net>
To: Geoff Wing <mason@primenet.com.au>
Cc: zsh-workers@sunsite.dk
Subject: Re: revisiting history-file rewriting
Date: Wed, 16 Mar 2005 18:49:19 -0800	[thread overview]
Message-ID: <20050317024919.GD13937@blorf.net> (raw)
In-Reply-To: <slrnd3hpn7.4uj.mason@g.primenet.com.au>

[-- Attachment #1: Type: text/plain, Size: 724 bytes --]

On Thu, Mar 17, 2005 at 02:09:43AM +0000, Geoff Wing wrote:
> Didn't we used to do $HISTFILE.<pid> ?  Is that out of favour?

We used to use that idiom as a stepping stone to creating a
$HISTFILE.LOCK file, but now days we use the gettempfile() function
instead (since this avoids a potential problem with the same pid being
allocated on different hosts).  The name used once we lock down the
system can be constant without clashing with another process, but I
suppose it could clash with a file that the user has created.  The
attached patch can be applied to my previous patch to make the history
code use gettempfile() to return an opened $HISTFILE.XXXXXX name for
the new file instead of using $HISTFILE.new.

..wayne..

[-- Attachment #2: hist-tempname.patch --]
[-- Type: text/plain, Size: 1404 bytes --]

--- Src/hist.c.new	2005-03-16 18:26:16.638813544 -0800
+++ Src/hist.c	2005-03-16 18:40:32.629680617 -0800
@@ -2049,9 +2049,14 @@ savehistfile(char *fn, int err, int writ
 	out = fdopen(open(unmeta(fn),
 			 O_CREAT | O_WRONLY | O_TRUNC | O_NOCTTY, 0600), "w");
     } else {
-	tmpfile = bicat(unmeta(fn), ".new");
-	unlink(tmpfile);
-	out = fdopen(open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600), "w");
+	int fd;
+	if ((fd = gettempfile(fn, 0, &tmpfile)) >= 0) {
+	    if (!(out = fdopen(fd, "w")))
+		close(fd);
+	} else {
+	    tmpfile = bicat(unmeta(fn), ".XXXXXX");
+	    out = NULL;
+	}
     }
     if (out) {
 	for (; he && he->histnum <= xcurhist; he = down_histent(he)) {
@@ -2097,8 +2102,10 @@ savehistfile(char *fn, int err, int writ
 	}
 	fclose(out);
 	if (tmpfile) {
-	    if (rename(tmpfile, unmeta(fn)) < 0)
-		zerr("can't rename %s.new to $HISTFILE", fn, 0);
+	    if (rename(tmpfile, unmeta(fn)) < 0) {
+		tmpfile = metafy(tmpfile, -1, META_REALLOC);
+		zerr("can't rename %s to $HISTFILE", tmpfile, 0);
+	    }
 	    free(tmpfile);
 	}
 
@@ -2122,7 +2129,8 @@ savehistfile(char *fn, int err, int writ
 	}
     } else if (err) {
 	if (tmpfile) {
-	    zerr("can't write history file %s.new", fn, 0);
+	    tmpfile = metafy(tmpfile, -1, META_REALLOC);
+	    zerr("can't write history file %s", tmpfile, 0);
 	    free(tmpfile);
 	} else
 	    zerr("can't write history file %s", fn, 0);

  reply	other threads:[~2005-03-17  2:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-16 20:40 Wayne Davison
2005-03-17  2:09 ` Geoff Wing
2005-03-17  2:49   ` Wayne Davison [this message]
2005-03-18 16:18     ` Andrey Borzenkov
2005-03-18 17:51       ` Wayne Davison
2005-03-18 18:02         ` Wayne Davison
2005-03-17 18:03 ` Bart Schaefer
2005-03-18 18:19   ` Wayne Davison
2005-03-18 21:15     ` Bart Schaefer
2005-03-18 21:47       ` Wayne Davison
2005-03-18 10:49 ` Peter Stephenson
2005-03-18 17:58   ` Wayne Davison
2005-03-18 18:24     ` Peter Stephenson
2005-03-18 22:53 ` Wayne Davison
2005-03-20  1:57   ` Bart Schaefer
2005-03-21 10:39     ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050317024919.GD13937@blorf.net \
    --to=wayned@users.sourceforge.net \
    --cc=mason@primenet.com.au \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).