2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
32 * replog.c - routines which read and write replication log files.
39 #include <ac/stdlib.h>
42 #include <ac/string.h>
43 #include <ac/syslog.h>
45 #include <ac/unistd.h>
55 * Copy the replication log. Returns 0 on success, 1 if a temporary
56 * error occurs, and -1 if a fatal error occurs.
65 FILE *rfp; /* replog fp */
66 FILE *lfp; /* replog lockfile fp */
67 FILE *dfp; /* duplicate replog fp */
68 FILE *dlfp; /* duplicate replog lockfile fp */
69 static char buf[ MAXPATHLEN ];
70 static char rbuf[ 1024 ];
74 LDAP_LOG ( SLURPD, ARGS, "copy_replog: "
75 "copy replog \"%s\" to \"%s\"\n", src, dst, 0 );
77 Debug( LDAP_DEBUG_ARGS,
78 "copy replog \"%s\" to \"%s\"\n",
83 * Make sure the destination directory is writable. If not, exit
87 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
92 if ( access( buf, W_OK ) < 0 ) {
94 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
95 "Error: (%ld): Directory %s is not writable\n",
96 (long) getpid(), buf, 0 );
98 Debug( LDAP_DEBUG_ANY,
99 "Error: copy_replog (%ld): Directory %s is not writable\n",
100 (long) getpid(), buf, 0 );
105 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
110 if ( access( buf, W_OK ) < 0 ) {
112 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
113 "Error: (%ld): Directory %s is not writable\n",
114 (long) getpid(), buf, 0 );
116 Debug( LDAP_DEBUG_ANY,
117 "Error: copy_replog (%ld): Directory %s is not writable\n",
118 (long) getpid(), buf, 0 );
124 rfp = lock_fopen( src, "r", &lfp );
127 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
128 "Error: Can't lock replog \"%s\" for read: %s\n",
129 src, sys_errlist[ errno ], 0 );
131 Debug( LDAP_DEBUG_ANY,
132 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
133 src, sys_errlist[ errno ], 0 );
139 dfp = lock_fopen( dst, "a", &dlfp );
142 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
143 "Error: Can't lock replog \"%s\" for write: %s\n",
144 src, sys_errlist[ errno ], 0 );
146 Debug( LDAP_DEBUG_ANY,
147 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
148 src, sys_errlist[ errno ], 0 );
150 lock_fclose( rfp, lfp );
155 * Make our own private copy of the replication log.
157 while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
160 /* Only truncate the source file if we're not in one-shot mode */
161 if ( !sglob->one_shot_mode ) {
162 /* truncate replication log */
163 truncate( src, (off_t) 0 );
166 if ( lock_fclose( dfp, dlfp ) == EOF ) {
168 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
169 "Error: Error closing \"%s\"\n", src, 0, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "Error: copy_replog: Error closing \"%s\"\n",
176 if ( lock_fclose( rfp, lfp ) == EOF ) {
178 LDAP_LOG ( SLURPD, ERR, "copy_replog: "
179 "Error: Error closing \"%s\"\n", src, 0, 0 );
181 Debug( LDAP_DEBUG_ANY,
182 "Error: copy_replog: Error closing \"%s\"\n",
193 * Return 1 if the given file exists and has a nonzero size,
194 * 0 if it is empty or nonexistent.
201 static struct stat stbuf;
203 if ( stat( filename, &stbuf ) < 0 ) {
206 return( stbuf.st_size > (off_t ) 0 );