[refactoring] Introduce ISys.file_exists

Summary:
This diff introduces [ISys.file_exists] that is similar to [Sys.file_exists_exn], but returns
[false] when the result is known, instead of raising an exception.

Reviewed By: jvillard

Differential Revision: D28059863

fbshipit-source-id: d54851cfb
master
Sungkeun Cho 4 years ago committed by Facebook GitHub Bot
parent 3f053e389c
commit 0a48a6cba0

@ -2617,10 +2617,8 @@ and (_ : bool ref) =
let inferconfig_dir = let inferconfig_dir =
let rec find dir = let rec find dir =
match Sys.file_exists ~follow_symlinks:false (dir ^/ CommandDoc.inferconfig_file) with if ISys.file_exists ~follow_symlinks:false (dir ^/ CommandDoc.inferconfig_file) then Some dir
| `Yes -> else
Some dir
| `No | `Unknown ->
let parent = Filename.dirname dir in let parent = Filename.dirname dir in
let is_root = String.equal dir parent in let is_root = String.equal dir parent in
if is_root then None else find parent if is_root then None else find parent

@ -7,7 +7,6 @@
*) *)
open! IStd open! IStd
open PolyVariantEqual
(** Database of analysis results *) (** Database of analysis results *)
@ -76,7 +75,7 @@ let filename_from_string s = s
let filename_add_suffix fn s = fn ^ s let filename_add_suffix fn s = fn ^ s
let file_exists path = Sys.file_exists path = `Yes let file_exists path = ISys.file_exists path
(** Return the time when a file was last modified. The file must exist. *) (** Return the time when a file was last modified. The file must exist. *)
let file_modified_time ?(symlink = false) fname = let file_modified_time ?(symlink = false) fname =

@ -361,7 +361,7 @@ let setup_log_file () =
let logfile_path = let logfile_path =
ResultsDirEntryName.get_path ~results_dir:Config.toplevel_results_dir Logs ResultsDirEntryName.get_path ~results_dir:Config.toplevel_results_dir Logs
in in
let preexisting_logfile = PolyVariantEqual.( = ) (Sys.file_exists logfile_path) `Yes in let preexisting_logfile = ISys.file_exists logfile_path in
let chan = Stdlib.open_out_gen [Open_append; Open_creat] 0o666 logfile_path in let chan = Stdlib.open_out_gen [Open_append; Open_creat] 0o666 logfile_path in
let file_fmt = let file_fmt =
let f = F.formatter_of_out_channel chan in let f = F.formatter_of_out_channel chan in

@ -229,7 +229,7 @@ let logger =
in in
let is_toplevel_process = CommandLineOption.is_originator && not !ProcessPoolState.in_child in let is_toplevel_process = CommandLineOption.is_originator && not !ProcessPoolState.in_child in
( if is_toplevel_process then ( if is_toplevel_process then
let preexisting_logfile = PolyVariantEqual.( = ) (Sys.file_exists log_file) `Yes in let preexisting_logfile = ISys.file_exists log_file in
if preexisting_logfile then Unix.unlink log_file ) ; if preexisting_logfile then Unix.unlink log_file ) ;
let out_channel = Stdlib.open_out_gen [Open_append; Open_creat] 0o666 log_file in let out_channel = Stdlib.open_out_gen [Open_append; Open_creat] 0o666 log_file in
let logger = F.formatter_of_out_channel out_channel in let logger = F.formatter_of_out_channel out_channel in

