]> git.sur5r.net Git - openldap/blob - libraries/liblutil/csn.c
Reverse unintended commit
[openldap] / libraries / liblutil / csn.c
1 /*
2  * Copyright 1998-2003 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 #include <lutil.h>
36
37 size_t
38 lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
39 {
40         static time_t csntime;
41         static unsigned int csnop;
42
43         time_t t;
44         unsigned int op;
45         struct tm *ltm;
46         int n;
47
48         time( &t );
49         if ( t > csntime ) {
50                 csntime = t;
51                 csnop = 0;
52         }
53         op = ++csnop;
54
55         ltm = gmtime( &t );
56         n = snprintf( buf, len, "%4d%02d%02d%02d:%02d:%02dZ#0x%04x#%d#%04x",
57             ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour,
58             ltm->tm_min, ltm->tm_sec, op, replica, mod );
59
60         if( n < 0 ) return 0;
61         return ( (size_t) n < len ) ? n : 0;
62 }
63
64 #ifdef TEST
65 int
66 main(int argc, char **argv)
67 {
68         char buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
69
70         if ( ! lutil_csnstr( buf, (size_t) 10, 0, 0 ) ) {
71                 fprintf(stderr, "failed lutil_csnstr\n");
72         }
73         if ( ! lutil_csnstr( buf, sizeof(buf), 0, 0 ) ) {
74                 fprintf(stderr, "failed lutil_csnstr\n");
75         }
76 }
77 #endif