-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
blob.go
326 lines (304 loc) · 8.86 KB
/
blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2018 David Crawshaw <[email protected]>
// Copyright (c) 2021 Roxy Light <[email protected]>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier: ISC
package sqlite
import (
"errors"
"fmt"
"io"
"unsafe"
"modernc.org/libc"
lib "modernc.org/sqlite/lib"
)
const blobBufSize = 4096
var (
mainCString = mustCString("main")
tempCString = mustCString("temp")
)
// OpenBlob opens a blob in a particular {database,table,column,row}.
//
// https://www.sqlite.org/c3ref/blob_open.html
func (c *Conn) OpenBlob(dbn, table, column string, row int64, write bool) (*Blob, error) {
if c == nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: nil connection", table, column)
}
return c.openBlob(dbn, table, column, row, write)
}
func (c *Conn) openBlob(dbn, table, column string, row int64, write bool) (_ *Blob, err error) {
cdb, freeCDB, err := cDBName(dbn)
if err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
defer freeCDB()
var writeFlag int32
if write {
writeFlag = 1
}
buf, err := malloc(c.tls, blobBufSize)
if err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
defer func() {
if err != nil {
libc.Xfree(c.tls, buf)
}
}()
ctable, err := libc.CString(table)
if err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
defer libc.Xfree(c.tls, ctable)
ccolumn, err := libc.CString(column)
if err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
defer libc.Xfree(c.tls, ccolumn)
blobPtrPtr, err := malloc(c.tls, ptrSize)
if err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
defer libc.Xfree(c.tls, blobPtrPtr)
for {
if err := c.interrupted(); err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
res := ResultCode(lib.Xsqlite3_blob_open(
c.tls,
c.conn,
cdb,
ctable,
ccolumn,
row,
writeFlag,
blobPtrPtr,
))
switch res {
case ResultLockedSharedCache:
if err := waitForUnlockNotify(c.tls, c.conn, c.unlockNote).ToError(); err != nil {
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, err)
}
// loop
case ResultOK:
blobPtr := *(*uintptr)(unsafe.Pointer(blobPtrPtr))
return &Blob{
conn: c,
blob: blobPtr,
buf: buf,
size: lib.Xsqlite3_blob_bytes(c.tls, blobPtr),
}, nil
default:
return nil, fmt.Errorf("sqlite: open blob %q.%q: %w", table, column, c.extreserr(res))
}
}
}
// Blob provides streaming access to SQLite blobs.
type Blob struct {
conn *Conn
blob uintptr
buf uintptr
off int32
size int32
}
func (blob *Blob) bufSlice() []byte {
return libc.GoBytes(blob.buf, blobBufSize)
}
// Read reads up to len(p) bytes from the blob into p.
// https://www.sqlite.org/c3ref/blob_read.html
func (blob *Blob) Read(p []byte) (int, error) {
if blob.blob == 0 {
return 0, fmt.Errorf("sqlite: read blob: %w", errInvalidBlob)
}
if blob.off >= blob.size {
return 0, io.EOF
}
if err := blob.conn.interrupted(); err != nil {
return 0, fmt.Errorf("sqlite: read blob: %w", err)
}
if rem := blob.size - blob.off; len(p) > int(rem) {
p = p[:rem]
}
fullLen := len(p)
for len(p) > 0 {
nn := int32(blobBufSize)
if int(nn) > len(p) {
nn = int32(len(p))
}
res := ResultCode(lib.Xsqlite3_blob_read(blob.conn.tls, blob.blob, blob.buf, nn, blob.off))
if err := res.ToError(); err != nil {
return fullLen - len(p), fmt.Errorf("sqlite: read blob: %w", err)
}
copy(p, blob.bufSlice()[:int(nn)])
p = p[nn:]
blob.off += nn
}
return fullLen, nil
}
// WriteTo copies the blob to w until there's no more data to write or
// an error occurs.
func (blob *Blob) WriteTo(w io.Writer) (n int64, err error) {
if blob.blob == 0 {
return 0, fmt.Errorf("sqlite: read blob: %w", errInvalidBlob)
}
if blob.off >= blob.size {
return 0, nil
}
if err := blob.conn.interrupted(); err != nil {
return 0, fmt.Errorf("sqlite: read blob: %w", err)
}
for blob.off < blob.size {
buf := blob.bufSlice()
if remaining := int(blob.size - blob.off); len(buf) > remaining {
buf = buf[:remaining]
}
res := ResultCode(lib.Xsqlite3_blob_read(blob.conn.tls, blob.blob, blob.buf, int32(len(buf)), blob.off))
if err := res.ToError(); err != nil {
return n, fmt.Errorf("sqlite: read blob: %w", err)
}
nn, err := w.Write(buf)
blob.off += int32(nn)
n += int64(nn)
if err != nil {
return n, err
}
}
return n, nil
}
// Write writes len(p) from p to the blob.
// https://www.sqlite.org/c3ref/blob_write.html
func (blob *Blob) Write(p []byte) (int, error) {
if blob.blob == 0 {
return 0, fmt.Errorf("sqlite: write blob: %w", errInvalidBlob)
}
if err := blob.conn.interrupted(); err != nil {
return 0, fmt.Errorf("sqlite: write blob: %w", err)
}
fullLen := len(p)
for len(p) > 0 {
nn := copy(blob.bufSlice(), p)
res := ResultCode(lib.Xsqlite3_blob_write(blob.conn.tls, blob.blob, blob.buf, int32(nn), blob.off))
if err := res.ToError(); err != nil {
return fullLen - len(p), fmt.Errorf("sqlite: write blob: %w", err)
}
p = p[nn:]
blob.off += int32(nn)
}
return fullLen, nil
}
// WriteString writes s to the blob.
// https://www.sqlite.org/c3ref/blob_write.html
func (blob *Blob) WriteString(s string) (int, error) {
if blob.blob == 0 {
return 0, fmt.Errorf("sqlite: write blob: %w", errInvalidBlob)
}
if err := blob.conn.interrupted(); err != nil {
return 0, fmt.Errorf("sqlite: write blob: %w", err)
}
fullLen := len(s)
for len(s) > 0 {
nn := copy(blob.bufSlice(), s)
res := ResultCode(lib.Xsqlite3_blob_write(blob.conn.tls, blob.blob, blob.buf, int32(nn), blob.off))
if err := res.ToError(); err != nil {
return fullLen - len(s), fmt.Errorf("sqlite: write blob: %w", err)
}
s = s[nn:]
blob.off += int32(nn)
}
return fullLen, nil
}
// ReadFrom copies data from r to the blob until EOF or error.
func (blob *Blob) ReadFrom(r io.Reader) (n int64, err error) {
if blob.blob == 0 {
return 0, fmt.Errorf("sqlite: write blob: %w", errInvalidBlob)
}
if err := blob.conn.interrupted(); err != nil {
return 0, fmt.Errorf("sqlite: write blob: %w", err)
}
for {
nn, err := r.Read(blob.bufSlice())
if nn > 0 {
res := ResultCode(lib.Xsqlite3_blob_write(blob.conn.tls, blob.blob, blob.buf, int32(nn), blob.off))
if err := res.ToError(); err != nil {
return n, fmt.Errorf("sqlite: write blob: %w", err)
}
n += int64(nn)
blob.off += int32(nn)
}
if err != nil {
if err == io.EOF {
err = nil
}
return n, err
}
}
}
// Seek sets the offset for the next Read or Write and returns the offset.
// Seeking past the end of the blob returns an error.
func (blob *Blob) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
// use offset directly
case io.SeekCurrent:
offset += int64(blob.off)
case io.SeekEnd:
offset += int64(blob.size)
default:
return int64(blob.off), fmt.Errorf("sqlite: seek blob: invalid whence %d", whence)
}
if offset < 0 {
return int64(blob.off), fmt.Errorf("sqlite: seek blob: negative offset %d", offset)
}
if offset > int64(blob.size) {
return int64(blob.off), fmt.Errorf("sqlite: seek blob: offset %d is past size %d", offset, blob.size)
}
blob.off = int32(offset)
return offset, nil
}
// Size returns the number of bytes in the blob.
func (blob *Blob) Size() int64 {
return int64(blob.size)
}
// Close releases any resources associated with the blob handle.
// https://www.sqlite.org/c3ref/blob_close.html
func (blob *Blob) Close() error {
if blob.blob == 0 {
return errInvalidBlob
}
libc.Xfree(blob.conn.tls, blob.buf)
blob.buf = 0
res := ResultCode(lib.Xsqlite3_blob_close(blob.conn.tls, blob.blob))
blob.blob = 0
if err := res.ToError(); err != nil {
return fmt.Errorf("sqlite: close blob: %w", err)
}
return nil
}
var errInvalidBlob = errors.New("invalid blob")
// cDBName converts a database name into a C string.
func cDBName(dbn string) (uintptr, func(), error) {
switch dbn {
case "", "main":
return mainCString, func() {}, nil
case "temp":
return tempCString, func() {}, nil
default:
cdb, err := libc.CString(dbn)
if err != nil {
return 0, nil, err
}
return cdb, func() { libc.Xfree(nil, cdb) }, nil
}
}
// TODO: Blob Reopen