]> git.sur5r.net Git - openldap/blob - servers/slurpd/replog.c
lots of cleanup; few improvements; fix RDN selection bug when creating connection...
[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     Debug( LDAP_DEBUG_ARGS,
74             "copy replog \"%s\" to \"%s\"\n", 
75             src, dst, 0 );
76
77     /*
78      * Make sure the destination directory is writable.  If not, exit
79      * with a fatal error.
80      */
81     strcpy( buf, src );
82     if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
83         strcpy( buf, "." );
84     } else {
85         *p = '\0';
86     }
87     if ( access( buf, W_OK ) < 0 ) {
88         Debug( LDAP_DEBUG_ANY,
89                 "Error: copy_replog (%ld): Directory %s is not writable\n",
90                 (long) getpid(), buf, 0 );
91         return( -1 );
92     }
93     strcpy( buf, dst );
94     if (( p = strrchr( buf, LDAP_DIRSEP[0] )) == NULL ) {
95         strcpy( buf, "." );
96     } else {
97         *p = '\0';
98     }
99     if ( access( buf, W_OK ) < 0 ) {
100         Debug( LDAP_DEBUG_ANY,
101                 "Error: copy_replog (%ld): Directory %s is not writable\n",
102                 (long) getpid(), buf, 0 );
103         return( -1 );
104     }
105
106     /* lock src */
107     rfp = lock_fopen( src, "r", &lfp );
108     if ( rfp == NULL ) {
109         Debug( LDAP_DEBUG_ANY,
110                 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
111                 src, sys_errlist[ errno ], 0 );
112         return( 1 );
113     }
114
115     /* lock dst */
116     dfp = lock_fopen( dst, "a", &dlfp );
117     if ( dfp == NULL ) {
118                 Debug( LDAP_DEBUG_ANY,
119                         "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
120                         dst, sys_errlist[ errno ], 0 );
121         lock_fclose( rfp, lfp );
122         return( 1 );
123     }
124
125     /*
126      * Make our own private copy of the replication log.
127      */
128     while (( p = fgets( rbuf, sizeof( rbuf ), rfp )) != NULL ) {
129         fputs( rbuf, dfp );
130     }
131     /* Only truncate the source file if we're not in one-shot mode */
132     if ( !sglob->one_shot_mode ) {
133         /* truncate replication log */
134         truncate( src, (off_t) 0 );
135     }
136
137     if ( lock_fclose( dfp, dlfp ) == EOF ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "Error: copy_replog: Error closing \"%s\"\n",
140                         dst, 0, 0 );
141     }
142     if ( lock_fclose( rfp, lfp ) == EOF ) {
143         Debug( LDAP_DEBUG_ANY,
144                 "Error: copy_replog: Error closing \"%s\"\n",
145                 src, 0, 0 );
146     }
147     return( rc );
148 }
149
150
151
152
153 /*
154  * Return 1 if the given file exists and has a nonzero size,
155  * 0 if it is empty or nonexistent.
156  */
157 int
158 file_nonempty(
159     char        *filename
160 )
161 {
162     static struct stat  stbuf;
163
164     if ( stat( filename, &stbuf ) < 0 ) {
165         return( 0 );
166     } else {
167         return( stbuf.st_size > (off_t ) 0 );
168     }
169 }