From 764187ce43f4f2ae98eaa4b49826daed8721d63b Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 13 Dec 2021 20:01:30 +0000 Subject: [PATCH] OpamSystem.real_path: Remove the double chdir trick on OCaml >= 4.13.0 --- src/core/opamCompat.ml | 19 ++++++++++++++++++- src/core/opamSystem.ml | 20 +++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/core/opamCompat.ml b/src/core/opamCompat.ml index f664d89ae05..6fa5cdea452 100644 --- a/src/core/opamCompat.ml +++ b/src/core/opamCompat.ml @@ -44,13 +44,30 @@ end #endif module Unix = -#if OCAML_VERSION >= (4, 6, 0) +#if OCAML_VERSION >= (4, 13, 0) Unix #else struct include Unix + let realpath p = + (* Sets path to s and returns the old path *) + let getchdir s = + let p = + try Sys.getcwd () + with Sys_error _ -> + let p = OpamCoreConfig.(!r.log_dir) in + mkdir p; p + in + chdir s; + p + in + try getchdir (getchdir s) with File_not_found _ -> s + +#if OCAML_VERSION < (4, 06, 0) let map_file = Bigarray.Genarray.map_file +#endif + end #endif diff --git a/src/core/opamSystem.ml b/src/core/opamSystem.ml index df2c4dd3ac1..844b6dfdb19 100644 --- a/src/core/opamSystem.ml +++ b/src/core/opamSystem.ml @@ -317,26 +317,12 @@ let remove file = else remove_file file -(* Sets path to s and returns the old path *) -let getchdir s = - let p = - try Sys.getcwd () - with Sys_error _ -> - let p = OpamCoreConfig.(!r.log_dir) in - mkdir p; p - in - chdir s; - p - -let normalize s = - try getchdir (getchdir s) with File_not_found _ -> s - let real_path p = (* if Filename.is_relative p then *) match (try Some (Sys.is_directory p) with Sys_error _ -> None) with | None -> let rec resolve dir = - if Sys.file_exists dir then normalize dir else + if Sys.file_exists dir then Unix.realpath dir else let parent = Filename.dirname dir in if dir = parent then dir else Filename.concat (resolve parent) (Filename.basename dir) @@ -346,9 +332,9 @@ let real_path p = else p in resolve p - | Some true -> normalize p + | Some true -> Unix.realpath p | Some false -> - let dir = normalize (Filename.dirname p) in + let dir = Unix.realpath (Filename.dirname p) in match Filename.basename p with | "." -> dir | base -> dir / base