]> git.sur5r.net Git - openldap/blob - libraries/liblutil/csn.c
104ad5cd4df841ba70ee5cfe943b72a94dff9c30
[openldap] / libraries / liblutil / csn.c
1 /*
2  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* Portions
6  * Copyright 2000, John E. Schimmel, All rights reserved.
7  * This software is not subject to any license of Mirapoint, Inc.
8  *
9  * This is free software; you can redistribute and use it
10  * under the same terms as OpenLDAP itself.
11  */
12 /* Adapted for incorporatation into OpenLDAP by Kurt Zeilenga */
13
14 /*
15  * This file contains routines to generate a change sequence number.  Every
16  * add delete, and modification is given a unique identifier for use in
17  * resolving conflicts during replication operations.
18  *
19  * These routines are based upon draft-ietf-ldup-model-03.txt, and will
20  * need to be revisited once standardized.
21  *
22  * The format of a CSN string is: yyyymmddhh:mm:ssz#0xSSSS#d#0xssss
23  * where SSSS is a counter of operations within a timeslice, d is an
24  * offset into a list of replica records, and ssss is a counter of
25  * modifications within this operation.
26  *
27  * Calls to this routine MUST be serialized with other calls
28  * to gmtime().
29  */
30 #include "portable.h"
31
32 #include <stdio.h>
33 #include <ac/time.h>
34
35 int
36 lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
37 {
38         static time_t csntime;
39         static unsigned int csnop;
40
41         time_t t;
42         unsigned int op;
43         struct tm *ltm;
44         int n;
45
46         time( &t );
47         if ( t > csntime ) {
48                 csntime = t;
49                 csnop = 0;
50         }
51         op = ++csnop;
52
53         ltm = gmtime( &t );
54         n = snprintf( buf, len, "%4d%02d%02d%02d:%02d:%02dZ#0x%04x#%d#%04x",
55             ltm->tm_year + 1900, ltm->tm_mon, ltm->tm_mday, ltm->tm_hour,
56             ltm->tm_min, ltm->tm_sec, op, replica, mod );
57
58         return ( n < len ) ? 1 : 0;
59 }
60
61 #ifdef TEST
62 int
63 main(int argc, char **argv)
64 {
65         char buf[256];
66
67         if ( ! lutil_csnstr( buf, (size_t) 10, 0, 0 ) ) {
68                 fprintf(stderr, "failed lutil_csnstr\n");
69         }
70         if ( ! lutil_csnstr( buf, sizeof(buf), 0, 0 ) ) {
71                 fprintf(stderr, "failed lutil_csnstr\n");
72         }
73 }
74 #endif