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 F = Format
module L = Logging module L = Logging
module Unix = Core.Std.Unix
module In_channel = Core.Std.In_channel
(** {2 Source Dirs} *) (** {2 Source Dirs} *)
@ -96,16 +98,7 @@ let filename_create_dir fname =
then create_dir dirname then create_dir dirname
let read_whole_file fd = let read_whole_file fd =
let stats = Unix.fstat fd in In_channel.input_all (Unix.in_channel_of_descr fd)
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
(** Update the file contents with the update function provided. (** Update the file contents with the update function provided.
If the directory does not exist, it is created. 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. *) A lock is used to allow write attempts in parallel. *)
let update_file_with_lock dir fname update = let update_file_with_lock dir fname update =
let reset_file fd = let reset_file fd =
let n = Unix.lseek fd 0 Unix.SEEK_SET in let n = Unix.lseek fd 0L ~mode:Unix.SEEK_SET in
if n <> 0 then if n <> 0L then
begin begin
L.stderr "reset_file: lseek fail@."; L.stderr "reset_file: lseek fail@.";
assert false assert false
end in end in
create_dir dir; create_dir dir;
let path = Filename.concat dir fname in let path = Filename.concat dir fname in
let fd = Unix.openfile path [Unix.O_CREAT; Unix.O_SYNC; Unix.O_RDWR] 0o640 in let fd = Unix.openfile path ~mode:Unix.[O_CREAT; O_SYNC; O_RDWR] ~perm:0o640 in
Unix.lockf fd Unix.F_LOCK 0; Unix.lockf fd ~mode:Unix.F_LOCK ~len:0L;
let buf = read_whole_file fd in let buf = read_whole_file fd in
reset_file fd; reset_file fd;
let str = update buf in let str = update buf in
let i = Unix.write fd str 0 (Bytes.length str) in let i = Unix.write fd ~buf:str ~pos:0 ~len:(String.length str) in
if (i = (Bytes.length str)) if (i = String.length str) then (
then (Unix.lockf fd Unix.F_ULOCK 0; Unix.close fd) Unix.lockf fd ~mode:Unix.F_ULOCK ~len:0L;
else (L.err "@.save_with_lock: fail on path: %s@." path; Unix.close fd
assert false) ) 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. *) (** Read a file using a lock to allow write attempts in parallel. *)
let read_file_with_lock dir fname = let read_file_with_lock dir fname =
let path = Filename.concat dir fname in let path = Filename.concat dir fname in
try 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 try
Unix.lockf fd Unix.F_RLOCK 0; Unix.lockf fd ~mode:Unix.F_RLOCK ~len:0L;
let buf = read_whole_file fd in 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; Unix.close fd;
Some buf Some buf
with Unix.Unix_error _ -> with Unix.Unix_error _ ->
@ -212,7 +208,7 @@ module Results_dir = struct
| filename:: dir_path -> filename, dir_path | filename:: dir_path -> filename, dir_path
| [] -> raise (Failure "create_path") in | [] -> raise (Failure "create_path") in
let full_fname = Filename.concat (create dir_path) filename 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 end
(** origin of a analysis artifact: current results dir, a spec library, or models *) (** 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. *) This guarantees that it appears updated after start. *)
let mark_file_updated fname = let mark_file_updated fname =
let near_future = Unix.gettimeofday () +. 1. in 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]. *) (** Fold over all file paths recursively under [dir] which match [p]. *)
let fold_paths_matching ~dir ~p ~init ~f = 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 val find_source_dirs : unit -> source_dir list
(** Read a file using a lock to allow write attempts in parallel. *) (** 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. (** Update the file contents with the update function provided.
If the directory does not exist, it is created. 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. 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. *) 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) *) (** get the path of the global type environment (only used in Java) *)
val global_tenv_fname : filename val global_tenv_fname : filename

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

Loading…
Cancel
Save