获取规范文件路径

Obtaining a canonical file path

在OCaml中,如何获得一个canonical file path,相当于Java的File.getCanonicalPath()(即规范化的绝对​​路径)?

模块 Filename 具有 dirname 功能,但它不会将 /etc/../usr 等文件路径规范化为 /usr,例如。

JaneStreet Ocaml 核心有 Filename.realpath

此外,google 用于 ocaml realpath(参见 C 中的 realpath(3);您可以为其编写一些包装程序)

我们的 phat 库定义 normalized 相当准确。对于大多数用例来说,该库有点过分了,但我们为大量处理文件系统的应用程序编写了它。我们很快就会发布到 opam,但现在你必须做几个别针。这是一个完整的工作示例:

$ opam pin add solvuu_build https://github.com/solvuu/solvuu_build
$ opam pin add phat https://github.com/solvuu/phat.git
$ utop
# #require "phat.pure";;
# module Phat = Phat_pure.Std;;
# Phat.(abs_dir "/a/../b/./c/d/" |> ok_exn |> normalize |> to_string);;
- : string = "/b/c/d"