2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 * replog.c - routines which read and write replication log files.
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
40 * Copy the replication log. Returns 0 on success, 1 if a temporary
41 * error occurs, and -1 if a fatal error occurs.
50 FILE *rfp; /* replog fp */
51 FILE *lfp; /* replog lockfile fp */
52 FILE *dfp; /* duplicate replog fp */
53 FILE *dlfp; /* duplicate replog lockfile fp */
54 static char buf[ MAXPATHLEN ];
55 static char rbuf[ 1024 ];
58 Debug( LDAP_DEBUG_ARGS,
59 "copy replog \"%s\" to \"%s\"\n",
63 * Make sure the destination directory is writable. If not, exit
67 if (( p = strrchr( buf, '/' )) == NULL ) {
72 if ( access( buf, W_OK ) < 0 ) {
73 Debug( LDAP_DEBUG_ANY,
74 "Error: copy_replog (%ld): Directory %s is not writable\n",
75 (long) getpid(), buf, 0 );
79 if (( p = strrchr( buf, '/' )) == NULL ) {
84 if ( access( buf, W_OK ) < 0 ) {
85 Debug( LDAP_DEBUG_ANY,
86 "Error: copy_replog (%ld): Directory %s is not writable\n",
87 (long) getpid(), buf, 0 );
92 rfp = lock_fopen( src, "r", &lfp );
94 Debug( LDAP_DEBUG_ANY,
95 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
96 src, sys_errlist[ errno ], 0 );
101 dfp = lock_fopen( dst, "a", &dlfp );
103 Debug( LDAP_DEBUG_ANY,
104 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
105 src, sys_errlist[ errno ], 0 );
106 lock_fclose( rfp, lfp );
111 * Make our own private copy of the replication log.
113 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
116 /* Only truncate the source file if we're not in one-shot mode */
117 if ( !sglob->one_shot_mode ) {
118 /* truncate replication log */
119 truncate( src, (off_t) 0 );
122 if ( lock_fclose( dfp, dlfp ) == EOF ) {
123 Debug( LDAP_DEBUG_ANY,
124 "Error: copy_replog: Error closing \"%s\"\n",
127 if ( lock_fclose( rfp, lfp ) == EOF ) {
128 Debug( LDAP_DEBUG_ANY,
129 "Error: copy_replog: Error closing \"%s\"\n",
139 * Return 1 if the given file exists and has a nonzero size,
140 * 0 if it is empty or nonexistent.
147 static struct stat stbuf;
149 if ( stat( filename, &stbuf ) < 0 ) {
152 return( stbuf.st_size > (off_t ) 0 );