1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2001-01-21 01:49:05 +08:00
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
2001-01-21 01:49:05 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1996 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* replog.c - routines which read and write replication log files.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
2002-05-16 18:51:45 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/errno.h>
|
1999-11-02 01:21:24 +08:00
|
|
|
#include <ac/param.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/syslog.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/stat.h>
|
1999-03-02 06:37:05 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "slurp.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the replication log. Returns 0 on success, 1 if a temporary
|
|
|
|
* error occurs, and -1 if a fatal error occurs.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
copy_replog(
|
|
|
|
char *src,
|
|
|
|
char *dst
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
FILE *rfp; /* replog fp */
|
|
|
|
FILE *lfp; /* replog lockfile fp */
|
|
|
|
FILE *dfp; /* duplicate replog fp */
|
|
|
|
FILE *dlfp; /* duplicate replog lockfile fp */
|
|
|
|
static char buf[ MAXPATHLEN ];
|
|
|
|
static char rbuf[ 1024 ];
|
|
|
|
char *p;
|
|
|
|
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ARGS, "copy_replog: "
|
|
|
|
"copy replog \"%s\" to \"%s\"\n", src, dst, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
|
|
|
"copy replog \"%s\" to \"%s\"\n",
|
|
|
|
src, dst, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the destination directory is writable. If not, exit
|
|
|
|
* with a fatal error.
|
|
|
|
*/
|
|
|
|
strcpy( buf, src );
|
2002-05-16 18:51:45 +08:00
|
|
|
if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
strcpy( buf, "." );
|
|
|
|
} else {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
if ( access( buf, W_OK ) < 0 ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
2002-04-16 04:40:15 +08:00
|
|
|
"Error: (%ld): Directory %s is not writable\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
(long) getpid(), buf, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
"Error: copy_replog (%ld): Directory %s is not writable\n",
|
|
|
|
(long) getpid(), buf, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
strcpy( buf, dst );
|
2002-05-16 18:51:45 +08:00
|
|
|
if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
strcpy( buf, "." );
|
|
|
|
} else {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
if ( access( buf, W_OK ) < 0 ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
2002-04-16 04:40:15 +08:00
|
|
|
"Error: (%ld): Directory %s is not writable\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
(long) getpid(), buf, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
"Error: copy_replog (%ld): Directory %s is not writable\n",
|
|
|
|
(long) getpid(), buf, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock src */
|
|
|
|
rfp = lock_fopen( src, "r", &lfp );
|
|
|
|
if ( rfp == NULL ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
2002-04-16 04:40:15 +08:00
|
|
|
"Error: Can't lock replog \"%s\" for read: %s\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
src, sys_errlist[ errno ], 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
|
|
|
|
src, sys_errlist[ errno ], 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock dst */
|
|
|
|
dfp = lock_fopen( dst, "a", &dlfp );
|
|
|
|
if ( dfp == NULL ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
2002-04-16 04:40:15 +08:00
|
|
|
"Error: Can't lock replog \"%s\" for write: %s\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
src, sys_errlist[ errno ], 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
|
|
|
|
src, sys_errlist[ errno ], 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-11-12 03:17:25 +08:00
|
|
|
lock_fclose( rfp, lfp );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make our own private copy of the replication log.
|
|
|
|
*/
|
2002-09-17 05:35:33 +08:00
|
|
|
while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
fputs( rbuf, dfp );
|
|
|
|
}
|
|
|
|
/* Only truncate the source file if we're not in one-shot mode */
|
|
|
|
if ( !sglob->one_shot_mode ) {
|
|
|
|
/* truncate replication log */
|
|
|
|
truncate( src, (off_t) 0 );
|
|
|
|
}
|
|
|
|
|
1998-11-12 03:17:25 +08:00
|
|
|
if ( lock_fclose( dfp, dlfp ) == EOF ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
|
|
|
"Error: Error closing \"%s\"\n", src, 0, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: copy_replog: Error closing \"%s\"\n",
|
|
|
|
src, 0, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-11-12 03:17:25 +08:00
|
|
|
if ( lock_fclose( rfp, lfp ) == EOF ) {
|
2002-04-16 04:40:15 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
|
|
|
|
"Error: Error closing \"%s\"\n", src, 0, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Error: copy_replog: Error closing \"%s\"\n",
|
|
|
|
src, 0, 0 );
|
2002-04-16 04:40:15 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 if the given file exists and has a nonzero size,
|
|
|
|
* 0 if it is empty or nonexistent.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
file_nonempty(
|
|
|
|
char *filename
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static struct stat stbuf;
|
|
|
|
|
|
|
|
if ( stat( filename, &stbuf ) < 0 ) {
|
|
|
|
return( 0 );
|
|
|
|
} else {
|
|
|
|
return( stbuf.st_size > (off_t ) 0 );
|
|
|
|
}
|
|
|
|
}
|