From 96bb119393d8606d9408d72aa5cb468702bef625 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 4 Nov 2022 15:29:03 +0000 Subject: T4798: add a file path validator --- src/file_path.ml | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/file_path.ml (limited to 'src') diff --git a/src/file_path.ml b/src/file_path.ml new file mode 100644 index 0000000..8c05ffd --- /dev/null +++ b/src/file_path.ml @@ -0,0 +1,48 @@ +type opts = { + must_be_file : bool; + parent : string option; + strict : bool; +} + +let default_opts = { + must_be_file = true; + parent = None; + strict = false +} + +let opts = ref default_opts + +let path_arg = ref "" + +let args = [ + ("--file", Arg.Unit (fun () -> opts := {!opts with must_be_file=true}), "Path must point to a file and not a directory (default)"); + ("--directory", Arg.Unit (fun () -> opts := {!opts with must_be_file=false}), "Path must point to a directory"); + ("--parent-dir", Arg.String (fun s -> opts := {!opts with parent=(Some s)}), "Path must be inside specific parent directory"); + ("--strict", Arg.Unit (fun () -> opts := {!opts with strict=true}), "Treat warnings as errors"); +] +let usage = Printf.sprintf "Usage: %s [OPTIONS] " Sys.argv.(0) + +let () = if Array.length Sys.argv = 1 then (Arg.usage args usage; exit 1) +let () = Arg.parse args (fun s -> path_arg := s) usage + +let fail msg = + let () = print_endline msg in + exit 1 + +let () = + let path = !path_arg in + let opts = !opts in + (* First, check if the file/dir path exists at all. *) + let exists = FileUtil.test FileUtil.Exists path in + if not exists then Printf.ksprintf fail {|Incorrect path %s: no such file or directory|} path else + (* If yes, check if it's of the correct type: file or directory. *) + let is_file = FileUtil.test FileUtil.Is_file path in + if ((not is_file) && opts.must_be_file) then Printf.ksprintf fail {|%s is a directory, not a file|} path else + if (is_file && (not opts.must_be_file)) then Printf.ksprintf fail {|%s is a file, not a directory|} path else + match opts.parent with + | None -> + exit 0 + | Some parent -> + if not (FilePath.is_subdir (FilePath.reduce path) (FilePath.reduce parent)) then + let msg = Printf.sprintf {|Path %s is not under %s directory|} path parent in + if opts.strict then fail msg else Printf.printf "Warning: %s\n" msg -- cgit v1.2.3 From 3883bb64a45794904f042e2f1e4458eb4700de8b Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 12 Dec 2022 10:58:55 -0600 Subject: validators: T4875: add option lookup-path to cat with file/dir arg --- src/file_path.ml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/file_path.ml b/src/file_path.ml index 8c05ffd..ea3068c 100644 --- a/src/file_path.ml +++ b/src/file_path.ml @@ -1,12 +1,14 @@ type opts = { must_be_file : bool; parent : string option; + lookup_path : string option; strict : bool; } let default_opts = { must_be_file = true; parent = None; + lookup_path = None; strict = false } @@ -18,6 +20,7 @@ let args = [ ("--file", Arg.Unit (fun () -> opts := {!opts with must_be_file=true}), "Path must point to a file and not a directory (default)"); ("--directory", Arg.Unit (fun () -> opts := {!opts with must_be_file=false}), "Path must point to a directory"); ("--parent-dir", Arg.String (fun s -> opts := {!opts with parent=(Some s)}), "Path must be inside specific parent directory"); + ("--lookup-path", Arg.String (fun s -> opts := {!opts with lookup_path=(Some s)}), "Prefix path argument with lookup path"); ("--strict", Arg.Unit (fun () -> opts := {!opts with strict=true}), "Treat warnings as errors"); ] let usage = Printf.sprintf "Usage: %s [OPTIONS] " Sys.argv.(0) @@ -30,8 +33,12 @@ let fail msg = exit 1 let () = - let path = !path_arg in let opts = !opts in + let path = + match opts.lookup_path with + | None -> !path_arg + | Some lookup_path -> FilePath.concat lookup_path !path_arg + in (* First, check if the file/dir path exists at all. *) let exists = FileUtil.test FileUtil.Exists path in if not exists then Printf.ksprintf fail {|Incorrect path %s: no such file or directory|} path else -- cgit v1.2.3