]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_close.c
Clean up code after all the recent commits.
[netatalk.git] / libatalk / cnid / cnid_close.c
1 /*
2  * $Id: cnid_close.c,v 1.16 2001-12-13 03:31:34 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     CNID_private *db;
28     int rc;
29
30     if (!(db = CNID)) {
31         return;
32     }
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         char wd[MAXPATHLEN + 1];
38
39         /* Save the current working directory so we can restore it
40          * when we're done. */
41         getcwd(wd, MAXPATHLEN);
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             /* Checkpoint the databases until we can checkpoint no
50              * more. */
51             rc = txn_checkpoint(db->dbenv, 0, 0, 0);
52             while (rc == DB_INCOMPLETE) {
53                 rc = txn_checkpoint(db->dbenv, 0, 0, 0);
54             }
55
56             chdir(db->dbenv->db_log_dir ? db->dbenv->db_log_dir : db->dbenv->db_home);
57 #if DB_VERSION_MINOR > 2
58             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_LOG)) != 0) {
59 #else /* DB_VERSION_MINOR < 2 */
60             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_LOG, NULL)) != 0) {
61 #endif /* DB_VERSION_MINOR */
62                 syslog(LOG_ERR, "cnid_close: Unable to archive logfiles: %s",
63                        db_strerror(rc));
64             }
65
66             if (list != NULL) {
67                 for (first = list; *list != NULL; ++list) {
68                     if ((rc = remove(*list)) != 0) {
69 #ifdef DEBUG
70                         syslog(LOG_INFO, "cnid_close: failed to remove %s: %s",
71                                *list, strerror(rc));
72 #endif
73                     }
74                 }
75                 free(first);
76             }
77             chdir(wd);
78         }
79     }
80
81     db->db_didname->close(db->db_didname, 0);
82     db->db_devino->close(db->db_devino, 0);
83     db->db_cnid->close(db->db_cnid, 0);
84     db->dbenv->close(db->dbenv, 0);
85
86     if (db->lockfd > -1) {
87         close(db->lockfd); /* This will also release any locks we have. */
88     }
89
90     free(db);
91 }
92 #endif /* CNID_DB */