use scanf

This commit is contained in:
swrup 2026-02-23 06:30:39 +01:00
parent 5e9426267a
commit d7ccd69350
3 changed files with 39 additions and 58 deletions

View file

@ -21,34 +21,25 @@ type t = {
ht: (EddsaPublicKey.t, key) Hashtbl.t;
}
(* -- util -- *)
let parse_filename =
let scan_filename s =
Scanf.sscanf_opt s "%Lu-%Lu" (fun t1 t2 ->
(Absolute.of_s t1, Absolute.of_s t2))
in
fun fpath -> scan_filename (Fpath.filename fpath)
(* TODO time *)
let time_abs_of_string s =
int_of_string_opt s |> Option.map (fun n -> Absolute.of_s (Int64.of_int n))
let t1_t2_of_fpath fpath =
let fname = Fpath.filename fpath in
match String.split_on_char '-' fname with
| [] -> Fmt.failwith "not possible"
| [ t1; t2 ] -> (
match (time_abs_of_string t1, time_abs_of_string t2) with
| None, _ | _, None -> None
| Some t1, Some t2 -> Some (t1, t2))
| _ -> None
let time_abs_to_string abs =
abs
|> Timestamp.of_absolute
|> Timestamp.to_s
|> Option.get
|> Int64.to_int
|> string_of_int
let pp_filename =
let to_int64 abs =
abs |> Timestamp.of_absolute |> Timestamp.to_s |> function
| None ->
(* (= `never`) this should not happen given resonable config value *)
Fmt.failwith "encountered timestamp with value `never`"
| Some i -> i
in
fun ppf (t1, t2) -> Fmt.pf ppf "%Lu-%Lu" (to_int64 t1) (to_int64 t2)
let key_fpath k =
let t1 = time_abs_to_string k.t1 in
let t2 = time_abs_to_string k.t2 in
let fname = Fmt.str "%s-%s" t1 t2 in
let fname = Fmt.str "%a" pp_filename (k.t1, k.t2) in
Fpath.(v Cfg.key_dir / fname)
(* -- IO -- *)
@ -86,8 +77,7 @@ let get_key_dir_contents dir =
let gen_key t1 t2 =
let priv, pub = EddsaPrivateKey.generate () in
Log.debug (fun m ->
m "generated key (%s-%s):@,`%s`" (time_abs_to_string t1)
(time_abs_to_string t2)
m "generated key (%a):@,`%s`" pp_filename (t1, t2)
(EddsaPublicKey.to_b32 pub));
{ priv; pub; t1; t2 }
@ -135,7 +125,7 @@ let load_key fpath =
match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with
| true -> Ok None
| false -> (
match t1_t2_of_fpath fpath with
match parse_filename fpath with
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
| Some (t1, t2) ->
let* priv = read_key fpath in