Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does fs.read's length argument indicate? #52447

Closed
lucacasonato opened this issue Apr 10, 2024 · 6 comments
Closed

What does fs.read's length argument indicate? #52447

lucacasonato opened this issue Apr 10, 2024 · 6 comments
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors.

Comments

@lucacasonato
Copy link

lucacasonato commented Apr 10, 2024

Affected URL(s)

https://nodejs.org/api/fs.html#fsreadfd-buffer-offset-length-position-callback

Description of the problem

It is not clear from Node's fs.read() docs, whether the length argument indicates the maximum number of bytes that Node will attempt to read from the kernel, or whether Node will return exactly length bytes (possibly making multiple read syscalls in the process).

Ie: if I call fs.read() with length = 10, which of these holds:

  1. When reading a file that is 9 bytes long, exactly 9 bytes are read and bytesRead is set to 9.
  2. When reading a file that is 9 bytes long, the callback is called with an error because EOF was encountered before the buffer could be filled.
  3. When reading a file that is 11 bytes long, exactly 10 bytes are read and bytesRead is set to 10.
  4. When reading a file that is 11 bytes long, but the file is on a slow network filesystem that only reads in chunks of 5 bytes per second, 5 bytes are read and bytesRead is set to 5.

This is an important thing to document because it determines whether you need to wrap the read call in a loop (like the Linux syscall) if you need a minimum amount of bytes, or whether you do not have to do this.

@lucacasonato lucacasonato added the doc Issues and PRs related to the documentations. label Apr 10, 2024
@lucacasonato lucacasonato changed the title What does fs.read's length property indicate? What does fs.read's length argument indicate? Apr 10, 2024
@targos targos added the fs Issues and PRs related to the fs subsystem / file system. label Apr 10, 2024
@targos
Copy link
Member

targos commented Apr 10, 2024

@nodejs/fs

@mcollina
Copy link
Member

  1. no. bytesRead can be lower for any sort of reason.
  2. no
  3. no, bytesRead can be lower
  4. yes

This is an important thing to document because it determines whether you need to wrap the read call in a loop (like the Linux syscall) if you need a minimum amount of bytes, or whether you do not have to do this.

Yes, you need to loop.
It's equivalent of preadv2 https://man7.org/linux/man-pages/man2/preadv.2.html.

@lucacasonato
Copy link
Author

Great - thank you, this matches my expectation. Do you want to keep this issue open to track updates of the docs or shall I close it?

@joyeecheung joyeecheung added the good first issue Issues that are suitable for first-time contributors. label Apr 10, 2024
@joyeecheung
Copy link
Member

I think we should keep it open so that someone improves the doc to avoid confusing future readers.

@jsonOdison
Copy link

is it still open , can i still contribute ?
@joyeecheung

@LiviaMedeiros
Copy link
Contributor

#52453 was merged, so I guess this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests

6 participants