3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
20 * replog.c - routines which read and write replication log files.
27 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/syslog.h>
33 #include <ac/unistd.h>
43 * Copy the replication log. Returns 0 on success, 1 if a temporary
44 * error occurs, and -1 if a fatal error occurs.
53 FILE *rfp; /* replog fp */
54 FILE *lfp; /* replog lockfile fp */
55 FILE *dfp; /* duplicate replog fp */
56 FILE *dlfp; /* duplicate replog lockfile fp */
57 static char buf[ MAXPATHLEN ];
58 static char rbuf[ 1024 ];
62 LDAP_LOG ( SLURPD, ARGS, "copy_replog: "
63 "copy replog \"%s\" to \"%s\"\n", src, dst, 0 );
65 Debug( LDAP_DEBUG_ARGS,
66 "copy replog \"%s\" to \"%s\"\n",
71 * Make sure the destination directory is writable. If not, exit
75 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
80 if ( access( buf, W_OK ) < 0 ) {
82 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
83 "Error: (%ld): Directory %s is not writable\n",
84 (long) getpid(), buf, 0 );
86 Debug( LDAP_DEBUG_ANY,
87 "Error: copy_replog (%ld): Directory %s is not writable\n",
88 (long) getpid(), buf, 0 );
93 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
98 if ( access( buf, W_OK ) < 0 ) {
100 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
101 "Error: (%ld): Directory %s is not writable\n",
102 (long) getpid(), buf, 0 );
104 Debug( LDAP_DEBUG_ANY,
105 "Error: copy_replog (%ld): Directory %s is not writable\n",
106 (long) getpid(), buf, 0 );
112 rfp = lock_fopen( src, "r", &lfp );
115 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
116 "Error: Can't lock replog \"%s\" for read: %s\n",
117 src, sys_errlist[ errno ], 0 );
119 Debug( LDAP_DEBUG_ANY,
120 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
121 src, sys_errlist[ errno ], 0 );
127 dfp = lock_fopen( dst, "a", &dlfp );
130 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
131 "Error: Can't lock replog \"%s\" for write: %s\n",
132 src, sys_errlist[ errno ], 0 );
134 Debug( LDAP_DEBUG_ANY,
135 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
136 src, sys_errlist[ errno ], 0 );
138 lock_fclose( rfp, lfp );
143 * Make our own private copy of the replication log.
145 while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
148 /* Only truncate the source file if we're not in one-shot mode */
149 if ( !sglob->one_shot_mode ) {
150 /* truncate replication log */
151 truncate( src, (off_t) 0 );
154 if ( lock_fclose( dfp, dlfp ) == EOF ) {
156 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
157 "Error: Error closing \"%s\"\n", src, 0, 0 );
159 Debug( LDAP_DEBUG_ANY,
160 "Error: copy_replog: Error closing \"%s\"\n",
164 if ( lock_fclose( rfp, lfp ) == EOF ) {
166 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
167 "Error: Error closing \"%s\"\n", src, 0, 0 );
169 Debug( LDAP_DEBUG_ANY,
170 "Error: copy_replog: Error closing \"%s\"\n",
181 * Return 1 if the given file exists and has a nonzero size,
182 * 0 if it is empty or nonexistent.
189 static struct stat stbuf;
191 if ( stat( filename, &stbuf ) < 0 ) {
194 return( stbuf.st_size > (off_t ) 0 );