]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/db3/cnid_db3_close.c
big merge for db frontend and unicode.
[netatalk.git] / libatalk / cnid / db3 / cnid_db3_close.c
1 /*
2  * $Id: cnid_db3_close.c,v 1.1.4.1 2003-09-09 16:42:21 didg Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #ifdef CNID_BACKEND_DB3
10
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif /* HAVE_UNISTD_H */
14 #ifdef HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif /* HAVE_FCNTL_H */
17 #include <stdlib.h>
18 #include <atalk/logger.h>
19 #include <db.h>
20 #include <errno.h>
21 #include <string.h>
22
23 #include "cnid_db3_private.h"
24 #include "cnid_db3.h"
25
26 void cnid_db3_close(struct _cnid_db *cdb) {
27     CNID_private *db;
28     int rc;
29
30     if (!cdb) {
31             LOG(log_error, logtype_afpd, "cnid_close called with NULL argument !");
32             return;
33     }
34
35     if (!(db = cdb->_private)) {
36         return;
37     }
38
39     /* Flush the transaction log and delete the log file if we can. */
40     if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
41         struct flock lock;
42
43     lock.l_type = F_WRLCK;
44     lock.l_whence = SEEK_SET;
45     lock.l_start = lock.l_len = 0;
46     if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
47             char **list, **first;
48
49
50             /* Checkpoint the databases until we can checkpoint no
51              * more. */
52 #if DB_VERSION_MAJOR >= 4
53 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1
54             db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
55 #else
56             rc = db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
57 #endif /* DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1 */
58 #else
59             rc = txn_checkpoint(db->dbenv, 0, 0, 0);
60 #endif /* DB_VERSION_MAJOR >= 4 */
61 #if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1)
62             while (rc == DB_INCOMPLETE) {
63 #if DB_VERSION_MAJOR >= 4
64                 rc = db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
65 #else
66                 rc = txn_checkpoint(db->dbenv, 0, 0, 0);
67 #endif /* DB_VERSION_MAJOR >= 4 */
68             }
69 #endif /* DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1) */
70
71 #if DB_VERSION_MAJOR >= 4
72             if ((rc = db->dbenv->log_archive(db->dbenv, &list, DB_ARCH_ABS)) != 0) {
73 #elif DB_VERSION_MINOR > 2
74             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS)) != 0) {
75 #else /* DB_VERSION_MINOR < 2 */
76             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS, NULL)) != 0) {
77 #endif /* DB_VERSION_MINOR */
78                 LOG(log_error, logtype_default, "cnid_close: Unable to archive logfiles: %s", db_strerror(rc));
79             }
80
81             if (list != NULL) {
82                 for (first = list; *list != NULL; ++list) {
83                     if ((rc = remove(*list)) != 0) {
84 #ifdef DEBUG
85                             LOG(log_info, logtype_default, "cnid_close: failed to remove %s: %s", *list, strerror(rc));
86 #endif
87                         }
88                 }
89                 free(first);
90             }
91         }
92         (void)remove(db->lock_file);
93     }
94
95     db->db_didname->close(db->db_didname, 0);
96     db->db_devino->close(db->db_devino, 0);
97     db->db_cnid->close(db->db_cnid, 0);
98     db->dbenv->close(db->dbenv, 0);
99
100     if (db->lockfd > -1) {
101         close(db->lockfd); /* This will also release any locks we have. */
102     }
103
104     free(db);
105     free(cdb->volpath);
106     free(cdb);
107 }
108
109 #endif /* CNID_BACKEND_DB3 */