57 |
let rec safe_mkdir dir = |
let rec safe_mkdir dir = |
58 |
if Sys.file_exists dir then begin |
if Sys.file_exists dir then begin |
59 |
if not (is_directory dir) then |
if not (is_directory dir) then |
60 |
failwith (Printf.sprintf "%s not a directory" dir) |
failwith (Printf.sprintf "%s already exists but is not a directory" dir) |
61 |
end |
end |
62 |
else |
else |
63 |
if is_link dir then |
if is_link dir then |
64 |
failwith (Printf.sprintf "%s is an orphan symbolic link" dir) |
begin try |
65 |
|
let dir = opendir dir in () |
66 |
|
with |
67 |
|
Unix.Unix_error (EACCES, _, _) -> failwith (Printf.sprintf "access denied for directory %s" dir) |
68 |
|
| Unix.Unix_error (ENOENT, _, _) -> failwith (Printf.sprintf "directory %s not found, orphaned link?" dir) |
69 |
|
| e -> failwith (Printf.sprintf "error %s for directory %s" (Printexc2.to_string e) dir) |
70 |
|
end |
71 |
else begin |
else begin |
72 |
let predir = Filename.dirname dir in |
let predir = Filename.dirname dir in |
73 |
if predir <> dir then safe_mkdir predir; |
if predir <> dir then safe_mkdir predir; |
140 |
Sys.remove filename |
Sys.remove filename |
141 |
) files; |
) files; |
142 |
Unix.rmdir dirname |
Unix.rmdir dirname |
143 |
|
|
144 |
|
let random () = |
145 |
|
let s = String.create 7 in |
146 |
|
for i = 0 to 6 do |
147 |
|
s.[i] <- char_of_int (97 + Random.int 26) |
148 |
|
done; |
149 |
|
s |
150 |
|
|
151 |
|
let can_write_to_directory dirname = |
152 |
|
let temp_file = Filename.concat dirname "tmp_" ^ random () ^ "_mld.tmp" in |
153 |
|
try |
154 |
|
(let oc = open_out_gen [Open_creat; Open_wronly; Open_append] 0o600 temp_file in |
155 |
|
output_string oc "accesstest"; |
156 |
|
close_out oc); |
157 |
|
(try Sys.remove temp_file with _ -> ()) |
158 |
|
with |
159 |
|
Sys_error s when s = temp_file ^ ": " ^ (Unix.error_message Unix.EACCES) -> |
160 |
|
failwith (Printf.sprintf "can not create files in directory %s, check rights..." dirname) |
161 |
|
| Sys_error s -> failwith (Printf.sprintf "%s for directory %s" s dirname) |
162 |
|
| e -> failwith (Printf.sprintf "%s for directory %s" (Printexc2.to_string e) dirname) |