]> git.sur5r.net Git - openldap/blob - libraries/liblutil/csn.c
cleanup
[openldap] / libraries / liblutil / csn.c
1 /* csn.c - Change Sequence Number routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright 2000, John E. Schimmel, All rights reserved.
18  * This software is not subject to any license of Mirapoint, Inc.
19  *
20  * This is free software; you can redistribute and use it
21  * under the same terms as OpenLDAP itself.
22  */
23 /* This work was developed by John E. Schimmel and adapted for
24  * inclusion in OpenLDAP Software by Kurt D. Zeilenga.
25  */
26
27 /* This file contains routines to generate a change sequence number.
28  * Every add, delete, and modification is given a unique identifier
29  * for use in resolving conflicts during replication operations.
30  *
31  * These routines are (loosly) based upon draft-ietf-ldup-model-03.txt,
32  * A WORK IN PROGRESS.  The format will likely change.
33  *
34  * The format of a CSN string is: yyyymmddhhmmssz#s#r#c
35  * where s is a counter of operations within a timeslice, r is
36  * the replica id (normally zero), and c is a counter of
37  * modifications within this operation.  s, r, and c are
38  * represented in hex and zero padded to lengths of 6, 2, and
39  * 6, respectively.
40  *
41  * Calls to this routine MUST be serialized with other calls
42  * to gmtime().
43  */
44 #include "portable.h"
45
46 #include <stdio.h>
47 #include <ac/time.h>
48
49 #include <lutil.h>
50
51 size_t
52 lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
53 {
54         static time_t csntime;
55         static unsigned int csnop;
56
57         time_t t;
58         unsigned int op;
59         struct tm *ltm;
60 #ifdef HAVE_GMTIME_R
61         struct tm ltm_buf;
62 #endif
63         int n;
64
65         time( &t );
66         if ( t > csntime ) {
67                 csntime = t;
68                 csnop = 0;
69         }
70         op = csnop++;
71
72 #ifdef HAVE_GMTIME_R
73         ltm = gmtime_r( &t, &ltm_buf );
74 #else
75         ltm = gmtime( &t );
76 #endif
77         n = snprintf( buf, len,
78                 "%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
79             ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour,
80             ltm->tm_min, ltm->tm_sec, op, replica, mod );
81
82         if( n < 0 ) return 0;
83         return ( (size_t) n < len ) ? n : 0;
84 }
85
86 #ifdef TEST
87 int
88 main(int argc, char **argv)
89 {
90         char buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
91
92         if ( ! lutil_csnstr( buf, (size_t) 10, 0, 0 ) ) {
93                 fprintf(stderr, "failed lutil_csnstr\n");
94         }
95         if ( ! lutil_csnstr( buf, sizeof(buf), 0, 0 ) ) {
96                 fprintf(stderr, "failed lutil_csnstr\n");
97         }
98 }
99 #endif