Convert DB module to Core.Std.Unix

Reviewed By: cristianoc

Differential Revision: D4232429

fbshipit-source-id: ac6c20d
master
Josh Berdine 8 years ago committed by Facebook Github Bot
parent 57c37f93c9
commit 0467c9cde1

@ -14,6 +14,8 @@ open! Utils
module F = Format
module L = Logging
module Unix = Core.Std.Unix
module In_channel = Core.Std.In_channel
(** {2 Source Dirs} *)
@ -96,16 +98,7 @@ let filename_create_dir fname =
then create_dir dirname
let read_whole_file fd =
let stats = Unix.fstat fd in
let size = stats.Unix.st_size in
let buf = Bytes.create size in
let nread = Unix.read fd buf 0 size in
if nread != size then
begin
L.stderr "Error nread:%d size:%d@." nread size;
assert false
end;
buf
In_channel.input_all (Unix.in_channel_of_descr fd)
(** Update the file contents with the update function provided.
If the directory does not exist, it is created.
@ -113,34 +106,37 @@ let read_whole_file fd =
A lock is used to allow write attempts in parallel. *)
let update_file_with_lock dir fname update =
let reset_file fd =
let n = Unix.lseek fd 0 Unix.SEEK_SET in
if n <> 0 then
let n = Unix.lseek fd 0L ~mode:Unix.SEEK_SET in
if n <> 0L then
begin
L.stderr "reset_file: lseek fail@.";
assert false
end in
create_dir dir;
let path = Filename.concat dir fname in
let fd = Unix.openfile path [Unix.O_CREAT; Unix.O_SYNC; Unix.O_RDWR] 0o640 in
Unix.lockf fd Unix.F_LOCK 0;
let fd = Unix.openfile path ~mode:Unix.[O_CREAT; O_SYNC; O_RDWR] ~perm:0o640 in
Unix.lockf fd ~mode:Unix.F_LOCK ~len:0L;
let buf = read_whole_file fd in
reset_file fd;
let str = update buf in
let i = Unix.write fd str 0 (Bytes.length str) in
if (i = (Bytes.length str))
then (Unix.lockf fd Unix.F_ULOCK 0; Unix.close fd)
else (L.err "@.save_with_lock: fail on path: %s@." path;
assert false)
let i = Unix.write fd ~buf:str ~pos:0 ~len:(String.length str) in
if (i = String.length str) then (
Unix.lockf fd ~mode:Unix.F_ULOCK ~len:0L;
Unix.close fd
) else (
L.err "@.save_with_lock: fail on path: %s@." path;
assert false
)
(** Read a file using a lock to allow write attempts in parallel. *)
let read_file_with_lock dir fname =
let path = Filename.concat dir fname in
try
let fd = Unix.openfile path [Unix.O_RSYNC; Unix.O_RDONLY] 0o646 in
let fd = Unix.openfile path ~mode:Unix.[O_RSYNC; O_RDONLY] ~perm:0o646 in
try
Unix.lockf fd Unix.F_RLOCK 0;
Unix.lockf fd ~mode:Unix.F_RLOCK ~len:0L;
let buf = read_whole_file fd in
Unix.lockf fd Unix.F_ULOCK 0;
Unix.lockf fd ~mode:Unix.F_ULOCK ~len:0L;
Unix.close fd;
Some buf
with Unix.Unix_error _ ->
@ -212,7 +208,7 @@ module Results_dir = struct
| filename:: dir_path -> filename, dir_path
| [] -> raise (Failure "create_path") in
let full_fname = Filename.concat (create dir_path) filename in
Unix.openfile full_fname [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o777
Unix.openfile full_fname ~mode:Unix.[O_WRONLY; O_CREAT; O_TRUNC] ~perm:0o777
end
(** origin of a analysis artifact: current results dir, a spec library, or models *)
@ -246,7 +242,7 @@ let file_was_updated_after_start fname =
This guarantees that it appears updated after start. *)
let mark_file_updated fname =
let near_future = Unix.gettimeofday () +. 1. in
Unix.utimes fname near_future near_future
Unix.utimes fname ~access:near_future ~modif:near_future
(** Fold over all file paths recursively under [dir] which match [p]. *)
let fold_paths_matching ~dir ~p ~init ~f =

@ -99,13 +99,13 @@ val filename_create_dir : filename -> unit
val find_source_dirs : unit -> source_dir list
(** Read a file using a lock to allow write attempts in parallel. *)
val read_file_with_lock : string -> string -> bytes option
val read_file_with_lock : string -> string -> string option
(** Update the file contents with the update function provided.
If the directory does not exist, it is created.
If the file does not exist, it is created, and update is given the empty string.
A lock is used to allow write attempts in parallel. *)
val update_file_with_lock : string -> string -> (bytes -> bytes) -> unit
val update_file_with_lock : string -> string -> (string -> string) -> unit
(** get the path of the global type environment (only used in Java) *)
val global_tenv_fname : filename

@ -41,22 +41,22 @@ module Inference = struct
let update_count_str s_old =
let n =
if s_old = Bytes.empty then 0
else try int_of_string (Bytes.to_string s_old) with
if String.is_empty s_old then 0
else try int_of_string s_old with
| Failure _ ->
L.stderr "int_of_string %s@." (Bytes.to_string s_old);
L.stderr "int_of_string %s@." s_old;
assert false in
Bytes.of_string (string_of_int (n + 1))
string_of_int (n + 1)
let update_boolvec_str _s size index bval =
let s = if _s = Bytes.empty then Bytes.make size '0' else _s in
Bytes.set s index (if bval then '1' else '0');
let s = if String.is_empty _s then String.make size '0' else _s in
String.set s index (if bval then '1' else '0');
s
let mark_file update_str dir fname =
DB.update_file_with_lock dir fname update_str;
match DB.read_file_with_lock dir fname with
| Some buf -> L.stderr "Read %s: %s@." fname (Bytes.to_string buf)
| Some buf -> L.stderr "Read %s: %s@." fname buf
| None -> L.stderr "Read %s: None@." fname
let mark_file_count = mark_file update_count_str
@ -89,7 +89,7 @@ module Inference = struct
| None -> None
| Some buf ->
let boolvec = ref [] in
Bytes.iter (fun c -> boolvec := (c = '1') :: !boolvec) buf;
String.iter ~f:(fun c -> boolvec := (c = '1') :: !boolvec) buf;
Some (IList.rev !boolvec)
end (* Inference *)

Loading…
Cancel
Save