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.
25 #include <ac/string.h>
26 #include <ac/syslog.h>
28 #include <ac/unistd.h>
38 * Copy the replication log. Returns 0 on success, 1 if a temporary
39 * error occurs, and -1 if a fatal error occurs.
48 FILE *rfp; /* replog fp */
49 FILE *lfp; /* replog lockfile fp */
50 FILE *dfp; /* duplicate replog fp */
51 FILE *dlfp; /* duplicate replog lockfile fp */
52 static char buf[ MAXPATHLEN ];
53 static char rbuf[ 1024 ];
56 Debug( LDAP_DEBUG_ARGS,
57 "copy replog \"%s\" to \"%s\"\n",
61 * Make sure the destination directory is writable. If not, exit
65 if (( p = strrchr( buf, '/' )) == NULL ) {
70 if ( access( buf, W_OK ) < 0 ) {
71 Debug( LDAP_DEBUG_ANY,
72 "Error: copy_replog (%ld): Directory %s is not writable\n",
73 (long) getpid(), buf, 0 );
77 if (( p = strrchr( buf, '/' )) == NULL ) {
82 if ( access( buf, W_OK ) < 0 ) {
83 Debug( LDAP_DEBUG_ANY,
84 "Error: copy_replog (%ld): Directory %s is not writable\n",
85 (long) getpid(), buf, 0 );
90 rfp = lock_fopen( src, "r", &lfp );
92 Debug( LDAP_DEBUG_ANY,
93 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
94 src, sys_errlist[ errno ], 0 );
99 dfp = lock_fopen( dst, "a", &dlfp );
101 Debug( LDAP_DEBUG_ANY,
102 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
103 src, sys_errlist[ errno ], 0 );
104 lock_fclose( rfp, lfp );
109 * Make our own private copy of the replication log.
111 while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
114 /* Only truncate the source file if we're not in one-shot mode */
115 if ( !sglob->one_shot_mode ) {
116 /* truncate replication log */
117 truncate( src, (off_t) 0 );
120 if ( lock_fclose( dfp, dlfp ) == EOF ) {
121 Debug( LDAP_DEBUG_ANY,
122 "Error: copy_replog: Error closing \"%s\"\n",
125 if ( lock_fclose( rfp, lfp ) == EOF ) {
126 Debug( LDAP_DEBUG_ANY,
127 "Error: copy_replog: Error closing \"%s\"\n",
137 * Return 1 if the given file exists and has a nonzero size,
138 * 0 if it is empty or nonexistent.
145 static struct stat stbuf;
147 if ( stat( filename, &stbuf ) < 0 ) {
150 return( stbuf.st_size > (off_t ) 0 );