@ -44,7 +44,7 @@ module RunState = struct
Was it created using an older version of infer?" Config.results_dir err_msg) ) Was it created using an older version of infer?" Config.results_dir err_msg) )
msg msg
in in
if PolyVariantEqual.(Sys.file_exists state_file_path <> `Yes) then if not (ISys.file_exists state_file_path) then
error "save state not found: '%s' does not exist" state_file_path error "save state not found: '%s' does not exist" state_file_path
else else
match Atdgen_runtime.Util.Json.from_file Runstate_j.read_t state_file_path with match Atdgen_runtime.Util.Json.from_file Runstate_j.read_t state_file_path with

@ -6,7 +6,6 @@
*) *)
open! IStd open! IStd
open PolyVariantEqual
module L = Logging module L = Logging
type t = type t =
@ -169,7 +168,7 @@ let exists_cache = String.Table.create ~size:256 ()
let path_exists abs_path = let path_exists abs_path =
try String.Table.find_exn exists_cache abs_path try String.Table.find_exn exists_cache abs_path
with Not_found_s _ | Caml.Not_found -> with Not_found_s _ | Caml.Not_found ->
let result = Sys.file_exists abs_path = `Yes in let result = ISys.file_exists abs_path in
String.Table.set exists_cache ~key:abs_path ~data:result ; String.Table.set exists_cache ~key:abs_path ~data:result ;
result result

@ -447,7 +447,7 @@ let get_available_memory_MB () =
try Scanf.sscanf line "MemAvailable: %u kB" (fun mem_kB -> Some (mem_kB / 1024)) try Scanf.sscanf line "MemAvailable: %u kB" (fun mem_kB -> Some (mem_kB / 1024))
with Scanf.Scan_failure _ -> scan_for_expected_output in_channel ) with Scanf.Scan_failure _ -> scan_for_expected_output in_channel )
in in
if Sys.file_exists proc_meminfo <> `Yes then None if not (ISys.file_exists proc_meminfo) then None
else with_file_in proc_meminfo ~f:scan_for_expected_output else with_file_in proc_meminfo ~f:scan_for_expected_output

@ -37,10 +37,7 @@ let check_for_existing_file args =
Create empty file empty file and pass that to clang. This is to enable compilation to continue *) Create empty file empty file and pass that to clang. This is to enable compilation to continue *)
match List.hd rest with match List.hd rest with
| Some arg -> | Some arg ->
if if Str.string_match clang_ignore_regex arg 0 && not (ISys.file_exists arg) then (
Str.string_match clang_ignore_regex arg 0
&& PolyVariantEqual.(Sys.file_exists arg <> `Yes)
then (
Unix.mkdir_p (Filename.dirname arg) ; Unix.mkdir_p (Filename.dirname arg) ;
let file = Unix.openfile ~mode:[Unix.O_CREAT; Unix.O_RDONLY] arg in let file = Unix.openfile ~mode:[Unix.O_CREAT; Unix.O_RDONLY] arg in
Unix.close file ) Unix.close file )

@ -371,7 +371,7 @@ let inline_argument_files buck_args =
let expand_buck_arg buck_arg = let expand_buck_arg buck_arg =
if String.is_prefix ~prefix:"@" buck_arg then if String.is_prefix ~prefix:"@" buck_arg then
let file_name = String.chop_prefix_exn ~prefix:"@" buck_arg in let file_name = String.chop_prefix_exn ~prefix:"@" buck_arg in
if PolyVariantEqual.(Sys.file_exists file_name <> `Yes) then [buck_arg] if not (ISys.file_exists file_name) then [buck_arg]
(* Arguments that start with @ could mean something different than an arguments file in buck. *) (* Arguments that start with @ could mean something different than an arguments file in buck. *)
else else
let expanded_args = let expanded_args =

@ -48,10 +48,9 @@ let run_buck_build prog buck_build_args =
let dirname = Filename.dirname full_path in let dirname = Filename.dirname full_path in
let get_path results_dir = ResultsDirEntryName.get_path ~results_dir CaptureDependencies in let get_path results_dir = ResultsDirEntryName.get_path ~results_dir CaptureDependencies in
(* Buck can either give the full path to infer-deps.txt ... *) (* Buck can either give the full path to infer-deps.txt ... *)
if PolyVariantEqual.(Sys.file_exists (get_path dirname) = `Yes) then get_path dirname :: acc if ISys.file_exists (get_path dirname) then get_path dirname :: acc
(* ... or a folder which contains infer-deps.txt *) (* ... or a folder which contains infer-deps.txt *)
else if PolyVariantEqual.(Sys.file_exists (get_path full_path) = `Yes) then else if ISys.file_exists (get_path full_path) then get_path full_path :: acc
get_path full_path :: acc
else acc else acc
| _ -> | _ ->
L.internal_error "Couldn't parse buck target output: %s@\n" line ; L.internal_error "Couldn't parse buck target output: %s@\n" line ;
@ -69,9 +68,7 @@ let merge_deps_files depsfiles =
Utils.fold_folders ~init:[] ~path:buck_out ~f:(fun acc dir -> Utils.fold_folders ~init:[] ~path:buck_out ~f:(fun acc dir ->
if if
String.is_substring dir ~substring:"infer-out" String.is_substring dir ~substring:"infer-out"
&& PolyVariantEqual.( && ISys.file_exists (ResultsDirEntryName.get_path ~results_dir:dir CaptureDB)
Sys.file_exists (ResultsDirEntryName.get_path ~results_dir:dir CaptureDB)
= `Yes)
then Printf.sprintf "\t\t%s" dir :: acc then Printf.sprintf "\t\t%s" dir :: acc
else acc ) else acc )
in in

