]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_close.c
Fix the log_archive function call so that it is compatible with db 3 3.3.x.
[netatalk.git] / libatalk / cnid / cnid_close.c
1 /*
2  * $Id: cnid_close.c,v 1.8 2001-09-28 14:51:47 jmarcus Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #ifdef CNID_DB
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif /* HAVE_UNISTD_H */
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h>
15 #endif /* HAVE_FCNTL_H */
16 #include <stdlib.h>
17 #include <syslog.h>
18 #include <db.h>
19 #include <errno.h>
20 #include <string.h>
21
22 #include <atalk/cnid.h>
23
24 #include "cnid_private.h"
25
26 void cnid_close(void *CNID)
27 {
28   CNID_private *db;
29   int rc = 0;
30
31   if (!(db = CNID))
32     return;
33
34   /* flush the transaction log and delete the log file if we can. */
35   if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
36     struct flock lock;
37
38     lock.l_type = F_WRLCK;
39     lock.l_whence = SEEK_SET;
40     lock.l_start = lock.l_len = 0;
41     if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
42       char **list, **first;
43
44       rc = txn_checkpoint(db->dbenv, 0, 0, 0);
45       while (rc == DB_INCOMPLETE)
46                 rc = txn_checkpoint(db->dbenv, 0, 0, 0);
47
48       /* we've checkpointed, so clean up the log files.
49        * NOTE: any real problems will make log_archive return an error. */
50       chdir(db->dbenv->db_log_dir ? db->dbenv->db_log_dir : db->dbenv->db_home);
51 #if DB_VERSION_MINOR > 2
52       if (!log_archive(db->dbenv, &first, DB_ARCH_LOG)) {
53 #else /* DB_VERSION_MINOR < 2 */
54       if (!log_archive(db->dbenv, &first, DB_ARCH_LOG, NULL)) {
55 #endif /* DB_VERSION_MINOR */
56         list = first;
57         while (*list) {
58           if (truncate(*list, 0) < 0)
59             syslog(LOG_INFO, "cnid_close: failed to truncate %s: %s",
60                    *list, strerror(errno));
61           list++;
62         }
63         free(first); 
64       }
65     }
66   }
67
68   db->db_didname->close(db->db_didname, 0);
69   db->db_devino->close(db->db_devino, 0);
70   db->db_cnid->close(db->db_cnid, 0);
71   db->dbenv->close(db->dbenv, 0);
72   
73   if (db->lockfd > -1)
74     close(db->lockfd); /* this will also close any lock we have. */
75
76   free(db);
77 }
78 #endif /* CNID_DB */