You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust代码在Alpine Linux Docker中遇创建时间不可用错误的原因与解决

问题:Rust代码获取最新目录在Alpine Docker中因创建时间不支持报错

我使用以下Rust代码获取/opt目录下最新创建的子目录名称:

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环境时出现如下panic:

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中正常运行这段代码?


解决方案

  1. 问题根源
    Alpine Linux默认使用musl libc,而musl目前不支持获取文件的创建时间(std::fs::Metadata::created()方法),这是平台层面的限制,并非Docker配置缺失。macOS基于BSD文件系统,原生支持创建时间,因此代码能正常运行。

  2. 可行修复方案

    • 替换为修改时间(推荐):Linux系统普遍支持文件修改时间(modified()),这是更通用的时间维度。只需将代码中的created()替换为modified()即可适配Alpine环境:
      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().modified().unwrap()); // 替换为modified()
      
          match latest_directory {
              Some(directory) => {
                  let name = directory.file_name().unwrap_or_default().to_str().unwrap();
                  print!("{}", name);
              }
              None => {}
          }
      }
      
    • 切换到glibc镜像:如果业务逻辑必须依赖创建时间,可以将Docker镜像改为基于glibc的发行版(如debian、ubuntu),这些系统的libc支持created()方法。
  3. 代码健壮性优化
    建议移除代码中的所有unwrap()调用,改用match或if let处理可能的错误,避免因意外情况导致panic。例如:

use std::fs;
use std::io;

fn main() -> io::Result<()> {
    let subdirectories = fs::read_dir("/opt")?
        .filter_map(Result::ok)
        .filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
        .map(|entry| entry.path())
        .collect::<Vec<_>>();
    
    let latest_directory = subdirectories.iter()
        .max_by_key(|&dir| dir.metadata().and_then(|m| m.modified()).unwrap_or_default());

    if let Some(directory) = latest_directory {
        if let Some(name_os) = directory.file_name() {
            if let Some(name) = name_os.to_str() {
                print!("{}", name);
            }
        }
    }

    Ok(())
}

内容的提问来源于stack exchange,提问作者Dolphin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:46:03