Skip to content

Reading more bytes than a file contains yields EOF instead of data #12892

Closed

Description

@jdm

The most recent Rust upgrade for Servo has uncovered a problem with the code at larsbergstrom/servo@3445fc9#diff-b8f19d42333587901f805bdc9be6df88R16 . A call to reader.read_bytes(1024) on a file that is <1024 bytes long does not yield any data, and instead returns an immediate EOF error result.

Activity

  1. alexcrichton commented on Mar 14, 2014

    @alexcrichton
    Member

    Hm, it may be something else going on, this appears to work for me:

    $ cat foo.rs
    use std::io::File;
    
    fn main() {
        let mut buf = [0, .. 2048];
        let mut f = File::open(&Path::new("foo.rs"));
        println!("{}", f.read(buf));
    }
    $ rustc foo.rs
    $ ./foo
    Ok(149)
    
  2. zkamsler commented on Mar 14, 2014

    @zkamsler
    Contributor

    This appears to the documented behavior of read_bytes (not be be confused with read). It basically reads until it has read the specified number of bytes or there is an error.

  3. jdm commented on Mar 14, 2014

    @jdm
    ContributorAuthor

    Wow. That seems like a really niche use case in which it's acceptable to potentially lose bytes.

  4. alexcrichton commented on Mar 14, 2014

    @alexcrichton
    Member

    Ah sorry, yes, I see what's going on now. read_bytes is for reading an exact number of bytes, not for generally "read some bytes".

    Perhaps is should be renamed to read_exact, it may have made more sense when conditions existed.

  5. jdm commented on Mar 14, 2014

    @jdm
    ContributorAuthor

    I'll note that using push_bytes doesn't seem to be much of an improvement if you're trying to read a file in chunks; I'm seeing similar behaviour with it.

  6. zkamsler commented on Mar 14, 2014

    @zkamsler
    Contributor

    push_bytes will also bubble up an EOF if there are less than the amount requested, but the bytes that were read will still have been pushed to the buffer. All read_bytes does is call push_bytes on a new ~[u8].

    See source:
    https://github.com/mozilla/rust/blob/b4d324334cb48198c27d782002d75eba14a6abde/src/libstd/io/mod.rs#L380

    But it definitely is not that great if you don't care about reading the exact number of bytes.

  7. sfackler commented on Mar 14, 2014

    @sfackler
    Member

    👍 to read_exact.

  8. added a commit that references this issue on Mar 22, 2014
    092afdb
  9. added a commit that references this issue on Jul 28, 2024
  10. added 2 commits that reference this issue on Aug 26, 2024
  11. added a commit that references this issue on Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions