Skip to content

Commit

Permalink
irmin-pack: add path to No_such_file_or_directory
Browse files Browse the repository at this point in the history
To give context in logs when this error is triggered.
  • Loading branch information
metanivek committed Jan 26, 2023
1 parent 7197e8c commit eaf725d
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/control_file_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module type S = sig
type open_error :=
[ `Corrupted_control_file
| `Io_misc of Io.misc_error
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_file
| `Closed
| `Unknown_major_pack_version of string ]
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type base_error =
[ `Double_close
| `File_exists of string
| `Invalid_parent_directory
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_file
| `Read_out_of_bounds
| `Invalid_argument
Expand Down
12 changes: 6 additions & 6 deletions src/irmin-pack/unix/file_manager.ml
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ struct
let no_migrate = Irmin_pack.Conf.no_migrate config in
match Io.classify_path root with
| `File | `Other -> Error (`Not_a_directory root)
| `No_such_file_or_directory -> Error `No_such_file_or_directory
| `No_such_file_or_directory -> Error (`No_such_file_or_directory root)
| `Directory -> (
let path = Irmin_pack.Layout.V4.control ~root in
match Io.classify_path path with
Expand All @@ -621,11 +621,11 @@ struct
Control.open_ ~readonly:true ~path
(* If no control file, then check whether the store is in v1 or v2. *)
|> Result.map_error (function
| `No_such_file_or_directory -> (
| `No_such_file_or_directory _ -> (
let pack = Irmin_pack.Layout.V1_and_v2.pack ~root in
match Io.classify_path pack with
| `File -> `Migration_needed
| `No_such_file_or_directory -> `No_such_file_or_directory
| `No_such_file_or_directory -> `No_such_file_or_directory pack
| `Directory | `Other -> `Invalid_layout)
| error -> error)
in
Expand Down Expand Up @@ -693,20 +693,20 @@ struct
| Ok v -> Ok v
| Error `Double_close | Error `Invalid_argument | Error `Closed ->
assert false
| Error `No_such_file_or_directory -> Error `Invalid_layout
| Error (`No_such_file_or_directory _) -> Error `Invalid_layout
| Error `Not_a_file -> Error `Invalid_layout
| Error `Corrupted_legacy_file | Error `Read_out_of_bounds ->
Error `Corrupted_legacy_file
| Error (`Io_misc _) as e -> e
in
match Io.classify_path root with
| `No_such_file_or_directory -> Error `No_such_file_or_directory
| `No_such_file_or_directory -> Error (`No_such_file_or_directory root)
| `File | `Other -> Error (`Not_a_directory root)
| `Directory -> (
let path = Irmin_pack.Layout.V4.control ~root in
match Control.open_ ~path ~readonly:true with
| Ok _ -> Ok `V3
| Error `No_such_file_or_directory -> v2_or_v1 ()
| Error (`No_such_file_or_directory _) -> v2_or_v1 ()
| Error `Not_a_file -> Error `Invalid_layout
| Error `Closed -> assert false
| Error
Expand Down
6 changes: 3 additions & 3 deletions src/irmin-pack/unix/file_manager_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module type S = sig
| `Invalid_layout
| `Io_misc of Control.Io.misc_error
| `Migration_needed
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_directory of string
| `Not_a_file
| `Read_out_of_bounds
Expand Down Expand Up @@ -148,7 +148,7 @@ module type S = sig
| `Corrupted_mapping_file of string
| `Io_misc of Io.misc_error
| `Migration_needed
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_file
| `Closed
| `V3_store_from_the_future
Expand Down Expand Up @@ -227,7 +227,7 @@ module type S = sig
| `Corrupted_legacy_file
| `Invalid_layout
| `Io_misc of Io.misc_error
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_directory of string
| `Unknown_major_pack_version of string ]

Expand Down
10 changes: 6 additions & 4 deletions src/irmin-pack/unix/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ module Unix = struct
type create_error = [ `Io_misc of misc_error | `File_exists of string ]

type open_error =
[ `Io_misc of misc_error | `No_such_file_or_directory | `Not_a_file ]
[ `Io_misc of misc_error
| `No_such_file_or_directory of string
| `Not_a_file ]

type read_error =
[ `Io_misc of misc_error
Expand All @@ -75,7 +77,7 @@ module Unix = struct
type mkdir_error =
[ `Io_misc of misc_error
| `File_exists of string
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Invalid_parent_directory ]

let raise_misc_error (x, y, z) = raise (Unix.Unix_error (x, y, z))
Expand Down Expand Up @@ -135,7 +137,7 @@ module Unix = struct
let open_ ~path ~readonly =
match classify_path path with
| `Directory | `Other -> Error `Not_a_file
| `No_such_file_or_directory -> Error `No_such_file_or_directory
| `No_such_file_or_directory -> Error (`No_such_file_or_directory path)
| `File -> (
let mode = Unix.(if readonly then O_RDONLY else O_RDWR) in
try
Expand Down Expand Up @@ -282,7 +284,7 @@ module Unix = struct
with Unix.Unix_error (e, s1, s2) -> Error (`Io_misc (e, s1, s2)))
| `Directory, (`File | `Directory | `Other) -> Error (`File_exists path)
| `No_such_file_or_directory, `No_such_file_or_directory ->
Error `No_such_file_or_directory
Error (`No_such_file_or_directory path)
| _ -> Error `Invalid_parent_directory

let unlink path =
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/io_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Make (Io : Io.S) : S with module Io = Io = struct
[ `Double_close
| `File_exists of string
| `Invalid_parent_directory
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Not_a_file
| `Read_out_of_bounds
| `Invalid_argument
Expand Down
10 changes: 7 additions & 3 deletions src/irmin-pack/unix/io_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module type S = sig
type create_error = [ `Io_misc of misc_error | `File_exists of string ]

type open_error =
[ `Io_misc of misc_error | `No_such_file_or_directory | `Not_a_file ]
[ `Io_misc of misc_error
| `No_such_file_or_directory of string
| `Not_a_file ]

type read_error =
[ `Io_misc of misc_error
Expand All @@ -52,7 +54,7 @@ module type S = sig
type mkdir_error =
[ `Io_misc of misc_error
| `File_exists of string
| `No_such_file_or_directory
| `No_such_file_or_directory of string
| `Invalid_parent_directory ]

(** {1 Safe Functions}
Expand Down Expand Up @@ -105,7 +107,9 @@ module type S = sig
val size_of_path :
string ->
( int63,
[> `Io_misc of misc_error | `No_such_file_or_directory | `Not_a_file ] )
[> `Io_misc of misc_error
| `No_such_file_or_directory of string
| `Not_a_file ] )
result

val classify_path :
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/mapping_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module Make (Io : Io.S) = struct
(`Corrupted_mapping_file
(__FILE__ ^ ": mapping mmap size did not meet size requirements"))
)
| _ -> Error `No_such_file_or_directory
| _ -> Error (`No_such_file_or_directory path)

let create ?report_mapping_size ~root ~generation ~register_entries () =
assert (generation > 0);
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/mapping_file_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module type S = sig
module Errs : Io_errors.S with module Io = Io

type open_error :=
[ `Corrupted_mapping_file of string | `No_such_file_or_directory ]
[ `Corrupted_mapping_file of string | `No_such_file_or_directory of string ]

val create :
?report_mapping_size:(int63 -> unit) ->
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-pack/unix/store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module Maker (Config : Conf.S) = struct
match Io.classify_path path with
| `Directory -> Some path
| `No_such_file_or_directory ->
Errs.raise_error `No_such_file_or_directory
Errs.raise_error (`No_such_file_or_directory path)
| `File | `Other -> Errs.raise_error (`Not_a_directory path))
in
let fm =
Expand Down
24 changes: 21 additions & 3 deletions test/irmin-pack/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,29 @@ module Alcotest = struct

let int63 = testable Int63.pp Int63.equal

let check_raises_pack_error msg pass f =
Lwt.try_bind f
(fun _ ->
Alcotest.failf
"Fail %s: expected function to raise, but it returned instead." msg)
(function
| Irmin_pack_unix.Errors.Pack_error e as exn -> (
match pass e with
| true -> Lwt.return_unit
| false ->
Alcotest.failf
"Fail %s: function raised unexpected exception %s" msg
(Printexc.to_string exn))
| exn ->
Alcotest.failf
"Fail %s: expected function to raise Pack_error, but it raised \
%s instead"
msg (Printexc.to_string exn))

(** TODO: upstream this to Alcotest *)
let check_raises_lwt msg exn (type a) (f : unit -> a Lwt.t) =
Lwt.catch
(fun x ->
let* (_ : a) = f x in
Lwt.try_bind f
(fun _ ->
Alcotest.failf
"Fail %s: expected function to raise %s, but it returned instead." msg
(Printexc.to_string exn))
Expand Down
7 changes: 7 additions & 0 deletions test/irmin-pack/common.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ module Alcotest : sig
val int63 : Int63.t testable
val kind : Irmin_pack.Pack_value.Kind.t testable
val hash : Schema.Hash.t testable

val check_raises_pack_error :
string ->
(Irmin_pack_unix.Errors.base_error -> bool) ->
(unit -> _ Lwt.t) ->
unit Lwt.t

val check_raises_lwt : string -> exn -> (unit -> _ Lwt.t) -> unit Lwt.t

val check_repr :
Expand Down
7 changes: 3 additions & 4 deletions test/irmin-pack/test_gc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,9 @@ module Concurrent_gc = struct
let killed = kill_gc t in
let* () =
if killed then
Alcotest.check_raises_lwt "Gc process killed"
(Irmin_pack_unix.Errors.Pack_error
(`Gc_process_died_without_result_file
"cancelled \"No_such_file_or_directory\""))
Alcotest.check_raises_pack_error "Gc process killed"
(function
| `Gc_process_died_without_result_file _ -> true | _ -> false)
(fun () -> finalise_gc t)
else Lwt.return_unit
in
Expand Down
5 changes: 4 additions & 1 deletion test/irmin-pack/test_upgrade.ml
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,10 @@ let open_ro t current_phase =
let+ repo =
match (t.setup.start_mode, current_phase) with
| From_scratch, S1_before_start ->
fail_and_skip `No_such_file_or_directory
let missing_path =
Irmin_pack.Layout.V1_and_v2.pack ~root:root_local_build
in
fail_and_skip (`No_such_file_or_directory missing_path)
| From_v2, S1_before_start -> fail_and_skip `Migration_needed
| (From_v2 | From_v3 | From_v3_c0_gced | From_scratch), _ ->
Store.v t.setup ~readonly:true ~fresh:false root_local_build
Expand Down

0 comments on commit eaf725d

Please sign in to comment.