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