#include "portable.h"
#include <stdio.h>
#include <sys/stat.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "slurp.h"
#include "globals.h"
static int
Rq_lock(
Rq *rq
)
{
return( ldap_pvt_thread_mutex_lock( &rq->rq_mutex ));
}
static int
Rq_unlock(
Rq *rq
)
{
return( ldap_pvt_thread_mutex_unlock( &rq->rq_mutex ));
}
static Re *
Rq_gethead(
Rq *rq
)
{
return( rq == NULL ? NULL : rq->rq_head );
}
static Re *
Rq_getnext(
Re *re
)
{
if ( re == NULL ) {
return NULL;
} else {
return( re->re_getnext( re ));
}
}
static int
Rq_delhead(
Rq *rq
)
{
Re *savedhead;
int rc;
if ( rq == NULL ) {
return( -1 );
}
savedhead = rq->rq_head;
if ( savedhead == NULL ) {
return( 0 );
}
if ( savedhead->re_getrefcnt( savedhead ) != 0 ) {
Debug( LDAP_DEBUG_ANY, "Warning: attempt to delete when refcnt != 0\n",
0, 0, 0 );
return( -1 );
}
rq->rq_head = rq->rq_head->re_getnext( rq->rq_head );
rc = savedhead->re_free( savedhead );
rq->rq_nre--;
return( rc );
}
static int
Rq_add(
Rq *rq,
char *buf
)
{
Re *re;
int wasempty = 0;
rq->rq_lock( rq );
if ( Re_init( &re ) < 0 ) {
rq->rq_unlock( rq );
return -1;
}
if ( re->re_parse( re, buf ) < 0 ) {
re->re_free( re );
rq->rq_unlock( rq );
return -1;
}
if ( rq->rq_head == NULL ) {
rq->rq_head = re;
rq->rq_tail = re;
wasempty = 1;
} else {
rq->rq_tail->re_next = re;
}
re->re_seq = 0;
if ( !wasempty && ( rq->rq_tail->re_timestamp == re->re_timestamp )) {
re->re_seq = rq->rq_tail->re_seq + 1;
}
rq->rq_tail = re;
rq->rq_nre++;
ldap_pvt_thread_cond_broadcast( &rq->rq_more );
rq->rq_unlock( rq );
return 0;
}
static void
Rq_gc(
Rq *rq
)
{
if ( rq == NULL ) {
Debug( LDAP_DEBUG_ANY, "Rq_gc: rq is NULL!\n", 0, 0, 0 );
return;
}
rq->rq_lock( rq );
while (( rq->rq_head != NULL ) &&
( rq->rq_head->re_getrefcnt( rq->rq_head ) == 0 )) {
rq->rq_delhead( rq );
rq->rq_ndel++;
}
rq->rq_unlock( rq );
return;
}
static void
Rq_dump(
Rq *rq
)
{
Re *re;
FILE *fp;
int tmpfd;
if ( rq == NULL ) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
return;
}
if (unlink(SLURPD_DUMPFILE) == -1 && errno != ENOENT) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: \"%s\" exists, and cannot unlink\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
if (( tmpfd = open(SLURPD_DUMPFILE, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
if (( fp = fdopen( tmpfd, "w" )) == NULL ) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot fdopen \"%s\" for write\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
rq->rq_lock( rq );
for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
re->re_dump( re, fp );
}
rq->rq_unlock( rq );
fclose( fp );
return;
}
static int
Rq_write(
Rq *rq,
FILE *fp
)
{
Re *re;
time_t now;
if ( rq == NULL ) {
return -1;
}
Debug( LDAP_DEBUG_ARGS, "re-write on-disk replication log\n",
0, 0, 0 );
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
fseek( fp, 0L, SEEK_SET );
rq->rq_lock( rq );
for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
if ( re->re_write( NULL, re, fp ) < 0 ) {
fflush( fp );
rq->rq_unlock( rq );
return -1;
}
}
fflush( fp );
sglob->srpos = ftell( fp );
if ( ftruncate( fileno( fp ), sglob->srpos ) < 0 ) {
Debug( LDAP_DEBUG_ANY, "Error truncating replication log: %s\n",
sys_errlist[ errno ], 0, 0 );
}
rq->rq_ndel = 0;
time( &now );
rq->rq_lasttrim = now;
rq->rq_unlock( rq );
return 0;
}
static int
Rq_needtrim(
Rq *rq
)
{
int rc = 0;
time_t now;
if ( rq == NULL ) {
return 0;
}
rq->rq_lock( rq );
time( &now );
if ( now > ( rq->rq_lasttrim + TRIMCHECK_INTERVAL )) {
rc = ( rq->rq_ndel >= 50 );
} else {
rc = 0;
}
rq->rq_unlock( rq );
return rc;
}
static int
Rq_getcount(
Rq *rq,
int type
)
{
int count = 0;
Re *re;
if ( rq == NULL ) {
return 0;
}
rq->rq_lock( rq );
if ( type == RQ_COUNT_ALL ) {
count = rq->rq_nre;
} else {
for ( re = rq->rq_gethead( rq ); re != NULL;
re = rq->rq_getnext( re )) {
if ( type == RQ_COUNT_NZRC ) {
if ( re->re_getrefcnt( re ) > 0 ) {
count++;
}
}
}
}
rq->rq_unlock( rq );
return count;
}
int
Rq_init(
Rq **rq
)
{
(*rq) = (Rq *) malloc( sizeof( Rq ));
if ( *rq == NULL ) {
return -1;
}
(*rq)->rq_gethead = Rq_gethead;
(*rq)->rq_getnext = Rq_getnext;
(*rq)->rq_delhead = Rq_delhead;
(*rq)->rq_add = Rq_add;
(*rq)->rq_gc = Rq_gc;
(*rq)->rq_lock = Rq_lock;
(*rq)->rq_unlock = Rq_unlock;
(*rq)->rq_dump = Rq_dump;
(*rq)->rq_needtrim = Rq_needtrim;
(*rq)->rq_write = Rq_write;
(*rq)->rq_getcount = Rq_getcount;
ldap_pvt_thread_mutex_init( &((*rq)->rq_mutex) );
ldap_pvt_thread_cond_init( &((*rq)->rq_more) );
(*rq)->rq_head = NULL;
(*rq)->rq_tail = NULL;
(*rq)->rq_nre = 0;
(*rq)->rq_ndel = 0;
(*rq)->rq_lasttrim = (time_t) 0L;
return 0;
}