3 * Copyright 1998-2000 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 ];
60 Debug( LDAP_DEBUG_ARGS,
61 "copy replog \"%s\" to \"%s\"\n",
65 * Make sure the destination directory is writable. If not, exit
69 if (( p = strrchr( buf, '/' )) == NULL ) {
74 if ( access( buf, W_OK ) < 0 ) {
75 Debug( LDAP_DEBUG_ANY,
76 "Error: copy_replog (%ld): Directory %s is not writable\n",
77 (long) getpid(), buf, 0 );
81 if (( p = strrchr( buf, '/' )) == NULL ) {
86 if ( access( buf, W_OK ) < 0 ) {
87 Debug( LDAP_DEBUG_ANY,
88 "Error: copy_replog (%ld): Directory %s is not writable\n",
89 (long) getpid(), buf, 0 );
94 rfp = lock_fopen( src, "r", &lfp );
96 Debug( LDAP_DEBUG_ANY,
97 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
98 src, sys_errlist[ errno ], 0 );
103 dfp = lock_fopen( dst, "a", &dlfp );
105 Debug( LDAP_DEBUG_ANY,
106 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
107 src, sys_errlist[ errno ], 0 );
108 lock_fclose( rfp, lfp );
113 * Make our own private copy of the replication log.
115 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
118 /* Only truncate the source file if we're not in one-shot mode */
119 if ( !sglob->one_shot_mode ) {
120 /* truncate replication log */
121 truncate( src, (off_t) 0 );
124 if ( lock_fclose( dfp, dlfp ) == EOF ) {
125 Debug( LDAP_DEBUG_ANY,
126 "Error: copy_replog: Error closing \"%s\"\n",
129 if ( lock_fclose( rfp, lfp ) == EOF ) {
130 Debug( LDAP_DEBUG_ANY,
131 "Error: copy_replog: Error closing \"%s\"\n",
141 * Return 1 if the given file exists and has a nonzero size,
142 * 0 if it is empty or nonexistent.
149 static struct stat stbuf;
151 if ( stat( filename, &stbuf ) < 0 ) {
154 return( stbuf.st_size > (off_t ) 0 );