aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2021-09-20 10:08:54 +0200
committerAlban Gruin2021-09-20 10:08:54 +0200
commit00cd66b16956a75a1c8dfb1f267b9a26603c09c7 (patch)
tree52dfe555af7de269518fcaed07420200bd04f7d2
parent9522c817654c435f1929f4a37961780346406cf7 (diff)
ics, ucs: generate event IDs with a random number
Until now, ucs generated unique IDs with the datetime and the number of an event, starting by 0. Unfortunately, if two calendars were generated at the same second, they could have collisions. To avoid that, the ICS generator now uses a base ID generated randomly, which should be enough to avoid collisions. The RNG is self-initialised in the main function. Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r--src/ics.ml4
-rw-r--r--src/ucs.ml3
2 files changed, 5 insertions, 2 deletions
diff --git a/src/ics.ml b/src/ics.ml
index 732cbd5..4de1897 100644
--- a/src/ics.ml
+++ b/src/ics.ml
@@ -19,6 +19,8 @@ open CalendarLib
module StringSet = Set.Make (String)
+let rng_bound = int_of_float (2. ** 30.) - 1
+
let to_date = Printer.Calendar.sprint "%Y%m%dT%H%M%S"
let current_date () = to_date @@ Calendar.now ()
@@ -101,5 +103,5 @@ let to_string tz events =
VERSION:2.0\r\n\
PRODID:-//ucs//\r\n"
^ vtimezone
- ^ gen_events 0 "" events
+ ^ gen_events (Random.int rng_bound) "" events
^ "END:VCALENDAR\r\n"
diff --git a/src/ucs.ml b/src/ucs.ml
index 9950d8c..74849e7 100644
--- a/src/ucs.ml
+++ b/src/ucs.ml
@@ -1,5 +1,5 @@
(*
- * Copyright (C) 2020 Alban Gruin
+ * Copyright (C) 2020, 2021 Alban Gruin
*
* ucs is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@@ -67,4 +67,5 @@ let () =
Term.(const run $ base_url $ celcat_url $ port $ socket),
Term.info "ucs" ~version:(Version.version ()) ~doc ~exits in
CalendarLib.Time_Zone.(change Local);
+ Random.self_init ();
Term.(exit @@ eval cmd)