From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@sunsite.dk
Subject: Re: zsh history gets destroyed when running out of disk space
Date: Fri, 22 Jun 2007 21:28:25 +0100 [thread overview]
Message-ID: <20070622212825.a8d29679.p.w.stephenson@ntlworld.com> (raw)
In-Reply-To: <20070621080454.GA3303@fsst.voodoo.lan>
On Thu, 21 Jun 2007 10:04:54 +0200
Frank Terbeck <ft@bewatermyfriend.org> wrote:
> Michael Prokop <news@michael-prokop.at>:
> > Problem:
> >
> > Zsh truncates the zsh history file if you are running out of disk
> > space (AKA ENOSPC). If you don't have any space left in your $HOME
> > and exit zsh you'll find an empty $HISTFILE left.
>
> Hey, I experienced just the same a few days back. So, I also think
> this should be addressed.
The following detects errors during writing better. If you're writing a
new file it'll leave it wherever it gets to; if you're replacing an old
file it will leave the old file alone. That's about the best we can do.
I haven't tested this on a failure, but it should be better than what
we've got.
Index: Src/hist.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/hist.c,v
retrieving revision 1.66
diff -u -r1.66 hist.c
--- Src/hist.c 1 Nov 2006 12:25:22 -0000 1.66
+++ Src/hist.c 22 Jun 2007 20:19:50 -0000
@@ -2158,6 +2158,7 @@
Histent he;
zlong xcurhist = curhist - !!(histactive & HA_ACTIVE);
int extended_history = isset(EXTENDEDHISTORY);
+ int ret;
if (!interact || savehistsiz <= 0 || !hist_ring
|| (!fn && !(fn = getsparam("HISTFILE"))))
@@ -2242,59 +2243,69 @@
}
t = start = he->node.nam;
if (extended_history) {
- fprintf(out, ": %ld:%ld;", (long)he->stim,
- he->ftim? (long)(he->ftim - he->stim) : 0L);
+ ret = fprintf(out, ": %ld:%ld;", (long)he->stim,
+ he->ftim? (long)(he->ftim - he->stim) : 0L);
} else if (*t == ':')
- fputc('\\', out);
+ ret = fputc('\\', out);
- for (; *t; t++) {
+ for (; ret >= 0 && *t; t++) {
if (*t == '\n')
- fputc('\\', out);
- fputc(*t, out);
+ if ((ret = fputc('\\', out)) < 0)
+ break;
+ if ((ret = fputc(*t, out)) < 0)
+ break;
}
- fputc('\n', out);
+ if (ret < 0 || (ret = fputc('\n', out)) < 0)
+ break;
}
- if (start && writeflags & HFILE_USE_OPTIONS) {
+ if (ret >= 0 && start && writeflags & HFILE_USE_OPTIONS) {
struct stat sb;
- fflush(out);
- if (fstat(fileno(out), &sb) == 0) {
- lasthist.fsiz = sb.st_size;
- lasthist.mtim = sb.st_mtime;
+ if ((ret = fflush(out)) >= 0) {
+ if (fstat(fileno(out), &sb) == 0) {
+ lasthist.fsiz = sb.st_size;
+ lasthist.mtim = sb.st_mtime;
+ }
+ zsfree(lasthist.text);
+ lasthist.text = ztrdup(start);
}
- zsfree(lasthist.text);
- lasthist.text = ztrdup(start);
- }
- fclose(out);
- if (tmpfile) {
- if (rename(tmpfile, unmeta(fn)) < 0)
- zerr("can't rename %s.new to $HISTFILE", fn);
- free(tmpfile);
}
+ if (fclose(out) < 0 && ret >= 0)
+ ret = -1;
+ if (ret >= 0) {
+ if (tmpfile) {
+ if (rename(tmpfile, unmeta(fn)) < 0)
+ zerr("can't rename %s.new to $HISTFILE", fn);
+ free(tmpfile);
+ }
- if (writeflags & HFILE_SKIPOLD
- && !(writeflags & (HFILE_FAST | HFILE_NO_REWRITE))) {
- int remember_histactive = histactive;
-
- /* Zeroing histactive avoids unnecessary munging of curline. */
- histactive = 0;
- /* The NULL leaves HISTFILE alone, preserving fn's value. */
- pushhiststack(NULL, savehistsiz, savehistsiz, -1);
-
- hist_ignore_all_dups |= isset(HISTSAVENODUPS);
- readhistfile(fn, err, 0);
- hist_ignore_all_dups = isset(HISTIGNOREALLDUPS);
- if (histlinect)
- savehistfile(fn, err, 0);
+ if (writeflags & HFILE_SKIPOLD
+ && !(writeflags & (HFILE_FAST | HFILE_NO_REWRITE))) {
+ int remember_histactive = histactive;
+
+ /* Zeroing histactive avoids unnecessary munging of curline. */
+ histactive = 0;
+ /* The NULL leaves HISTFILE alone, preserving fn's value. */
+ pushhiststack(NULL, savehistsiz, savehistsiz, -1);
+
+ hist_ignore_all_dups |= isset(HISTSAVENODUPS);
+ readhistfile(fn, err, 0);
+ hist_ignore_all_dups = isset(HISTIGNOREALLDUPS);
+ if (histlinect)
+ savehistfile(fn, err, 0);
- pophiststack();
- histactive = remember_histactive;
+ pophiststack();
+ histactive = remember_histactive;
+ }
}
- } else if (err) {
+ } else
+ ret = -1;
+
+ if (ret < 0 && err) {
if (tmpfile) {
- zerr("can't write history file %s.new", fn);
+ zerr("can't write history file %s.new: %e", fn);
free(tmpfile);
} else
- zerr("can't write history file %s", fn);
+ zerr("can't write history file %s: %e", fn);
}
unlockhistfile(fn);
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
next prev parent reply other threads:[~2007-06-22 20:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-11 20:22 Michael Prokop
2007-06-21 8:04 ` Frank Terbeck
2007-06-22 20:28 ` Peter Stephenson [this message]
2007-06-22 22:40 ` Michael Prokop
2007-06-23 18:09 ` 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=20070622212825.a8d29679.p.w.stephenson@ntlworld.com \
--to=p.w.stephenson@ntlworld.com \
--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).