@ -93,21 +93,17 @@ let expand_target acc (target, target_path) =
let expand_dir acc (target, target_path) = let expand_dir acc (target, target_path) =
(* invariant: [target_path] is absolute *) (* invariant: [target_path] is absolute *)
let db_file = ResultsDirEntryName.get_path ~results_dir:target_path CaptureDB in let db_file = ResultsDirEntryName.get_path ~results_dir:target_path CaptureDB in
match Sys.file_exists db_file with if ISys.file_exists db_file then
| `Yes ->
(* there is a capture DB at this path, so terminate expansion and generate deps line *) (* there is a capture DB at this path, so terminate expansion and generate deps line *)
let line = Printf.sprintf "%s\t-\t%s" target target_path in let line = Printf.sprintf "%s\t-\t%s" target target_path in
line :: acc line :: acc
| `No | `Unknown -> ( else
(* no capture DB was found, so look for, and inline, an [infer-deps.txt] file *) (* no capture DB was found, so look for, and inline, an [infer-deps.txt] file *)
let infer_deps = let infer_deps = ResultsDirEntryName.get_path ~results_dir:target_path CaptureDependencies in
ResultsDirEntryName.get_path ~results_dir:target_path CaptureDependencies if ISys.file_exists infer_deps then
in
match Sys.file_exists infer_deps with
| `Yes ->
Utils.with_file_in infer_deps Utils.with_file_in infer_deps
~f:(In_channel.fold_lines ~init:acc ~f:(fun acc line -> line :: acc)) ~f:(In_channel.fold_lines ~init:acc ~f:(fun acc line -> line :: acc))
| `No | `Unknown -> else (
L.internal_error "No capture DB or infer-deps file in %s@." target_path ; L.internal_error "No capture DB or infer-deps file in %s@." target_path ;
acc ) acc )
in in

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*) *)
open! IStd open! IStd
open PolyVariantEqual
(** entry points for top-level functionalities such as capture, analysis, and reporting *) (** entry points for top-level functionalities such as capture, analysis, and reporting *)
@ -85,7 +84,7 @@ let reset_duplicates_file () =
let create () = let create () =
Unix.close (Unix.openfile ~perm:0o0666 ~mode:[Unix.O_CREAT; Unix.O_WRONLY] start) Unix.close (Unix.openfile ~perm:0o0666 ~mode:[Unix.O_CREAT; Unix.O_WRONLY] start)
in in
if Sys.file_exists start = `Yes then delete () ; if ISys.file_exists start then delete () ;
create () create ()

@ -18,7 +18,7 @@ type fold_state = {files: string list; opts: string list; opt_st: string list; f
(* see GradleTest.ml *) (* see GradleTest.ml *)
let parse_gradle_line ~line = let parse_gradle_line ~line =
let concat_st lst st = if List.is_empty st then lst else String.concat ~sep:" " st :: lst in let concat_st lst st = if List.is_empty st then lst else String.concat ~sep:" " st :: lst in
let file_exist file = PolyVariantEqual.(Sys.file_exists file = `Yes) in let file_exist file = ISys.file_exists file in
let rev_args = line |> String.strip |> String.split ~on:' ' |> List.rev in let rev_args = line |> String.strip |> String.split ~on:' ' |> List.rev in
let res = let res =
List.fold rev_args ~init:{files= []; opts= []; opt_st= []; file_st= []} List.fold rev_args ~init:{files= []; opts= []; opt_st= []; file_st= []}

