]> git.sur5r.net Git - openldap/blob - servers/slurpd/replog.c
Happy new year
[openldap] / servers / slurpd / replog.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30
31 /*
32  * replog.c - routines which read and write replication log files.
33  */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38
39 #include <ac/stdlib.h>
40 #include <ac/errno.h>
41 #include <ac/param.h>
42 #include <ac/string.h>
43 #include <ac/syslog.h>
44 #include <ac/time.h>
45 #include <ac/unistd.h>
46
47 #include <sys/stat.h>
48
49 #include <fcntl.h>
50
51 #include "slurp.h"
52 #include "globals.h"
53
54 /*
55  * Copy the replication log.  Returns 0 on success, 1 if a temporary
56  * error occurs, and -1 if a fatal error occurs.
57  */
58 int
59 copy_replog(
60     char        *src,
61     char        *dst
62 )
63 {
64     int         rc = 0;
65     FILE        *rfp;   /* replog fp */
66     FILE        *lfp;   /* replog lockfile fp */
67     FILE        *dfp;   /* duplicate replog fp */
68     FILE        *dlfp;  /* duplicate replog lockfile fp */
69     static char buf[ MAXPATHLEN ];
70     static char rbuf[ 1024 ];
71     char        *p;
72
73 #ifdef NEW_LOGGING
74         LDAP_LOG ( SLURPD, ARGS, "copy_replog: "
75                 "copy replog \"%s\" to \"%s\"\n", src, dst, 0 );
76 #else
77     Debug( LDAP_DEBUG_ARGS,
78             "copy replog \"%s\" to \"%s\"\n", 
79             src, dst, 0 );
80 #endif
81
82     /*
83      * Make sure the destination directory is writable.  If not, exit
84      * with a fatal error.
85      */
86     strcpy( buf, src );
87     if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
88         strcpy( buf, "." );
89     } else {
90         *p = '\0';
91     }
92     if ( access( buf, W_OK ) < 0 ) {
93 #ifdef NEW_LOGGING
94         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
95                 "Error: (%ld): Directory %s is not writable\n",
96                 (long) getpid(), buf, 0 );
97 #else
98         Debug( LDAP_DEBUG_ANY,
99                 "Error: copy_replog (%ld): Directory %s is not writable\n",
100                 (long) getpid(), buf, 0 );
101 #endif
102         return( -1 );
103     }
104     strcpy( buf, dst );
105     if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
106         strcpy( buf, "." );
107     } else {
108         *p = '\0';
109     }
110     if ( access( buf, W_OK ) < 0 ) {
111 #ifdef NEW_LOGGING
112         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
113                 "Error: (%ld): Directory %s is not writable\n",
114                 (long) getpid(), buf, 0 );
115 #else
116         Debug( LDAP_DEBUG_ANY,
117                 "Error: copy_replog (%ld): Directory %s is not writable\n",
118                 (long) getpid(), buf, 0 );
119 #endif
120         return( -1 );
121     }
122
123     /* lock src */
124     rfp = lock_fopen( src, "r", &lfp );
125     if ( rfp == NULL ) {
126 #ifdef NEW_LOGGING
127         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
128                 "Error: Can't lock replog \"%s\" for read: %s\n",
129                 src, sys_errlist[ errno ], 0 );
130 #else
131         Debug( LDAP_DEBUG_ANY,
132                 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
133                 src, sys_errlist[ errno ], 0 );
134 #endif
135         return( 1 );
136     }
137
138     /* lock dst */
139     dfp = lock_fopen( dst, "a", &dlfp );
140     if ( dfp == NULL ) {
141 #ifdef NEW_LOGGING
142         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
143                 "Error: Can't lock replog \"%s\" for write: %s\n",
144                 src, sys_errlist[ errno ], 0 );
145 #else
146         Debug( LDAP_DEBUG_ANY,
147                 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
148                 src, sys_errlist[ errno ], 0 );
149 #endif
150         lock_fclose( rfp, lfp );
151         return( 1 );
152     }
153
154     /*
155      * Make our own private copy of the replication log.
156      */
157     while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
158         fputs( rbuf, dfp );
159     }
160     /* Only truncate the source file if we're not in one-shot mode */
161     if ( !sglob->one_shot_mode ) {
162         /* truncate replication log */
163         truncate( src, (off_t) 0 );
164     }
165
166     if ( lock_fclose( dfp, dlfp ) == EOF ) {
167 #ifdef NEW_LOGGING
168         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
169                 "Error: Error closing \"%s\"\n", src, 0, 0 );
170 #else
171         Debug( LDAP_DEBUG_ANY,
172                 "Error: copy_replog: Error closing \"%s\"\n",
173                 src, 0, 0 );
174 #endif
175     }
176     if ( lock_fclose( rfp, lfp ) == EOF ) {
177 #ifdef NEW_LOGGING
178         LDAP_LOG ( SLURPD, ERR, "copy_replog: "
179                 "Error: Error closing \"%s\"\n", src, 0, 0 );
180 #else
181         Debug( LDAP_DEBUG_ANY,
182                 "Error: copy_replog: Error closing \"%s\"\n",
183                 src, 0, 0 );
184 #endif
185     }
186     return( rc );
187 }
188
189
190
191
192 /*
193  * Return 1 if the given file exists and has a nonzero size,
194  * 0 if it is empty or nonexistent.
195  */
196 int
197 file_nonempty(
198     char        *filename
199 )
200 {
201     static struct stat  stbuf;
202
203     if ( stat( filename, &stbuf ) < 0 ) {
204         return( 0 );
205     } else {
206         return( stbuf.st_size > (off_t ) 0 );
207     }
208 }