为什么我会收到“当前此平台上不提供创建时间”?那该怎么办呢?

问题描述 投票:0回答:1

我正在使用此代码来获取最新的目录名称:

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 => {
            
        }
    }
}

这在我的本地 macOS 上运行良好,但是当我部署此代码以在 Docker 中与 Alpine Linux 一起运行时,它显示错误:

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

我错过了什么吗?可以在 Docker 中运行此代码吗?

docker rust filesystems alpine-linux
1个回答
1
投票

POSIX 不要求 Unix 系统支持包括文件创建时间戳,仅支持上次访问(atime)、上次修改(mtime)和上次状态更改(ctime)的时间戳。一些 Unix 系统do使用通常称为“btime”(出生时间)的字段包含此信息。

但是,Linux 并未在

struct stat
中公开此值,并且并非所有文件系统都支持它。值得注意的是,ext4、xfs 和 btrfs 都是如此,它们是一些最常见的文件系统,但 Linux 支持多种当前不公开此信息的文件系统(并且可能支持也可能根本不支持),包括各种各种 FAT、NTFS 和 UDF。

可以使用

statx
系统调用,但显然 Rust 目前还不支持它,它是在 2016 年才添加的,这意味着仍然有操作系统可能不支持它(RHEL 7、例如,2014 年推出)。 glibc 和 musl 都支持它,但是 Alpine 使用的 musl 是 2020 年才添加的,这可能太新了,无法被 Rust 标准库依赖。

通常,获取最新文件或目录的最便携方式是修改时间。这几乎是普遍可用的,包括在 POSIX 中。访问时间可能不太有用,并且经常出于性能原因而被禁用。

© www.soinside.com 2019 - 2024. All rights reserved.