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