Skip to content

Commit

Permalink
refactor: remove Path.is_directory
Browse files Browse the repository at this point in the history
Replaced with Path.stat.

Signed-off-by: Ali Caglayan <alizter@gmail.com>
  • Loading branch information
Alizter committed Aug 29, 2023
1 parent 3af76a8 commit 88f3127
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 77 deletions.
13 changes: 6 additions & 7 deletions bin/describe/describe_workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -630,15 +630,14 @@ let term : unit Term.t =
(Memo.List.map ~f:(fun dir ->
let p = Path.Source.(relative root) (Common.prefix_target common dir) in
let s = Path.source p in
if not @@ Path.exists s
then
match Path.stat s with
| Ok { Unix.st_kind = Unix.S_DIR; _ } -> Memo.return p
| Ok _ ->
User_error.raise
[ Pp.textf "No such file or directory: %s" (Path.to_string s) ];
if not @@ Path.is_directory s
then
[ Pp.textf "File exists, but is not a directory: %s" (Path.to_string s) ]
| Error _ ->
User_error.raise
[ Pp.textf "File exists, but is not a directory: %s" (Path.to_string s) ];
Memo.return p))
[ Pp.textf "No such file or directory: %s" (Path.to_string s) ]))
>>= Crawl.workspace options setup context
>>| Sanitize_for_tests.Workspace.sanitize context
>>| Descr.Workspace.to_dyn options
Expand Down
25 changes: 12 additions & 13 deletions bin/dune_init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,23 @@ module File = struct
let name = "dune" in
let full_path = Path.relative path name in
let content =
if not (Path.exists full_path)
then []
else if Path.is_directory full_path
then
match Path.stat full_path with
| Ok { Unix.st_kind = S_REG; _ } ->
(match Io.with_lexbuf_from_file ~f:Dune_lang.Format.parse full_path with
| Dune_lang.Format.Sexps content -> content
| Dune_lang.Format.OCaml_syntax _ ->
User_error.raise
[ Pp.textf
"Cannot load dune file %s because it uses OCaml syntax"
(Path.to_string_maybe_quoted full_path)
])
| Ok { Unix.st_kind = S_DIR; _ } ->
User_error.raise
[ Pp.textf
"\"%s\" already exists and is a directory"
(Path.to_absolute_filename full_path)
]
else (
match Io.with_lexbuf_from_file ~f:Dune_lang.Format.parse full_path with
| Dune_lang.Format.Sexps content -> content
| Dune_lang.Format.OCaml_syntax _ ->
User_error.raise
[ Pp.textf
"Cannot load dune file %s because it uses OCaml syntax"
(Path.to_string_maybe_quoted full_path)
])
| _ -> []
in
Dune { path; name; content }
;;
Expand Down
14 changes: 8 additions & 6 deletions bin/install_uninstall.ml
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ module File_ops_real (W : sig
match Fpath.mkdir_p (Path.to_string p) with
| Created -> ()
| Already_exists ->
(match Path.is_directory p with
| true -> ()
| false ->
(match Path.stat_exn p with
| { Unix.st_kind = S_DIR; _ } -> ()
| _ ->
User_error.raise
[ Pp.textf "Please delete file %s manually." (Path.to_string_maybe_quoted p) ])
;;
Expand Down Expand Up @@ -753,9 +753,11 @@ let install_uninstall ~what =
if copy
then
let* () =
(match Path.is_directory dst with
| true -> Ops.remove_dir_if_exists ~if_non_empty:Fail dst
| false -> Ops.remove_file_if_exists dst);
(match Path.stat dst with
| Ok { Unix.st_kind = S_DIR; _ } ->
Ops.remove_dir_if_exists ~if_non_empty:Fail dst
| Ok { Unix.st_kind = S_REG; _ } -> Ops.remove_file_if_exists dst
| _ -> ());
print_line
~verbosity
"%s %s"
Expand Down
8 changes: 6 additions & 2 deletions bin/ocaml/utop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ let term =
and+ args = Arg.(value & pos_right 0 string [] (Arg.info [] ~docv:"ARGS")) in
let config = Common.init common in
let dir = Common.prefix_target common dir in
if not (Path.is_directory (Path.of_string dir))
then User_error.raise [ Pp.textf "cannot find directory: %s" (String.maybe_quoted dir) ];
let () =
match Path.stat (Path.of_string dir) with
| Ok { Unix.st_kind = S_DIR; _ } -> ()
| _ ->
User_error.raise [ Pp.textf "cannot find directory: %s" (String.maybe_quoted dir) ]
in
let sctx, utop_path =
Scheduler.go ~common ~config (fun () ->
let open Fiber.O in
Expand Down
4 changes: 0 additions & 4 deletions otherlibs/stdune/src/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ val readdir_unsorted_with_kinds
Result.t

val is_dir_sep : char -> bool

(** [is_dir t] checks if [t] is a directory. It swallows permission errors so the preferred way is to use [stat] instead *)
val is_directory : t -> bool

val rmdir : t -> unit
val unlink : t -> unit
val unlink_no_err : t -> unit
Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/no_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Path = struct

module Untracked = struct
let exists = exists
let is_directory = is_directory
let readdir_unsorted = readdir_unsorted
let readdir_unsorted_with_kinds = readdir_unsorted_with_kinds
let stat = stat
Expand Down
19 changes: 10 additions & 9 deletions src/dune_file_watcher/dune_file_watcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -682,15 +682,16 @@ let wait_for_initial_watches_established_blocking t =
(* Return the parent directory of [ext] if [ext] denotes a file. *)
let parent_directory ext =
let rec loop p =
if Path.is_directory (Path.external_ p)
then Some ext
else (
match Path.External.parent p with
| None ->
User_warning.emit
[ Pp.textf "Refusing to watch %s" (Path.External.to_string ext) ];
None
| Some ext -> loop ext)
match Path.stat (Path.external_ p) with
| Ok { Unix.st_kind = S_DIR; _ } -> Some p
| Ok { Unix.st_kind = S_REG; _ } ->
(match Path.External.parent p with
| None ->
User_warning.emit
[ Pp.textf "Refusing to watch %s" (Path.External.to_string ext) ];
None
| Some p -> loop p)
| _ -> None
in
loop ext
;;
Expand Down
38 changes: 17 additions & 21 deletions src/dune_pkg/lock_dir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,17 @@ module Write_disk = struct
values indicate that it's unsafe to remove the existing directory and lock
directory regeneration should not proceed. *)
let check_existing_lock_dir path =
match Path.exists path with
| false -> Ok `Non_existant
| true ->
(match Path.is_directory path with
| false -> Error `Not_directory
| true ->
let metadata_path = Path.relative path metadata in
(match Path.exists metadata_path && not (Path.is_directory metadata_path) with
| false -> Error `No_metadata_file
| true ->
(match Metadata.load metadata_path ~f:(Fun.const (Decoder.return ())) with
| Ok () -> Ok `Is_existing_lock_dir
| Error exn -> Error (`Failed_to_parse_metadata exn))))
match Path.stat path with
| Ok { Unix.st_kind = S_DIR; _ } ->
let metadata_path = Path.relative path metadata in
(match Path.stat metadata_path with
| Ok { Unix.st_kind = S_REG; _ } ->
(match Metadata.load metadata_path ~f:(Fun.const (Decoder.return ())) with
| Ok () -> Ok `Is_existing_lock_dir
| Error exn -> Error (`Failed_to_parse_metadata exn))
| _ -> Error `No_metadata_file)
| Ok _ -> Error `Not_directory
| Error _ -> Ok `Non_existant
;;

(* Removes the exitsing lock directory at the specified path if it exists and
Expand Down Expand Up @@ -473,16 +471,14 @@ struct
;;

let check_path lock_dir_path =
match Path.exists (Path.source lock_dir_path) with
| false ->
match Path.stat (Path.source lock_dir_path) with
| Ok { Unix.st_kind = S_DIR; _ } -> ()
| Ok _ ->
User_error.raise
[ Pp.textf "%s is not a directory" (Path.Source.to_string lock_dir_path) ]
| Error _ ->
User_error.raise
[ Pp.textf "%s does not exist" (Path.Source.to_string lock_dir_path) ]
| true ->
(match Path.is_directory (Path.source lock_dir_path) with
| false ->
User_error.raise
[ Pp.textf "%s is not a directory" (Path.Source.to_string lock_dir_path) ]
| true -> ())
;;

let load lock_dir_path =
Expand Down
27 changes: 13 additions & 14 deletions src/dune_pkg/opam_repo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,25 @@ let validate_repo_file opam_repo_dir_path =
;;

let of_opam_repo_dir_path opam_repo_dir_path =
if not (Path.exists opam_repo_dir_path)
then
User_error.raise
[ Pp.textf "%s does not exist" (Path.to_string_maybe_quoted opam_repo_dir_path) ];
if not (Path.is_directory opam_repo_dir_path)
then
User_error.raise
[ Pp.textf "%s is not a directory" (Path.to_string_maybe_quoted opam_repo_dir_path)
];
let packages_dir_path = opam_repo_dir_path / "packages" in
if not (Path.exists packages_dir_path && Path.is_directory packages_dir_path)
then
match Path.stat opam_repo_dir_path, Path.stat packages_dir_path with
| Ok { Unix.st_kind = S_DIR; _ }, Ok { Unix.st_kind = S_DIR; _ } ->
validate_repo_file opam_repo_dir_path;
{ packages_dir_path }
| Ok { Unix.st_kind = S_DIR; _ }, _ ->
User_error.raise
[ Pp.textf
"%s doesn't look like a path to an opam repository as it lacks a subdirectory \
named \"packages\""
(Path.to_string_maybe_quoted opam_repo_dir_path)
];
validate_repo_file opam_repo_dir_path;
{ packages_dir_path }
]
| Ok _, _ ->
User_error.raise
[ Pp.textf "%s is not a directory" (Path.to_string_maybe_quoted opam_repo_dir_path)
]
| Error _, _ ->
User_error.raise
[ Pp.textf "%s does not exist" (Path.to_string_maybe_quoted opam_repo_dir_path) ]
;;

(* Return the path to the directory containing the version directories for a package name *)
Expand Down

0 comments on commit 88f3127

Please sign in to comment.