Skip to content

Commit

Permalink
OpamSystem.real_path: Remove the double chdir trick on OCaml >= 4.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kit-ty-kate committed Dec 13, 2021
1 parent 5305e15 commit 764187c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
19 changes: 18 additions & 1 deletion src/core/opamCompat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 3 additions & 17 deletions src/core/opamSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 764187c

Please sign in to comment.