Source file file_path_unix.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
open! Core
module Unix = Core_unix
include File_path_unix_intf
let executable_name = lazy (File_path.of_string Sys_unix.executable_name)
include struct
let read_file path = In_channel.read_all (File_path.to_string path)
let write_file path ~contents =
Out_channel.write_all (File_path.to_string path) ~data:contents
;;
let load_as_sexp path ~of_sexp =
Parsexp.Conv_single.parse_string_exn (read_file path) of_sexp
;;
let load_as_sexps path ~of_sexp =
Parsexp.Conv_many.parse_string_exn (read_file path) of_sexp
;;
let load_sexp path = load_as_sexp path ~of_sexp:Fn.id
let load_sexps path = load_as_sexps path ~of_sexp:Fn.id
let write_with_buffer path ~f =
let buf = Buffer.create 16 in
f buf;
write_file path ~contents:(Buffer.contents buf)
;;
let save_as_sexps path xs ~sexp_of =
write_with_buffer path ~f:(fun buf ->
List.iter xs ~f:(fun x ->
Sexp.to_buffer_hum (sexp_of x) ~buf;
Buffer.add_char buf '\n'))
;;
let save_as_sexp path x ~sexp_of = save_as_sexps path [ x ] ~sexp_of
let save_sexp path sexp = save_as_sexp path sexp ~sexp_of:Fn.id
let save_sexps path sexps = save_as_sexps path sexps ~sexp_of:Fn.id
end
include struct
let realpath_relative_to_cwd path =
File_path.Absolute.of_string (Filename_unix.realpath (File_path.to_string path))
;;
let realpath_absolute path = realpath_relative_to_cwd (File_path.of_absolute path)
let realpath path ~relative_to =
realpath_absolute (File_path.make_absolute path ~under:relative_to)
;;
end
include struct
let exists_exn path = Sys_unix.file_exists_exn (File_path.to_string path)
let is_directory_exn path = Sys_unix.is_directory_exn (File_path.to_string path)
let is_file_exn path = Sys_unix.is_file_exn (File_path.to_string path)
let exists path = Sys_unix.file_exists (File_path.to_string path)
let is_directory path = Sys_unix.is_directory (File_path.to_string path)
let is_file path = Sys_unix.is_file (File_path.to_string path)
let ls_dir path =
Sys_unix.ls_dir (File_path.to_string path)
|> List.map ~f:File_path.Part.of_string
|> List.sort ~compare:File_path.Part.compare
;;
end
include struct
let unlink path = Unix.unlink (File_path.to_string path)
let rename ~src ~dst =
Unix.rename ~src:(File_path.to_string src) ~dst:(File_path.to_string dst)
;;
let mkdir ?(parents = false) path =
if parents
then Unix.mkdir_p (File_path.to_string path)
else Unix.mkdir (File_path.to_string path)
;;
let rmdir path = Unix.rmdir (File_path.to_string path)
let chdir path = Unix.chdir (File_path.to_string path)
let getcwd () = Unix.getcwd () |> File_path.Absolute.of_string
end
include struct
let make_absolute_under_cwd path =
match File_path.to_variant path with
| Absolute abspath -> abspath
| Relative relpath -> File_path.Absolute.append (getcwd ()) relpath
;;
let make_relative_to_cwd path =
match File_path.to_relative path with
| Some _ as some -> some
| None -> File_path.make_relative path ~if_under:(getcwd ())
;;
let make_relative_to_cwd_exn path =
match File_path.to_relative path with
| Some relpath -> relpath
| None -> File_path.make_relative_exn path ~if_under:(getcwd ())
;;
let make_relative_to_cwd_if_possible path =
if File_path.is_relative path
then path
else File_path.make_relative_if_possible path ~if_under:(getcwd ())
;;
end