/src/gdbm/src/gdbmclose.c
Line | Count | Source |
1 | | /* gdbmclose.c - Close a previously opened dbm file. */ |
2 | | |
3 | | /* This file is part of GDBM, the GNU data base manager. |
4 | | Copyright (C) 1990-2025 Free Software Foundation, Inc. |
5 | | |
6 | | GDBM is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3, or (at your option) |
9 | | any later version. |
10 | | |
11 | | GDBM is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with GDBM. If not, see <http://www.gnu.org/licenses/>. */ |
18 | | |
19 | | /* Include system configuration before all else. */ |
20 | | #include "autoconf.h" |
21 | | |
22 | | #include "gdbmdefs.h" |
23 | | |
24 | | /* Close the dbm file and free all memory associated with the file DBF. |
25 | | */ |
26 | | |
27 | | int |
28 | | gdbm_close (GDBM_FILE dbf) |
29 | 3.67k | { |
30 | 3.67k | int syserrno; |
31 | | |
32 | 3.67k | gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE); |
33 | | |
34 | 3.67k | if (dbf->desc != -1) |
35 | 3.67k | { |
36 | | /* Make sure the database is all on disk. */ |
37 | 3.67k | if (dbf->read_write != GDBM_READER) |
38 | 3.67k | gdbm_file_sync (dbf); |
39 | | |
40 | 3.67k | _gdbmsync_done (dbf); |
41 | | |
42 | | /* Close the file and free all malloc'ed memory. */ |
43 | 3.67k | #if HAVE_MMAP |
44 | 3.67k | _gdbm_mapped_unmap (dbf); |
45 | 3.67k | #endif |
46 | 3.67k | if (dbf->file_locking) |
47 | 3.67k | _gdbm_unlock_file (dbf); |
48 | | |
49 | 3.67k | if (close (dbf->desc)) |
50 | 3.67k | GDBM_SET_ERRNO (dbf, GDBM_FILE_CLOSE_ERROR, FALSE); |
51 | 3.67k | } |
52 | | |
53 | 3.67k | syserrno = gdbm_last_syserr (dbf); |
54 | | |
55 | 3.67k | gdbm_clear_error (dbf); |
56 | | |
57 | 3.67k | free (dbf->name); |
58 | 3.67k | free (dbf->dir); |
59 | | |
60 | 3.67k | _gdbm_cache_free (dbf); |
61 | | |
62 | 3.67k | free (dbf->header); |
63 | 3.67k | free (dbf); |
64 | 3.67k | if (gdbm_errno) |
65 | 0 | { |
66 | 0 | errno = syserrno; |
67 | 0 | return -1; |
68 | 0 | } |
69 | 3.67k | return 0; |
70 | 3.67k | } |