2003-11-26 07:17:08 +08:00
|
|
|
/* csn.c - Change Sequence Number routines */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2009-01-22 08:40:04 +08:00
|
|
|
* Copyright 2000-2009 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* Portions Copyright 2000-2003 Kurt D. Zeilenga.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2001-12-05 14:30:58 +08:00
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* Portions Copyright 2000, John E. Schimmel, All rights reserved.
|
2001-12-05 14:30:58 +08:00
|
|
|
* This software is not subject to any license of Mirapoint, Inc.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute and use it
|
|
|
|
* under the same terms as OpenLDAP itself.
|
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work was developed by John E. Schimmel and adapted for
|
|
|
|
* inclusion in OpenLDAP Software by Kurt D. Zeilenga.
|
|
|
|
*/
|
2001-12-05 14:30:58 +08:00
|
|
|
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This file contains routines to generate a change sequence number.
|
|
|
|
* Every add, delete, and modification is given a unique identifier
|
|
|
|
* for use in resolving conflicts during replication operations.
|
2001-12-05 14:30:58 +08:00
|
|
|
*
|
2003-11-19 06:10:12 +08:00
|
|
|
* These routines are (loosly) based upon draft-ietf-ldup-model-03.txt,
|
|
|
|
* A WORK IN PROGRESS. The format will likely change.
|
2001-12-05 14:30:58 +08:00
|
|
|
*
|
2003-11-19 06:10:12 +08:00
|
|
|
* The format of a CSN string is: yyyymmddhhmmssz#s#r#c
|
|
|
|
* where s is a counter of operations within a timeslice, r is
|
|
|
|
* the replica id (normally zero), and c is a counter of
|
|
|
|
* modifications within this operation. s, r, and c are
|
2007-02-03 07:10:30 +08:00
|
|
|
* represented in hex and zero padded to lengths of 6, 3, and
|
|
|
|
* 6, respectively. (In previous implementations r was only 2 digits.)
|
2001-12-05 14:30:58 +08:00
|
|
|
*
|
|
|
|
* Calls to this routine MUST be serialized with other calls
|
|
|
|
* to gmtime().
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
2001-12-09 10:34:45 +08:00
|
|
|
#include <lutil.h>
|
|
|
|
|
2007-02-11 21:45:39 +08:00
|
|
|
/* Must be mutex-protected, because lutil_gettime needs mutex protection */
|
2001-12-05 15:25:25 +08:00
|
|
|
size_t
|
2001-12-05 14:30:58 +08:00
|
|
|
lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
|
|
|
|
{
|
2007-02-03 07:10:30 +08:00
|
|
|
struct lutil_tm tm;
|
2001-12-05 14:30:58 +08:00
|
|
|
int n;
|
|
|
|
|
2007-02-03 07:10:30 +08:00
|
|
|
lutil_gettime( &tm );
|
|
|
|
|
2003-11-19 06:10:12 +08:00
|
|
|
n = snprintf( buf, len,
|
2007-02-03 07:10:30 +08:00
|
|
|
"%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
|
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
|
2007-02-11 21:42:29 +08:00
|
|
|
tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
|
2001-12-05 14:30:58 +08:00
|
|
|
|
2001-12-09 10:34:45 +08:00
|
|
|
if( n < 0 ) return 0;
|
|
|
|
return ( (size_t) n < len ) ? n : 0;
|
2001-12-05 14:30:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2002-08-31 18:48:02 +08:00
|
|
|
char buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
|
2001-12-05 14:30:58 +08:00
|
|
|
|
|
|
|
if ( ! lutil_csnstr( buf, (size_t) 10, 0, 0 ) ) {
|
|
|
|
fprintf(stderr, "failed lutil_csnstr\n");
|
|
|
|
}
|
|
|
|
if ( ! lutil_csnstr( buf, sizeof(buf), 0, 0 ) ) {
|
|
|
|
fprintf(stderr, "failed lutil_csnstr\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|