Re: [PATCH v5 04/22] sequencer: make file exists check more efficient

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Denton Liu <liu.denton@xxxxxxxxx> 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@xxxxxxxxx>
> ---
>  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;
>  	}



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux