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