]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_close.c
Add support for long filename mangling. Basically, tis code will take
[netatalk.git] / libatalk / cnid / cnid_close.c
1 /*
2  * $Id: cnid_close.c,v 1.23 2002-05-29 18:02:59 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 <atalk/logger.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
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             int cfd = -1;
44
45
46             /* Checkpoint the databases until we can checkpoint no
47              * more. */
48             rc = txn_checkpoint(db->dbenv, 0, 0, 0);
49             while (rc == DB_INCOMPLETE) {
50                 rc = txn_checkpoint(db->dbenv, 0, 0, 0);
51             }
52
53 #if DB_VERSION_MINOR > 2
54             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS)) != 0) {
55 #else /* DB_VERSION_MINOR < 2 */
56             if ((rc = log_archive(db->dbenv, &list, DB_ARCH_ABS, NULL)) != 0) {
57 #endif /* DB_VERSION_MINOR */
58                 LOG(log_error, logtype_default, "cnid_close: Unable to archive logfiles: %s", db_strerror(rc));
59             }
60
61             if (list != NULL) {
62                 for (first = list; *list != NULL; ++list) {
63                     if ((rc = remove(*list)) != 0) {
64 #ifdef DEBUG
65                         LOG(log_info, logtype_default, "cnid_close: failed to remove %s: %s", *list, strerror(rc));
66 #endif
67                     }
68                 }
69                 free(first);
70             }
71         }
72         (void)remove(db->lock_file);
73     }
74
75     db->db_didname->close(db->db_didname, 0);
76     db->db_devino->close(db->db_devino, 0);
77     db->db_cnid->close(db->db_cnid, 0);
78 #ifdef FILE_MANGLING
79     db->db_mangle->close(db->db_mangle, 0);
80 #endif /* FILE_MANGLING */
81     db->dbenv->close(db->dbenv, 0);
82
83     if (db->lockfd > -1) {
84         close(db->lockfd); /* This will also release any locks we have. */
85     }
86
87     free(db);
88 }
89 #endif /* CNID_DB */