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