2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 * replog.c - routines which read and write replication log files.
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
29 #include <sys/param.h>
38 extern FILE *lock_fopen LDAP_P(( char *, char *, FILE ** ));
41 * Forward declarations
43 int file_nonempty LDAP_P(( char * ));
47 * Forward declarations
49 static int duplicate_replog( char *, char * );
53 * Copy the replication log. Returns 0 on success, 1 if a temporary
54 * error occurs, and -1 if a fatal error occurs.
63 FILE *rfp; /* replog fp */
64 FILE *lfp; /* replog lockfile fp */
65 FILE *dfp; /* duplicate replog fp */
66 FILE *dlfp; /* duplicate replog lockfile fp */
67 static char buf[ MAXPATHLEN ];
68 static char rbuf[ 1024 ];
71 Debug( LDAP_DEBUG_ARGS,
72 "copy replog \"%s\" to \"%s\"\n",
76 * Make sure the destination directory is writable. If not, exit
80 if (( p = strrchr( buf, '/' )) == NULL ) {
85 if ( access( buf, W_OK ) < 0 ) {
86 Debug( LDAP_DEBUG_ANY,
87 "Error: copy_replog (%d): Directory %s is not writable\n",
92 if (( p = strrchr( buf, '/' )) == NULL ) {
97 if ( access( buf, W_OK ) < 0 ) {
98 Debug( LDAP_DEBUG_ANY,
99 "Error: copy_replog (%d): Directory %s is not writable\n",
105 rfp = lock_fopen( src, "r", &lfp );
107 Debug( LDAP_DEBUG_ANY,
108 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
109 src, sys_errlist[ errno ], 0 );
114 dfp = lock_fopen( dst, "a", &dlfp );
116 Debug( LDAP_DEBUG_ANY,
117 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
118 src, sys_errlist[ errno ], 0 );
119 lock_fclose( rfp, lfp );
124 * Make our own private copy of the replication log.
126 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
129 /* Only truncate the source file if we're not in one-shot mode */
130 if ( !sglob->one_shot_mode ) {
131 /* truncate replication log */
132 truncate( src, (off_t) 0 );
135 if ( lock_fclose( dfp, dlfp ) == EOF ) {
136 Debug( LDAP_DEBUG_ANY,
137 "Error: copy_replog: Error closing \"%s\"\n",
140 if ( lock_fclose( rfp, lfp ) == EOF ) {
141 Debug( LDAP_DEBUG_ANY,
142 "Error: copy_replog: Error closing \"%s\"\n",
152 * Return 1 if the given file exists and has a nonzero size,
153 * 0 if it is empty or nonexistent.
160 static struct stat stbuf;
162 if ( stat( filename, &stbuf ) < 0 ) {
165 return( stbuf.st_size > (off_t ) 0 );