-
Notifications
You must be signed in to change notification settings - Fork 12
/
os_vms.c
1105 lines (955 loc) · 28.8 KB
/
os_vms.c
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
/*
** 2013 March 8
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code that is specific to OpenVMS.
*/
#include "sqliteInt.h"
#if SQLITE_OS_VMS /* This file is used for OpenVMS only */
#include <armdef.h>
#include <atrdef.h>
#include <chpdef.h>
#include <descrip.h>
#include <efndef.h>
#include <fatdef.h>
#include <fibdef.h>
#include <iodef.h>
#include <jpidef.h>
#include <lckdef.h>
#include <libfisdef.h>
#include <lib$routines.h>
#include <lkidef.h>
#include <mth$routines.h>
#include <psldef.h>
#include <rms.h>
#include <sbkdef.h>
#include <secdef.h>
#include <ssdef.h>
#include <starlet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <str$routines.h>
#include <stsdef.h>
#include <time.h>
#include <tis.h>
#include <unistd.h>
#ifdef vax
# include <sys$routines.h>
# define FAT struct fatdef
#endif
/*
** Include code that is common to all os_*.c files
*/
#include "os_common.h"
#ifdef min
# undef min
#endif
#define min(m, n) ((m) < (n) ? (m) : (n))
#define LOCK_NAME_MAX 31
struct ile3 {
unsigned short buflen;
unsigned short itmcod;
void *bufadr;
unsigned int *retlen;
};
/*
** This array provides the conversion between SQLite locking levels
** and the equivalent OpenVMS DLM levels.
*/
const static int lock_modes[] = {
LCK$K_NLMODE, /* SQLITE_LOCK_NONE */
LCK$K_PRMODE, /* SQLITE_LOCK_SHARED */
LCK$K_PWMODE, /* SQLITE_LOCK_RESERVED */
-1, /* SQLITE_LOCK_PENDING */
LCK$K_EXMODE /* SQLITE_LOCK_EXCLUSIVE */
};
/*
** The vmsFile structure is a subclass of sqlite3_file *, specific to the
** OpenVMS portability layer.
*/
typedef struct vmsFile vmsFile;
struct vmsFile {
sqlite3_io_methods const *pMethod; /*** Must be first ***/
sqlite3_vfs *pVfs; /* The VFS that created his vmsFile */
int szChunk;
int szHint;
char lock_name[LOCK_NAME_MAX+1];
char esa[NAM$C_MAXRSS+1];
struct {
unsigned short status;
unsigned short reserved;
unsigned long id;
} lksb;
struct FAB fab;
struct NAM nam;
unsigned short chan;
};
/*****************************************************************************
** The next group of routines implement the I/O methods specified
** by the sqlite3_io_methods object.
******************************************************************************/
static int vmsClose(
sqlite3_file *id /* File to close */
){
vmsFile *pFile = (vmsFile *)id;
sys$deq(pFile->lksb.id, 0, PSL$C_USER, LCK$M_CANCEL);
sys$dassgn(pFile->chan);
return SQLITE_OK;
}
static int vmsRead(
sqlite3_file *id, /* File to read from */
void *vBuf, /* Write content into this buffer */
int amt, /* Number of bytes to read */
sqlite3_int64 offset /* Begin reading at this offset */
){
int bcnt;
char *pBuf = vBuf;
int remainder;
char buf[SQLITE_DEFAULT_SECTOR_SIZE];
unsigned short iosb[4];
int status = SS$_NORMAL;
vmsFile *pFile = (vmsFile *)id;
/*
** Determine the virtual block we are to read from, and a possbile byte
** position within that block.
*/
int vbn = (offset / SQLITE_DEFAULT_SECTOR_SIZE) + 1;
int vpos = offset % SQLITE_DEFAULT_SECTOR_SIZE;
if( vpos ){
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_READVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn++, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
bcnt = min(amt, SQLITE_DEFAULT_SECTOR_SIZE - vpos);
memcpy(pBuf, &buf[vpos], bcnt);
pBuf += bcnt;
amt -= bcnt;
}
}
if( (amt >= SQLITE_DEFAULT_SECTOR_SIZE) && $VMS_STATUS_SUCCESS(status) ){
remainder = amt % SQLITE_DEFAULT_SECTOR_SIZE;
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_READVBLK, iosb,
0, 0, pBuf, amt - remainder, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
bcnt = iosb[1] | (iosb[2] << 16);
if( bcnt < (amt - remainder) ){
remainder = amt - bcnt;
status = SS$_ENDOFFILE;
}else{
bcnt = amt - remainder;
}
pBuf += bcnt;
vbn += bcnt / SQLITE_DEFAULT_SECTOR_SIZE;
amt = remainder;
}
}
if( (amt > 0) && $VMS_STATUS_SUCCESS(status) ){
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_READVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
memcpy(pBuf, buf, amt);
}
}
if( status == SS$_ENDOFFILE ){
/*
** Unread parts of the buffer must be zero-filled.
*/
memset(pBuf, '\0', amt);
return SQLITE_IOERR_SHORT_READ;
}else if( !$VMS_STATUS_SUCCESS(status) ){
return SQLITE_IOERR_READ;
}
return SQLITE_OK;
}
static int vmsWrite(
sqlite3_file *id, /* File to read from */
const void *vBuf, /* The bytes to be written */
int amt, /* Number of bytes to write */
sqlite3_int64 offset /* Offset into the file to begin writing at */
){
struct atrdef atr[2];
struct ile3 jpilst[2];
FAT fat;
struct fibdef fib;
char buf[SQLITE_DEFAULT_SECTOR_SIZE];
const char *pBuf = vBuf;
int bcnt, extend = 0, filesize, needed, remainder;
struct dsc$descriptor fibdsc;
unsigned short iosb[4];
int status;
vmsFile *pFile = (vmsFile *)id;
/*
** Determine the virtual block we are to write to, and a possbile byte
** position within that block.
*/
int vbn = (offset / SQLITE_DEFAULT_SECTOR_SIZE) + 1;
int vpos = offset % SQLITE_DEFAULT_SECTOR_SIZE;
memset(&fib, 0, sizeof(fib));
fib.fib$v_writethru = 1;
fib.fib$w_fid[0] = pFile->nam.nam$w_fid[0];
fib.fib$w_fid[1] = pFile->nam.nam$w_fid[1];
fib.fib$w_fid[2] = pFile->nam.nam$w_fid[2];
fibdsc.dsc$w_length = sizeof(fib);
fibdsc.dsc$a_pointer = (char *)&fib;
atr[0].atr$w_size = ATR$S_RECATTR;
atr[0].atr$w_type = ATR$C_RECATTR;
atr[0].atr$l_addr = &fat;
atr[1].atr$w_size = 0;
atr[1].atr$w_type = 0;
/*
** Before doing a write we determine the size of the file
** and if we need to extend it to perform the requested
** I/O.
*/
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_ACCESS, iosb, 0,
0, &fibdsc, 0, 0, 0, atr, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
filesize = (fat.fat$w_hiblkh << 16) | fat.fat$w_hiblkl;
needed = (vbn + (amt / SQLITE_DEFAULT_SECTOR_SIZE)) + 1;
if( pFile->szHint > 0 ){
needed = pFile->szHint > needed ? pFile->szHint : needed;
pFile->szHint = 0;
}
if( filesize < needed ){
fib.fib$v_extend = 1;
fib.fib$l_exvbn = 0;
fib.fib$l_exsz = 0;
/*
** Here we first check to see if the user has indicated their
** own extension size, otherwise we attempt to respect the
** default extensions of the file. Someone might have
** SET FILE/EXTENSION on the database file as a performance
** measure. However, if none is set, we just extend by how much
** we need of the system default, whichever is larger.
*/
if( pFile->szChunk > 0 ){
do{
fib.fib$l_exsz += pFile->szChunk;
}while( fib.fib$l_exsz < needed );
}else{
if( fat.fat$w_defext ){
do{
fib.fib$l_exsz += fat.fat$w_defext;
}while( fib.fib$l_exsz < needed );
}else{
jpilst[0].itmcod = JPI$_RMS_EXTEND_SIZE;
jpilst[0].buflen = sizeof(extend);
jpilst[0].bufadr = &extend;
jpilst[0].retlen = 0;
jpilst[1].itmcod = 0;
jpilst[1].buflen = 0;
sys$getjpiw(EFN$C_ENF, 0, 0, jpilst, iosb, 0, 0);
fib.fib$l_exsz = extend ? extend : needed;
fib.fib$v_aldef = 1;
}
}
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_MODIFY, iosb,
0, 0, &fibdsc, 0, 0, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
fib.fib$v_extend = 0;
fib.fib$l_exvbn += fib.fib$l_exsz;
fat.fat$w_ffbyte = 0;
fat.fat$w_efblkh = (unsigned short)(fib.fib$l_exvbn >> 16);
fat.fat$w_efblkl = (unsigned short)fib.fib$l_exvbn;
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_MODIFY, iosb,
0, 0, &fibdsc, 0, 0, 0, atr, 0);
if( $VMS_STATUS_SUCCESS(status) ){
status = iosb[0];
}
}
}
}
if( vpos && $VMS_STATUS_SUCCESS(status) ){
/*
** The requested offset is NOT on a block boundry, so we need
** to read the first block, apply the portion of pBuf and then
** write it back.
*/
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_READVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
bcnt = min(amt, SQLITE_DEFAULT_SECTOR_SIZE - vpos);
memcpy(&buf[vpos], pBuf, bcnt);
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_WRITEVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn++, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
amt -= bcnt;
if( bcnt <= 0 ){
pBuf += bcnt;
}
}
}
}
if( (amt >= SQLITE_DEFAULT_SECTOR_SIZE) && $VMS_STATUS_SUCCESS(status) ){
/*
** Now, let's write out the middle of the buffer to
** the file.
*/
remainder = amt % SQLITE_DEFAULT_SECTOR_SIZE;
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_WRITEVBLK, iosb,
0, 0, pBuf, amt - remainder, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
if( bcnt > 0 ){
pBuf += (amt - remainder);
vbn += (amt / SQLITE_DEFAULT_SECTOR_SIZE);
}
amt = remainder;
}
}
if( (amt > 0) && $VMS_STATUS_SUCCESS(status) ){
/*
** Finally, we write any trailing bytes out to the file.
*/
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_READVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
memcpy(buf, pBuf, amt);
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_WRITEVBLK, iosb,
0, 0, buf, SQLITE_DEFAULT_SECTOR_SIZE, vbn, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status) ){
status = iosb[0];
}
}
}
return $VMS_STATUS_SUCCESS(status) ? SQLITE_OK : SQLITE_IOERR_WRITE;
}
static int vmsTruncate(
sqlite3_file *id, /* File to truncate */
sqlite3_int64 nByte /* Size to truncate file to */
){
struct fibdef fib;
struct dsc$descriptor fibdsc;
unsigned short iosb[4];
vmsFile *pFile = (vmsFile *)id;
int status;
memset(&fib, 0, sizeof(fib));
fib.fib$v_trunc = 1;
fib.fib$l_exvbn = (nByte / 512) + ((nByte % 512) ? 1 : 0);
fibdsc.dsc$w_length = sizeof(fib);
fibdsc.dsc$a_pointer = (char *)&fib;
sys$qiow(EFN$C_ENF, pFile->chan, IO$_MODIFY, iosb, 0, 0, &fibdsc,
0, 0, 0, 0, 0);
return SQLITE_OK;
}
/*
** Exterything goes straight to disk, so this isn't really even
** necessary.
*/
static int vmsSync(
sqlite3_file *id, /* File to sync */
int flags
){
return SQLITE_OK;
}
static int vmsFileSize(
sqlite3_file *id, /* File to get size of */
sqlite3_int64 *pSize /* Write size of file here */
){
struct atrdef atr[2];
FAT fat;
struct fibdef fib;
struct dsc$descriptor fibdsc;
unsigned short iosb[4];
vmsFile *pFile = (vmsFile *)id;
int status;
memset(&fib, 0, sizeof(fib));
fib.fib$w_fid[0] = pFile->nam.nam$w_fid[0];
fib.fib$w_fid[1] = pFile->nam.nam$w_fid[1];
fib.fib$w_fid[2] = pFile->nam.nam$w_fid[2];
fibdsc.dsc$w_length = sizeof(fib);
fibdsc.dsc$a_pointer = (char *)&fib;
atr[0].atr$w_size = ATR$S_RECATTR;
atr[0].atr$w_type = ATR$C_RECATTR;
atr[0].atr$l_addr = &fat;
atr[1].atr$w_size = 0;
atr[1].atr$w_type = 0;
status = sys$qiow(EFN$C_ENF, pFile->chan, IO$_ACCESS, iosb, 0, 0,
&fibdsc, 0, 0, 0, atr, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(iosb[0]) ){
*pSize = ((fat.fat$w_hiblkh << 16) | fat.fat$w_hiblkl) *
SQLITE_DEFAULT_SECTOR_SIZE;
return SQLITE_OK;
}
return SQLITE_IOERR_FSTAT;
}
static int vmsLock(
sqlite3_file *id, /* File to query */
int locktype /* Requested lock mode */
){
vmsFile *pFile = (vmsFile *)id;
int status;
status = sys$enqw(EFN$C_ENF, lock_modes[locktype], &pFile->lksb,
LCK$M_CONVERT, 0, 0, 0, 0, 0, PSL$C_USER, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(pFile->lksb.status) ){
return SQLITE_OK;
}
return SQLITE_BUSY;
}
/*
** This routine checks if there is a RESERVED lock held on the specified
** file by this or any other process. If such a lock is held, set *pResOut
** to a non-zero value otherwise *pResOut is set to zero. The return value
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
*/
static int vmsCheckReservedLock(
sqlite3_file *id, /* File to query */
int *pResOut /* Is current lock RESERVED? */
){
char state[3];
int iosb[2];
int rc = SQLITE_OK;
struct ile3 itmlst[2];
vmsFile *pFile = (vmsFile *)id;
int status;
*pResOut = 0;
itmlst[0].itmcod = LKI$_STATE;
itmlst[0].buflen = sizeof(state);
itmlst[0].bufadr = state;
itmlst[0].retlen = 0;
itmlst[1].buflen = 0;
itmlst[1].itmcod = 0;
status = sys$getlkiw(EFN$C_ENF, &pFile->lksb.id, itmlst, iosb, 0, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status = iosb[0]) ){
if( state[1] == lock_modes[SQLITE_LOCK_RESERVED] ){
*pResOut = 1;
}
}else{
rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
}
return rc;
}
static int vmsUnLock(
sqlite3_file *id, /* File to query */
int locktype /* Requested lock mode */
){
vmsFile *pFile = (vmsFile *)id;
int status;
status = sys$enqw(EFN$C_ENF, lock_modes[locktype], &pFile->lksb,
LCK$M_CONVERT, 0, 0, 0, 0, 0, PSL$C_USER, 0, 0);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(pFile->lksb.status) ){
return SQLITE_OK;
}
return SQLITE_IOERR_UNLOCK;
}
static int vmsFileControl(
sqlite3_file *id, /* File to query */
int op,
void *pArg
){
vmsFile *pFile = (vmsFile *)id;
switch( op ){
case SQLITE_FCNTL_CHUNK_SIZE: {
pFile->szChunk = *(int *)pArg;
return SQLITE_OK;
}
case SQLITE_FCNTL_SIZE_HINT: {
pFile->szHint = (*(int *)pArg / SQLITE_DEFAULT_SECTOR_SIZE) + 1;
return SQLITE_OK;
}
case SQLITE_FCNTL_VFSNAME: {
*(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
return SQLITE_OK;
}
}
return SQLITE_NOTFOUND;
}
static int vmsSectorSize(
sqlite3_file *id /* File to get sector size from */
){
return SQLITE_DEFAULT_SECTOR_SIZE;
}
static int vmsDeviceCharacteristics(
sqlite3_file *id /* File to close */
){
return SQLITE_IOCAP_ATOMIC512 | SQLITE_IOCAP_SEQUENTIAL;
}
static int vmsShmMap(
sqlite3_file *id, /* Handle open on database file */
int iRegion, /* Region to retrieve */
int szRegion, /* Size of regions */
int bExtend, /* True to extend file if necessary */
void volatile **pp /* OUT: Mapped memory */
){
vmsFile *pFile = (vmsFile *)id;
int status;
#if 0
// generate the lock name...
status = sys$enqw(EFN$C_ENF, LCK$K_NLMODE, &lksb,
LCK$M_NOQUEUE | LCK$M_SYSTEM | LCK$M_EXPEDITE, lock_name,
0, 0, 0, 0, PSL$C_USER, 0, 0);
if( $VMS_STATUS_SUCCESS(status) ){
status = sys$enqw(EFN$C_ENF, LCK$K_EXMODE, &lksb,
LCK$M_NOQUEUE | LCK$M_SYSTEM | LCK$M_CONVERT, 0,
if( $VMS_STATUS_SUCCESS(status) ){
status = sys$mgblsc();
if( $VMS_STATUS_SUCCESS(status) ){
} else if( status == ... ){
status = sys$crmpsc();
} else {
// error
}
} else {
// if because its already allocate?
// mpgblsc
// else
// error
}
} else {
// report an error...
}
#endif
return SQLITE_OK;
}
static int vmsShmLock(
sqlite3_file *id,
int offset,
int n,
int flags
){
int lkmode;
#if 0
if( flags & SQLITE_SHM_UNLOCK ){
lkmode = LCK$K_NLMODE;
} else if( flags & SQLITE_SHM_SHARED ){
lkmode = LCK$K_PRMODE;
} else if( flags & SQLITE_SHM_EXCLUSIVE ){
lkmode = LCK$K_EXMODE;
}
status = sys$enqw(EFN$C_ENF, lkmode,
// report appropriate status value...
#endif
return SQLITE_OK;
}
static void vmsShmBarrier(
sqlite3_file *id
){
int status;
#if 0
status = sys$updsecw();
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(status == iosb[0]) ){
// report success...
} else {
// translate error
}
#endif
}
static int vmsShmUnmap(
sqlite3_file *id,
int deleteFlag
){
// hmmm, how to unmap a temporary section? $delva?
return SQLITE_OK;
}
/*
** Here ends the implementation of all sqlite3_file methods.
**
********************** End sqlite3_file Methods *******************************
******************************************************************************/
/*
** This vector defines all the methods that can operate on an
** sqlite3_file for OpenVMS.
*/
static const sqlite3_io_methods vmsIoMethod = {
2, /* iVersion */
vmsClose, /* xClose */
vmsRead, /* xRead */
vmsWrite, /* xWrite */
vmsTruncate, /* xTruncate */
vmsSync, /* xSync */
vmsFileSize, /* xFileSize */
vmsLock, /* xLock */
vmsUnLock, /* xUnlock */
vmsCheckReservedLock, /* xCheckReservedLock */
vmsFileControl, /* xFileControl */
vmsSectorSize, /* xSectorSize */
vmsDeviceCharacteristics, /* xDeviceCharacteristics */
vmsShmMap, /* xShmMap */
vmsShmLock, /* xShmLock */
vmsShmBarrier, /* xShmBarrier */
vmsShmUnmap /* xShmUnmap */
};
/****************************************************************************
**************************** sqlite3_vfs methods ****************************
**
** This division contains the implementation of methods on the
** sqlite3_vfs object.
*/
static int vmsOpen(
sqlite3_vfs *pVfs, /* VFS record */
const char *zFilename, /* Name of file to open */
sqlite3_file *id, /* Write the SQLite file handle here */
int flags, /* Open mode flags */
int *pOutFlags /* Status return flags */
){
vmsFile *pFile = (vmsFile *)id;
struct dsc$descriptor resnam;
int status;
memset(pFile, 0, sizeof(*pFile));
pFile->pMethod = &vmsIoMethod;
pFile->pVfs = pVfs;
pFile->nam = cc$rms_nam;
pFile->nam.nam$l_esa = pFile->esa;
pFile->nam.nam$b_ess = NAM$C_MAXRSS;
pFile->fab = cc$rms_fab;
pFile->fab.fab$l_nam = &pFile->nam;
pFile->fab.fab$l_fna = (char *)zFilename;
pFile->fab.fab$b_fns = zFilename ? strlen(zFilename) : 0;
pFile->fab.fab$b_org = FAB$C_SEQ;
pFile->fab.fab$b_rfm = FAB$C_FIX;
pFile->fab.fab$w_mrs = 512;
pFile->fab.fab$v_shrput = 1;
pFile->fab.fab$v_shrupd = 1;
pFile->fab.fab$v_upi = 1;
pFile->fab.fab$v_ufo = 1;
pFile->fab.fab$b_rtv = 255;
pFile->fab.fab$v_cbt = 1;
if( !zFilename || !*zFilename ){
pFile->fab.fab$v_put = 1;
pFile->fab.fab$v_tmd = 1;
pFile->fab.fab$v_upd = 1;
status = sys$create(&pFile->fab);
}else{
if( flags & SQLITE_OPEN_DELETEONCLOSE ){
pFile->fab.fab$v_dlt = 1;
}
if( flags & SQLITE_OPEN_READONLY ){
pFile->fab.fab$v_get = 1;
}else{
pFile->fab.fab$v_put = 1;
pFile->fab.fab$v_upd = 1;
}
if( flags & SQLITE_OPEN_EXCLUSIVE ){
status = sys$create(&pFile->fab);
}else if( flags & SQLITE_OPEN_CREATE ){
pFile->fab.fab$v_cif = 1;
status = sys$create(&pFile->fab);
}else{
status = sys$open(&pFile->fab);
}
}
if( $VMS_STATUS_SUCCESS(status) ){
pFile->chan = pFile->fab.fab$l_stv;
pFile->esa[pFile->nam.nam$b_esl] = 0;
if( pOutFlags ){
if( flags & SQLITE_OPEN_READWRITE ){
*pOutFlags = SQLITE_OPEN_READWRITE;
}else{
*pOutFlags = SQLITE_OPEN_READONLY;
}
}
sprintf(pFile->lock_name, "SQLITE3_%04X%04X%04X",
pFile->nam.nam$w_fid[0], pFile->nam.nam$w_fid[1],
pFile->nam.nam$w_fid[2]);
resnam.dsc$w_length = strlen(pFile->lock_name);
resnam.dsc$b_dtype = DSC$K_DTYPE_T;
resnam.dsc$b_class = DSC$K_CLASS_S;
resnam.dsc$a_pointer = pFile->lock_name;
status = sys$enqw(EFN$C_ENF, LCK$K_NLMODE, &pFile->lksb,
LCK$M_NOQUEUE | LCK$M_SYSTEM | LCK$M_EXPEDITE,
&resnam, 0, NULL, NULL, NULL, PSL$C_USER, NULL, NULL);
if( $VMS_STATUS_SUCCESS(status)
&& $VMS_STATUS_SUCCESS(pFile->lksb.status) ){
return SQLITE_OK;
}
sys$close(&pFile->fab);
}
return SQLITE_CANTOPEN;
}
static int vmsDelete(
sqlite3_vfs *pVfs, /* VFS record */
const char *zFilename, /* Name of file to delete */
int syncDit /* Not used on OpenVMS */
){
struct FAB fab;
int status;
fab = cc$rms_fab;
fab.fab$l_fna = (char *)zFilename;
fab.fab$b_fns = strlen(zFilename);
status = sys$erase(&fab);
if( $VMS_STATUS_SUCCESS(status)
|| (status == RMS$_FNF) ){
return SQLITE_OK;
}
return SQLITE_IOERR_DELETE;
}
static int vmsAccess(
sqlite3_vfs *pVfs, /* VFS record */
const char *zFilename, /* Name of file to check */
int flags, /* Type of test to make on this file */
int *pResOut /* Result */
){
static struct dsc$descriptor username = { 0 };
int access;
int seflags = CHP$M_OBSERVE;
struct dsc$descriptor file;
struct ile3 itmlst[3];
int result;
int status;
static $DESCRIPTOR(class, "FILE");
if( !username.dsc$a_pointer ){
long jpicod = JPI$_USERNAME;
username.dsc$b_dtype = DSC$K_DTYPE_T;
username.dsc$b_class = DSC$K_CLASS_D;
status = lib$getjpi(&jpicod, 0, 0, 0, &username, 0);
if( !$VMS_STATUS_SUCCESS(status) ){
return SQLITE_IOERR_ACCESS;
}
}
file.dsc$w_length = strlen(zFilename);
file.dsc$a_pointer = (char *)zFilename;
access = ARM$M_READ;
if( flags == SQLITE_ACCESS_READWRITE ){
access |= ARM$M_WRITE;
}
itmlst[0].itmcod = CHP$_ACCESS;
itmlst[0].buflen = sizeof(access);
itmlst[0].bufadr = &access;
itmlst[0].retlen = NULL;
itmlst[1].itmcod = CHP$_FLAGS;
itmlst[1].buflen = sizeof(seflags);
itmlst[1].bufadr = &seflags;
itmlst[1].retlen = NULL;
itmlst[2].itmcod = CHP$_END;
itmlst[2].buflen = 0;
status = sys$check_access(0, &file, &username, itmlst, 0, &class, 0, 0);
if( $VMS_STATUS_SUCCESS(status) ){
if( pResOut ){
*pResOut = 1;
}
return SQLITE_OK;
}else{
if( pResOut ){
*pResOut = 0;
}
if( flags == SQLITE_ACCESS_EXISTS ){
if( status == RMS$_FNF ){
return SQLITE_OK;
}
}else if( status == SS$_NOPRIV || status == SS$_NOCALLPRIV ){
return SQLITE_OK;
}
}
return SQLITE_IOERR_ACCESS;
}
static int vmsFullPathname(
sqlite3_vfs *pVfs, /* VFS record */
const char *zRelative, /* Input path */
int nFull, /* Size of output buffer in bytes */
char *zFull /* Output buffer */
){
struct FAB fab;
struct NAM nam;
int status;
nam = cc$rms_nam;
nam.nam$v_synchk = 1;
nam.nam$l_esa = zFull;
nam.nam$b_ess = nFull - 1; /* subtract 1 for terminating NULL */
fab = cc$rms_fab;
fab.fab$l_nam = &nam;
fab.fab$l_fna = (char *)zRelative;
fab.fab$b_fns = strlen(zRelative);
status = sys$parse(&fab);
if( $VMS_STATUS_SUCCESS(status) ){
*nam.nam$l_ver = '\0';
return SQLITE_OK;
}
return SQLITE_ERROR;
}
#ifndef SQLITE_OMIT_LOAD_EXTENSION
/*
** -- possibly allocate a structure of a pair of descriptors
** that contains the default part and the name part, after
** parsing the incoming name. Need to check what we have...
*/
static void *vmsDlOpen(
sqlite3_vfs *notUsed,
const char *zName
){
struct dsc$descriptor *dImagename = 0;
struct dsc$descriptor dName;
int status;
static $DESCRIPTOR(prefix, "SQLITE3_EXTENSION_");
dImagename = sqlite3_malloc(sizeof(*dImagename));
if( dImagename ){
dImagename->dsc$w_length = 0;
dImagename->dsc$b_dtype = DSC$K_DTYPE_T;
dImagename->dsc$b_class = DSC$K_CLASS_D;
dImagename->dsc$a_pointer = 0;
dName.dsc$w_length = strlen(zName);
dName.dsc$b_dtype = DSC$K_DTYPE_T;
dName.dsc$b_class = DSC$K_CLASS_S;
dName.dsc$a_pointer = (char *)zName;
status = str$concat(dImagename, &prefix, &dName);
if( $VMS_STATUS_SUCCESS(status) ){
return dImagename;
}else{
sqlite3_free(dImagename);
}
}
return 0;
}
static void (*vmsDlSym(
sqlite3_vfs *pVfs,
void *pHandle,
const char *zSymbol
))(void){
struct dsc$descriptor *dName = pHandle;
struct dsc$descriptor dSymbol;
int flags = LIB$M_FIS_MIXEDCASE;
void (*result)(void) = 0;
dSymbol.dsc$w_length = strlen(zSymbol);
dSymbol.dsc$b_dtype = DSC$K_DTYPE_T;
dSymbol.dsc$b_class = DSC$K_CLASS_S;
dSymbol.dsc$a_pointer = (char *)zSymbol;
lib$find_image_symbol(dName, &dSymbol, &result, 0, flags);
return result;
}
# define vmsDlError 0
static void vmsDlClose(
sqlite3_vfs *notUsed,
void *pHandle
){
struct dsc$descriptor *dName = pHandle;
if( dName ){
str$free1_dx(dName);
sqlite3_free(dName);
}
}
#else
# define vmsDlOpen 0
# define vmsDlError 0
# define vmsDlSym 0
# define vmsDlClose 0
#endif
static int vmsRandomness(
sqlite3_vfs *pVfs, /* VFS record */
int nBuf, /* Length of output buffer */
char *zBuf /* Buffer to write random data to */
){
static int seed = 0;
char *pBuf = zBuf;
float rval;
int bcnt;
if( !seed ){
unsigned int time[2];
sys$gettim(time);
seed = time[0] ^ time[1] ^ getpid();
}
while( pBuf < (zBuf + nBuf) ){
rval = mth$random(&seed);
bcnt = min(pBuf - (zBuf + nBuf), sizeof(rval));
memcpy(pBuf, &rval, bcnt);
pBuf += bcnt;
}