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