]> git.sur5r.net Git - openldap/blob - servers/slapd/lock.c
When recreating a database from an ldif file created by ldbmcat,
[openldap] / servers / slapd / lock.c
1 /* lock.c - routines to open and apply an advisory lock to a file */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9 #include <ac/time.h>
10 #include <ac/unistd.h>
11
12 #ifdef HAVE_SYS_FILE_H
13 #include <sys/file.h>
14 #endif
15 #ifdef HAVE_SYS_PARAM_H
16 #include <sys/param.h>
17 #endif
18 #include "slap.h"
19
20 FILE *
21 lock_fopen( char *fname, char *type, FILE **lfp )
22 {
23         FILE    *fp;
24         char    buf[MAXPATHLEN];
25
26         /* open the lock file */
27         strcpy( buf, fname );
28         strcat( buf, ".lock" );
29         if ( (*lfp = fopen( buf, "w" )) == NULL ) {
30                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", buf, 0, 0 );
31                 return( NULL );
32         }
33
34         /* acquire the lock */
35         ldap_lockf( fileno(*lfp) );
36
37         /* open the log file */
38         if ( (fp = fopen( fname, type )) == NULL ) {
39                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\"\n", fname, 0, 0 );
40                 ldap_unlockf( fileno(*lfp) );
41                 fclose( *lfp );
42                 *lfp = NULL;
43                 return( NULL );
44         }
45
46         return( fp );
47 }
48
49 int
50 lock_fclose( FILE *fp, FILE *lfp )
51 {
52         /* unlock */
53         ldap_unlockf( fileno(lfp) );
54         fclose( lfp );
55
56         return( fclose( fp ) );
57 }