Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: revisiting history-file rewriting



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..
--- 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);


Messages sorted by: Reverse Date, Date, Thread, Author