From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@zsh.org
Subject: Re: Any way to allow clobbering empty files when noclobber is set?
Date: Mon, 8 Jun 2020 10:30:32 +0100 (BST) [thread overview]
Message-ID: <1300207061.4910.1591608632997@mail2.virginmedia.com> (raw)
In-Reply-To: <20200608032730.614e197c@tarpaulin.shahaf.local2>
Here's an updated patch, which I think incorporates the
current thinking, which seems pretty much to have iterated
to a conclusion. The effective change is in fact now
trivial --- an "if" with a return.
Webmail again, as it seemed pretty non-invasive last time...
pws
diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
index 2b7637ff4..6da68308f 100644
--- a/Doc/Zsh/options.yo
+++ b/Doc/Zsh/options.yo
@@ -1168,6 +1168,22 @@ If the option is not set, and the option tt(APPEND_CREATE) is also
not set, `tt(>>!)' or `tt(>>|)' must be used to create a file.
If either option is set, `tt(>>)' may be used.
)
+pindex(CLOBBER_EMPTY)
+pindex(NO_CLOBBER_EMPTY)
+pindex(CLOBBEREMPTY)
+pindex(NOCLOBBEREMPTY)
+cindex(clobbering, of empty files)
+cindex(file clobbering, of empty files)
+item(tt(CLOBBER_EMPTY))(
+This option is only used if the option tt(CLOBBER) is not set: note that
+it is set by default.
+
+If this option is set, then regular files of zero length may be
+ovewritten (`clobbered'). Note that it is possible another process
+has written to the file between this test and use of the file by
+the current process. This option should therefore not be used in
+cases where files to be clobbered may be written to asynchronously.
+)
pindex(CORRECT)
pindex(NO_CORRECT)
pindex(NOCORRECT)
diff --git a/Src/exec.c b/Src/exec.c
index 29f4fc5ca..7b087f3b0 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2143,14 +2143,15 @@ clobber_open(struct redir *f)
{
struct stat buf;
int fd, oerrno;
+ char *ufname = unmeta(f->name);
/* If clobbering, just open. */
if (isset(CLOBBER) || IS_CLOBBER_REDIR(f->type))
- return open(unmeta(f->name),
+ return open(ufname,
O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666);
/* If not clobbering, attempt to create file exclusively. */
- if ((fd = open(unmeta(f->name),
+ if ((fd = open(ufname,
O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY, 0666)) >= 0)
return fd;
@@ -2158,11 +2159,27 @@ clobber_open(struct redir *f)
* Try opening, and if it's a regular file then close it again *
* because we weren't supposed to open it. */
oerrno = errno;
- if ((fd = open(unmeta(f->name), O_WRONLY | O_NOCTTY)) != -1) {
- if(!fstat(fd, &buf) && !S_ISREG(buf.st_mode))
- return fd;
+ if ((fd = open(ufname, O_WRONLY | O_NOCTTY)) != -1) {
+ if(!fstat(fd, &buf)) {
+ if (!S_ISREG(buf.st_mode))
+ return fd;
+ /*
+ * If CLOBBER_EMPTY is in effect and the file is empty,
+ * we are allowed to re-use it.
+ *
+ * Note: there is an intrinsic race here because another
+ * process can write to this file at any time. The only fix
+ * would be file locking, which we wish to avoid in basic
+ * file operations at this level. This would not be
+ * fixed. just additionally complicated, by re-opening the
+ * file and truncating.
+ */
+ if (isset(CLOBBEREMPTY) && buf.st_size == 0)
+ return fd;
+ }
close(fd);
}
+
errno = oerrno;
return -1;
}
diff --git a/Src/options.c b/Src/options.c
index 7586d21d2..fba021e7d 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -114,6 +114,7 @@ static struct optname optns[] = {
{{NULL, "checkjobs", OPT_EMULATE|OPT_ZSH}, CHECKJOBS},
{{NULL, "checkrunningjobs", OPT_EMULATE|OPT_ZSH}, CHECKRUNNINGJOBS},
{{NULL, "clobber", OPT_EMULATE|OPT_ALL}, CLOBBER},
+{{NULL, "clobberempty", 0}, CLOBBEREMPTY},
{{NULL, "combiningchars", 0}, COMBININGCHARS},
{{NULL, "completealiases", 0}, COMPLETEALIASES},
{{NULL, "completeinword", 0}, COMPLETEINWORD},
diff --git a/Src/zsh.h b/Src/zsh.h
index 1f2d774a1..ed123f2b9 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2378,6 +2378,7 @@ enum {
CHECKJOBS,
CHECKRUNNINGJOBS,
CLOBBER,
+ CLOBBEREMPTY,
APPENDCREATE,
COMBININGCHARS,
COMPLETEALIASES,
diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst
index d60519064..993138e7d 100644
--- a/Test/A04redirect.ztst
+++ b/Test/A04redirect.ztst
@@ -708,3 +708,17 @@
cat <&$testfd
0:Regression test for here document with fd declarator
> This is, in some sense, a here document.
+
+ (setopt noclobber clobberempty
+ rm -f foo
+ touch foo
+ print Works >foo
+ cat foo
+ print Works not >foo
+ # Make sure the file was not harmed
+ cat foo
+ )
+0:CLOBBER_EMPTY
+>Works
+>Works
+?(eval):6: file exists: foo
next prev parent reply other threads:[~2020-06-08 9:31 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20200603020919eucas1p13e26ebcbb335784d14bfb97b137f385a@eucas1p1.samsung.com>
2020-06-03 2:08 ` Martin Tournoij
2020-06-03 12:04 ` Peter Stephenson
2020-06-04 1:48 ` Daniel Shahaf
2020-06-04 2:43 ` Bart Schaefer
2020-06-04 4:06 ` Daniel Shahaf
2020-06-04 5:00 ` Bart Schaefer
2020-06-05 3:10 ` Daniel Shahaf
2020-06-05 3:18 ` Daniel Shahaf
2020-06-06 1:07 ` Bart Schaefer
2020-06-06 4:48 ` Daniel Shahaf
2020-06-06 7:04 ` Bart Schaefer
2020-06-04 6:31 ` Martin Tournoij
2020-06-05 2:22 ` Daniel Shahaf
2020-06-04 2:13 ` Vin Shelton
2020-06-04 2:35 ` Bart Schaefer
2020-06-04 2:36 ` Daniel Shahaf
2020-06-04 11:57 ` Vin Shelton
2020-06-04 5:06 ` Bart Schaefer
2020-06-04 5:41 ` Roman Perepelitsa
2020-06-05 2:07 ` Daniel Shahaf
2020-06-05 4:38 ` Roman Perepelitsa
2020-06-06 1:41 ` Bart Schaefer
2020-06-06 4:55 ` Daniel Shahaf
2020-06-06 6:25 ` Roman Perepelitsa
2020-06-06 7:08 ` Bart Schaefer
2020-06-06 8:03 ` Daniel Shahaf
[not found] ` <1941572212.466119.1591360860372@mail2.virginmedia.com>
[not found] ` <e7f7dfe2-eb4a-457b-85fb-091935a74c0e@www.fastmail.com>
2020-06-06 11:57 ` Peter Stephenson
2020-06-06 12:48 ` Roman Perepelitsa
2020-06-06 15:24 ` Bart Schaefer
2020-06-06 16:24 ` Peter Stephenson
2020-06-07 11:55 ` Daniel Shahaf
2020-06-07 17:00 ` Peter Stephenson
2020-06-08 3:27 ` Daniel Shahaf
2020-06-08 9:30 ` Peter Stephenson [this message]
2020-06-07 17:20 ` Bart Schaefer
2020-06-06 15:09 ` Bart Schaefer
2020-06-04 6:47 ` Martin Tournoij
2020-06-04 9:42 ` Peter Stephenson
2020-06-04 12:20 ` Roman Perepelitsa
2020-06-04 12:26 ` Peter Stephenson
2020-06-04 12:15 ` Peter Stephenson
2020-06-04 20:35 ` Bart Schaefer
2020-06-05 1:59 ` Daniel Shahaf
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=1300207061.4910.1591608632997@mail2.virginmedia.com \
--to=p.w.stephenson@ntlworld.com \
--cc=zsh-workers@zsh.org \
/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).