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