2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 ];
73 Debug( LDAP_DEBUG_ARGS,
74 "copy replog \"%s\" to \"%s\"\n",
78 * Make sure the destination directory is writable. If not, exit
82 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
87 if ( access( buf, W_OK ) < 0 ) {
88 Debug( LDAP_DEBUG_ANY,
89 "Error: copy_replog (%ld): Directory %s is not writable\n",
90 (long) getpid(), buf, 0 );
94 if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
99 if ( access( buf, W_OK ) < 0 ) {
100 Debug( LDAP_DEBUG_ANY,
101 "Error: copy_replog (%ld): Directory %s is not writable\n",
102 (long) getpid(), buf, 0 );
107 rfp = lock_fopen( src, "r", &lfp );
109 Debug( LDAP_DEBUG_ANY,
110 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
111 src, sys_errlist[ errno ], 0 );
116 dfp = lock_fopen( dst, "a", &dlfp );
118 Debug( LDAP_DEBUG_ANY,
119 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
120 dst, sys_errlist[ errno ], 0 );
121 lock_fclose( rfp, lfp );
126 * Make our own private copy of the replication log.
128 while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
131 /* Only truncate the source file if we're not in one-shot mode */
132 if ( !sglob->one_shot_mode ) {
133 /* truncate replication log */
134 truncate( src, (off_t) 0 );
137 if ( lock_fclose( dfp, dlfp ) == EOF ) {
138 Debug( LDAP_DEBUG_ANY,
139 "Error: copy_replog: Error closing \"%s\"\n",
142 if ( lock_fclose( rfp, lfp ) == EOF ) {
143 Debug( LDAP_DEBUG_ANY,
144 "Error: copy_replog: Error closing \"%s\"\n",
154 * Return 1 if the given file exists and has a nonzero size,
155 * 0 if it is empty or nonexistent.
162 static struct stat stbuf;
164 if ( stat( filename, &stbuf ) < 0 ) {
167 return( stbuf.st_size > (off_t ) 0 );