]> git.sur5r.net Git - openldap/blob - servers/slurpd/replog.c
f4c738061a339f47656ff3a62f3a3b18888b8ff1
[openldap] / servers / slurpd / replog.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14
15 /*
16  * replog.c - routines which read and write replication log files.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/errno.h>
24 #include <ac/string.h>
25 #include <ac/syslog.h>
26 #include <ac/time.h>
27 #include <ac/unistd.h>
28
29 #include <sys/stat.h>
30
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34
35 #include <fcntl.h>
36
37 #include "slurp.h"
38 #include "globals.h"
39
40 /*
41  * Copy the replication log.  Returns 0 on success, 1 if a temporary
42  * error occurs, and -1 if a fatal error occurs.
43  */
44 int
45 copy_replog(
46     char        *src,
47     char        *dst
48 )
49 {
50     int         rc = 0;
51     FILE        *rfp;   /* replog fp */
52     FILE        *lfp;   /* replog lockfile fp */
53     FILE        *dfp;   /* duplicate replog fp */
54     FILE        *dlfp;  /* duplicate replog lockfile fp */
55     static char buf[ MAXPATHLEN ];
56     static char rbuf[ 1024 ];
57     char        *p;
58
59     Debug( LDAP_DEBUG_ARGS,
60             "copy replog \"%s\" to \"%s\"\n", 
61             src, dst, 0 );
62
63     /*
64      * Make sure the destination directory is writable.  If not, exit
65      * with a fatal error.
66      */
67     strcpy( buf, src );
68     if (( p = strrchr( buf, '/' )) == NULL ) {
69         strcpy( buf, "." );
70     } else {
71         *p = '\0';
72     }
73     if ( access( buf, W_OK ) < 0 ) {
74         Debug( LDAP_DEBUG_ANY,
75                 "Error: copy_replog (%ld): Directory %s is not writable\n",
76                 (long) getpid(), buf, 0 );
77         return( -1 );
78     }
79     strcpy( buf, dst );
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 (%ld): Directory %s is not writable\n",
88                 (long) getpid(), buf, 0 );
89         return( -1 );
90     }
91
92     /* lock src */
93     rfp = lock_fopen( src, "r", &lfp );
94     if ( rfp == NULL ) {
95         Debug( LDAP_DEBUG_ANY,
96                 "Error: copy_replog: Can't lock replog \"%s\" for read: %s\n",
97                 src, sys_errlist[ errno ], 0 );
98         return( 1 );
99     }
100
101     /* lock dst */
102     dfp = lock_fopen( dst, "a", &dlfp );
103     if ( dfp == NULL ) {
104         Debug( LDAP_DEBUG_ANY,
105                 "Error: copy_replog: Can't lock replog \"%s\" for write: %s\n",
106                 src, sys_errlist[ errno ], 0 );
107         lock_fclose( rfp, lfp );
108         return( 1 );
109     }
110
111     /*
112      * Make our own private copy of the replication log.
113      */
114     while (( p = fgets( rbuf, sizeof( buf ), rfp )) != NULL ) {
115         fputs( rbuf, dfp );
116     }
117     /* Only truncate the source file if we're not in one-shot mode */
118     if ( !sglob->one_shot_mode ) {
119         /* truncate replication log */
120         truncate( src, (off_t) 0 );
121     }
122
123     if ( lock_fclose( dfp, dlfp ) == EOF ) {
124         Debug( LDAP_DEBUG_ANY,
125                 "Error: copy_replog: Error closing \"%s\"\n",
126                 src, 0, 0 );
127     }
128     if ( lock_fclose( rfp, lfp ) == EOF ) {
129         Debug( LDAP_DEBUG_ANY,
130                 "Error: copy_replog: Error closing \"%s\"\n",
131                 src, 0, 0 );
132     }
133     return( rc );
134 }
135
136
137
138
139 /*
140  * Return 1 if the given file exists and has a nonzero size,
141  * 0 if it is empty or nonexistent.
142  */
143 int
144 file_nonempty(
145     char        *filename
146 )
147 {
148     static struct stat  stbuf;
149
150     if ( stat( filename, &stbuf ) < 0 ) {
151         return( 0 );
152     } else {
153         return( stbuf.st_size > (off_t ) 0 );
154     }
155 }