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