1 /* csn.c - Change Sequence Number routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 /* Portions Copyright 2000, John E. Schimmel, All rights reserved.
18 * This software is not subject to any license of Mirapoint, Inc.
20 * This is free software; you can redistribute and use it
21 * under the same terms as OpenLDAP itself.
23 /* This work was developed by John E. Schimmel and adapted for
24 * inclusion in OpenLDAP Software by Kurt D. Zeilenga.
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.
31 * These routines are (loosly) based upon draft-ietf-ldup-model-03.txt,
32 * A WORK IN PROGRESS. The format will likely change.
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, 3, and
39 * 6, respectively. (In previous implementations r was only 2 digits.)
41 * Calls to this routine MUST be serialized with other calls
52 lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
55 static unsigned int csnop;
56 static int prev_sec, prev_usec;
63 if ( tm.tm_sec > prev_sec || ( tm.tm_sec == prev_sec &&
64 tm.tm_usec > prev_usec )) {
66 prev_usec = tm.tm_usec;
70 tm.tm_usec = prev_usec;
74 n = snprintf( buf, len,
75 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
76 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
77 tm.tm_min, tm.tm_sec, tm.tm_usec, op, replica, mod );
80 return ( (size_t) n < len ) ? n : 0;
85 main(int argc, char **argv)
87 char buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
89 if ( ! lutil_csnstr( buf, (size_t) 10, 0, 0 ) ) {
90 fprintf(stderr, "failed lutil_csnstr\n");
92 if ( ! lutil_csnstr( buf, sizeof(buf), 0, 0 ) ) {
93 fprintf(stderr, "failed lutil_csnstr\n");