Debian Codegraph @

ext/dbm/odbm.scm

raw
1 ;;;
2 ;;; odbm - original dbm interface
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (define-module dbm.odbm
35 (extend dbm)
36 (use scheme.list)
37 (export <odbm>
38 ;; low level funcions
39 odbm-init odbm-close odbm-closed?
40 odbm-store odbm-fetch odbm-delete
41 odbm-firstkey odbm-nextkey)
42 )
43 (select-module dbm.odbm)
44
45 ;; Legacy DBM has some limitations.
46 ;; * only one dbm file can be opened at a time.
47 ;; (this is checked in C code)
48 ;; * no flags (read-only, read-write or create)
49 ;; * dbm changes file mode to #o000 when opened, to prevent
50 ;; other processes to access to the same file (I guess).
51 ;; this won't work well if the process dies before changing
52 ;; back the permission. We reset the permission after
53 ;; opening the file.
54
55 ;;;
56 ;;; High-level dbm interface
57 ;;;
58
59 (define-class <odbm-meta> (<dbm-meta>)
60 ())
61
62 (define-class <odbm> (<dbm>)
63 ()
64 :metaclass <odbm-meta>)
65
66 (define-method dbm-open ((self <odbm>))
67 (next-method)
68 (unless (slot-bound? self 'path)
69 (error "path must be set to open odbm database"))
70 (let* ([path (slot-ref self 'path)]
71 [rwmode (slot-ref self 'rw-mode)]
72 [fmode (slot-ref self 'file-mode)]
73 [dirfile (string-append path ".dir")]
74 [pagfile (string-append path ".pag")]
75 [exists? (and (file-exists? dirfile) (file-exists? pagfile))]
76 [create (^[]
77 (with-output-to-file dirfile (^[] #f))
78 (with-output-to-file pagfile (^[] #f)))])
79 (case rwmode
80 [(:read)
81 (unless exists?
82 (errorf "dbm-open: no database file ~s.dir or ~s.pag" path path))]
83 [(:write)
84 ;; dir and pag files must exist
85 (unless exists? (create))]
86 [(:create)
87 ;; a trick: remove dir and pag file first if :create
88 (when exists?
89 (sys-unlink dirfile) (sys-unlink pagfile))
90 (create)])
91 (odbm-init path)
92 ;; adjust mode properly
93 (sys-chmod dirfile fmode)
94 (sys-chmod pagfile fmode)
95 self))
96
97 ;;
98 ;; close operation
99 ;;
100
101 (define-method dbm-close ((self <odbm>))
102 (odbm-close))
103
104 (define-method dbm-closed? ((self <odbm>))
105 (odbm-closed?))
106
107 ;;
108 ;; common operations
109 ;;
110
111 (define-method dbm-put! ((self <odbm>) key value)
112 (next-method)
113 (when (positive? (odbm-store (%dbm-k2s self key) (%dbm-v2s self value)))
114 (error "dbm-put! failed" self)))
115
116 (define-method dbm-get ((self <odbm>) key . args)
117 (next-method)
118 (cond [(odbm-fetch (%dbm-k2s self key)) => (cut %dbm-s2v self <>)]
119 [(pair? args) (car args)] ;fall-back value
120 [else (errorf "odbm: no data for key ~s in database ~s" key self)]))
121
122 (define-method dbm-exists? ((self <odbm>) key)
123 (next-method)
124 (if (odbm-fetch (%dbm-k2s self key)) #t #f))
125
126 (define-method dbm-delete! ((self <odbm>) key)
127 (next-method)
128 (when (positive? (odbm-delete (%dbm-k2s self key)))
129 (errorf "dbm-delete!: deleteting key ~s from ~s failed" key self)))
130
131 (define-method dbm-fold ((self <odbm>) proc knil)
132 (next-method)
133 (let loop ([key (odbm-firstkey)] [r '()])
134 (if key
135 (let1 val (odbm-fetch key)
136 (loop (odbm-nextkey key)
137 (proc (%dbm-s2k self key) (%dbm-s2v self val) r)))
138 r)))
139
140
141 (define (odbm-files name)
142 (map (cut string-append name <>) '(".pag" ".dir")))
143
144 (define-method dbm-db-exists? ((class <odbm-meta>) name)
145 (every file-exists? (odbm-files name)))
146
147 (define-method dbm-db-remove ((class <odbm-meta>) name)
148 (for-each sys-unlink (odbm-files name)))
149
150 (define-method dbm-db-copy ((class <odbm-meta>) from to . keys)
151 (apply %dbm-copy2
152 (append (append-map list (odbm-files from) (odbm-files to)) keys)))
153
154 (define-method dbm-db-move ((class <odbm-meta>) from to . keys)
155 (apply %dbm-rename2
156 (append (append-map list (odbm-files from) (odbm-files to))
157 keys)))
158
159 ;;;
160 ;;; Low-level binginds
161 ;;;
162
163 (inline-stub
164 (declcode
165 (.include <gauche/priv/configP.h>)
166 (.include "dbmconf.h")
167 (.cond
168 ["HAVE_DBM_H" (.include <dbm.h>)]
169 ["HAVE_GDBM_SLASH_DBM_H" (.include <gdbm/dbm.h>)]
170 ["HAVE_GDBM_MINUS_DBM_H" (.include <gdbm-dbm.h>)])
171
172 ;; Avoid conflicting with C++ keyword
173 (.define delete_ delete)
174 )
175
176 ;; data conversion macros
177 (define-cise-stmt TO_DATUM
178 [(_ datum scm)
179 (let ((tmp (gensym)))
180 `(let* ((,tmp :: (const ScmStringBody*) (SCM_STRING_BODY ,scm)))
181 (set! (ref ,datum dptr) (cast char* (SCM_STRING_BODY_START ,tmp)))
182 (set! (ref ,datum dsize) (SCM_STRING_BODY_SIZE ,tmp))))])
183
184 (define-cise-stmt FROM_DATUM
185 [(_ scm datum)
186 `(cond [(ref ,datum dptr)
187 (set! ,scm (Scm_MakeString (ref ,datum dptr) (ref ,datum dsize)
188 -1 SCM_STRING_COPYING))]
189 [else
190 (set! ,scm SCM_FALSE)])])
191
192 (define-cise-stmt CHECK_ODBM
193 [(_)
194 `(unless odbm_opened (Scm_Error "odbm file already closed"))])
195
196 ;; Original dbm allows to open only one file at a time.
197 ;; The static variable odbm_opened tracks the status.
198 ;; TODO: MT SAFENESS
199 (define-cvar odbm_opened::int :static FALSE)
200
201 ;; bindings
202 (define-cproc odbm-init (name::<string>) ::<int>
203 (when odbm_opened (Scm_Error "dbm file is already opened."))
204 (let* ([r::int (dbminit (Scm_GetString name))])
205 (when (< r 0) (Scm_SysError "couldn't open dbm database %S" name))
206 (set! odbm_opened TRUE)
207 (return r)))
208
209 (define-cproc odbm-close () ::<void>
210 (when odbm_opened (dbmclose) (set! odbm_opened FALSE)))
211
212 (define-cproc odbm-closed? () ::<boolean>
213 (return (not odbm_opened)))
214
215 (define-cproc odbm-store (key::<string> val::<string>) ::<int>
216 (let* ([dkey::datum] [dval::datum])
217 (CHECK_ODBM) (TO_DATUM dkey key) (TO_DATUM dval val)
218 (return (store dkey dval))))
219
220 (define-cproc odbm-fetch (key::<string>)
221 (let* ([dkey::datum] [dval::datum])
222 (CHECK_ODBM) (TO_DATUM dkey key) (set! dval (fetch dkey))
223 (FROM_DATUM SCM_RESULT dval)))
224
225 (define-cproc odbm-delete (key::<string>) ::<int>
226 (let* ([dkey::datum])
227 (CHECK_ODBM) (TO_DATUM dkey key) (return (delete_ dkey))))
228
229 (define-cproc odbm-firstkey ()
230 (let* ([dkey::datum])
231 (CHECK_ODBM) (set! dkey (firstkey)) (FROM_DATUM SCM_RESULT dkey)))
232
233 (define-cproc odbm-nextkey (key::<string>)
234 (let* ([dkey::datum] [dnkey::datum])
235 (CHECK_ODBM) (TO_DATUM dkey key) (set! dnkey (nextkey dkey))
236 (FROM_DATUM SCM_RESULT dnkey)))
237 )