daemon: utils: New functions and tests.

These utility functions will be used in the OCaml inspection code.
This commit is contained in:
Richard W.M. Jones
2017-07-17 17:01:48 +01:00
parent 2ffb8a6b25
commit 11b8a5db19
3 changed files with 38 additions and 0 deletions

View File

@@ -212,3 +212,20 @@ let proc_unmangle_path path =
let is_small_file path =
is_regular_file path &&
(stat path).st_size <= 2 * 1048 * 1024
let read_small_file filename =
if not (is_small_file filename) then (
eprintf "%s: not a regular file or too large\n" filename;
None
)
else (
let content = read_whole_file filename in
let lines = String.nsplit "\n" content in
Some lines
)
let unix_canonical_path path =
let is_absolute = String.length path > 0 && path.[0] = '/' in
let path = String.nsplit "/" path in
let path = List.filter ((<>) "") path in
(if is_absolute then "/" else "") ^ String.concat "/" path