alexcrichton / filetime

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

Creation time on Linux is None #74

Closed olekhov closed 10 months ago

olekhov commented 2 years ago

Is this a bug? Or am I missing some configuration features?

use std::fs;
use filetime::FileTime;

fn main() {
    let metadata = fs::metadata("foo.txt").unwrap();
    let ctime = FileTime::from_creation_time(&metadata).unwrap();
    println!("{}", ctime);
}

This code panics as from_creation_time returns None.

fogti commented 2 years ago

please/{can you} post the output of stat foo.txt?

olekhov commented 2 years ago
$ cat foo.txt
Hello
$ stat foo.txt
  File: foo.txt
  Size: 6               Blocks: 8          IO Block: 4096   regular file
Device: 8,2     Inode: 58334882    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ basilio)   Gid: ( 1000/ basilio)
Access: 2021-12-13 13:54:01.202819851 +0300
Modify: 2021-12-13 13:54:01.202819851 +0300
Change: 2021-12-13 13:54:01.202819851 +0300
 Birth: 2021-12-13 13:54:01.202819851 +0300
$ cat src/main.rs
use std::fs;
use filetime::FileTime;

fn main() {
    let metadata = fs::metadata("foo.txt").unwrap();
    let ctime = FileTime::from_creation_time(&metadata).unwrap();
    println!("{}", ctime);
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/fttest`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:6:57
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ uname -a
Linux basilioe480 5.14.14-artix1-1 #1 SMP PREEMPT Thu, 21 Oct 2021 13:26:57 +0000 x86_64 GNU/Linux

file is located in home directory, this is ext4 filesystem on ordinary hdd (TOSHIBA HDWL110 1Tb)

fogti commented 2 years ago

oh ok, this appears to be unimplemented currently... https://github.com/alexcrichton/filetime/blob/bb4058bf5ef15476ac6c4a49e2bb08f7bf81dc90/src/unix/mod.rs#L83-L114

fogti commented 2 years ago

I suppose libc::statx could be used to fix this.

e-dong commented 1 year ago

You can try this: https://doc.rust-lang.org/nightly/std/fs/struct.Metadata.html#method.created

let metadata = fs::metadata("foo.txt").unwrap();
println!("{:?}", metadata);
assert!(FileTime::from_creation_time(&metadata) == None);
println!("{:?}", metadata.created());

I get

Metadata { file_type: FileType(FileType { mode: 33188 }), is_dir: false, is_file: true, permissions: Permissions(FilePermissions { mode: 33188 }), modified: Ok(SystemTime { tv_sec: 1694807941, tv_nsec: 195322506 }), accessed: Ok(SystemTime
{ tv_sec: 1694866182, tv_nsec: 841703386 }), created: Ok(SystemTime { tv_sec: 1694807941, tv_nsec: 195322506 }), .. }
Ok(SystemTime { tv_sec: 1694807941, tv_nsec: 195322506 })

stat foo.txt

[eric@ezekiel ~/creation-time-rust]$ stat foo.txt
  File: foo.txt
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: 259,4   Inode: 1318833     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    eric)   Gid: (  984/   users)
Access: 2023-09-16 08:09:42.841703386 -0400
Modify: 2023-09-15 15:59:01.195322506 -0400
Change: 2023-09-15 15:59:01.195322506 -0400
 Birth: 2023-09-15 15:59:01.195322506 -0400