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