-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3pth.scm
2173 lines (1935 loc) · 68.1 KB
/
sqlite3pth.scm
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; (C) 2008, 2010, 2015, 2019 Jörg F. Wittenberger -*-Scheme-*-
;; TODO: integrate with http://www.chust.org/fossils/dbi/index
(declare
(fixnum)
(usual-integrations)
(disable-interrupts)
; (no-procedure-checks-for-usual-bindings)
#;(bound-to-procedure
mutex-lock! mutex-unlock!)
(foreign-declare #<<EOF
#include <pthread.h>
//#define TRACE 1
#define CHECK_PROTOCOL 1
#if 1
#define L_malloc(x) malloc(x)
#define L_free(x) free(x)
#else
int n_malloc = 0;
static void * L_malloc(size_t n)
{
n_malloc++;
fprintf(stderr, "malloced %d\n", n_malloc);
return malloc(n);
}
static void L_free(void *x)
{
n_malloc--;
free(x);
}
#endif
typedef C_word obj;
#define FALSE_OBJ C_SCHEME_FALSE
#include "sqlite/sqlite3.h"
#include <assert.h>
static int rs_sqlite3_auth_unrestricted(void* userdata, int opcode,
const char* arg1, const char* arg2,
const char* dbname, const char* trigger)
{
return SQLITE_OK;
}
static void sqlite3_set_authorizer_unrestricted(sqlite3 *cnx)
{
sqlite3_set_authorizer(cnx, rs_sqlite3_auth_unrestricted, NULL);
}
/* ** AUTHORIZATION HANDLING ** */
static int rs_sqlite3_auth_restricted(void* userdata, int opcode,
const char* arg1, const char* arg2,
const char* dbname, const char* trigger)
{
switch(opcode) {
case SQLITE_CREATE_INDEX: /* Index Name Table Name */
case SQLITE_REINDEX:
case SQLITE_CREATE_TABLE: /* Table Name NULL */
case SQLITE_CREATE_VTABLE: /* Table Name Module Name */
case SQLITE_ALTER_TABLE: /* Database Name Table Name */
case SQLITE_CREATE_TEMP_INDEX: /* Index Name Table Name */
case SQLITE_CREATE_TEMP_TABLE: /* Table Name NULL */
case SQLITE_CREATE_TEMP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_CREATE_TEMP_VIEW: /* View Name NULL */
case SQLITE_CREATE_TRIGGER: /* Trigger Name Table Name */
case SQLITE_CREATE_VIEW: /* View Name NULL */
case SQLITE_DELETE: /* Table Name NULL */
case SQLITE_DROP_INDEX: /* Index Name Table Name */
case SQLITE_DROP_TABLE: /* Table Name NULL */
case SQLITE_DROP_VTABLE: /* Table Name Module Name */
case SQLITE_DROP_TEMP_INDEX: /* Index Name Table Name */
case SQLITE_DROP_TEMP_TABLE: /* Table Name NULL */
case SQLITE_DROP_TEMP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_DROP_TEMP_VIEW: /* View Name NULL */
case SQLITE_DROP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_DROP_VIEW: /* View Name NULL */
case SQLITE_INSERT: /* Table Name NULL */
case SQLITE_PRAGMA: /* Pragma Name 1st arg or NULL */
case SQLITE_READ: /* Table Name Column Name */
case SQLITE_SELECT: /* NULL NULL */
#if SQLITE_VERSION_NUMBER > 3003007
case SQLITE_FUNCTION: /* Function Name NULL */
#endif
case SQLITE_TRANSACTION: /* NULL NULL */
case SQLITE_UPDATE: /* Table Name Column Name */
case SQLITE_ANALYZE: /* Table Name NULL */
case SQLITE_RECURSIVE: /* NULL NULL */
return SQLITE_OK;
case SQLITE_ATTACH: /* Filename NULL */
case SQLITE_DETACH: /* Database Name NULL */
default:
fprintf(stderr, "auth_restricted deny %d\n", opcode);
return SQLITE_DENY;
}
}
/* KLUDGE: FIXME: we need to know what temp tables are, not code them hard */
static int is_temporary_table(void *userdata, const char *table)
{
if (strcmp(table, "sqlite_temp_master") == 0) return 1;
if (strcmp(table, "current_message") == 0) return 1;
return 0;
}
static int rs_sqlite3_auth_restricted_ro(void* userdata, int opcode,
const char* arg1, const char* arg2,
const char* dbname, const char* trigger)
{
switch(opcode) {
case SQLITE_CREATE_INDEX: /* Index Name Table Name */
case SQLITE_CREATE_TABLE: /* Table Name NULL */
case SQLITE_ALTER_TABLE: /* Database Name Table Name */
return SQLITE_DENY;
case SQLITE_CREATE_TEMP_INDEX: /* Index Name Table Name */
case SQLITE_CREATE_TEMP_TABLE: /* Table Name NULL */
case SQLITE_CREATE_TEMP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_CREATE_TEMP_VIEW: /* View Name NULL */
return SQLITE_OK;
case SQLITE_CREATE_TRIGGER: /* Trigger Name Table Name */
case SQLITE_CREATE_VIEW: /* View Name NULL */
case SQLITE_DELETE: /* Table Name NULL */
case SQLITE_DROP_INDEX: /* Index Name Table Name */
case SQLITE_DROP_TABLE: /* Table Name NULL */
return SQLITE_DENY;
case SQLITE_DROP_TEMP_INDEX: /* Index Name Table Name */
case SQLITE_DROP_TEMP_TABLE: /* Table Name NULL */
case SQLITE_DROP_TEMP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_DROP_TEMP_VIEW: /* View Name NULL */
return SQLITE_OK;
case SQLITE_DROP_TRIGGER: /* Trigger Name Table Name */
case SQLITE_DROP_VIEW: /* View Name NULL */
return SQLITE_DENY;
case SQLITE_INSERT: /* Table Name NULL */
if (is_temporary_table(userdata,arg1))
return SQLITE_OK;
return SQLITE_DENY;
case SQLITE_PRAGMA: /* Pragma Name 1st arg or NULL */
return SQLITE_DENY;
case SQLITE_READ: /* Table Name Column Name */
case SQLITE_SELECT: /* NULL NULL */
#if SQLITE_VERSION_NUMBER > 3003007
case SQLITE_FUNCTION: /* Function Name NULL */
#endif
case SQLITE_RECURSIVE: /* NULL NULL */
return SQLITE_OK;
case SQLITE_TRANSACTION: /* NULL NULL */
return SQLITE_DENY;
case SQLITE_UPDATE: /* Table Name Column Name */
/* FIXME: this is somehow needed to select from fts tables. */
#if 0
if (is_temporary_table(userdata,arg1))
return SQLITE_OK;
return SQLITE_DENY;
#else
return SQLITE_OK;
#endif
case SQLITE_ATTACH: /* Filename NULL */
case SQLITE_DETACH: /* Database Name NULL */
default:
return SQLITE_DENY;
}
}
/* ** misc functions ** */
static void sqlite3_concat(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
int len=0, i=0, j=0;
char *r = NULL;
for(;i<argc; ++i) len+=sqlite3_value_bytes(argv[i]);
r = malloc(len+1);
for(i=0, j=0; i<argc; ++i) {
int s = sqlite3_value_bytes(argv[i]);
strncpy(r+j, (const char*)sqlite3_value_text(argv[i]), s);
j += s;
}
r[j]='\0';
sqlite3_result_text(ctx, r, len, free);
}
static int sq_sqlite3_create_functions(sqlite3 *conn)
{
return sqlite3_create_function(conn, "concat", -1, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
NULL, sqlite3_concat, NULL, NULL);
}
static void sqlite3_set_authorizer_restricted_ro(sqlite3 *cnx)
{
sqlite3_set_authorizer(cnx, rs_sqlite3_auth_restricted_ro, NULL);
}
static void sqlite3_set_authorizer_restricted(sqlite3 *cnx)
{
sqlite3_set_authorizer(cnx, rs_sqlite3_auth_restricted, NULL);
}
/* setup function table */
static void sqlite3_setup_full(sqlite3 *cnx)
{
sq_sqlite3_create_functions(cnx);
}
static void sqlite3_setup_restricted(sqlite3 *cnx)
{
sqlite3_set_authorizer(cnx, rs_sqlite3_auth_restricted, NULL);
sq_sqlite3_create_functions(cnx);
}
static void sqlite3_setup_restricted_ro(sqlite3 *cnx)
{
sqlite3_set_authorizer(cnx, rs_sqlite3_auth_restricted_ro, NULL);
sq_sqlite3_create_functions(cnx);
}
static void (*setup_table[4])(sqlite3 *) = {
NULL,
sqlite3_setup_full,
sqlite3_setup_restricted,
sqlite3_setup_restricted_ro
};
static void sqlite3_setup(sqlite3 *cnx, int i)
{
void (*f)(sqlite3 *);
assert(i<4);
f=setup_table[i];
if(f) (*f)(cnx);
}
static pthread_mutex_t the_shared_memory_mux;
static pthread_cond_t the_shared_memory_cond;
static struct callback_args *the_shared_memory_for_open = NULL;
void lock_callback_open_parameters(struct callback_args * x)
{
pthread_mutex_lock(&the_shared_memory_mux);
while( the_shared_memory_for_open != NULL )
pthread_cond_wait(&the_shared_memory_cond, &the_shared_memory_mux);
the_shared_memory_for_open = x;
pthread_mutex_unlock(&the_shared_memory_mux);
}
void unlock_callback_open_parameters(struct callback_args * x)
{
#ifdef TRACE
fprintf(stderr, "unlock_callback_open_parameters %p\n", the_shared_memory_for_open);
#endif
if( x != NULL && x == the_shared_memory_for_open) {
pthread_mutex_lock(&the_shared_memory_mux);
the_shared_memory_for_open = NULL;
pthread_mutex_unlock(&the_shared_memory_mux);
pthread_cond_signal(&the_shared_memory_cond);
}
}
typedef int (*C_pthread_request_function_t)(void *);
extern int
start_asynchronous_request(C_pthread_request_function_t function,
void *data, void *callback);
extern void
C_interrupt_call(void *callback, void *converter, void *result);
struct callback_args {
pthread_mutex_t mux;
pthread_cond_t cond;
C_GC_ROOT *ref;
unsigned int op;
size_t size;
int amount;
sqlite_int64 offset;
void *buf;
/* char buf[1]; */
};
struct open_args {
sqlite3 *cnx;
int setup;
struct callback_args *sm;
char *vfs;
char dbn[2];
};
static int
pthread_sqlite3_open(void *data)
{
struct open_args *a = data;
int rc;
#ifdef TRACE
fprintf(stderr, "DB %s vfs %s SM %p mode %s\n", a->dbn, a->vfs, a->sm, a->setup == 3 ? "SQLITE_OPEN_READONLY" : "SQLITE_OPEN_CREATE");
#endif
if(a->sm != NULL) {
lock_callback_open_parameters(a->sm);
}
rc = sqlite3_open_v2( a->dbn,
&a->cnx,
( a->setup == 3 ? SQLITE_OPEN_READONLY :
( SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE ) )
| SQLITE_OPEN_NOMUTEX,
a->vfs);
/* unlock_callback_open_parameters(a->sm); done within open ASAP */
if( rc == SQLITE_OK) {
sqlite3_setup(a->cnx, a->setup);
return SQLITE_OK;
} else {
#ifdef TRACE
fprintf(stderr, "open failed DB %s vfs %s SM %p code %d %s: %s\n", a->dbn, a->vfs, a->sm, rc, sqlite3_errmsg(a->cnx), strerror(sqlite3_system_errno(a->cnx)));
#endif
/* Open failed, and callback did not unlock parameters. */
unlock_callback_open_parameters(a->sm);
return rc;
}
/* Supposed unreached case. Should we assert? */
return SQLITE_MISUSE;
}
struct sqlite3_db {
sqlite3 *cnx;
size_t bufsize;
void *buf;
};
static int pthread_sqlite3_close(void *data)
{
struct sqlite3_db *a = data;
int rc = (/*sqlite3MemdebugDump("/tmp/sqlite"),*/ sqlite3_close(a->cnx));
L_free(a->buf);
L_free(a);
return rc;
}
struct prepare_args {
sqlite3_stmt *stmt;
int tail;
sqlite3 *db;
int sql_len;
int offset;
char sql[1];
};
static int pthread_sqlite3_prepare(void *data)
{
struct prepare_args *a = data;
int rc;
const char *tail;
#ifdef TRACE
fprintf(stderr, "prepar %p >>%s<< %d %d\n", a->db, a->sql, a->offset, a->sql_len);
#endif
rc = sqlite3_prepare_v2( a->db,
a->sql + a->offset,
a->sql_len - a->offset,
&a->stmt,
&tail );
if (a->stmt != NULL) {
a->tail = tail - a->sql;
}
return rc;
}
/*
** 2009 Juli 24
**
** This file contains OS interface code that is used with Askemos(R).
**/
/*
** standard include files.
*/
#include <string.h>
#include <pthread.h>
/*
** Maximum pathname length supported by the callback backend.
*/
#define CALLBACK_MAX_PATHNAME 512
#define CB_IO_TYPE_BLKSZ 1
#define CB_IO_TYPE_FSIZE 2
#define CB_IO_TYPE_READ 3
#define CB_IO_TYPE_WRITE 4
#define CB_IO_TYPE_TRUNC 5
#define CB_IO_TYPE_CLOSE 6
#define CB_OP_CALL 2
#define CB_OP_RETURN 1
#define CB_OP_BUSY 3
#define CB_OP_SHIFT 2
/*
** Pointer-Makros
*/
/* pointer to the parent sqlite3_vfs structure (e.g. unix) */
sqlite3_vfs *pVfs = NULL;
// "host" callback
static void *the_callback_callback = NULL;
static void *the_callback_arg_converter = NULL;
typedef struct callback_file callback_file;
struct callback_file {
sqlite3_io_methods *ioMethods;
const char* zName;
/* shared memory
*
* form: #((int opcode) (string buf) (int amount) (int offset) (pointer (condition done))
*/
struct callback_args *sm;
// pointer to the sqlite3_file structure of the parent vfs (e.g. unix vfs)
sqlite3_file pReal[1];
};
/*
** Method declarations for callback_file.
*/
static int callbackClose(sqlite3_file*);
static int callbackRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
static int callbackWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
static int callbackTruncate(sqlite3_file*, sqlite3_int64 size);
static int callbackFileSize(sqlite3_file*, sqlite3_int64 *pSize);
static int callbackFileControl(sqlite3_file*, int op, void *pArg);
static int callbackSectorSize(sqlite3_file*);
static int callbackDeviceCharacteristics(sqlite3_file*);
static int callbackCloseNot(sqlite3_file*);
static int callbackReadNot(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
static int callbackWriteNot(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
static int callbackTruncateNot(sqlite3_file*, sqlite3_int64 size);
static int callbackSyncNot(sqlite3_file*, int flags);
static int callbackFileSizeNot(sqlite3_file*, sqlite3_int64 *pSize);
static int callbackSyncNot(sqlite3_file*, int flags);
static int callbackLockNot(sqlite3_file*, int);
static int callbackUnlockNot(sqlite3_file*, int);
static int callbackCheckReservedLockNot(sqlite3_file*, int *pResOut);
static int callbackSectorSizeNot(sqlite3_file*);
/*
** Method declarations for callback_vfs.
*/
static int callbackOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
static int callbackDelete(sqlite3_vfs*, const char *zName, int syncDir);
static int callbackAccess(sqlite3_vfs*, const char *zName, int flags, int *);
static int callbackFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
static void *callbackDlOpen(sqlite3_vfs*, const char *zFilename);
static void callbackDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
static void (*callbackDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
static void callbackDlClose(sqlite3_vfs*, void*);
static int callbackRandomness(sqlite3_vfs*, int nByte, char *zOut);
static int callbackSleep(sqlite3_vfs*, int microseconds);
static int callbackCurrentTime(sqlite3_vfs*, double*);
static sqlite3_vfs callback_vfs = {
3, /* iVersion */
sizeof(callback_file), /* szOsFile */
CALLBACK_MAX_PATHNAME, /* mxPathname */
0, /* pNext */
"askemos", /* zName */
0, /* pAppData */
callbackOpen, /* xOpen */
callbackDelete, /* xDelete */
callbackAccess, /* xAccess */
callbackFullPathname, /* xFullPathname */
callbackDlOpen, /* xDlOpen */
callbackDlError, /* xDlError */
callbackDlSym, /* xDlSym */
callbackDlClose, /* xDlClose */
callbackRandomness, /* xRandomness */
callbackSleep, /* xSleep */
callbackCurrentTime, /* xCurrentTime */
0, /* xGetLastError */
0, /* xCurrentTimeInt64 */
0, /* xSetSystemCall */
0, /* xGetSystemCall */
0, /* xNextSystemCall */
};
static sqlite3_io_methods callback_io_methods = {
1, /* iVersion */
callbackClose, /* xClose */
callbackRead, /* xRead */
callbackWrite, /* xWrite */
callbackTruncate, /* xTruncate */
callbackSyncNot, /* xSync */
callbackFileSize, /* xFileSize */
callbackLockNot, /* xLock */
callbackUnlockNot, /* xUnlock */
callbackCheckReservedLockNot, /* xCheckReservedLock */
callbackFileControl, /* xFileControl */
callbackSectorSize, /* xSectorSize */
callbackDeviceCharacteristics /* xDeviceCharacteristics */
};
static sqlite3_io_methods callback_io_noop_methods = {
1, /* iVersion */
callbackCloseNot, /* xClose */
callbackReadNot, /* xRead */
callbackWriteNot, /* xWrite */
callbackTruncateNot, /* xTruncate */
callbackSyncNot, /* xSync */
callbackFileSizeNot, /* xFileSize */
callbackLockNot, /* xLock */
callbackUnlockNot, /* xUnlock */
callbackCheckReservedLockNot, /* xCheckReservedLock */
callbackFileControl, /* xFileControl */
callbackSectorSizeNot, /* xSectorSize */
callbackDeviceCharacteristics /* xDeviceCharacteristics */
};
/*
** Open an callback file handle.
*/
static int callbackOpen(
sqlite3_vfs *pCallbackVfs,
const char *zName,
sqlite3_file *pFile,
int flags,
int *pOutFlags
){
callback_file *p = (callback_file *) pFile;
/* Only for main db, to save energy. */
if(flags & SQLITE_OPEN_MAIN_DB) {
#ifdef TRACE
fprintf(stderr, "A open Main %s\n", zName);
#endif
p->ioMethods = &callback_io_methods;
p->zName = zName;
p->sm = the_shared_memory_for_open;
unlock_callback_open_parameters(the_shared_memory_for_open);
return SQLITE_OK;
} else if (flags & SQLITE_OPEN_MAIN_JOURNAL) {
p->ioMethods = &callback_io_noop_methods;
return SQLITE_OK;
} else if (flags & SQLITE_OPEN_WAL) {
p->ioMethods = &callback_io_noop_methods;
return SQLITE_OK;
} else {
fprintf(stderr, "A Unhandled flags %x\n", flags);
return SQLITE_MISUSE;
}
#ifdef TRACE
fprintf(stderr, "A open Else %s\n", zName);
#endif
return pVfs->xOpen(pVfs, zName, pFile, flags, pOutFlags);
}
/*
* fill callback args
*/
static callback_file *
fill_callback_args(sqlite3_file *sf, short type, void *zBuf, int iAmt,
sqlite_int64 iOfst)
{
callback_file *p = (callback_file *) sf;
struct callback_args *a = p->sm;
#ifdef CHECK_PROTOCOL
if(a==NULL) return NULL;
#endif
pthread_mutex_lock(&a->mux);
while( a->op & CB_OP_BUSY ) pthread_cond_wait(&a->cond, &a->mux);
a->op = type << CB_OP_SHIFT | CB_OP_CALL;
a->amount = iAmt;
a->offset = iOfst;
a->buf = zBuf;
return p;
}
static int call_callback(callback_file *cf)
{
#ifdef CHECK_PROTOCOL
if(cf==NULL) return SQLITE_PROTOCOL;
else {
#endif
unsigned int ret;
struct callback_args *a = cf->sm;
// pthread_mutex_lock(&a->mux); -- done in fill_callback_args
// calling chicken to add the blocklist to the mailbox (read)
C_interrupt_call(the_callback_callback, the_callback_arg_converter, a);
// waiting for the call to complete
do {
pthread_cond_wait(&a->cond, &a->mux);
ret = a->op;
} while ( !(ret & CB_OP_RETURN) );
a->op = 0;
pthread_mutex_unlock(&a->mux);
ret >>= CB_OP_SHIFT;
#ifdef TRACE
fprintf(stderr, "CB ret %p %d\n", cf, ret);
#endif
return ret;
#ifdef CHECK_PROTOCOL
}
#endif
}
/*
** Close an callback-file.
*/
static int callbackCloseNot(sqlite3_file *pFile)
{
return SQLITE_OK;
}
static int callbackClose(sqlite3_file *sf){
int rc;
#ifdef TRACE
fprintf(stderr, "A close %p\n", sf);
#endif
rc = call_callback(fill_callback_args(sf, CB_IO_TYPE_CLOSE, NULL, 0, 0));
#ifdef CHECK_PROTOCOL
((callback_file *)sf)->sm = NULL;
#endif
return rc;
}
/*
** Read data from an callback-file.
*/
static int callbackReadNot(sqlite3_file *sf, void *zBuf,
int iAmt, sqlite_int64 iOfst)
{
#ifdef TRACE
fprintf(stderr, "read not %p\n", sf);
#endif
return SQLITE_IOERR;
}
static int callbackRead(sqlite3_file *sf, void *zBuf,
int iAmt, sqlite_int64 iOfst)
{
#ifdef TRACE
fprintf(stderr, "cb read %p %p %d %ld\n", sf, zBuf, iAmt, (long int) iOfst);
#endif
return call_callback(fill_callback_args(sf, CB_IO_TYPE_READ, zBuf, iAmt, iOfst));
}
/*
** Write data to an callback-file.
*/
static int callbackWriteNot(sqlite3_file *pFile, const void *zBuf,
int iAmt, sqlite_int64 iOfst) {
return SQLITE_OK;
}
static int callbackWrite(
sqlite3_file *sf, // structure
const void *zBuf, // buffer with data
int iAmt, // amount of bytes to write
sqlite_int64 iOfst // offset of file to write
){
#ifdef TRACE
fprintf(stderr, "cb write %p %p %d %ld\n", sf, zBuf, iAmt, (long int) iOfst);
#endif
return call_callback(fill_callback_args(sf, CB_IO_TYPE_WRITE, (void*) zBuf, iAmt, iOfst));
}
/*
** Truncate an callback-file.
*/
static int callbackTruncateNot(sqlite3_file *pFile, sqlite_int64 size)
{
return SQLITE_OK;
}
static int callbackTruncate(sqlite3_file *sf, sqlite_int64 size)
{
#ifdef TRACE
fprintf(stderr, "Atruncate\n");
#endif
return call_callback(fill_callback_args(sf, CB_IO_TYPE_TRUNC, NULL, 0, size));
}
/*
** Sync an callback-file.
*/
static int callbackSyncNot(sqlite3_file *pFile, int flags)
{
return SQLITE_OK;
}
/*
** Return the current file-size of an callback-file.
*/
static int callbackFileSizeNot(sqlite3_file *pFile, sqlite_int64 *pSize)
{
*pSize = 0;
return SQLITE_OK;
}
static int callbackFileSize(sqlite3_file *sf, sqlite_int64 *pSize)
{
callback_file *p = (callback_file *) sf;
struct callback_args *a = p->sm;
int rc;
#ifdef TRACE
fprintf(stderr, "cb fileSize %p\n", sf);
#endif
rc = call_callback(fill_callback_args(sf, CB_IO_TYPE_FSIZE, pSize, 0, 0));
#ifdef TRACE
fprintf(stderr, "cb fileSize %p: %ld\n", sf, (long) a->offset);
#endif
if(rc == SQLITE_OK) *pSize = a->offset;
return rc;
}
/*
** Lock an callback-file.
*/
static int callbackLockNot(sqlite3_file *pFile, int eLock)
{
return SQLITE_OK;
}
static int callbackLock(sqlite3_file *pFile, int eLock)
{
callback_file *p = (callback_file *) pFile;
#ifdef TRACE
fprintf(stderr, "Alock\n");
#endif
return SQLITE_OK;
}
/*
** Unlock an callback-file.
*/
static int callbackUnlockNot(sqlite3_file *pFile, int eLock)
{
return SQLITE_OK;
}
static int callbackUnlock(sqlite3_file *pFile, int eLock)
{
callback_file *p = (callback_file *) pFile;
#ifdef TRACE
fprintf(stderr, "Aunlock %s\n", p->zName);
#endif
return SQLITE_OK;
}
/*
** Check if another file-handle holds a RESERVED lock on an callback-file.
*/
static int callbackCheckReservedLockNot(sqlite3_file *pFile, int *pResOut)
{
return SQLITE_OK;
}
static int callbackCheckReservedLock(sqlite3_file *pFile, int *pResOut)
{
callback_file *p = (callback_file *) pFile;
#ifdef TRACE
fprintf(stderr, "cb check reserved lock\n");
#endif
return SQLITE_OK;
}
/*
** File control method. For custom operations on an callback-file.
*/
static int callbackFileControl(sqlite3_file *pFile, int op, void *pArg)
{
return SQLITE_NOTFOUND;
}
/*
** Return the sector-size in bytes for an callback-file.
*/
static int callbackSectorSizeNot(sqlite3_file *pFile)
{
return 0;
}
static int callbackSectorSize(sqlite3_file *sf)
{
callback_file *p = (callback_file *) sf;
struct callback_args *a = p->sm;
call_callback(fill_callback_args(sf, CB_IO_TYPE_BLKSZ, NULL, 0, 0));
#ifdef TRACE
fprintf(stderr, "SectorSize %p %ld\n", sf, (long) a->offset);
#endif
return a->offset;
}
/*
** Return the device characteristic flags supported by an callback-file.
*/
static int callbackDeviceCharacteristics(sqlite3_file *pFile)
{
return SQLITE_IOCAP_ATOMIC | SQLITE_IOCAP_SAFE_APPEND;
}
/*
** Delete the file located at zPath. If the dirSync argument is true,
** ensure the file-system modifications are synced to disk before
** returning.
*/
static int callbackDelete(sqlite3_vfs *aVfs, const char *zPath, int dirSync)
{
return SQLITE_OK;
}
/*
** Test for access permissions. Return true if the requested permission
** is available, or false otherwise.
*/
static int callbackAccess(
sqlite3_vfs *aVfs,
const char *zPath,
int flags,
int *pResOut
)
{
// This VFS does not use `zPath` at all, no reason to return anything but success.
*pResOut = 0;
#ifdef TRACE
fprintf(stderr, "A access %s want %d got %d\n", zPath, flags, *pResOut);
#endif
return SQLITE_OK;
}
/*
** Populate buffer zOut with the full canonical pathname corresponding
** to the pathname in zPath. zOut is guaranteed to point to a buffer
** of at least (CALLBACK_MAX_PATHNAME+1) bytes.
*/
static int callbackFullPathname(
sqlite3_vfs *aVfs,
const char *zPath,
int nOut,
char *zOut
){
// This VFS does not use `zPath` at all, we simply copy the input.
C_strncpy(zOut, zPath, nOut);
#ifdef TRACE
fprintf(stderr, "cb fpn %s\n", zPath);
#endif
return SQLITE_OK;
}
/*
** Open the dynamic library located at zPath and return a handle.
*/
static void *callbackDlOpen(sqlite3_vfs *aVfs, const char *zPath){
#ifdef TRACE
fprintf(stderr, "Adlop\n");
#endif
return pVfs->xDlOpen(pVfs, zPath);
}
/*
** Populate the buffer zErrMsg (size nByte bytes) with a human readable
** utf-8 string describing the most recent error encountered associated
** with dynamic libraries.
*/
static void callbackDlError(sqlite3_vfs *aVfs, int nByte, char *zErrMsg){
#ifdef TRACE
fprintf(stderr, "Adlerr\n");
#endif
return pVfs->xDlError(pVfs, nByte, zErrMsg);
}
/*
** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
*/
static void (*callbackDlSym(sqlite3_vfs *aVfs, void *p, const char *zSym))(void){
#ifdef TRACE
fprintf(stderr, "Adlsy\n");
#endif
return pVfs->xDlSym(pVfs, p, zSym);
}
/*
** Close the dynamic library handle pHandle.
*/
static void callbackDlClose(sqlite3_vfs *aVfs, void *pHandle){
#ifdef TRACE
fprintf(stderr, "Adlclo\n");
#endif
return pVfs->xDlClose(pVfs, pHandle);
}
/*
** Populate the buffer pointed to by zBufOut with nByte bytes of
** random data.
*/
static int callbackRandomness(sqlite3_vfs *aVfs, int nByte, char *zBufOut){
#ifdef TRACE
fprintf(stderr, "arand\n");
#endif
return pVfs->xRandomness(pVfs, nByte, zBufOut);
}
/*
** Sleep for nMicro microseconds. Return the number of microseconds
** actually slept.
*/
static int callbackSleep(sqlite3_vfs *aVfs, int nMicro){
#ifdef TRACE
fprintf(stderr, "Aslee\n");
#endif
return pVfs->xSleep(pVfs, nMicro);
}
/*
** Return the current time as a Julian Day number in *pTimeOut.
*/
static int callbackCurrentTime(sqlite3_vfs *pVfs, double *result){
#ifdef TRACE
fprintf(stderr, "Act\n");
#endif
*result=-1.0;
return -1;
}
/*
** Initialising the wrapper vfs callback_vfs
*/
#define max(a,b) ((a) > (b) ? (a) : (b))
/* Include this to run sqlite3 from fixed memory
*
* REQUIRED: configure sqlite3 with --enable-threadsafe=yes
*/
static void ac_config_sqlite3() {
if(sqlite3_config(SQLITE_CONFIG_MULTITHREAD) != SQLITE_OK)
fprintf(stderr, "SQLITE3 config MULTITHREAD failed.\n");
{
size_t size=5000000;
void* heap = malloc(size);
if(heap==NULL)
fprintf(stderr, "SQLITE3 Heap Allocation Failed \n");
else if(sqlite3_config(SQLITE_CONFIG_HEAP, heap, size, sizeof(C_word))
!= SQLITE_OK) {
fprintf(stderr, "SQLITE3 Heap Config Failed \n");
}}
}
#ifdef TRACE
static
void sqlite3logfn(void *id, int err, const char* msg)
{
const char *tag = id;
fprintf(stderr, "%s %s\n", id, msg);
}
#endif
static
sqlite3_vfs *callback_sqlite3_vfs_init(void *cb, void *cbr) {
/* ac_config_sqlite3(); */
#ifdef TRACE
if(sqlite3_config(SQLITE_CONFIG_LOG, sqlite3logfn, "SQLite Log: ") != SQLITE_OK)
fprintf(stderr, "SQLITE3 config LOG failed.\n");
fprintf(stderr, "regring %s \n", callback_vfs.zName);
#endif
pthread_mutex_init(&the_shared_memory_mux, NULL);
pthread_cond_init(&the_shared_memory_cond, NULL);
if(pVfs == NULL ) {
pVfs = sqlite3_vfs_find(NULL); // fetch default vfs
if( !pVfs ){
return NULL;
}
callback_vfs.szOsFile = callback_vfs.szOsFile - sizeof(sqlite3_file) + pVfs->szOsFile;
sqlite3_vfs_register(&callback_vfs, 0);
}
the_callback_callback = cb;
the_callback_arg_converter = cbr;
#ifdef TRACE
fprintf(stderr, "registered %s \n", callback_vfs.zName);
#endif
return &callback_vfs;
}