Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining a canonical file path

Tags:

filepath

ocaml

In OCaml, how can I obtain a canonical file path, equivalent to Java's File.getCanonicalPath() (that is, a normalized absolute path)?

Module Filename has a dirname function, but it does not normalize file paths such as /etc/../usr into /usr, for instance.

like image 430
anol Avatar asked Nov 22 '25 07:11

anol


2 Answers

JaneStreet Ocaml Core has Filename.realpath.

Also, google for ocaml realpath (see realpath(3) in C; you might code some wrapper for it)

like image 180
Basile Starynkevitch Avatar answered Nov 24 '25 22:11

Basile Starynkevitch


Our phat library defines normalized rather precisely. The library is overkill for most use cases, but we wrote it for applications that deal heavily with the file system. We'll release to opam soon, but for now you'll have to do a couple of pins. Here is a full working example:

$ 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"
like image 31
Ashish Agarwal Avatar answered Nov 24 '25 22:11

Ashish Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!