-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
the write_at
function from std::os::unix::prelude::FileExt
doesn't work as described
#113627
Comments
Wow those docs are terrible, how did that happen? We can immediately patch the docs to code that actually would work to write to a file, but we need to define what "extended appropriately" means. i.e. Is this intended behavior? But perhaps more importantly, we need to figure out why the hell we have doctest examples that don't actually work and what can we do to fix that? |
Yeah it's weird that we have a non-working example. That should be fixed asap. It looks like it was copy/pasted from I'll mark this as help wanted. Fixing that should not be controversial. |
The issue with the final code in the OP is that it opens the file in append only mode. The docs do not cover this case at all so I can't say what the intended behaviour is. I guess |
( for the doc fix part, anyways. ) |
It does explain that returning short writes is valid, so I guess the behavior exhibited in the append-only case presented here is technically acceptable behavior. 🫠 |
On Linux,
|
There was a kernel proposal to fix this via a flag on |
We discussed this in the libs-api meeting. It seems that there are 2 actionable items here, both in the documentation:
|
@rustbot claim |
fix docs & example for `std::os::unix::prelude::FileExt::write_at` Changelog: * used `File::create` instead of `File::read` to get a writeable file * explicity mentioned the bug with `pwrite64` in docs Unfortunately, I don't think that there is really much we can do about this since the feature has already been stabilised. We could potentially add a clippy lint warning people on Linux that using `write_at` with the `O_APPEND` flag does not exhibit the behaviour that they would have assumed. fixes rust-lang#113627
fix docs & example for `std::os::unix::prelude::FileExt::write_at` Changelog: * used `File::create` instead of `File::read` to get a writeable file * explicity mentioned the bug with `pwrite64` in docs Unfortunately, I don't think that there is really much we can do about this since the feature has already been stabilised. We could potentially add a clippy lint warning people on Linux that using `write_at` with the `O_APPEND` flag does not exhibit the behaviour that they would have assumed. fixes rust-lang#113627
I'm pretty new to rust and I'm working on a small project where I need to write into a file some data at various offsets, so the
write_at
function seems to perfectly fit my need.So I tried this code (with is the provided example):
and I do
cargo run
I expected to see this happen: a
foo.txt
in the current directory with a size of 15 bytesInstead, this happened: Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
So, I modifed the filename to
/tmp/foo.txt
(in order to have an absolute path).I got the same error.
So, I created the file with
touch /tmp/foo.txt
Then I got the error:
Error: Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }
So, I add the line
println!("{:?}", file);
to get some info on the fileThen I got:
File { fd: 3, path: "/tmp/foo.txt", read: true, write: false }
So, the file is open read-only, I searched and modified the code to:
let file = File::options().append(true).open("/tmp/foo.txt")?;
Then I got:
File { fd: 3, path: "/tmp/foo.txt", read: false, write: true }
and finally no error (on stdout),But the file is only 5-byte long whereas I expected it to be 15-byte long.
And checking
foo.txt
content withod -t x1 -a /tmp/foo.txt
gives:The final code is
and you need to manually create the file before running the binary.
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: