logarion-2021/lib/file_store.ml
orbifx 6ba60d90cc List file names and new fox pts/0 192.168.88.21 Sat Jun 19 16:14 still logged in
fox      pts/0        192.168.88.21    Fri Jun 18 15:03 - 15:07  (00:04)
fox      pts/0        192.168.88.21    Fri Jun 18 13:09 - 13:15  (00:05)
fox      pts/0        192.168.88.21    Thu Jun 17 22:08 - 00:23  (02:15)
fox      pts/0        192.168.88.21    Thu Jun 17 11:15 - 15:59  (04:44)
fox      pts/0        192.168.88.21    Wed Jun 16 09:30 - 20:51  (11:21)
fox      pts/0        192.168.88.21    Tue Jun 15 22:33 - 23:37  (01:04)
fox      pts/0        192.168.88.21    Tue Jun 15 11:38 - 13:13  (01:35)
fox      pts/0        192.168.88.21    Mon Jun 14 21:07 - 21:09  (00:01)
fox      pts/0        192.168.88.21    Mon Jun 14 09:11 - 19:43  (10:31)
fox      pts/0        192.168.88.21    Sun Jun 13 17:15 - 17:53  (00:38)
fox      pts/0        192.168.88.21    Sun Jun 13 09:45 - 15:43  (05:57)
fox      pts/0        192.168.88.21    Sat Jun 12 15:59 - 17:51  (01:51)
fox      pts/0        192.168.88.21    Fri Jun 11 18:59 - 19:40  (00:41)
fox      pts/0        192.168.88.21    Fri Jun 11 12:52 - 16:53  (04:00)
fox      pts/0        192.168.88.21    Wed Jun  9 09:15 - 09:56  (00:40)
fox      pts/0        192.168.88.21    Tue Jun  8 18:35 - 18:37  (00:01)
fox      pts/0        192.168.88.21    Tue Jun  8 17:32 - 17:44  (00:11)
fox      pts/0        192.168.88.21    Tue Jun  8 09:38 - 13:44  (04:06)
fox      pts/0        192.168.88.21    Mon Jun  7 20:41 - 21:59  (01:18)
fox      pts/0        192.168.88.21    Mon Jun  7 14:49 - 14:52  (00:02)
fox      pts/0        192.168.88.21    Mon Jun  7 11:15 - 14:49  (03:33)
fox      pts/0        192.168.88.5     Sun Jun  6 11:07 - 15:53  (04:46)
fox      pts/0        192.168.88.5     Sat Jun  5 21:36 - 00:19  (02:43)
fox      pts/0        192.168.88.5     Sat Jun  5 17:48 - 19:42  (01:54)
fox      pts/0        192.168.88.21    Fri Jun  4 20:01 - 20:05  (00:03)
fox      pts/0        192.168.88.21    Fri Jun  4 18:39 - 18:40  (00:01)
fox      pts/0        192.168.88.21    Fri Jun  4 10:09 - 11:26  (01:17)
fox      pts/0        192.168.88.21    Thu Jun  3 18:50 - 23:05  (04:14)
fox      pts/0        192.168.88.21    Tue Jun  1 09:31 - 12:28  (02:57)
fox      pts/0        192.168.88.21    Mon May 31 09:31 - 17:31  (08:00)
fox      pts/0        192.168.88.21    Sun May 30 21:32 - 00:28  (02:56)
fox      pts/0        192.168.88.21    Sat May 29 11:26 - 12:07  (00:41)
fox      pts/0        192.168.88.21    Fri May 28 18:22 - 21:27  (03:04)
fox      pts/0        192.168.88.21    Tue May 25 10:44 - 11:42  (00:57)
fox      pts/0        192.168.88.21    Sat May 22 14:23 - 21:06  (06:42)
fox      pts/0        192.168.88.21    Thu May 20 12:21 - 13:08  (00:47)
fox      pts/0        192.168.88.21    Wed May 19 23:04 - 23:10  (00:05)
fox      pts/0        192.168.88.21    Mon May 17 22:16 - 23:48  (01:32)
fox      pts/0        192.168.88.21    Mon May 17 18:24 - 20:31  (02:07)
fox      pts/0        192.168.88.5     Mon May 17 11:04 - 11:34  (00:30)
fox      pts/0        192.168.88.21    Sat May 15 13:08 - 14:24  (01:15)
fox      pts/0        192.168.88.21    Fri May 14 22:22 - 22:23  (00:00)
fox      pts/0        192.168.88.21    Fri May 14 22:15 - 22:22  (00:06)
fox      pts/0        192.168.88.21    Fri May 14 18:08 - 19:01  (00:53)
fox      pts/0        192.168.88.21    Fri May 14 15:44 - 15:45  (00:00)
fox      pts/0        192.168.88.21    Fri May 14 15:44 - 15:44  (00:00)
fox      pts/0        192.168.88.21    Thu May 13 23:27 - 23:36  (00:09)
fox      pts/0        192.168.88.21    Fri May  7 18:19 - 21:27  (03:07)
fox      pts/0        192.168.88.21    Fri May  7 10:28 - 14:58  (04:29)
fox      pts/0        192.168.88.21    Tue May  4 20:19 - 20:25  (00:06)
fox      pts/0        192.168.88.5     Fri Apr 30 13:04 - 15:06  (02:01)
fox      pts/0        192.168.88.5     Tue Apr 27 13:05 - 19:15  (06:10)
fox      pts/0        192.168.88.5     Sun Apr 25 11:19 - 12:05  (00:45)
fox      pts/0        192.168.88.21    Fri Apr 23 12:39 - 14:57  (02:17)
fox      pts/0        192.168.88.21    Tue Apr 20 12:57 - 13:39  (00:41)
fox      pts/0        192.168.88.5     Tue Apr 20 10:20 - 11:47  (01:26)
fox      pts/0        192.168.88.5     Mon Apr 19 19:26 - 21:18  (01:52)
fox      pts/0        192.168.88.5     Sun Apr 18 22:02 - 02:13  (04:11)
fox      pts/0        192.168.88.5     Sun Apr 18 10:52 - 13:42  (02:50)
fox      pts/0        192.168.88.5     Sat Apr 17 17:52 - 17:56  (00:04)
fox      pts/0        192.168.88.21    Fri Apr 16 23:37 - 23:41  (00:03)
reboot   system boot  5.11.2-1-ARCH    Thu Jan  1 01:00   still running
fox      pts/0        192.168.88.21    Fri Apr 16 22:10 - 22:50  (00:40)
fox      pts/0        192.168.88.21    Fri Apr 16 13:47 - 14:32  (00:44)
fox      pts/0        192.168.88.5     Fri Apr 16 13:19 - 13:25  (00:06)
fox      pts/0        192.168.88.5     Fri Apr 16 10:03 - 10:54  (00:51)
fox      pts/0        192.168.88.21    Mon Apr 12 22:05 - 01:32  (03:26)
fox      pts/0        192.168.88.5     Fri Apr  9 11:13 - 18:39 (1+07:25)
fox      pts/0        192.168.88.21    Wed Apr  7 18:51 - 19:15  (00:23)
fox      pts/0        192.168.88.21    Tue Apr  6 22:54 - 23:26  (00:32)
fox      pts/0        192.168.88.21    Fri Apr  2 19:22 - 01:16  (05:54)
fox      pts/0        192.168.88.21    Fri Apr  2 13:54 - 18:00  (04:05)
fox      pts/0        192.168.88.21    Thu Apr  1 09:25 - 16:00  (06:35)
fox      pts/0        192.168.88.21    Wed Mar 31 11:10 - 22:22  (11:11)
fox      pts/0        192.168.88.21    Wed Mar 31 00:14 - 00:15  (00:01)
fox      pts/0        192.168.88.21    Tue Mar 30 21:44 - 00:07  (02:22)
fox      pts/0        192.168.88.21    Tue Mar 30 14:04 - 14:44  (00:39)
fox      pts/0        192.168.88.21    Wed Mar 24 20:36 - 22:06  (01:29)
fox      pts/0        192.168.88.21    Wed Mar 24 09:17 - 09:25  (00:08)
fox      pts/0        192.168.88.21    Tue Mar 16 13:26 - 13:33  (00:07)
fox      pts/0        192.168.88.21    Mon Mar 15 15:56 - 15:58  (00:01)
fox      pts/0        192.168.88.21    Sun Mar 14 13:06 - 17:48  (04:42)
reboot   system boot  5.11.2-1-ARCH    Thu Jan  1 01:00 - 22:52 (18733+21:51)
fox      pts/0        192.168.88.21    Sun Mar 14 11:31 - 13:01  (01:29)
fox      pts/0        192.168.88.21    Sat Mar 13 12:11 - 18:54  (06:42)
fox      pts/0        192.168.88.21    Fri Mar 12 21:58 - 00:18  (02:19)
fox      pts/0        192.168.88.21    Thu Mar 11 15:56 - 23:13  (07:17)
fox      pts/0        192.168.88.21    Tue Mar  9 21:30 - 00:37  (03:06)
fox      pts/0        192.168.88.21    Tue Mar  9 16:21 - 16:22  (00:00)
fox      pts/0        192.168.88.21    Mon Mar  8 12:46 - 13:15  (00:29)
fox      pts/0        192.168.88.21    Sun Mar  7 17:53 - 17:55  (00:02)
fox      pts/0        192.168.88.21    Sun Mar  7 16:19 - 16:31  (00:11)
fox      pts/0        192.168.88.21    Sun Mar  7 16:11 - 16:12  (00:00)
fox      pts/0        192.168.88.21    Sun Mar  7 16:00 - 16:05  (00:05)
fox      pts/0        192.168.88.21    Sun Mar  7 09:39 - 16:00  (06:20)
fox      pts/0        192.168.88.21    Sat Mar  6 15:33 - 21:53  (06:20)
fox      pts/0        192.168.88.21    Sat Mar  6 13:03 - 14:40  (01:36)
fox      pts/0        192.168.88.21    Fri Mar  5 19:13 - 01:10  (05:56)
fox      pts/0        192.168.88.21    Thu Mar  4 17:22 - 19:55  (02:32)
fox      pts/0        192.168.88.21    Wed Mar  3 11:58 - 23:47  (11:48)
fox      pts/0        192.168.88.21    Tue Mar  2 19:21 - 00:22  (05:00)
fox      pts/0        192.168.88.21    Tue Mar  2 13:30 - 16:34  (03:04)
fox      pts/0        192.168.88.21    Mon Mar  1 23:04 - 23:38  (00:33)
fox      pts/0        192.168.88.21    Mon Mar  1 17:21 - 20:41  (03:19)
fox      pts/0        192.168.88.21    Sun Feb 28 19:53 - 20:24  (00:31)
fox      pts/0        192.168.88.21    Sun Feb 28 17:15 - 19:41  (02:26)
fox      pts/0        192.168.88.21    Sun Feb 28 14:40 - 14:51  (00:10)
fox      pts/0        192.168.88.21    Thu Feb 25 21:44 - 00:02  (02:17)
fox      pts/0        192.168.88.21    Wed Feb 24 10:38 - 11:42  (01:03)
reboot   system boot  5.11.0-2-ARCH    Thu Jan  1 01:00 - 13:02 (18700+13:02)
fox      pts/0        192.168.88.21    Wed Feb 24 09:55 - 10:36  (00:40)
fox      pts/0        192.168.88.21    Mon Feb 22 16:53 - 23:53  (07:00)
fox      pts/0        192.168.88.21    Mon Feb 22 12:10 - 14:22  (02:11)
fox      pts/0        192.168.88.21    Sun Feb 21 15:50 - 21:20  (05:29)
fox      pts/0        192.168.88.21    Sun Feb 21 09:41 - 10:51  (01:10)
fox      pts/0        192.168.88.21    Sat Feb 20 21:10 - 21:13  (00:02)
fox      pts/0        192.168.88.21    Sat Feb 20 14:04 - 21:10  (07:05)
fox      pts/0        192.168.88.21    Fri Feb 19 09:38 - 17:08  (07:29)
fox      pts/0        192.168.88.21    Thu Feb 18 23:05 - 00:54  (01:48)
fox      pts/0        192.168.88.21    Wed Feb 17 08:38 - 16:46  (08:07)
fox      pts/0        192.168.88.21    Mon Feb 15 22:52 - 23:36  (00:44)
fox      pts/0        192.168.88.21    Mon Feb 15 13:54 - 20:37  (06:43)
fox      pts/0        192.168.88.21    Sun Feb 14 09:16 - 16:11  (06:55)
fox      pts/0        192.168.88.21    Sat Feb 13 08:58 - 00:52  (15:54)
fox      pts/0        192.168.88.21    Fri Feb 12 16:33 - 21:56  (05:23)
fox      pts/0        192.168.88.21    Fri Feb 12 11:17 - 14:11  (02:53)
fox      pts/0        192.168.88.21    Wed Feb 10 10:17 - 13:37  (03:20)
fox      pts/0        192.168.88.21    Tue Feb  9 09:54 - 23:35  (13:40)
fox      pts/0        192.168.88.21    Sun Feb  7 09:45 - 20:50  (11:05)
fox      pts/0        192.168.88.21    Sat Feb  6 19:32 - 00:12  (04:39)
fox      pts/0        192.168.88.21    Sat Feb  6 11:35 - 18:41  (07:06)
fox      pts/0        192.168.88.21    Fri Feb  5 23:02 - 23:13  (00:11)
fox      pts/0        192.168.88.21    Wed Feb  3 19:04 - 23:42  (04:38)
fox      pts/0        192.168.88.21    Tue Feb  2 10:11 - 01:01  (14:50)
fox      pts/0        192.168.88.21    Mon Feb  1 18:38 - 21:13  (02:35)
fox      pts/5        192.168.88.5     Mon Feb  1 10:30 - 11:12  (00:41)
fox      pts/0        192.168.88.21    Mon Feb  1 10:03 - 10:45  (00:41)
fox      pts/0        192.168.88.5     Sun Jan 31 16:37 - 19:36  (02:59)
fox      pts/0        192.168.88.21    Sat Jan 30 10:02 - 20:55  (10:52)
fox      pts/0        192.168.88.21    Thu Jan 28 17:13 - 19:55  (02:41)
fox      pts/0        192.168.88.21    Sun Jan 24 16:34 - 01:57  (09:22)
fox      pts/0        192.168.88.21    Sun Jan 24 14:14 - 15:41  (01:26)
fox      pts/0        192.168.88.21    Thu Jan 21 09:49 - 21:04  (11:14)
fox      pts/0        192.168.88.21    Wed Jan 20 21:59 - 00:14  (02:14)
fox      pts/0        192.168.88.21    Mon Jan 18 21:42 - 22:34  (00:51)
fox      pts/0        192.168.88.21    Sun Jan 17 17:26 - 19:22  (01:56)
fox      pts/0        192.168.88.21    Fri Jan 15 18:09 - 18:10  (00:00)
fox      pts/0        192.168.88.5     Tue Jan 12 21:48 - 23:22  (01:34)
fox      pts/0        192.168.88.21    Sun Jan 10 10:42 - 13:45  (03:02)
fox      pts/0        192.168.88.5     Sat Jan  9 19:19 - 22:00  (02:40)
fox      pts/0        192.168.88.21    Sat Jan  9 16:43 - 17:14  (00:30)
fox      pts/0        192.168.88.5     Fri Jan  8 21:13 - 22:33  (01:19)
fox      pts/0        192.168.88.5     Thu Jan  7 22:50 - 00:34  (01:44)
fox      pts/0        192.168.88.5     Mon Jan  4 21:45 - 02:18  (04:32)
fox      pts/0        192.168.88.21    Mon Jan  4 10:00 - 18:18  (08:17)
fox      pts/5        192.168.88.5     Sun Jan  3 13:33 - 15:06  (01:33)
fox      pts/5        192.168.88.5     Sun Jan  3 13:31 - 13:32  (00:01)
fox      pts/5        192.168.88.5     Sun Jan  3 13:28 - 13:31  (00:02)
fox      pts/0        192.168.88.21    Sun Jan  3 09:45 - 13:42  (03:57)
fox      pts/0        192.168.88.5     Sat Jan  2 22:45 - 00:25  (01:39)
fox      pts/0        192.168.88.21    Sat Jan  2 14:34 - 20:40  (06:06)
fox      pts/0        192.168.88.21    Thu Dec 31 09:39 - 16:34  (06:54)
fox      pts/0        192.168.88.21    Wed Dec 30 21:31 - 23:33  (02:02)
fox      pts/0        192.168.88.21    Tue Dec 29 21:15 - 23:16  (02:01)
fox      pts/0        192.168.88.21    Tue Dec 29 19:16 - 20:33  (01:16)
fox      pts/0        192.168.88.21    Mon Dec 28 17:56 - 23:03  (05:06)
fox      pts/0        192.168.88.21    Sun Dec 27 23:01 - 00:39  (01:37)
fox      pts/0        192.168.88.21    Sun Dec 27 14:18 - 14:38  (00:20)
fox      pts/0        192.168.88.21    Sun Dec 27 09:58 - 12:34  (02:35)
root     pts/1        tmux(4435).%0    Sat Dec 26 21:13 - 21:15  (00:01)
fox      pts/0        192.168.88.21    Sat Dec 26 20:45 - 23:39  (02:54)
reboot   system boot  5.8.9-1-ARCH     Thu Jan  1 01:00 - 10:37 (18682+10:37)
root     pts/1        tmux(9747).%0    Sat Dec 26 19:20 - 20:02  (00:42)
fox      pts/0        192.168.88.21    Sat Dec 26 19:17 - 20:02  (00:44)
fox      pts/0        192.168.88.21    Sat Dec 26 19:16 - 19:17  (00:00)
fox      pts/0        192.168.88.21    Sat Dec 26 15:07 - 17:49  (02:41)
fox      pts/0        192.168.88.21    Wed Dec 23 18:16 - 20:56  (02:39)
fox      pts/0        192.168.88.21    Wed Dec 23 13:49 - 18:16  (04:26)
fox      pts/0        192.168.88.5     Mon Dec 14 20:50 - 20:52  (00:02)
fox      pts/0        192.168.88.5     Wed Dec  9 21:45 - 22:52  (01:06)
fox      pts/0        192.168.88.5     Mon Dec  7 13:21 - 13:36  (00:15)
fox      pts/0        192.168.88.5     Sat Dec  5 15:42 - 16:06  (00:23)
fox      pts/0        192.168.88.5     Mon Nov 23 17:35 - 17:35  (00:00)
fox      pts/0        192.168.88.5     Sun Nov 22 10:45 - 17:36  (06:50)
fox      pts/0        192.168.88.5     Thu Nov 19 21:41 - 23:32  (01:50)
fox      pts/5        192.168.88.5     Thu Nov 19 15:08 - 15:35  (00:26)
fox      pts/0        192.168.88.5     Thu Nov 19 14:46 - 15:19  (00:33)
fox      pts/0        192.168.88.5     Wed Nov 18 13:41 - 15:20  (01:38)
fox      pts/0        192.168.88.5     Mon Nov 16 16:56 - 20:27  (03:31)
fox      pts/0        192.168.88.5     Sat Nov 14 14:44 - 15:57  (01:12)
fox      pts/0        192.168.88.5     Sat Nov 14 09:37 - 10:14  (00:37)
fox      pts/0        192.168.88.5     Fri Nov 13 22:09 - 00:10  (02:00)
fox      pts/0        192.168.88.5     Fri Nov 13 21:54 - 22:09  (00:14)
fox      pts/0        192.168.88.5     Wed Nov 11 22:48 - 23:01  (00:13)
fox      pts/0        192.168.88.5     Wed Nov 11 20:56 - 21:26  (00:29)
fox      pts/0        192.168.88.5     Tue Nov 10 16:55 - 16:55  (00:00)
fox      pts/0        192.168.88.5     Mon Nov  9 21:28 - 21:35  (00:07)
fox      pts/0        192.168.88.5     Mon Nov  9 14:24 - 14:24  (00:00)
fox      pts/0        192.168.88.5     Sun Nov  8 13:36 - 13:38  (00:02)
fox      pts/0        192.168.88.5     Sun Nov  8 13:00 - 13:10  (00:09)
fox      pts/0        192.168.88.5     Sat Nov  7 12:59 - 13:27  (00:27)
fox      pts/0        192.168.88.5     Thu Nov  5 21:34 - 00:51  (03:16)
fox      pts/0        192.168.88.5     Wed Nov  4 21:31 - 22:08  (00:36)
fox      pts/0        192.168.88.5     Wed Nov  4 17:44 - 20:31  (02:47)
fox      pts/0        192.168.88.5     Wed Nov  4 17:06 - 17:44  (00:37)
fox      pts/0        192.168.88.5     Tue Nov  3 22:43 - 23:57  (01:14)
fox      pts/0        192.168.88.5     Tue Nov  3 22:42 - 22:43  (00:00)
fox      pts/0        192.168.88.5     Tue Nov  3 09:25 - 09:32  (00:07)
fox      pts/0        192.168.88.5     Sun Oct 25 21:24 - 00:17  (02:52)
fox      pts/0        192.168.88.5     Sun Oct 25 10:55 - 13:32  (02:37)
fox      pts/0        192.168.88.5     Fri Oct 23 13:57 - 14:54  (00:56)

wtmp begins Fri Oct 23 13:57:08 2020 subcommand
2021-06-19 17:16:50 +01:00

160 lines
5.5 KiB
OCaml

type t = string
type item_t = string
type archive_t = {
name: string; archivists: Person.Set.t; id: Id.t;
kv: string Store.KV.t; store: t }
type record_t = Text.t * item_t
let extensions = [ ".txt" ]
let to_string f =
let ic = open_in f in
let n = in_channel_length ic in
let s = Bytes.create n in
really_input ic s 0 n;
close_in ic;
Bytes.to_string s
let file path content = let out = open_out path in
output_string out content; close_out out
let (//) a b = a ^ "/" ^ b
let to_text dir filename = let f = dir // filename in
to_string f |> Text.of_string |> Result.map_error (fun m -> f^": "^m)
let text_filetype dir name = try
let open Fpath in
let v = v name in
mem_ext extensions v && not (Sys.is_directory @@ dir^"/"^name)
with Invalid_argument str ->
prerr_endline ("file: " ^ name ^ " invalid (" ^ str ^ ")"); false
let newest (a,_pa) (b,_pb) = Text.newest a b
let oldest (a,_pa) (b,_pb) = Text.oldest a b
let list_iter fn {store;_} paths =
let link f = match to_text store f with Ok t -> fn store t f | Error s -> prerr_endline s in
List.iter link paths
let iter_valid_text dir pred fn p =
if text_filetype dir p then
match to_text dir p with Error x -> prerr_endline x | Ok t -> if pred t then fn (t, p)
let fold_valid_text dir pred fn acc p =
if not (text_filetype dir p) then acc else
match to_text dir p with
Error _x -> acc | Ok t -> if pred t then fn acc (t, p) else acc
let iter ?(predicate=fun _ -> true) ?order fn {store;_} =
match order with
| None -> Array.iter (iter_valid_text store predicate fn) @@ Sys.readdir store
| Some comp ->
List.iter fn @@ List.fast_sort comp
@@ Array.fold_left (fold_valid_text store predicate (fun a e -> List.cons e a)) []
@@ Sys.readdir store
let fold ?(predicate=fun _ -> true) ?order fn acc {store;_} =
match order with
| None -> Array.fold_left (fold_valid_text store predicate fn) acc @@ Sys.readdir store
| Some comp ->
List.fold_left fn acc @@ List.fast_sort comp
@@ Array.fold_left (fold_valid_text store predicate (fun a e -> List.cons e a)) []
@@ Sys.readdir store
let with_id { store; _ } id =
let matched acc path =
if not (text_filetype store path) then acc
else
match to_text store path with
| Error x -> prerr_endline x; acc
| Ok text ->
if text.Text.uuid <> id then acc
else
match acc with
| Ok None -> Ok (Some text)
| Ok (Some prev) -> if prev = text then acc else Error [text; prev]
| Error x -> Error (text :: x)
in
Array.fold_left matched (Ok None) @@ Sys.readdir store
module Directory = struct
let print ?(descr="") dir result =
let () = match result with
| Ok true -> print_endline ("Created " ^ descr ^ " directory " ^ dir)
| Ok false -> print_endline ("Using existing " ^ descr ^ " directory " ^ dir)
| Error (`Msg msg) -> prerr_endline @@ "Failed making " ^ descr ^ ". " ^ msg
in
result
let directory dir = Result.bind (Fpath.of_string dir) Bos.OS.Dir.create
let rec directories = function
| [] -> Ok ()
| (d, descr)::tl ->
match directory d |> print ~descr d with
| Ok _ -> directories tl
| Error _ -> Error (d, descr)
end
let copy ?(recursive = false) src dst =
Bos.OS.Cmd.run (Bos.Cmd.(v "cp" %% (on recursive @@ v "-r") % src % dst))
let versioned_basename_of_title ?(version=0) repo extension (title : string) =
let basename = Text.string_alias title in
let rec next version =
let candidate = repo // basename ^ "." ^ string_of_int version ^ extension in
if Sys.file_exists candidate then next (succ version) else candidate
in
next version
let uuid_filename repo extension text =
let basename = Text.alias text in
let candidate = repo // String.sub (Id.to_string text.uuid) 0 6 ^ "." ^ basename ^ extension in
if Sys.file_exists candidate then Error "Name clash, try again" else Ok candidate
let with_text {store;_} new_text =
let extension = List.hd extensions in
Result.bind (uuid_filename store extension new_text) @@
fun path ->
try file path (Text.to_string new_text); Ok (path, new_text) with Sys_error s -> Error s
let basic_config () =
"Archive-Name: "
^ "\nArchive-ID: " ^ Id.(generate () |> to_string)
^ "\nArchivists: " ^ Bos.OS.Env.opt_var "USER" ~absent:""
|> Bytes.of_string
let init ?(dotdir=".logarion/") () =
match Directory.directories [dotdir, "dotdir"] with
| Error (_dir, _desc) -> ()
| Ok () ->
let config_file =
open_out_gen [Open_creat; Open_excl; Open_wronly]
0o700 (dotdir // "config") in
output_bytes config_file (basic_config ());
close_out config_file
module Config = struct
type t = archive_t
let key_value k v a = match k with
| "Archive-Name" -> { a with name = String.trim v }
| "Archive-ID" -> { a with id = Option.get (Id.of_string (String.trim v)) }
| "Archivists" -> { a with archivists = Person.Set.of_string v }
| _ -> { a with kv = Store.KV.add k (String.trim v) a.kv }
end
let of_path store =
let open Text_parse in
let subsyntaxes = [| (module Parsers.Key_value.Make (Config) : Parser.S with type t = Config.t); (module Parsers.Key_value.Make (Config)); |] in
let of_string text acc = Parser.parse subsyntaxes { text; pos = 0; right_boundary = String.length text - 1 } acc in
Ok (of_string (to_string @@ store ^ "/.logarion/config")
{
name = "";
archivists = Person.Set.empty;
id = Id.nil;
kv = Store.KV.empty;
store = Bos.OS.Env.opt_var "LOGARION_DIR" ~absent:(Sys.getcwd ())
}
)