]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_close.c
in db result checking replaced EAGAIN by DB_LOCK_DEADLOCK; fixed a potential transact...
[netatalk.git] / libatalk / cnid / cnid_close.c
1 /*
2  * $Id: cnid_close.c,v 1.5 2001-08-16 14:30:29 uhees 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 <stdlib.h>
16 #include <syslog.h>
17 #include <db.h>
18 #include <errno.h>
19 #include <string.h>
20
21 #include <atalk/cnid.h>
22
23 #include "cnid_private.h"
24
25 void cnid_close(void *CNID)
26 {
27   CNID_private *db;
28
29   if (!(db = CNID))
30     return;
31
32   /* flush the transaction log and delete the log file if we can. */
33   if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
34     struct flock lock;
35
36     lock.l_type = F_WRLCK;
37     lock.l_whence = SEEK_SET;
38     lock.l_start = lock.l_len = 0;
39     if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
40       char **list, **first;
41
42       errno = txn_checkpoint(db->dbenv, 0, 0, 0);
43       while (errno == DB_INCOMPLETE)
44                 errno = txn_checkpoint(db->dbenv, 0, 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, &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: %s",
54                    *list, strerror(errno));
55           list++;
56         }
57         free(first); 
58       }
59     }
60   }
61
62   db->db_didname->close(db->db_didname, 0);
63   db->db_devino->close(db->db_devino, 0);
64   db->db_cnid->close(db->db_cnid, 0);
65   db->dbenv->close(db->dbenv, 0);
66   
67   if (db->lockfd > -1)
68     close(db->lockfd); /* this will also close any lock we have. */
69
70   free(db);
71 }