2021-01-08 12:47:17 +00:00
|
|
|
module Asn = struct
|
|
|
|
let decode_strict codec cs =
|
|
|
|
match Asn.decode codec cs with
|
|
|
|
| Ok (a, cs) ->
|
2021-08-03 07:26:07 +00:00
|
|
|
if Cstruct.length cs = 0
|
2021-01-08 12:47:17 +00:00
|
|
|
then Ok a
|
|
|
|
else Error "trailing bytes"
|
|
|
|
| Error (`Parse msg) -> Error ("parse error: " ^ msg)
|
|
|
|
|
|
|
|
let projections_of asn =
|
|
|
|
let c = Asn.codec Asn.der asn in
|
|
|
|
(decode_strict c, Asn.encode c)
|
|
|
|
|
|
|
|
let console =
|
|
|
|
Asn.S.(sequence_of
|
|
|
|
(sequence2
|
|
|
|
(required ~label:"delta" int)
|
|
|
|
(required ~label:"data" utf8_string)))
|
|
|
|
|
|
|
|
let console_of_cs, console_to_cs = projections_of console
|
|
|
|
end
|
|
|
|
|
2021-07-05 12:45:08 +00:00
|
|
|
type untyped_id = int64
|
|
|
|
let untyped_id = Caqti_type.int64
|
|
|
|
type 'a id = untyped_id
|
|
|
|
let id (_ : 'a) : 'a id Caqti_type.t = untyped_id
|
|
|
|
let any_id : 'a id Caqti_type.t = untyped_id
|
2021-01-08 12:47:17 +00:00
|
|
|
|
2022-02-22 12:16:42 +00:00
|
|
|
let id_to_int64 (id : 'a id) : int64 = id
|
|
|
|
|
2021-01-28 11:17:06 +00:00
|
|
|
type file = {
|
|
|
|
filepath : Fpath.t;
|
|
|
|
sha256 : Cstruct.t;
|
2021-02-25 14:27:45 +00:00
|
|
|
size : int;
|
2021-01-28 11:17:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
let uuid =
|
2022-05-09 19:13:32 +00:00
|
|
|
let encode uuid = Ok (Uuidm.to_string uuid) in
|
2021-01-08 12:47:17 +00:00
|
|
|
let decode s =
|
2022-05-09 19:13:32 +00:00
|
|
|
Uuidm.of_string s
|
2021-01-08 12:47:17 +00:00
|
|
|
|> Option.to_result ~none:"failed to decode uuid"
|
|
|
|
in
|
|
|
|
Caqti_type.custom ~encode ~decode Caqti_type.string
|
|
|
|
|
|
|
|
|
|
|
|
let ptime =
|
|
|
|
let encode t = Ok (Ptime.Span.to_d_ps (Ptime.to_span t)) in
|
|
|
|
let decode (d, ps) = Ok (Ptime.v (d, ps))
|
|
|
|
in
|
2024-08-13 11:07:50 +00:00
|
|
|
let rep = Caqti_type.(t2 int int64) in
|
2021-01-08 12:47:17 +00:00
|
|
|
Caqti_type.custom ~encode ~decode rep
|
|
|
|
|
|
|
|
let fpath =
|
|
|
|
let encode t = Ok (Fpath.to_string t) in
|
|
|
|
let decode s = Fpath.of_string s
|
|
|
|
|> Result.map_error (fun (`Msg s) -> s) in
|
|
|
|
Caqti_type.custom ~encode ~decode Caqti_type.string
|
|
|
|
|
|
|
|
let cstruct =
|
|
|
|
let encode t = Ok (Cstruct.to_string t) in
|
|
|
|
let decode s = Ok (Cstruct.of_string s) in
|
|
|
|
Caqti_type.custom ~encode ~decode Caqti_type.octets
|
|
|
|
|
2021-01-28 11:17:06 +00:00
|
|
|
let file =
|
2023-09-14 08:58:09 +00:00
|
|
|
let encode { filepath; sha256; size } =
|
|
|
|
Ok (filepath, sha256, size) in
|
|
|
|
let decode (filepath, sha256, size) =
|
|
|
|
Ok { filepath; sha256; size } in
|
2024-08-13 11:07:50 +00:00
|
|
|
Caqti_type.custom ~encode ~decode Caqti_type.(t3 fpath cstruct int)
|
2021-01-28 11:17:06 +00:00
|
|
|
|
|
|
|
let file_opt =
|
2024-08-13 11:07:50 +00:00
|
|
|
let rep = Caqti_type.(t3 (option fpath) (option cstruct) (option int)) in
|
2021-01-28 11:17:06 +00:00
|
|
|
let encode = function
|
2023-09-14 08:58:09 +00:00
|
|
|
| Some { filepath; sha256; size } ->
|
|
|
|
Ok (Some filepath, Some sha256, Some size)
|
2021-01-28 11:17:06 +00:00
|
|
|
| None ->
|
2023-09-14 08:58:09 +00:00
|
|
|
Ok (None, None, None)
|
2021-01-28 11:17:06 +00:00
|
|
|
in
|
|
|
|
let decode = function
|
2023-09-14 08:58:09 +00:00
|
|
|
| (Some filepath, Some sha256, Some size) ->
|
|
|
|
Ok (Some { filepath; sha256; size })
|
|
|
|
| (None, None, None) ->
|
2021-01-28 11:17:06 +00:00
|
|
|
Ok None
|
|
|
|
| _ ->
|
|
|
|
(* This should not happen if the database is well-formed *)
|
|
|
|
Error "Some but not all fields NULL"
|
|
|
|
in
|
|
|
|
Caqti_type.custom ~encode ~decode rep
|
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
let execution_result =
|
|
|
|
let encode = function
|
2021-07-12 13:29:10 +00:00
|
|
|
| Builder.Exited v -> Ok (v, None)
|
|
|
|
| Builder.Signalled v -> Ok (v lsl 8, None)
|
|
|
|
| Builder.Stopped v -> Ok (v lsl 16, None)
|
|
|
|
| Builder.Msg msg -> Ok (65536, Some msg)
|
2021-01-08 12:47:17 +00:00
|
|
|
in
|
2021-07-12 13:29:10 +00:00
|
|
|
let decode (code, msg) =
|
|
|
|
if code <= 0xFF then
|
|
|
|
Ok (Builder.Exited code)
|
|
|
|
else if code <= 0xFFFF then
|
|
|
|
Ok (Builder.Signalled (code lsr 8))
|
|
|
|
else if code <= 0xFFFFFF then
|
|
|
|
Ok (Builder.Stopped (code lsr 16))
|
|
|
|
else if code = 65536 then
|
|
|
|
match msg with
|
|
|
|
| None -> Error "bad encoding"
|
|
|
|
| Some m -> Ok (Builder.Msg m)
|
|
|
|
else
|
|
|
|
Error "bad encoding (unknown number)"
|
2021-01-08 12:47:17 +00:00
|
|
|
in
|
2024-08-13 11:07:50 +00:00
|
|
|
let rep = Caqti_type.(t2 int (option string)) in
|
2021-01-08 12:47:17 +00:00
|
|
|
Caqti_type.custom ~encode ~decode rep
|
|
|
|
|
|
|
|
let console =
|
|
|
|
let encode console = Ok (Asn.console_to_cs console) in
|
|
|
|
let decode data = Asn.console_of_cs data in
|
|
|
|
Caqti_type.custom ~encode ~decode cstruct
|
2021-01-20 21:50:35 +00:00
|
|
|
|
|
|
|
let user_info =
|
2024-08-13 11:07:50 +00:00
|
|
|
let rep = Caqti_type.(t7 string cstruct cstruct int int int bool) in
|
2021-02-23 15:20:18 +00:00
|
|
|
let encode { Builder_web_auth.username;
|
|
|
|
password_hash = `Scrypt (password_hash, password_salt, {
|
|
|
|
Builder_web_auth.scrypt_n; scrypt_r; scrypt_p
|
2021-06-08 14:54:23 +00:00
|
|
|
});
|
|
|
|
restricted; }
|
2021-02-23 15:20:18 +00:00
|
|
|
=
|
2024-08-13 11:07:50 +00:00
|
|
|
Ok (username, password_hash, password_salt, scrypt_n, scrypt_r, scrypt_p, restricted)
|
2021-01-20 21:50:35 +00:00
|
|
|
in
|
2024-08-13 11:07:50 +00:00
|
|
|
let decode (username, password_hash, password_salt, scrypt_n, scrypt_r, scrypt_p, restricted) =
|
2021-02-23 15:20:18 +00:00
|
|
|
Ok { Builder_web_auth.username;
|
|
|
|
password_hash =
|
|
|
|
`Scrypt (password_hash, password_salt,
|
|
|
|
{ Builder_web_auth.scrypt_n;
|
2021-06-08 14:54:23 +00:00
|
|
|
scrypt_r; scrypt_p });
|
|
|
|
restricted; } in
|
2021-01-20 21:50:35 +00:00
|
|
|
Caqti_type.custom ~encode ~decode rep
|
2021-07-05 12:45:08 +00:00
|
|
|
|
|
|
|
(* this doesn't really belong in this module, but we need access to the type of [id] *)
|
|
|
|
let last_insert_rowid =
|
2022-04-04 16:30:21 +00:00
|
|
|
let open Caqti_request.Infix in
|
|
|
|
Caqti_type.unit ->! any_id @@
|
2021-07-05 12:45:08 +00:00
|
|
|
"SELECT last_insert_rowid()"
|