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

@ -7,7 +7,6 @@
*)
open! IStd
open PolyVariantEqual
(** Database of analysis results *)
@ -76,7 +75,7 @@ let filename_from_string s = 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. *)
let file_modified_time ?(symlink = false) fname =

@ -361,7 +361,7 @@ let setup_log_file () =
let logfile_path =
ResultsDirEntryName.get_path ~results_dir:Config.toplevel_results_dir Logs
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 file_fmt =
let f = F.formatter_of_out_channel chan in

@ -229,7 +229,7 @@ let logger =
in
let is_toplevel_process = CommandLineOption.is_originator && not !ProcessPoolState.in_child in
( 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 ) ;
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

@ -44,7 +44,7 @@ module RunState = struct
Was it created using an older version of infer?" Config.results_dir err_msg) )
msg
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
else
match Atdgen_runtime.Util.Json.from_file Runstate_j.read_t state_file_path with

@ -6,7 +6,6 @@
*)
open! IStd
open PolyVariantEqual
module L = Logging
type t =
@ -169,7 +168,7 @@ let exists_cache = String.Table.create ~size:256 ()
let path_exists abs_path =
try String.Table.find_exn exists_cache abs_path
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 ;
result

@ -447,7 +447,7 @@ let get_available_memory_MB () =
try Scanf.sscanf line "MemAvailable: %u kB" (fun mem_kB -> Some (mem_kB / 1024))
with Scanf.Scan_failure _ -> scan_for_expected_output in_channel )
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

@ -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 *)
match List.hd rest with
| Some arg ->
if
Str.string_match clang_ignore_regex arg 0
&& PolyVariantEqual.(Sys.file_exists arg <> `Yes)
then (
if Str.string_match clang_ignore_regex arg 0 && not (ISys.file_exists arg) then (
Unix.mkdir_p (Filename.dirname arg) ;
let file = Unix.openfile ~mode:[Unix.O_CREAT; Unix.O_RDONLY] arg in
Unix.close file )

@ -371,7 +371,7 @@ let inline_argument_files buck_args =
let expand_buck_arg buck_arg =
if String.is_prefix ~prefix:"@" buck_arg then
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. *)
else
let expanded_args =

@ -48,10 +48,9 @@ let run_buck_build prog buck_build_args =
let dirname = Filename.dirname full_path in
let get_path results_dir = ResultsDirEntryName.get_path ~results_dir CaptureDependencies in
(* 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 *)
else if PolyVariantEqual.(Sys.file_exists (get_path full_path) = `Yes) then
get_path full_path :: acc
else if ISys.file_exists (get_path full_path) then get_path full_path :: acc
else acc
| _ ->
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 ->
if
String.is_substring dir ~substring:"infer-out"
&& PolyVariantEqual.(
Sys.file_exists (ResultsDirEntryName.get_path ~results_dir:dir CaptureDB)
= `Yes)
&& ISys.file_exists (ResultsDirEntryName.get_path ~results_dir:dir CaptureDB)
then Printf.sprintf "\t\t%s" dir :: acc
else acc )
in

@ -93,23 +93,19 @@ let expand_target acc (target, target_path) =
let expand_dir acc (target, target_path) =
(* invariant: [target_path] is absolute *)
let db_file = ResultsDirEntryName.get_path ~results_dir:target_path CaptureDB in
match Sys.file_exists db_file with
| `Yes ->
(* 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
line :: acc
| `No | `Unknown -> (
(* no capture DB was found, so look for, and inline, an [infer-deps.txt] file *)
let infer_deps =
ResultsDirEntryName.get_path ~results_dir:target_path CaptureDependencies
in
match Sys.file_exists infer_deps with
| `Yes ->
Utils.with_file_in infer_deps
~f:(In_channel.fold_lines ~init:acc ~f:(fun acc line -> line :: acc))
| `No | `Unknown ->
L.internal_error "No capture DB or infer-deps file in %s@." target_path ;
acc )
if ISys.file_exists db_file then
(* 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
line :: acc
else
(* no capture DB was found, so look for, and inline, an [infer-deps.txt] file *)
let infer_deps = ResultsDirEntryName.get_path ~results_dir:target_path CaptureDependencies in
if ISys.file_exists infer_deps then
Utils.with_file_in infer_deps
~f:(In_channel.fold_lines ~init:acc ~f:(fun acc line -> line :: acc))
else (
L.internal_error "No capture DB or infer-deps file in %s@." target_path ;
acc )
in
let target_path =
if Filename.is_absolute target_path then target_path else Config.project_root ^/ target_path

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

@ -18,7 +18,7 @@ type fold_state = {files: string list; opts: string list; opt_st: string list; f
(* see GradleTest.ml *)
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 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 res =
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 of_path path =
let full_path = Utils.filename_to_absolute ~root:Config.project_root path in
match Sys.file_exists full_path with
| `Yes ->
Some full_path
| _ ->
L.debug Capture Medium "Path %s not found" full_path ;
None
if ISys.file_exists full_path then Some full_path
else (
L.debug Capture Medium "Path %s not found" full_path ;
None )
in
let string_sep = Char.to_string JFile.sep in
List.filter_map paths ~f:of_path |> String.concat ~sep:string_sep

@ -7,7 +7,6 @@
*)
open! IStd
open PolyVariantEqual
open Javalib_pack
open Sawja_pack
module L = Logging
@ -124,7 +123,7 @@ let cache_classname cn =
split [] (Filename.dirname path)
in
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)
in
mkdir splitted_root_dir Filename.dir_sep ;
@ -133,7 +132,7 @@ let cache_classname cn =
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 is_synthetic = function

@ -7,7 +7,6 @@
*)
open! IStd
open PolyVariantEqual
open Javalib_pack
module L = Logging
@ -111,7 +110,7 @@ let do_all_files sources program =
(* loads the source files and translates them *)
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 ->
()
| false, false ->

@ -34,7 +34,7 @@ let collect_specs_filenames jar_filename =
let load_models ~jar_filename =
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@."
| None ->
models_jar := Some jar_filename ;

Loading…
Cancel
Save