]> git.sur5r.net Git - openldap/blob - servers/slurpd/replog.c
a9ecdb7ecc60ec4b3a64baddede5a1af7ee7babf
[openldap] / servers / slurpd / replog.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13
14 /*
15  * replog.c - routines which read and write replication log files.
16  */
17
18 #include "portable.h"
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <syslog.h>
23 #include <ac/time.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <ac/string.h>
30
31 #include "slurp.h"
32 #include "globals.h"
33
34 /*
35  * Externs
36  */
37 extern FILE *lock_fopen LDAP_P(( char *, char *, FILE ** ));
38 extern char *ch_malloc LDAP_P(( unsigned long ));
39
40 /*
41  * Forward declarations
42  */
43 int file_nonempty LDAP_P(( char * ));
44
45
46 #ifdef DECL_SYS_ERRLIST
47 extern char *sys_errlist[];
48 #endif
49
50 /*
51  * Forward declarations
52  */
53 static int duplicate_replog( char *, char * );
54
55
56 /*
57  * Copy the replication log.  Returns 0 on success, 1 if a temporary
58  * error occurs, and -1 if a fatal error occurs.
59  */
60 int
61 copy_replog(
62     char        *src,
63     char        *dst
64 )
65 {
66     int         rc = 0;
67     FILE        *rfp;   /* replog fp */
68     FILE        *lfp;   /* replog lockfile fp */
69     FILE        *dfp;   /* duplicate replog fp */
70     FILE        *dlfp;  /* duplicate replog lockfile fp */
71     static char buf[ MAXPATHLEN ];
72     static char rbuf[ 1024 ];
73     char        *p;
74
75     Debug( LDAP_DEBUG_ARGS,
76             "copy replog \"%s\" to \"%s\"\n", 
77             src, dst, 0 );
78
79     /*
80      * Make sure the destination directory is writable.  If not, exit
81      * with a fatal error.
82      */
83     strcpy( buf, src );
84     if (( p = strrchr( buf, '/' )) == NULL ) {
85         strcpy( buf, "." );
86     } else {
87         *p = '\0';
88     }
89     if ( access( buf, W_OK ) < 0 ) {
90         Debug( LDAP_DEBUG_ANY,
91                 "Error: copy_replog (%d): Directory %s is not writable\n",
92                 getpid(), buf, 0 );
93         return( -1 );
94     }
95     strcpy( buf, dst );
96     if (( p = strrchr( buf, '/' )) == NULL ) {
97         strcpy( buf, "." );
98     } else {
99         *p = '\0';
100     }
101     if ( access( buf, W_OK ) < 0 ) {
102         Debug( LDAP_DEBUG_ANY,
103                 "Error: copy_replog (%d): Directory %s is not writable\n",
104                 getpid(), buf, 0 );
105         return( -1 );
106     }
107
108     /* lock src */
109     rfp = lock_fopen( src, "r", &lfp );
110     if ( rfp == NULL ) {
111         Debug( LDAP_DEBUG_ANY,
112                 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
113                 src, sys_errlist[ errno ], 0 );
114         return( 1 );
115     }
116
117     /* lock dst */
118     dfp = lock_fopen( dst, "a", &dlfp );
119     if ( dfp == NULL ) {
120         Debug( LDAP_DEBUG_ANY,
121                 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
122                 src, sys_errlist[ errno ], 0 );
123         lock_fclose( rfp );
124         return( 1 );
125     }
126
127     /*
128      * Make our own private copy of the replication log.
129      */
130     while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
131         fputs( rbuf, dfp );
132     }
133     /* Only truncate the source file if we're not in one-shot mode */
134     if ( !sglob->one_shot_mode ) {
135         /* truncate replication log */
136         truncate( src, (off_t) 0 );
137     }
138
139     if ( lock_fclose( rfp, lfp ) == EOF ) {
140         Debug( LDAP_DEBUG_ANY,
141                 "Error: copy_replog: Error closing \"%s\"\n",
142                 src, 0, 0 );
143     }
144     if ( lock_fclose( dfp, dlfp ) == EOF ) {
145         Debug( LDAP_DEBUG_ANY,
146                 "Error: copy_replog: Error closing \"%s\"\n",
147                 src, 0, 0 );
148     }
149     return( rc );
150 }
151
152
153
154
155 /*
156  * Return 1 if the given file exists and has a nonzero size,
157  * 0 if it is empty or nonexistent.
158  */
159 int
160 file_nonempty(
161     char        *filename
162 )
163 {
164     static struct stat  stbuf;
165
166     if ( stat( filename, &stbuf ) < 0 ) {
167         return( 0 );
168     } else {
169         return( stbuf.st_size > (off_t ) 0 );
170     }
171 }