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.
29 #include <ac/string.h>
30 #include <ac/syslog.h>
32 #include <ac/unistd.h>
42 * Copy the replication log. Returns 0 on success, 1 if a temporary
43 * error occurs, and -1 if a fatal error occurs.
52 FILE *rfp; /* replog fp */
53 FILE *lfp; /* replog lockfile fp */
54 FILE *dfp; /* duplicate replog fp */
55 FILE *dlfp; /* duplicate replog lockfile fp */
56 static char buf[ MAXPATHLEN ];
57 static char rbuf[ 1024 ];
61 LDAP_LOG (( "replog", LDAP_LEVEL_ARGS, "copy_replog: "
62 "copy replog \"%s\" to \"%s\"\n", src, dst ));
64 Debug( LDAP_DEBUG_ARGS,
65 "copy replog \"%s\" to \"%s\"\n",
70 * Make sure the destination directory is writable. If not, exit
74 if (( p = strrchr( buf, '/' )) == NULL ) {
79 if ( access( buf, W_OK ) < 0 ) {
81 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
82 "Error: (%ld): Directory %s is not writable\n",
83 (long) getpid(), buf ));
85 Debug( LDAP_DEBUG_ANY,
86 "Error: copy_replog (%ld): Directory %s is not writable\n",
87 (long) getpid(), buf, 0 );
92 if (( p = strrchr( buf, '/' )) == NULL ) {
97 if ( access( buf, W_OK ) < 0 ) {
99 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
100 "Error: (%ld): Directory %s is not writable\n",
101 (long) getpid(), buf ));
103 Debug( LDAP_DEBUG_ANY,
104 "Error: copy_replog (%ld): Directory %s is not writable\n",
105 (long) getpid(), buf, 0 );
111 rfp = lock_fopen( src, "r", &lfp );
114 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
115 "Error: Can't lock replog \"%s\" for read: %s\n",
116 src, sys_errlist[ errno ] ));
118 Debug( LDAP_DEBUG_ANY,
119 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
120 src, sys_errlist[ errno ], 0 );
126 dfp = lock_fopen( dst, "a", &dlfp );
129 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
130 "Error: Can't lock replog \"%s\" for write: %s\n",
131 src, sys_errlist[ errno ] ));
133 Debug( LDAP_DEBUG_ANY,
134 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
135 src, sys_errlist[ errno ], 0 );
137 lock_fclose( rfp, lfp );
142 * Make our own private copy of the replication log.
144 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
147 /* Only truncate the source file if we're not in one-shot mode */
148 if ( !sglob->one_shot_mode ) {
149 /* truncate replication log */
150 truncate( src, (off_t) 0 );
153 if ( lock_fclose( dfp, dlfp ) == EOF ) {
155 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
156 "Error: Error closing \"%s\"\n", src ));
158 Debug( LDAP_DEBUG_ANY,
159 "Error: copy_replog: Error closing \"%s\"\n",
163 if ( lock_fclose( rfp, lfp ) == EOF ) {
165 LDAP_LOG (( "replog", LDAP_LEVEL_ERR, "copy_replog: "
166 "Error: Error closing \"%s\"\n", src ));
168 Debug( LDAP_DEBUG_ANY,
169 "Error: copy_replog: Error closing \"%s\"\n",
180 * Return 1 if the given file exists and has a nonzero size,
181 * 0 if it is empty or nonexistent.
188 static struct stat stbuf;
190 if ( stat( filename, &stbuf ) < 0 ) {
193 return( stbuf.st_size > (off_t ) 0 );