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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
open Odoc_utils
open StdLabels
open Or_error
type directory = Fpath.t
type file = Fpath.t
let mkdir_p dir =
let mkdir d =
try Unix.mkdir (Fpath.to_string d) 0o755 with
| Unix.Unix_error (Unix.EEXIST, _, _) -> ()
| exn -> raise exn
in
let rec dirs_to_create p acc =
if Sys.file_exists (Fpath.to_string p) then acc
else dirs_to_create (Fpath.parent p) (p :: acc)
in
List.iter (dirs_to_create (Fpath.normalize dir) []) ~f:mkdir
module File = struct
type t = file
let dirname = Fpath.parent
let basename = Fpath.base
let append = Fpath.append
let set_ext e p = Fpath.set_ext e p
let has_ext e p = Fpath.has_ext e p
let get_ext e = Fpath.get_ext e
let create ~directory ~name =
match Fpath.of_string name with
| Result.Error (`Msg e) -> invalid_arg ("Odoc.Fs.File.create: " ^ e)
| Result.Ok psuf -> Fpath.(normalize @@ (directory // psuf))
let to_string = Fpath.to_string
let segs = Fpath.segs
let of_string s =
match Fpath.of_string s with
| Result.Error (`Msg e) -> invalid_arg ("Odoc.Fs.File.of_string: " ^ e)
| Result.Ok p -> p
let read file =
let input_one_shot len ic =
let buf = Bytes.create len in
really_input ic buf 0 len;
close_in ic;
Result.Ok (Bytes.unsafe_to_string buf)
in
let input_stream file ic =
let bsize =
65536
in
let buf = Buffer.create bsize in
let rec loop () =
match Buffer.add_channel buf ic bsize with
| () -> loop ()
| exception End_of_file -> Result.Ok (Buffer.contents buf)
| exception Failure _ ->
Result.Error (`Msg (Printf.sprintf "%s: input too large" file))
in
loop ()
in
try
let file = Fpath.to_string file in
let with_ic k =
if file = "-" then k stdin else Io_utils.with_open_in_bin file k
in
with_ic @@ fun ic ->
match in_channel_length ic with
| 0 -> input_stream file ic
| len when len <= Sys.max_string_length -> input_one_shot len ic
| len ->
let err = Printf.sprintf "%s: file too large (%d bytes)" file len in
Result.Error (`Msg err)
with Sys_error e -> Result.Error (`Msg e)
let copy ~src ~dst =
try
Io_utils.with_open_in_bin (Fpath.to_string src) (fun ic ->
mkdir_p (dirname dst);
Io_utils.with_open_out_bin (Fpath.to_string dst) (fun oc ->
let len = 65536 in
let buf = Bytes.create len in
let rec loop () =
let read = input ic buf 0 len in
if read > 0 then (
output oc buf 0 read;
loop ())
in
Ok (loop ())))
with Sys_error e -> Result.Error (`Msg e)
let exists file = Sys.file_exists (Fpath.to_string file)
let rec of_segs_tl acc = function
| [] -> acc
| hd :: tl -> of_segs_tl (Fpath.( / ) acc hd) tl
let of_segs = function
| [] -> invalid_arg "Fs.File.of_segs"
| "" :: rest -> of_segs_tl (Fpath.v "/") rest
| first :: rest -> of_segs_tl (Fpath.v first) rest
let append_segs path segs = of_segs_tl path segs
module Table = Hashtbl.Make (struct
type nonrec t = t
let equal = Fpath.equal
let hash = Hashtbl.hash
end)
end
module Directory = struct
type t = directory
let dirname = Fpath.parent
let basename = Fpath.base
let append = Fpath.append
let make_path p name =
match Fpath.of_string name with
| Result.Error _ as e -> e
| Result.Ok psuf ->
Result.Ok Fpath.(normalize @@ to_dir_path @@ (p // psuf))
let reach_from ~dir path =
match make_path dir path with
| Result.Error (`Msg e) -> invalid_arg ("Odoc.Fs.Directory.create: " ^ e)
| Result.Ok path ->
let pstr = Fpath.to_string path in
if Sys.file_exists pstr && not (Sys.is_directory pstr) then
invalid_arg "Odoc.Fs.Directory.create: not a directory";
path
let contains ~parentdir f = Fpath.is_rooted ~root:parentdir f
let compare = Fpath.compare
let mkdir_p dir = mkdir_p dir
let to_string = Fpath.to_string
let to_fpath x = x
let of_string s =
match Fpath.of_string s with
| Result.Error (`Msg e) -> invalid_arg ("Odoc.Fs.Directory.of_string: " ^ e)
| Result.Ok p -> Fpath.to_dir_path p
let of_file f = Fpath.to_dir_path f
let fold_files_rec ?(ext = "") f acc d =
let fold_non_dirs ext f acc files =
let is_dir d = try Sys.is_directory d with Sys_error _ -> false in
let has_ext ext file = Filename.check_suffix file ext in
let dirs, files = List.partition ~f:is_dir files in
let files = List.find_all ~f:(has_ext ext) files in
let f acc fn = f acc (Fpath.v fn) in
(List.fold_left ~f ~init:acc files, dirs)
in
let rec loop ext f acc = function
| (d :: ds) :: up ->
let rdir d =
try Array.to_list (Sys.readdir d) with Sys_error _ -> []
in
let files = List.rev (List.rev_map ~f:(Filename.concat d) (rdir d)) in
let acc, dirs = fold_non_dirs ext f acc files in
loop ext f acc (dirs :: ds :: up)
| [] :: up -> loop ext f acc up
| [] -> acc
in
loop ext f acc [ [ Fpath.to_string d ] ]
exception Stop_iter of msg
let fold_files_rec_result ?ext f acc d =
let f acc fn =
match f acc fn with Ok acc -> acc | Error e -> raise (Stop_iter e)
in
try Ok (fold_files_rec ?ext f acc d)
with Stop_iter (`Msg _ as e) -> Error e
module Table = Hashtbl.Make (struct
type nonrec t = t
let equal = Fpath.equal
let hash = Hashtbl.hash
end)
end