alexcrichton / filetime

Accessing file timestamps in a platform-agnostic fashion in Rust
Apache License 2.0
122 stars 56 forks source link

Support setting filetimes on open files #28

Closed kinnison closed 5 years ago

kinnison commented 6 years ago

Hi,

I realise this is a potentially difficult thing to get right across multiple platforms, but it'd be super-nice if the filetime crate supported setting the file times on open files. In code I am writing now, I have had to wrapper up futimens() for myself because I need to be able to re-stamp an open file from time to time to fit with the semantics of what is being worked with.

I ended up with:

mod platform {
    use libc::{futimens, timespec};
    use std::fs::File;
    use std::io::{Error, Result};
    use std::os::unix::io::AsRawFd;

    pub fn set_utime_now(fh: &File) -> Result<()> {
        let fd = fh.as_raw_fd();
        let rc = unsafe { futimens(fd, 0 as *const timespec) };
        if rc == 0 {
            Ok(())
        } else {
            Err(Error::last_os_error())
        }
    }
}

While this is pretty platform-specific and quite task-specific to myself, I would love to see a function (or trait I guess) in the filetime crate which was something along the lines of fn set_file_times(&File,Option<&FileTime>)

Is there any chance of this, or should I continue to use my hack for now?

Thanks,

Daniel.

alexcrichton commented 6 years ago

I'd definitely be up for this! I suspect windows has a way to do this as well

alexcrichton commented 5 years ago

Done in https://github.com/alexcrichton/filetime/pull/40

kinnison commented 5 years ago

Cool, thank you @alexcrichton and @rbtcollins