Message ID | dc4674d222317fa2f503e6e1b3cad6a78dd70676.1586269543.git.liu.denton@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | merge: learn --autostash | expand |
Denton Liu <liu.denton@gmail.com> writes: > We currently check whether a file exists and return early before reading > the file. Instead of accessing the file twice, always read the file and > check `errno` to see if the file doesn't exist. > > Signed-off-by: Denton Liu <liu.denton@gmail.com> > --- > sequencer.c | 6 ++---- > 1 file changed, 2 insertions(+), 4 deletions(-) It's not likely for us to break strbuf_read_file() in such a way that it would clobber errno when it sees a failure from open, so this should be OK. If we were racing with somebody else who is trying to remove 'path', we may have said "ah, the file is there, let's try to read --- oops, we cannot read it after all" with the current code, but the updated code would take advantage of the atomicity of open() to avoid such race. If we can open it, we would read it unless there is an I/O error. If we failed to open it, we know the file wasn't there when we attempted to read. Good. > diff --git a/sequencer.c b/sequencer.c > index faab0b13e8..a961cf5a9b 100644 > --- a/sequencer.c > +++ b/sequencer.c > @@ -433,11 +433,9 @@ static int read_oneliner(struct strbuf *buf, > { > int orig_len = buf->len; > > - if (!file_exists(path)) > - return 0; > - > if (strbuf_read_file(buf, path, 0) < 0) { > - warning_errno(_("could not read '%s'"), path); > + if (errno != ENOENT && errno != ENOTDIR) > + warning_errno(_("could not read '%s'"), path); > return 0; > }
diff --git a/sequencer.c b/sequencer.c index faab0b13e8..a961cf5a9b 100644 --- a/sequencer.c +++ b/sequencer.c @@ -433,11 +433,9 @@ static int read_oneliner(struct strbuf *buf, { int orig_len = buf->len; - if (!file_exists(path)) - return 0; - if (strbuf_read_file(buf, path, 0) < 0) { - warning_errno(_("could not read '%s'"), path); + if (errno != ENOENT && errno != ENOTDIR) + warning_errno(_("could not read '%s'"), path); return 0; }
We currently check whether a file exists and return early before reading the file. Instead of accessing the file twice, always read the file and check `errno` to see if the file doesn't exist. Signed-off-by: Denton Liu <liu.denton@gmail.com> --- sequencer.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)