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