vasi / positioned-io

Read and write from offsets in Rust.
MIT License
74 stars 18 forks source link

write_at to files on windows #9

Closed knokko closed 5 years ago

knokko commented 5 years ago

The write_at method on std::fs::File objects doesn't work on my laptop (Windows 10). I can not quickly test this on other operating systems.

Whenever I use the write_at method (and related methods like write_all_at) on files with only write-access, it will result in an Access denied (os error 5). When the file also has read-access, the write_at method will not give an error, but the call will not have any effect (the file will stay the same). Using sync_all or sync_data doesn't help.

To test this in a small scenario, I copied the write_all_at example code and modified it a little. (The try! appears to be no longer valid syntax and my foo.data is smaller than 2^20 bytes.)

extern crate positioned_io;
extern crate byteorder;

fn main(){
    help_main().unwrap();
}

fn help_main() -> Result<(), std::io::Error> {
    use positioned_io::WriteAt;
    use byteorder::{ByteOrder, LittleEndian};
    use std::fs::OpenOptions;

    // Put the integer in a buffer.
    let mut buf = vec![0; 4];
    LittleEndian::write_u32(&mut buf, 1234);

    // Write it to the file.
    let mut file = r#try!(OpenOptions::new().write(true).open("foo.data"));
    r#try!(file.write_all_at(1 << 4, &buf));
    Ok(())
}

and cargo.toml

[package]
name = "positioned"
version = "0.1.0"
authors = ["knokko <knokogator@hotmail.com>"]
edition = "2018"

[dependencies]
positioned-io = "0.2.2"
byteorder = "1.2"

Running this code will panic with Access denied (os error 5). using r#try!(OpenOptions::new().write(true).read(true).open("foo.data")); instead will not panic, but will not change foo.data either.

niklasf commented 5 years ago

Hi @knokko, I believe this may have been fixed in positioned-io 0.3. Unfortunately I currently can't reach the original maintainer who can publish it on crates.io. So I temporarily published it as positioned-io-preview.

Usage: https://github.com/vasi/positioned-io/tree/positioned-io-preview#preview-release

Updated documentation (some breaking changes, no longer recommends try!): https://docs.rs/positioned-io-preview

knokko commented 5 years ago

Hello @niklasf , this is indeed fixed in the preview. (Both the case with .write(true).read(true) and the case with only .write(true) . Thank you