openldap/libraries/liblutil/csn.c

81 lines
2.1 KiB
C
Raw Normal View History

2001-12-05 01:30:58 -05:00
/*
2003-01-03 15:20:47 -05:00
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
2001-12-05 01:30:58 -05:00
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/* Portions
* Copyright 2000, John E. Schimmel, All rights reserved.
* 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.
*/
/* Adapted for incorporatation into OpenLDAP by Kurt Zeilenga */
/*
* This file contains routines to generate a change sequence number. Every
* add, delete, and modification is given a unique identifier for use in
2001-12-05 01:30:58 -05:00
* resolving conflicts during replication operations.
*
* These routines are (loosly) based upon draft-ietf-ldup-model-03.txt,
* A WORK IN PROGRESS. The format will likely change.
2001-12-05 01:30:58 -05: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
* represented in hex and zero padded to lengths of 6, 2, and
* 6, respectively.
2001-12-05 01:30:58 -05: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-08 21:34:45 -05:00
#include <lutil.h>
size_t
2001-12-05 01:30:58 -05:00
lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
{
static time_t csntime;
static unsigned int csnop;
time_t t;
unsigned int op;
struct tm *ltm;
int n;
time( &t );
if ( t > csntime ) {
csntime = t;
csnop = 0;
}
op = ++csnop;
ltm = gmtime( &t );
n = snprintf( buf, len,
"%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour,
2001-12-05 01:30:58 -05:00
ltm->tm_min, ltm->tm_sec, op, replica, mod );
2001-12-08 21:34:45 -05:00
if( n < 0 ) return 0;
return ( (size_t) n < len ) ? n : 0;
2001-12-05 01:30:58 -05:00
}
#ifdef TEST
int
main(int argc, char **argv)
{
char buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
2001-12-05 01:30:58 -05: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