Skip to content
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

Windows ctime should be set to mtime if mtime < ctime #92

Open
Luro02 opened this issue Jan 3, 2023 · 0 comments
Open

Windows ctime should be set to mtime if mtime < ctime #92

Luro02 opened this issue Jan 3, 2023 · 0 comments

Comments

@Luro02
Copy link

Luro02 commented Jan 3, 2023

The code passes null pointer for the creation time, so it is not changed. (see documentation of SetFileTime)

filetime/src/windows.rs

Lines 34 to 54 in 1df0704

pub fn set_file_handle_times(
f: &File,
atime: Option<FileTime>,
mtime: Option<FileTime>,
) -> io::Result<()> {
let atime = atime.map(to_filetime);
let mtime = mtime.map(to_filetime);
return unsafe {
let ret = SetFileTime(
f.as_raw_handle() as HANDLE,
ptr::null(),
atime
.as_ref()
.map(|p| p as *const FILETIME)
.unwrap_or(ptr::null()),
mtime
.as_ref()
.map(|p| p as *const FILETIME)
.unwrap_or(ptr::null()),
);
if ret != 0 {

With this, it is possible to change modified time to a time earlier than the created time, which does not make sense:

//# filetime = "*"

use std::fs;
use std::io;
use std::path::Path;
use std::time::Duration;

use filetime::FileTime;

fn run() -> io::Result<()> {
    let filepath = Path::new("test.abc123");

    // create the example file if it does not exist:
    fs::write(filepath, "hello world")?;

    // query the metadata:
    let metadata = fs::metadata(filepath)?;

    let ctime = FileTime::from(metadata.created()?);
    let mtime = FileTime::from(metadata.modified()?);
    let atime = FileTime::from(metadata.accessed()?);

    println!("ctime: {}, mtime: {}, atime: {}", ctime, mtime, atime,);

    // note: as expected the created time will be the same or
    //       earlier than the last modification time:
    assert_eq!(ctime <= mtime, true);

    let new_time = FileTime::from(metadata.modified()? - Duration::from_secs(60 * 60 * 24));

    filetime::set_file_times(filepath, new_time, new_time)?;

    let metadata = fs::metadata(filepath)?;

    let ctime = FileTime::from(metadata.created()?);
    let mtime = FileTime::from(metadata.modified()?);
    let atime = FileTime::from(metadata.accessed()?);

    println!("ctime: {}, mtime: {}, atime: {}", ctime, mtime, atime,);

    // note: the modified time is now earlier than the created
    //       time, which should not be possible:
    assert_eq!(mtime < ctime, true);

    fs::remove_file(filepath)?;

    Ok(())
}

fn main() {
    run().expect("failed to execute");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant