source: code/trunk/lib/file_store.ml@ 17

Last change on this file since 17 was 16, checked in by fox, 3 years ago

Txt pull puts peer texts in $txtdir/peers

File size: 5.2 KB
Line 
1type t = string
2type item_t = t list
3type record_t = Text.t * item_t
4
5let extension = ".txt"
6let def_dir () = try Sys.getenv "txtdir" with Not_found ->
7 let share = Filename.concat (Sys.getenv "HOME") ".local/share/texts/" in
8 match Sys.is_directory share with true -> share
9 | false | exception (Sys_error _) -> "."
10
11let to_string f =
12 let ic = open_in f in
13 let s = really_input_string ic (in_channel_length ic) in
14 close_in ic;
15 s
16
17let fold_file_line fn init file = match open_in file with
18 | exception (Sys_error msg) -> prerr_endline msg; init
19 | file ->
20 let rec read acc = match input_line file with
21 | "" as s | s when String.get s 0 = '#' -> read acc
22 | s -> read (fn s acc)
23 | exception End_of_file -> close_in file; acc
24 in read init
25
26let file path str = let o = open_out path in output_string o str; close_out o
27
28let to_text path =
29 if Filename.extension path = extension then
30 (to_string path |> Text.of_string |> Result.map_error (fun m -> path ^": "^ m))
31 else Error (Printf.sprintf "Not txt: %s" path)
32
33let newest (a,_pa) (b,_pb) = Text.newest a b
34let oldest (a,_pa) (b,_pb) = Text.oldest a b
35
36let list_iter fn dir paths =
37 let link f = match to_text (Filename.concat dir f) with
38 | Ok t -> fn dir t f | Error s -> prerr_endline s in
39 List.iter link paths
40
41module TextMap = Map.Make(Text)
42
43type iteration_t = item_t TextMap.t
44let new_iteration = TextMap.empty
45
46(*let iter_valid_text pred fn path =*)
47(* match to_text path with Error _ -> () | Ok t -> if pred t then fn (t, p)*)
48
49let fold_valid_text pred it path =
50 match to_text path with Error _ -> it
51 | Ok t -> if pred t then (TextMap.update t
52 (function None -> Some [path] | Some ps -> Some (path::ps)) it
53 ) else it
54
55let split_filetypes files =
56 let acc (dirs, files) x = if Sys.is_directory x
57 then (x::dirs, files) else (dirs, x::files) in
58 List.fold_left acc ([],[]) files
59
60(* Compare file system nodes to skip reparsing? *)
61let list_fs ?(r=false) dir =
62 let valid_dir f = r && String.get f 0 <> '.' && Sys.is_directory f in
63 let expand_dir d = Array.(to_list @@ map (Filename.concat d) (Sys.readdir d)) in
64 let rec loop result = function
65 | f::fs when valid_dir f -> expand_dir f |> List.append fs |> loop result
66 | f::fs -> loop (f::result) fs
67 | [] -> result in
68 let dirs = if dir = "." then Array.to_list (Sys.readdir dir) else
69 if not r then expand_dir dir else [dir] in
70 loop [] dirs
71
72let list_take n =
73 let rec take acc n = function [] -> []
74 | x::_ when n = 1 -> x::acc
75 | x::xs -> take (x::acc) (n-1) xs
76 in take [] n
77
78let fold_sort_take ?(predicate=fun _ -> true) ?(number=None) comp flist =
79 (match number with None -> (fun x -> x) | Some n -> list_take n)
80 @@ List.fast_sort comp @@ TextMap.bindings
81 @@ List.fold_left (fold_valid_text predicate) new_iteration flist
82
83let iter ?(r=false) ?(dir=def_dir ()) ?(predicate=fun _ -> true) ?order ?number fn =
84 let flist = list_fs ~r dir in match order with
85 | Some comp -> List.iter fn @@ fold_sort_take ~predicate ~number comp flist
86 | None -> List.iter fn @@ TextMap.bindings @@
87 List.fold_left (fold_valid_text predicate) new_iteration flist
88
89let fold ?(r=false) ?(dir=def_dir ()) ?(predicate=fun _ -> true) ?order ?number fn acc =
90 let flist = list_fs ~r dir in match order with
91 | Some comp -> List.fold_left fn acc @@ fold_sort_take ~predicate ~number comp flist
92 | None -> List.fold_left fn acc @@ TextMap.bindings @@
93 List.fold_left (fold_valid_text predicate) new_iteration flist
94
95let with_dir ?(descr="") ?(perm=0o740) dir =
96 let mkdir dir = match Unix.mkdir dir perm with
97 | exception Unix.Unix_error (EEXIST, _, _) -> ()
98 | exception Unix.Unix_error (code, _fn, arg) ->
99 failwith @@ Printf.sprintf "Error %s making %s dir: %s"
100 (Unix.error_message code) descr arg
101 | _ -> () in
102 let rec mkeach path = function [] | [""] -> () | ""::t -> mkeach path t
103 | hd::t -> let d = Filename.concat path hd in mkdir d; mkeach d t in
104 mkeach
105 (if Filename.is_relative dir then "" else "/")
106 (String.split_on_char '/' dir)
107
108let rec with_dirs = function [] -> () | (d, descr)::tl -> with_dir ~descr d; with_dirs tl
109
110let versioned_basename_of_title ?(version=0) repo extension (title : string) =
111 let basename = Text.string_alias title in
112 let rec next version =
113 let candidate = Filename.concat repo
114 (basename ^ "." ^ string_of_int version ^ extension) in
115 if Sys.file_exists candidate then next (succ version) else candidate
116 in
117 next version
118
119let id_filename repo extension text =
120 let basename = Text.alias text in
121 let candidate = Filename.concat repo (text.id ^ "." ^ basename ^ extension) in
122 if Sys.file_exists candidate then Error "Name clash, try again" else Ok candidate
123
124let with_text ?(dir=def_dir ()) new_text =
125 match id_filename dir extension new_text with
126 | Error _ as e -> e
127 | Ok path ->
128 try file path (Text.to_string new_text); Ok (path, new_text)
129 with Sys_error s -> Error s
130
131module Config = struct
132 type t = string Store.KV.t
133 let key_value k v a = Store.KV.add k (String.trim v) a
134end
135
136let of_kv_file path =
137 let open Text_parse in
138 let subsyntaxes = Parsers.Key_value.[|
139 (module Make (Config) : Parser.S with type t = Config.t); (module Make (Config)); |] in
140 let of_string text acc =
141 Parser.parse subsyntaxes { text; pos = 0; right_boundary = String.length text - 1 } acc in
142 of_string (to_string @@ path) Store.KV.empty
Note: See TracBrowser for help on using the repository browser.