emacs-devel
[Top][All Lists]
Advanced

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

Re: how to reading 0 byte files properly


From: Binbin YE
Subject: Re: how to reading 0 byte files properly
Date: Fri, 18 Nov 2022 10:04:51 +0900

Thank you Andreas for pointing that out


at this moment "total" is READ_BUF_SIZE

```c
/* emacs/src/fileio.c:4613 */

/* In the following loop, HOW_MUCH contains the total bytes read so
   far for a regular file, and not changed for a special file.  But,
   before exiting the loop, it is set to a negative value if I/O
   error occurs.  */
how_much = 0;
```

I have confirmed the file is not seekable on my side using,  which is different from /proc files

```c
/* test.c:11 */

int fd = open("/proc/2051/arch_status", O_RDONLY);
int sek = lseek(fd, 0, SEEK_CUR);
printf("proc file is seekable %d\n", sek); // returns 0

fd = open("/run/test.json", O_RDONLY);
sek = lseek(fd, 0, SEEK_CUR);
printf("fuse file is seekable %d\n", sek); // returns -1
```

I think it hits this block. But I don't see anything special to increase the count. Could that mean emacs only reads "READ_BUF_SIZE" amount of data?

```c
/* emacs/src/fileio.c:4627 */

while (how_much < total)
  {
    /* `try' is reserved in some compilers (Microsoft C).  */
    ptrdiff_t trytry = min (total - how_much, READ_BUF_SIZE);
    ptrdiff_t this;

    if (!seekable && NILP (end))
```

should the fix be quitting at actual io?


Best,

Binbin


On Thu, Nov 17, 2022 at 7:15 PM Andreas Schwab <schwab@linux-m68k.org> wrote:
On Nov 17 2022, Binbin YE wrote:

> /* emacs/src/fileio.c:4587 */
>
> if (seekable || !NILP (end))
>   total = end_offset - beg_offset;
> else
>   /* For a special file, all we can do is guess.  */
>   total = READ_BUF_SIZE;
> ```
> Judging from the code, it assume the total size would be READ_BUF_SIZE

For a non-seekable file this is just a buffer size, see the read loop
later in the function (how_much stays zero then).

If the file is seekable, the important part is this:

          /* The file size returned from fstat may be zero, but data
             may be readable nonetheless, for example when this is a
             file in the /proc filesystem.  */
          if (end_offset == 0)
            end_offset = READ_BUF_SIZE;

--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]