Skip to content

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

@lucacasonato

Description

@lucacasonato

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions