dockerrustfilesystemsalpine-linux

Why do I get "creation time is not available on this platform currently"? And what to do about it?


I am using this code to get the latest dir name:

use std::fs;

fn main() {
    let subdirectories = fs::read_dir("/opt")
        .unwrap()
        .filter_map(Result::ok)
        .filter(|entry| entry.file_type().unwrap().is_dir())
        .map(|entry| entry.path())
        .collect::<Vec<_>>();
    let latest_directory = subdirectories
        .iter()
        .max_by_key(|&dir| dir.metadata().unwrap().created().unwrap());

    match latest_directory {
        Some(directory) => {
            let name = directory.file_name().unwrap_or_default().to_str().unwrap();
            print!("{}", name);
        }
        None => {
            
        }
    }
}

This works fine on my local macOS, but when I deployment this code to run with Alpine Linux in Docker it shows the error:

thread 'actix-rt|system:0|arbiter:0' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Unsupported, message: "creation time is not available on this platform currently" }', src/service/project/project_service.rs:201:62

Am I missing something? Is it possible to run this code in Docker?


Solution

  • POSIX does not require a Unix system to support including file creation timestamps, only timestamps for last access (atime), last modification (mtime), and last status change (ctime). Some Unix systems do include this information using a field often referred to as "btime" (birth time).

    However, Linux does not expose this value in struct stat, and it is not supported on all file systems. Notably, ext4, xfs, and btrfs do, which are some of the most common file systems, but Linux supports a variety of file systems which do not expose this information currently (and which may or may not support it at all), including various varieties of FAT, NTFS, and UDF.

    It is possible to use the statx system call, but apparently Rust doesn't support that at this moment, and it was only added in 2016, which means there are still OSes which probably wouldn't support it (RHEL 7, for example, came out in 2014). Both glibc and musl have support for it, but musl, which is used by Alpine, only added it in 2020, which may be too new to be depended on by the Rust standard library.

    Typically, the most portable way to get the latest file or directory is the modification time. That's available nearly universally, including in POSIX. The access time is probably less useful and is often disabled for performance reasons.