1 /* gdbmsetopt.c - set options pertaining to a GDBM descriptor. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1993, 1994, 2007, 2011 Free Software Foundation, Inc.
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)
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.
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/>. */
19 /* Include system configuration before all else. */
24 /* operate on an already open descriptor. */
27 getbool (void *optval, int optlen)
31 if (!optval || optlen != sizeof (int) ||
32 (((n = *(int*)optval) != TRUE) && n != FALSE))
34 gdbm_errno = GDBM_OPT_ILLEGAL;
41 get_size (void *optval, int optlen, size_t *ret)
45 gdbm_errno = GDBM_OPT_ILLEGAL;
48 if (optlen == sizeof (unsigned))
49 *ret = *(unsigned*) optval;
50 else if (optlen == sizeof (unsigned long))
51 *ret = *(unsigned long*) optval;
52 else if (optlen == sizeof (size_t))
53 *ret = *(size_t*) optval;
56 gdbm_errno = GDBM_OPT_ILLEGAL;
63 gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
72 case GDBM_SETCACHESIZE:
73 /* Optval will point to the new size of the cache. */
74 if (dbf->bucket_cache != NULL)
76 gdbm_errno = GDBM_OPT_ALREADY_SET;
80 if (get_size (optval, optlen, &sz))
82 return _gdbm_init_cache (dbf, (sz > 9) ? sz : 10);
84 case GDBM_GETCACHESIZE:
85 if (!optval || optlen != sizeof (size_t))
87 gdbm_errno = GDBM_OPT_ILLEGAL;
90 *(size_t*) optval = dbf->cache_size;
93 /* Obsolete form of GDBM_SETSYNCMODE. */
95 if ((n = getbool (optval, optlen)) == -1)
102 case GDBM_SETSYNCMODE:
103 /* Optval will point to either true or false. */
104 if ((n = getbool (optval, optlen)) == -1)
106 dbf->fast_write = !n;
109 case GDBM_GETSYNCMODE:
110 if (!optval || optlen != sizeof (int))
112 gdbm_errno = GDBM_OPT_ILLEGAL;
115 *(int*) optval = !dbf->fast_write;
118 /* CENTFREE - set or get the stat of the central block repository */
119 case GDBM_SETCENTFREE:
120 /* Optval will point to either true or false. */
121 if ((n = getbool (optval, optlen)) == -1)
123 dbf->central_free = n;
126 case GDBM_GETCENTFREE:
127 if (!optval || optlen != sizeof (int))
129 gdbm_errno = GDBM_OPT_ILLEGAL;
132 *(int*) optval = !dbf->central_free;
135 /* Coalesce state: */
136 case GDBM_SETCOALESCEBLKS:
137 /* Optval will point to either true or false. */
138 if ((n = getbool (optval, optlen)) == -1)
140 dbf->coalesce_blocks = n;
143 case GDBM_GETCOALESCEBLKS:
144 if (!optval || optlen != sizeof (int))
146 gdbm_errno = GDBM_OPT_ILLEGAL;
149 *(int*) optval = dbf->coalesce_blocks;
155 if ((n = getbool (optval, optlen)) == -1)
158 if (n == dbf->memory_mapping)
162 if (_gdbm_mapped_init (dbf) == 0)
163 dbf->memory_mapping = TRUE;
169 _gdbm_mapped_unmap (dbf);
170 dbf->memory_mapping = FALSE;
173 gdbm_errno = GDBM_OPT_ILLEGAL;
179 if (!optval || optlen != sizeof (int))
181 gdbm_errno = GDBM_OPT_ILLEGAL;
184 *(int*) optval = dbf->memory_mapping;
187 /* Maximum size of a memory mapped region */
188 case GDBM_SETMAXMAPSIZE:
191 size_t page_size = sysconf (_SC_PAGESIZE);
193 if (get_size (optval, optlen, &sz))
195 dbf->mapped_size_max = ((sz + page_size - 1) / page_size) *
197 _gdbm_mapped_init (dbf);
201 gdbm_errno = GDBM_OPT_ILLEGAL;
205 case GDBM_GETMAXMAPSIZE:
206 if (!optval || optlen != sizeof (size_t))
208 gdbm_errno = GDBM_OPT_ILLEGAL;
211 *(size_t*) optval = dbf->mapped_size_max;
216 if (!optval || optlen != sizeof (int))
218 gdbm_errno = GDBM_OPT_ILLEGAL;
223 int flags = dbf->read_write;
224 if (!dbf->fast_write)
226 if (!dbf->file_locking)
227 flags |= GDBM_NOLOCK;
228 if (!dbf->memory_mapping)
229 flags |= GDBM_NOMMAP;
230 *(int*) optval = flags;
235 if (!optval || optlen != sizeof (char*))
237 gdbm_errno = GDBM_OPT_ILLEGAL;
242 char *p = strdup (dbf->name);
245 gdbm_errno = GDBM_MALLOC_ERROR;
248 *(char**) optval = p;
253 gdbm_errno = GDBM_OPT_ILLEGAL;