Closed Qqwy closed 2 years ago
After trying running the code you've pasted above, I think the problem is that you're using cfb::open()
, which opens the file in read-only mode (to match the behavior of std::fs::File::open()
), rather than cfb::open_rw()
, which opens the file in read-write mode. So if the temp file already exists, your code will open the existing file in read-only mode, then fail with a "Bad file descriptor" error when it tries to update it.
Once I replace cfb::open()
with cfb::open_rw()
in your example code, it seems to work fine for me.
It's unfortunate that there's no type-level distinction between read-only and writable files—a read-only std::fs::File
still supports the std::io::Write
trait—since it means there's no compile-time error here.
Given the following simple example:
Expected result:
Upon running the code for the first time, I expect the file
/tmp/example.cfbfile
to be created, containing one stream named"one"
, whose contents is the byte sequence[42, 43, 44, 45]
. Upon running it one more time, I expect it to open the same file, and append to the existing stream. Its contents should now be[42, 43, 44, 45, 42, 43, 44, 45]
When running the program more times, this appending should continue.
Actual result
A panic on the
stream.seek(SeekFrom::Start(0)).unwrap();
line.To be exact:
This panic occurs on the code I had added to debug what was going on. When commenting the lines re-reading the stream after writing to it, the panic is prevented but the problem I originally encountered is visible: The stream will not get the new data appended to it. Instead, it seems to have its data replaced.
What is going wrong here? Am I making a mistake in using the library, or is there some internal problem?