alexcrichton / tar-rs

Tar file reading/writing for Rust
https://docs.rs/tar
Apache License 2.0
625 stars 184 forks source link

Error: "failed to write entire file" in .unpack() #306

Open ClementNerma opened 1 year ago

ClementNerma commented 1 year ago

I keep getting failed to write entire file errors when calling the .unpack() and .unpack_in() methods from Entry.

I saw that the destination file was correctly being created and filled with 4096 bytes of data, then the error appears.

I have no idea why this happens - could you please enlighten me?

Thanks in advance for your help!

NobodyXu commented 1 year ago

I saw that the destination file was correctly being created and filled with 4096 bytes of data, then the error appears.

Maybe there's some problem with the system you are running this on? Perhaps it runs out of the space? Or the admin has set an artifical limit?

ClementNerma commented 1 year ago

No as I'm able to extract the tarball with the tar command.

ClementNerma commented 1 year ago

I tried to copy the Entry's content to a buffer directly, and saw that zero bytes had been copied. So that's not a filesystem issue at all, even in memory the problem appears. And the 4096 allocated bytes on disk seem to be when the file was created, before any data was actually written to it.

ClementNerma commented 1 year ago

Ok so after a bit more testing it seems like the problem comes from the fact I iter over all .entries() values and store them in a Vec. From then, when I try to unpack one entry (even the first one), it fails for whatever reason.

If I don't store these entries in a vec and instead directly extract them one by one it works perfectly fine.

What could be causing this behaviour?

NobodyXu commented 1 year ago

That's because tar is a streaming format. When you iterate over them, tar::Archive calls the underlying Reader which implements Read to read the data, which changes the cursor.

ClementNerma commented 1 year ago

I see! I feel like this isn't explicited enough in the documentation, I didn't understand that at all. Maybe this could added in the docs of Entries and/or Entry to avoid other peoples such problems :) ?

NobodyXu commented 1 year ago

I see! I feel like this isn't explicited enough in the documentation, I didn't understand that at all. Maybe this could added in the docs of Entries and/or Entry to avoid other peoples such problems :) ?

That will certainly help. I remember similar things happen in zip-rs/zip where Entries of zip are stored. Since they both refers to the same underlying File, using one invalidates others.