diff options
author | Daniil Baturin <daniil@vyos.io> | 2022-12-29 14:32:33 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-29 14:32:33 +0000 |
commit | b8569f0c3e155ee5886b907957d24c268c4d016f (patch) | |
tree | 3df1ac8665f02b069b1184af8091042f1502b491 | |
parent | 541df959cfc92206e15f29d2d1745e627a22a707 (diff) | |
parent | 2da99f7795e08378b4370b3403f556fce864f83d (diff) | |
download | vyos-utils-b8569f0c3e155ee5886b907957d24c268c4d016f.tar.gz vyos-utils-b8569f0c3e155ee5886b907957d24c268c4d016f.zip |
Merge branch 'master' into url-validator
-rw-r--r--[-rwxr-xr-x] | debian/rules | 1 | ||||
-rw-r--r-- | debian/vyos-utils.install | 3 | ||||
-rw-r--r-- | src/file_path.ml | 55 |
3 files changed, 58 insertions, 1 deletions
diff --git a/debian/rules b/debian/rules index 7f137b3..73f3805 100755..100644 --- a/debian/rules +++ b/debian/rules @@ -17,6 +17,7 @@ override_dh_auto_install: mkdir -p $(DIR)/usr/libexec/vyos/validators cp _build/numeric $(DIR)/usr/libexec/vyos/validators cp _build/validate-value $(DIR)/usr/libexec/vyos/ + cp _build/file-path $(DIR)/usr/libexec/vyos/validators override_dh_auto_test: echo "No tests yet" diff --git a/debian/vyos-utils.install b/debian/vyos-utils.install index 77ea559..a67b3f8 100644 --- a/debian/vyos-utils.install +++ b/debian/vyos-utils.install @@ -1,2 +1,3 @@ -usr/libexec/vyos/validators/numeric usr/libexec/vyos/validate-value +usr/libexec/vyos/validators/numeric +usr/libexec/vyos/validators/file-path diff --git a/src/file_path.ml b/src/file_path.ml new file mode 100644 index 0000000..ea3068c --- /dev/null +++ b/src/file_path.ml @@ -0,0 +1,55 @@ +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 +} + +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"); + ("--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] <path>" 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 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 + (* 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 |