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>
29 #include <sys/param.h>
36 * Copy the replication log. Returns 0 on success, 1 if a temporary
37 * error occurs, and -1 if a fatal error occurs.
46 FILE *rfp; /* replog fp */
47 FILE *lfp; /* replog lockfile fp */
48 FILE *dfp; /* duplicate replog fp */
49 FILE *dlfp; /* duplicate replog lockfile fp */
50 static char buf[ MAXPATHLEN ];
51 static char rbuf[ 1024 ];
54 Debug( LDAP_DEBUG_ARGS,
55 "copy replog \"%s\" to \"%s\"\n",
59 * Make sure the destination directory is writable. If not, exit
63 if (( p = strrchr( buf, '/' )) == NULL ) {
68 if ( access( buf, W_OK ) < 0 ) {
69 Debug( LDAP_DEBUG_ANY,
70 "Error: copy_replog (%ld): Directory %s is not writable\n",
71 (long) getpid(), buf, 0 );
75 if (( p = strrchr( buf, '/' )) == NULL ) {
80 if ( access( buf, W_OK ) < 0 ) {
81 Debug( LDAP_DEBUG_ANY,
82 "Error: copy_replog (%ld): Directory %s is not writable\n",
83 (long) getpid(), buf, 0 );
88 rfp = lock_fopen( src, "r", &lfp );
90 Debug( LDAP_DEBUG_ANY,
91 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
92 src, sys_errlist[ errno ], 0 );
97 dfp = lock_fopen( dst, "a", &dlfp );
99 Debug( LDAP_DEBUG_ANY,
100 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
101 src, sys_errlist[ errno ], 0 );
102 lock_fclose( rfp, lfp );
107 * Make our own private copy of the replication log.
109 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
112 /* Only truncate the source file if we're not in one-shot mode */
113 if ( !sglob->one_shot_mode ) {
114 /* truncate replication log */
115 truncate( src, (off_t) 0 );
118 if ( lock_fclose( dfp, dlfp ) == EOF ) {
119 Debug( LDAP_DEBUG_ANY,
120 "Error: copy_replog: Error closing \"%s\"\n",
123 if ( lock_fclose( rfp, lfp ) == EOF ) {
124 Debug( LDAP_DEBUG_ANY,
125 "Error: copy_replog: Error closing \"%s\"\n",
135 * Return 1 if the given file exists and has a nonzero size,
136 * 0 if it is empty or nonexistent.
143 static struct stat stbuf;
145 if ( stat( filename, &stbuf ) < 0 ) {
148 return( stbuf.st_size > (off_t ) 0 );