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