@ -0,0 +1,11 @@
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
open! IStd
let file_exists ?follow_symlinks path =
match Sys.file_exists ?follow_symlinks path with `Yes -> true | `No | `Unknown -> false

@ -0,0 +1,12 @@
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
open! IStd
val file_exists : ?follow_symlinks:bool -> string -> bool
(** Similar to [Sys.file_exists_exn], but it returns [false] when the result is unknown, instead of
raising an exception. [follow_symlinks] is true by default. *)

@ -15,12 +15,10 @@ let split_classpath = String.split ~on:JFile.sep
let classpath_of_paths paths = let classpath_of_paths paths =
let of_path path = let of_path path =
let full_path = Utils.filename_to_absolute ~root:Config.project_root path in let full_path = Utils.filename_to_absolute ~root:Config.project_root path in
match Sys.file_exists full_path with if ISys.file_exists full_path then Some full_path
| `Yes -> else (
Some full_path
| _ ->
L.debug Capture Medium "Path %s not found" full_path ; L.debug Capture Medium "Path %s not found" full_path ;
None None )
in in
let string_sep = Char.to_string JFile.sep in let string_sep = Char.to_string JFile.sep in
List.filter_map paths ~f:of_path |> String.concat ~sep:string_sep List.filter_map paths ~f:of_path |> String.concat ~sep:string_sep

@ -7,7 +7,6 @@
*) *)
open! IStd open! IStd
open PolyVariantEqual
open Javalib_pack open Javalib_pack
open Sawja_pack open Sawja_pack
module L = Logging module L = Logging
@ -124,7 +123,7 @@ let cache_classname cn =
split [] (Filename.dirname path) split [] (Filename.dirname path)
in in
let rec mkdir l p = let rec mkdir l p =
let () = if Sys.file_exists p <> `Yes then Unix.mkdir p ~perm:493 in let () = if not (ISys.file_exists p) then Unix.mkdir p ~perm:493 in
match l with [] -> () | d :: tl -> mkdir tl (Filename.concat p d) match l with [] -> () | d :: tl -> mkdir tl (Filename.concat p d)
in in
mkdir splitted_root_dir Filename.dir_sep ; mkdir splitted_root_dir Filename.dir_sep ;
@ -133,7 +132,7 @@ let cache_classname cn =
Out_channel.close file_out Out_channel.close file_out
let is_classname_cached cn = Sys.file_exists (path_of_cached_classname cn) = `Yes let is_classname_cached cn = ISys.file_exists (path_of_cached_classname cn)
let test_source_file_location source_file program cn node = let test_source_file_location source_file program cn node =
let is_synthetic = function let is_synthetic = function

@ -7,7 +7,6 @@
*) *)
open! IStd open! IStd
open PolyVariantEqual
open Javalib_pack open Javalib_pack
module L = Logging module L = Logging
@ -111,7 +110,7 @@ let do_all_files sources program =
(* loads the source files and translates them *) (* loads the source files and translates them *)
let main load_sources_and_classes = let main load_sources_and_classes =
( match (Config.biabduction_models_mode, Sys.file_exists Config.biabduction_models_jar = `Yes) with ( match (Config.biabduction_models_mode, ISys.file_exists Config.biabduction_models_jar) with
| true, false -> | true, false ->
() ()
| false, false -> | false, false ->

@ -34,7 +34,7 @@ let collect_specs_filenames jar_filename =
let load_models ~jar_filename = let load_models ~jar_filename =
match !models_jar with match !models_jar with
| None when match Sys.file_exists jar_filename with `Yes -> false | _ -> true -> | None when not (ISys.file_exists jar_filename) ->
L.die InternalError "Java model file not found@." L.die InternalError "Java model file not found@."
| None -> | None ->
models_jar := Some jar_filename ; models_jar := Some jar_filename ;

Loading…
Cancel
Save