#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <ac/param.h>
#include <ac/string.h>
#include <ac/syslog.h>
#include <ac/time.h>
#include <ac/unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "slurp.h"
#include "globals.h"
int
copy_replog(
char *src,
char *dst
)
{
int rc = 0;
FILE *rfp;
FILE *lfp;
FILE *dfp;
FILE *dlfp;
static char buf[ MAXPATHLEN ];
static char rbuf[ 1024 ];
char *p;
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ARGS, "copy_replog: "
"copy replog \"%s\" to \"%s\"\n", src, dst, 0 );
#else
Debug( LDAP_DEBUG_ARGS,
"copy replog \"%s\" to \"%s\"\n",
src, dst, 0 );
#endif
strcpy( buf, src );
if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
strcpy( buf, "." );
} else {
*p = '\0';
}
if ( access( buf, W_OK ) < 0 ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: (%ld): Directory %s is not writable\n",
(long) getpid(), buf, 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog (%ld): Directory %s is not writable\n",
(long) getpid(), buf, 0 );
#endif
return( -1 );
}
strcpy( buf, dst );
if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
strcpy( buf, "." );
} else {
*p = '\0';
}
if ( access( buf, W_OK ) < 0 ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: (%ld): Directory %s is not writable\n",
(long) getpid(), buf, 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog (%ld): Directory %s is not writable\n",
(long) getpid(), buf, 0 );
#endif
return( -1 );
}
rfp = lock_fopen( src, "r", &lfp );
if ( rfp == NULL ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: Can't lock replog \"%s\" for read: %s\n",
src, sys_errlist[ errno ], 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
src, sys_errlist[ errno ], 0 );
#endif
return( 1 );
}
dfp = lock_fopen( dst, "a", &dlfp );
if ( dfp == NULL ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: Can't lock replog \"%s\" for write: %s\n",
src, sys_errlist[ errno ], 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
src, sys_errlist[ errno ], 0 );
#endif
lock_fclose( rfp, lfp );
return( 1 );
}
while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
fputs( rbuf, dfp );
}
if ( !sglob->one_shot_mode ) {
truncate( src, (off_t) 0 );
}
if ( lock_fclose( dfp, dlfp ) == EOF ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: Error closing \"%s\"\n", src, 0, 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog: Error closing \"%s\"\n",
src, 0, 0 );
#endif
}
if ( lock_fclose( rfp, lfp ) == EOF ) {
#ifdef NEW_LOGGING
LDAP_LOG ( SLURPD, ERR, "copy_replog: "
"Error: Error closing \"%s\"\n", src, 0, 0 );
#else
Debug( LDAP_DEBUG_ANY,
"Error: copy_replog: Error closing \"%s\"\n",
src, 0, 0 );
#endif
}
return( rc );
}
int
file_nonempty(
char *filename
)
{
static struct stat stbuf;
if ( stat( filename, &stbuf ) < 0 ) {
return( 0 );
} else {
return( stbuf.st_size > (off_t ) 0 );
}
}