]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Set timestamp part more accurately in guid_128_generate()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 28 Apr 2016 19:33:14 +0000 (22:33 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 29 Apr 2016 19:08:23 +0000 (22:08 +0300)
Previously a long-running process would keep the timestamp close to its
original start time. This doesn't really matter as long as GUIDs are treated
opaque, but some pieces of code prefer to try to use the timestamp fields
since they're already there. This makes such code work more nicely.

src/lib/guid.c

index 902c79658c558b34f545e4f2ea69cdd1b3f78eb9..b0524dbb51fd384032c175873923c94d753479f3 100644 (file)
@@ -1,6 +1,7 @@
 /* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "ioloop.h"
 #include "buffer.h"
 #include "sha1.h"
 #include "hash.h"
@@ -65,7 +66,15 @@ void guid_128_generate(guid_128_t guid_r)
                guid_static[2] = (pid & 0x00ff0000) >> 16;
                guid_static[3] = (pid & 0xff000000) >> 24;
                guid_128_host_hash_get(my_hostdomain(), guid_static+4);
-       } else if ((uint32_t)ts.tv_nsec < (uint32_t)-1) {
+       } else if (ioloop_timeval.tv_sec > ts.tv_sec ||
+                  (ioloop_timeval.tv_sec == ts.tv_sec &&
+                   ioloop_timeval.tv_usec > ts.tv_nsec*1000)) {
+               /* use ioloop's time since we have it. it doesn't provide any
+                  more uniqueness, but it allows finding out more reliably
+                  when a GUID was created. */
+               ts.tv_sec = ioloop_timeval.tv_sec;
+               ts.tv_nsec = ioloop_timeval.tv_usec*1000;
+       } else if ((uint32_t)ts.tv_nsec < 1000000000) {
                ts.tv_nsec++;
        } else {
                ts.tv_sec++;