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

File::metadata()?.len() returns 0 #92

Closed
bugadani opened this issue Nov 12, 2021 · 5 comments
Closed

File::metadata()?.len() returns 0 #92

bugadani opened this issue Nov 12, 2021 · 5 comments
Assignees
Labels

Comments

@bugadani
Copy link

bugadani commented Nov 12, 2021

I'm not sure where I should report issues that are either libc or std, so I hope this is either the right place or someone knows.

I'm trying to read the size of an open file. I have two options to do this:

  • fs::File::metadata()
  • a custom C implementation

The C impl looks like this:

size_t file_size_fd(int file) {
    struct stat st;

    if (fstat(file, &st) == 0) {
        return st.st_size;
    } else {
        return 0u;
    }
}

I have the following snippet:

    let size = unsafe { file_size_fd(f.as_raw_fd()) };
    let size2 = f.metadata().unwrap().len();

The C implementation returns the correct size (for example, 868 bytes), but the rust call gives me 0.

I'm using a fresh enough installation of esp-idf 4.3.1 and rust 1.56.0.1 [sic].

@MabezDev
Copy link
Member

So it seems the underlying metadata() call invokes this portion of code: https://github.com/rust-lang/rust/blob/c9c4b5d7276297679387189d96a952f2b760e7ad/library/std/src/sys/unix/fs.rs#L772-L789. The libc call it makes is fstat64, however a grep of the newlib component in esp-idf shows no results, but it has got fstat implemented at least.

I think this needs to be implemented in esp-idf, or maybe we need to patch the std library to use fstat for the espidf targets.

@bugadani
Copy link
Author

I am slightly confused by the heap of cfg macros, but isn't fstat imported as fstat64 in https://github.com/rust-lang/rust/blob/c9c4b5d7276297679387189d96a952f2b760e7ad/library/std/src/sys/unix/fs.rs#L59 for espidf targets as well?

@MabezDev MabezDev moved this to In Progress in esp-rs Mar 3, 2022
@MabezDev
Copy link
Member

MabezDev commented Mar 3, 2022

Finally tracked this one down, it seems the definition for stat on the C/Rust border.

  • size of libc::newlib::stat (Rust side), 80 bytes
  • size of stat on C side: 60 bytes

Now to figure out which side is wrong, and what fields need to change.

bors added a commit to rust-lang/libc that referenced this issue Mar 7, 2022
Correct the size of certain types on espidf platform

This was initially discovered in esp-rs/rust#92, the reason stat fails on the esp-idf platform is because the `stat` struct has a different layout on the Rust side compared to the C side.
bors added a commit to rust-lang/libc that referenced this issue Mar 11, 2022
Correct the size of certain types on espidf platform

This was initially discovered in esp-rs/rust#92, the reason stat fails on the esp-idf platform is because the `stat` struct has a different layout on the Rust side compared to the C side.
bors added a commit to rust-lang/libc that referenced this issue Mar 12, 2022
Correct the size of certain types on espidf platform

This was initially discovered in esp-rs/rust#92, the reason stat fails on the esp-idf platform is because the `stat` struct has a different layout on the Rust side compared to the C side.
@MabezDev
Copy link
Member

This has now been fixed in the esp-rs/rust 1.59.0.0.1 branch, upstream PR is pending.

Compiler builds are being worked on but you are welcome to build the compiler yourself if you want to test this out!

Thanks for your patience :)

Repository owner moved this from In Progress to Done in esp-rs Mar 14, 2022
@bugadani
Copy link
Author

bugadani commented Mar 14, 2022

Thanks a lot, I'll give it a try as soon as I can :)
Edit: actually, it looks like I've undeveloped the code that used this... Well, thanks anyway for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

3 participants