<commit_before>open Cmdliner

type result =
  | Ok
  | Error of string

let translate_error dest = function
  | (Unix.EACCES) -> 
    Error (Printf.sprintf "Permission denied writing %s" dest)
  | (Unix.EISDIR) ->
    Error (Printf.sprintf "%s already exists and is a directory" dest)
  | (Unix.ENOENT) ->
    Error (Printf.sprintf "Part of the path %s doesn't exist" dest)
  | (Unix.ENOSPC) -> Error "No space left on device"
  | (Unix.EROFS) ->
    Error (Printf.sprintf "%s is on a read-only filesystem" dest)
  | e -> Error (Unix.error_message e)

let make_dates days =
  let asn1_of_time time = 
    let tm = Unix.gmtime time in
    {
      (* irritatingly, posix months and Unix.tm months are differently indexed *)
      Asn.Time.date = Unix.(tm.tm_year, (tm.tm_mon + 1), tm.tm_mday);
      time = Unix.(tm.tm_hour, tm.tm_min, tm.tm_sec, 0.); (* no fractional secs in tm *)
      (* no tz info in tm, but we got it from gmtime so tzoffset should be 0 *)
      tz = None;
    }
  in
  let seconds = days * 24 * 60 * 60 in
  let start = asn1_of_time (Unix.time ()) in
  let expire = asn1_of_time (Unix.time () +. (float_of_int seconds)) in
  (start, expire)

let write_pem dest pem =
  try 
    let fd = Unix.openfile dest [Unix.O_WRONLY; Unix.O_CREAT] 0o600 in
    (* single_write promises either complete failure (resulting in an exception)
         or complete success, so disregard the returned number of bytes written
         and just handle the exceptions *)
    let _written_bytes = Unix.single_write fd (Cstruct.to_string pem) 0 (Cstruct.len pem) in
    let () = Unix.close fd in
    Ok
  with
  | Unix.Unix_error (e, _, _) -> translate_error dest e

let thing = "self-signed certificate or certificate signing request"<commit_msg>Unix.tm_year is year - 1900, thus we need to add 1900 here<commit_after>open Cmdliner

type result =
  | Ok
  | Error of string

let translate_error dest = function
  | (Unix.EACCES) -> 
    Error (Printf.sprintf "Permission denied writing %s" dest)
  | (Unix.EISDIR) ->
    Error (Printf.sprintf "%s already exists and is a directory" dest)
  | (Unix.ENOENT) ->
    Error (Printf.sprintf "Part of the path %s doesn't exist" dest)
  | (Unix.ENOSPC) -> Error "No space left on device"
  | (Unix.EROFS) ->
    Error (Printf.sprintf "%s is on a read-only filesystem" dest)
  | e -> Error (Unix.error_message e)

let make_dates days =
  let asn1_of_time time = 
    let tm = Unix.gmtime time in
    {
      (* irritatingly, posix months and Unix.tm months are differently indexed *)
      Asn.Time.date = Unix.(tm.tm_year + 1900, (tm.tm_mon + 1), tm.tm_mday);
      time = Unix.(tm.tm_hour, tm.tm_min, tm.tm_sec, 0.); (* no fractional secs in tm *)
      (* no tz info in tm, but we got it from gmtime so tzoffset should be 0 *)
      tz = None;
    }
  in
  let seconds = days * 24 * 60 * 60 in
  let start = asn1_of_time (Unix.time ()) in
  let expire = asn1_of_time (Unix.time () +. (float_of_int seconds)) in
  (start, expire)

let write_pem dest pem =
  try 
    let fd = Unix.openfile dest [Unix.O_WRONLY; Unix.O_CREAT] 0o600 in
    (* single_write promises either complete failure (resulting in an exception)
         or complete success, so disregard the returned number of bytes written
         and just handle the exceptions *)
    let _written_bytes = Unix.single_write fd (Cstruct.to_string pem) 0 (Cstruct.len pem) in
    let () = Unix.close fd in
    Ok
  with
  | Unix.Unix_error (e, _, _) -> translate_error dest e

let thing = "self-signed certificate or certificate signing request"
{'fraction_non_alphanumeric': 0.10446075663466968, 'fraction_numerical': 0.012987012987012988, 'mean_word_length': 2.9808988764044946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5773093', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1215, 'n_words': 420}