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