]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_close.c
Warning fixes.
[netatalk.git] / libatalk / cnid / cnid_close.c
1 /*
2  * $Id: cnid_close.c,v 1.30 2003-06-06 21:22:44 srittau 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 <atalk/logger.h>
18 #include <db.h>
19 #include <errno.h>
20 #include <string.h>
21
22 #include "cnid_private.h"
23 #include <atalk/cnid.h>
24
25 void cnid_close(void *CNID) {
26     CNID_private *db;
27
28     if (!(db = CNID)) {
29         return;
30     }
31
32 #ifndef CNID_DB_CDB
33     /* Flush the transaction log and delete the log file if we can. */
34     if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
35         struct flock lock;
36
37     lock.l_type = F_WRLCK;
38     lock.l_whence = SEEK_SET;
39     lock.l_start = lock.l_len = 0;
40     if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
41             char **list, **first;
42
43
44             /* Checkpoint the databases until we can checkpoint no
45              * more. */
46 #if DB_VERSION_MAJOR >= 4
47 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1
48             db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
49 #else
50             rc = db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
51 #endif /* DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1 */
52 #else
53             rc = txn_checkpoint(db->dbenv, 0, 0, 0);
54 #endif /* DB_VERSION_MAJOR >= 4 */
55 #if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1)
56             while (rc == DB_INCOMPLETE) {
57 #if DB_VERSION_MAJOR >= 4
58                 rc = db->dbenv->txn_checkpoint(db->dbenv, 0, 0, 0);
59 #else
60                 rc = txn_checkpoint(db->dbenv, 0, 0, 0);
61 #endif /* DB_VERSION_MAJOR >= 4 */
62             }
63 #endif /* DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 1) */
64
65 #if DB_VERSION_MAJOR >= 4
66             if ((rc = db->dbenv->log_archive(db->dbenv, &list, DB_ARCH_ABS)) != 0) {
67 #elif DB_VERSION_MINOR > 2
68             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS)) != 0) {
69 #else /* DB_VERSION_MINOR < 2 */
70             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS, NULL)) != 0) {
71 #endif /* DB_VERSION_MINOR */
72                 LOG(log_error, logtype_default, "cnid_close: Unable to archive logfiles: %s", db_strerror(rc));
73             }
74
75             if (list != NULL) {
76                 for (first = list; *list != NULL; ++list) {
77                     if ((rc = remove(*list)) != 0) {
78 #ifdef DEBUG
79                             LOG(log_info, logtype_default, "cnid_close: failed to remove %s: %s", *list, strerror(rc));
80 #endif
81                         }
82                 }
83                 free(first);
84             }
85         }
86         (void)remove(db->lock_file);
87     }
88 #endif /* CNID_DB_CDB */
89
90     db->db_didname->close(db->db_didname, 0);
91     db->db_devino->close(db->db_devino, 0);
92     db->db_cnid->close(db->db_cnid, 0);
93 #ifdef FILE_MANGLING
94     db->db_mangle->close(db->db_mangle, 0);
95 #endif /* FILE_MANGLING */
96     db->dbenv->close(db->dbenv, 0);
97
98 #ifndef CNID_DB_CDB
99     if (db->lockfd > -1) {
100         close(db->lockfd); /* This will also release any locks we have. */
101     }
102 #endif /* CNID_DB_CDB */
103
104     free(db);
105 }
106 #endif /* CNID_DB */