unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Binbin YE <phantom2501@gmail.com>
To: emacs-devel@gnu.org
Subject: Re: how to reading 0 byte files properly
Date: Fri, 18 Nov 2022 11:17:07 +0900	[thread overview]
Message-ID: <CABVdjjAkHrLA2SommKrW-G-40uQ8AsO5faNmb1rF7du=c4Xy3w@mail.gmail.com> (raw)
In-Reply-To: <87o7t5ubs9.fsf@igel.home>

[-- Attachment #1: Type: text/plain, Size: 2286 bytes --]

Sorry I put mailing list in  "cc",  but not "to"

Andreas
Thank you 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."
>

[-- Attachment #2: Type: text/html, Size: 3281 bytes --]

      parent reply	other threads:[~2022-11-18  2:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17  8:45 how to reading 0 byte files properly Binbin YE
2022-11-17 10:15 ` Andreas Schwab
2022-11-18  1:04   ` Binbin YE
2022-11-18  2:17   ` Binbin YE [this message]

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABVdjjAkHrLA2SommKrW-G-40uQ8AsO5faNmb1rF7du=c4Xy3w@mail.gmail.com' \
    --to=phantom2501@gmail.com \
    --cc=emacs-devel@gnu.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.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).