-
Notifications
You must be signed in to change notification settings - Fork 108
/
FilePlugin.c
1843 lines (1673 loc) · 52.9 KB
/
FilePlugin.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
/* Automatically generated by
VMPluginCodeGenerator * VMMaker.oscog-akg.2341 uuid: d0fa56c5-cccb-41b4-b28d-4c54c9acdccf
from
FilePlugin * VMMaker.oscog-akg.2341 uuid: d0fa56c5-cccb-41b4-b28d-4c54c9acdccf
*/
static char __buildInfo[] = "FilePlugin * VMMaker.oscog-akg.2341 uuid: d0fa56c5-cccb-41b4-b28d-4c54c9acdccf " __DATE__ ;
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "FilePlugin.h"
#include "sqMemoryAccess.h"
/*** Constants ***/
#if !defined(COGMTVM) /* Allow this to be overridden on the compiler command line */
# define COGMTVM 0
#endif
#define DirBadPath 2
#define DirNoMoreEntries 1
#if !defined(PharoVM) /* Allow this to be overridden on the compiler command line */
# define PharoVM 0
#endif
#define PrimErrBadArgument 3
#define PrimErrBadIndex 4
#define PrimErrInappropriate 6
#define PrimErrNoMemory 9
#define PrimErrObjectMayMove 14
#define PrimErrUnsupported 7
#define PrimNoErr 0
#if !defined(SPURVM) /* Allow this to be overridden on the compiler command line */
# define SPURVM 0
#endif
/*** Function Prototypes ***/
static sqInt asciiDirectoryDelimiter(void);
EXPORT(sqInt) connectToFdwrite(int fd, sqInt writeFlag);
EXPORT(sqInt) connectToFilewrite(void *cfile, sqInt writeFlag);
EXPORT(sqInt) fileOpenNamesizewritesecure(char *nameIndex, sqInt nameSize, sqInt writeFlag, sqInt secureFlag);
EXPORT(sqInt) fileOpenNewNamesizesecure(char *nameIndex, sqInt nameSize, sqInt secureFlag);
extern usqIntptr_t fileRecordSize(void);
extern SQFile * fileValueOf(sqInt objectPointer);
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) getThisSession(void);
EXPORT(sqInt) initialiseModule(void);
static sqInt makeDirEntryNamesizecreateDatemodDateisDirfileSize(char *entryName, sqInt entryNameSize, sqInt createDate, sqInt modifiedDate, sqInt dirFlag, squeakFileOffsetType fileSize);
#if PharoVM
static sqInt makeDirEntryNamesizecreateDatemodDateisDirfileSizeposixPermissionsisSymlink(char *entryName, sqInt entryNameSize, sqInt createDate, sqInt modifiedDate, sqInt dirFlag, squeakFileOffsetType fileSize, sqInt posixPermissions, sqInt symlinkFlag);
#endif /* PharoVM */
EXPORT(sqInt) moduleUnloaded(char *aModuleName);
static void * pointerFrom(sqInt pointerByteArray);
EXPORT(sqInt) primitiveConnectToFile(void);
EXPORT(sqInt) primitiveConnectToFileDescriptor(void);
EXPORT(sqInt) primitiveDirectoryCreate(void);
EXPORT(sqInt) primitiveDirectoryDelete(void);
EXPORT(sqInt) primitiveDirectoryDelimitor(void);
EXPORT(sqInt) primitiveDirectoryEntry(void);
EXPORT(sqInt) primitiveDirectoryGetMacTypeAndCreator(void);
EXPORT(sqInt) primitiveDirectoryLookup(void);
EXPORT(sqInt) primitiveDirectorySetMacTypeAndCreator(void);
EXPORT(sqInt) primitiveDisableFileAccess(void);
EXPORT(sqInt) primitiveFileAtEnd(void);
EXPORT(sqInt) primitiveFileClose(void);
EXPORT(sqInt) primitiveFileDelete(void);
EXPORT(sqInt) primitiveFileFlush(void);
EXPORT(sqInt) primitiveFileGetPosition(void);
EXPORT(sqInt) primitiveFileOpen(void);
EXPORT(sqInt) primitiveFileOpenNew(void);
EXPORT(sqInt) primitiveFileRead(void);
static sqInt primitiveFileReadWithoutPinning(void);
static sqInt primitiveFileReadWithPinning(void);
EXPORT(sqInt) primitiveFileRename(void);
EXPORT(sqInt) primitiveFileSetPosition(void);
EXPORT(sqInt) primitiveFileSize(void);
EXPORT(sqInt) primitiveFileStdioHandles(void);
EXPORT(sqInt) primitiveFileSync(void);
EXPORT(sqInt) primitiveFileTruncate(void);
EXPORT(sqInt) primitiveFileWrite(void);
EXPORT(sqInt) primitiveHasFileAccess(void);
EXPORT(sqInt) setInterpreter(struct VirtualMachine*anInterpreter);
EXPORT(sqInt) setMacFileTypeAndCreator(char *fileName, char *typeString, char *creatorString);
EXPORT(sqInt) shutdownModule(void);
/*** Variables ***/
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static void * (*arrayValueOf)(sqInt oop);
static sqInt (*booleanValueOf)(sqInt obj);
static sqInt (*byteSizeOf)(sqInt oop);
static sqInt (*characterObjectOf)(sqInt characterCode);
static sqInt (*characterTable)(void);
static sqInt (*classArray)(void);
static sqInt (*classByteArray)(void);
static sqInt (*classString)(void);
static sqInt (*failed)(void);
static sqInt (*falseObject)(void);
static sqInt (*fetchPointerofObject)(sqInt index, sqInt oop);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*fullGC)(void);
static sqInt (*instantiateClassindexableSize)(sqInt classPointer, sqInt size);
static sqInt (*integerObjectOf)(sqInt value);
static sqInt (*integerValueOf)(sqInt oop);
static void * (*ioLoadFunctionFrom)(char *functionName, char *moduleName);
static sqInt (*isKindOf)(sqInt oop, char *aString);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*isIntegerObject)(sqInt objectPointer);
static sqInt (*isWords)(sqInt oop);
static sqInt (*isWordsOrBytes)(sqInt oop);
static sqInt (*nilObject)(void);
static sqInt (*pop)(sqInt nItems);
static sqInt (*popthenPush)(sqInt nItems, sqInt oop);
static sqInt (*popRemappableOop)(void);
static sqInt (*positive32BitIntegerFor)(unsigned int integerValue);
static usqInt (*positive32BitValueOf)(sqInt oop);
static sqInt (*positive64BitIntegerFor)(usqLong integerValue);
static usqLong (*positive64BitValueOf)(sqInt oop);
static usqIntptr_t (*positiveMachineIntegerValueOf)(sqInt oop);
static sqInt (*primitiveFail)(void);
static sqInt (*primitiveFailFor)(sqInt reasonCode);
static sqInt (*primitiveFailureCode)(void);
static sqInt (*pushBool)(sqInt trueOrFalse);
static sqInt (*pushRemappableOop)(sqInt oop);
static sqInt (*slotSizeOf)(sqInt oop);
static sqInt (*stSizeOf)(sqInt oop);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*storePointerofObjectwithValue)(sqInt index, sqInt oop, sqInt valuePointer);
static void (*tenuringIncrementalGC)(void);
static sqInt (*topRemappableOop)(void);
static sqInt (*trueObject)(void);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern void * arrayValueOf(sqInt oop);
extern sqInt booleanValueOf(sqInt obj);
extern sqInt byteSizeOf(sqInt oop);
#if VM_PROXY_MAJOR > 1 || (VM_PROXY_MAJOR == 1 && VM_PROXY_MINOR >= 13)
extern sqInt characterObjectOf(sqInt characterCode);
#else
# define characterObjectOf(characterCode) 0
#endif
extern sqInt characterTable(void);
extern sqInt classArray(void);
extern sqInt classByteArray(void);
extern sqInt classString(void);
extern sqInt failed(void);
extern sqInt falseObject(void);
extern sqInt fetchPointerofObject(sqInt index, sqInt oop);
extern void * firstIndexableField(sqInt oop);
extern sqInt fullGC(void);
extern sqInt instantiateClassindexableSize(sqInt classPointer, sqInt size);
extern sqInt integerObjectOf(sqInt value);
extern sqInt integerValueOf(sqInt oop);
extern void * ioLoadFunctionFrom(char *functionName, char *moduleName);
extern sqInt isKindOf(sqInt oop, char *aString);
extern sqInt isBytes(sqInt oop);
#if !defined(isIntegerObject)
extern sqInt isIntegerObject(sqInt objectPointer);
#endif
extern sqInt isWords(sqInt oop);
extern sqInt isWordsOrBytes(sqInt oop);
extern sqInt nilObject(void);
extern sqInt pop(sqInt nItems);
extern sqInt popthenPush(sqInt nItems, sqInt oop);
extern sqInt popRemappableOop(void);
extern sqInt positive32BitIntegerFor(unsigned int integerValue);
extern usqInt positive32BitValueOf(sqInt oop);
extern sqInt positive64BitIntegerFor(usqLong integerValue);
extern usqLong positive64BitValueOf(sqInt oop);
extern usqIntptr_t positiveMachineIntegerValueOf(sqInt oop);
extern sqInt primitiveFail(void);
extern sqInt primitiveFailFor(sqInt reasonCode);
extern sqInt primitiveFailureCode(void);
extern sqInt pushBool(sqInt trueOrFalse);
extern sqInt pushRemappableOop(sqInt oop);
extern sqInt slotSizeOf(sqInt oop);
extern sqInt stSizeOf(sqInt oop);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern sqInt storePointerofObjectwithValue(sqInt index, sqInt oop, sqInt valuePointer);
extern void tenuringIncrementalGC(void);
extern sqInt topRemappableOop(void);
extern sqInt trueObject(void);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"FilePlugin * VMMaker.oscog-akg.2341 (i)"
#else
"FilePlugin * VMMaker.oscog-akg.2341 (e)"
#endif
;
static void * sCCPfn;
static void * sCDFfn;
static void * sCDPfn;
static void * sCGFTfn;
static void * sCLPfn;
static void * sCOFfn;
static void * sCRFfn;
static void * sCSFTfn;
static void * sDFAfn;
static void * sHFAfn;
/* FilePlugin>>#asciiDirectoryDelimiter */
static sqInt
asciiDirectoryDelimiter(void)
{
return dir_Delimitor();
}
/* Connect to the supplied file descriptor. Answer the file oop.
On POSIX platforms this translates to fdopen().
writeFlag must be compatible with the existing file access. */
/* FilePlugin>>#connectToFd:write: */
EXPORT(sqInt)
connectToFdwrite(int fd, sqInt writeFlag)
{
SQFile *file;
sqInt fileOop;
fileOop = instantiateClassindexableSize(classByteArray(), sizeof(SQFile));
/* begin fileValueOf: */
if (!((isBytes(fileOop))
&& ((byteSizeOf(fileOop)) == (sizeof(SQFile))))) {
primitiveFail();
file = null;
goto l1;
}
file = firstIndexableField(fileOop);
l1: /* end fileValueOf: */;
if (!(failed())) {
sqConnectToFileDescriptor(file, fd, writeFlag);
}
return fileOop;
}
/* Open the FILE* as file. Answer the file oop.
writeFlag must be compatible with the existing file access. */
/* FilePlugin>>#connectToFile:write: */
EXPORT(sqInt)
connectToFilewrite(void *cfile, sqInt writeFlag)
{
SQFile *file;
sqInt fileOop;
fileOop = instantiateClassindexableSize(classByteArray(), sizeof(SQFile));
/* begin fileValueOf: */
if (!((isBytes(fileOop))
&& ((byteSizeOf(fileOop)) == (sizeof(SQFile))))) {
primitiveFail();
file = null;
goto l1;
}
file = firstIndexableField(fileOop);
l1: /* end fileValueOf: */;
if (!(failed())) {
sqConnectToFile(file, cfile, writeFlag);
}
return fileOop;
}
/* Open the named file, possibly checking security. Answer the file oop. */
/* FilePlugin>>#fileOpenName:size:write:secure: */
EXPORT(sqInt)
fileOpenNamesizewritesecure(char *nameIndex, sqInt nameSize, sqInt writeFlag, sqInt secureFlag)
{
SQFile *file;
sqInt fileOop;
sqInt okToOpen;
fileOop = instantiateClassindexableSize(classByteArray(), sizeof(SQFile));
/* begin fileValueOf: */
if (!((isBytes(fileOop))
&& ((byteSizeOf(fileOop)) == (sizeof(SQFile))))) {
primitiveFail();
file = null;
goto l1;
}
file = firstIndexableField(fileOop);
l1: /* end fileValueOf: */;
if (!(failed())) {
if (secureFlag) {
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
if (sCOFfn != 0) {
okToOpen = ((sqInt (*) (char *, sqInt, sqInt)) sCOFfn)(nameIndex, nameSize, writeFlag);
if (!okToOpen) {
primitiveFail();
}
}
}
}
if (!(failed())) {
sqFileOpen(file, nameIndex, nameSize, writeFlag);
}
return fileOop;
}
/* Open the new named file, possibly checking security. Answer the file oop. */
/* FilePlugin>>#fileOpenNewName:size:secure: */
EXPORT(sqInt)
fileOpenNewNamesizesecure(char *nameIndex, sqInt nameSize, sqInt secureFlag)
{
sqInt exists;
SQFile *file;
sqInt fileOop;
sqInt okToOpen;
fileOop = instantiateClassindexableSize(classByteArray(), sizeof(SQFile));
/* begin fileValueOf: */
if (!((isBytes(fileOop))
&& ((byteSizeOf(fileOop)) == (sizeof(SQFile))))) {
primitiveFail();
file = null;
goto l1;
}
file = firstIndexableField(fileOop);
l1: /* end fileValueOf: */;
if (!(failed())) {
if (secureFlag) {
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
if (sCOFfn != 0) {
okToOpen = ((sqInt (*) (char *, sqInt, sqInt)) sCOFfn)(nameIndex, nameSize, true);
if (!okToOpen) {
primitiveFail();
}
}
}
}
if (!(failed())) {
exists = 0;
sqFileOpenNew(file, nameIndex, nameSize, &exists);
if ((failed())
&& (exists)) {
primitiveFailFor(PrimErrInappropriate);
}
}
return fileOop;
}
/* Return the size of a Smalltalk file record in bytes. */
/* FilePlugin>>#fileRecordSize */
usqIntptr_t
fileRecordSize(void)
{
return sizeof(SQFile);
}
/* Return a pointer to the first byte of of the file record within the given
Smalltalk object, or nil if objectPointer is not a file record.
*/
/* FilePlugin>>#fileValueOf: */
SQFile *
fileValueOf(sqInt objectPointer)
{
if (!((isBytes(objectPointer))
&& ((byteSizeOf(objectPointer)) == (sizeof(SQFile))))) {
primitiveFail();
return null;
}
return firstIndexableField(objectPointer);
}
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
return moduleName;
}
/* Exported entry point for the VM. Only used by AsynchFilePlugin and needs
to be reowrked now we have a VM global session Id capability
*/
/* FilePlugin>>#getThisSession */
EXPORT(sqInt)
getThisSession(void)
{
return sqFileThisSession();
}
/* FilePlugin>>#initialiseModule */
EXPORT(sqInt)
initialiseModule(void)
{
sCCPfn = ioLoadFunctionFrom("secCanCreatePathOfSize", "SecurityPlugin");
sCDPfn = ioLoadFunctionFrom("secCanDeletePathOfSize", "SecurityPlugin");
sCGFTfn = ioLoadFunctionFrom("secCanGetFileTypeOfSize", "SecurityPlugin");
sCLPfn = ioLoadFunctionFrom("secCanListPathOfSize", "SecurityPlugin");
sCSFTfn = ioLoadFunctionFrom("secCanSetFileTypeOfSize", "SecurityPlugin");
sDFAfn = ioLoadFunctionFrom("secDisableFileAccess", "SecurityPlugin");
sCDFfn = ioLoadFunctionFrom("secCanDeleteFileOfSize", "SecurityPlugin");
sCOFfn = ioLoadFunctionFrom("secCanOpenFileOfSizeWritable", "SecurityPlugin");
sCRFfn = ioLoadFunctionFrom("secCanRenameFileOfSize", "SecurityPlugin");
sHFAfn = ioLoadFunctionFrom("secHasFileAccess", "SecurityPlugin");
return sqFileInit();
}
/* FilePlugin>>#makeDirEntryName:size:createDate:modDate:isDir:fileSize: */
static sqInt
makeDirEntryNamesizecreateDatemodDateisDirfileSize(char *entryName, sqInt entryNameSize, sqInt createDate, sqInt modifiedDate, sqInt dirFlag, squeakFileOffsetType fileSize)
{
sqInt createDateOop;
sqInt fileSizeOop;
sqInt i;
sqInt modDateOop;
sqInt nameString;
sqInt results;
char *stringPtr;
/* allocate storage for results, remapping newly allocated
oops if required in case GC happens during allocation */
results = instantiateClassindexableSize(classArray(), 5);
#if SPURVM
nameString = instantiateClassindexableSize(classString(), entryNameSize);
createDateOop = positive32BitIntegerFor(createDate);
modDateOop = positive32BitIntegerFor(modifiedDate);
fileSizeOop = positive64BitIntegerFor(fileSize);
#else /* SPURVM */
pushRemappableOop(results);
nameString = instantiateClassindexableSize(classString(), entryNameSize);
pushRemappableOop(nameString);
createDateOop = positive32BitIntegerFor(createDate);
pushRemappableOop(createDateOop);
modDateOop = positive32BitIntegerFor(modifiedDate);
pushRemappableOop(modDateOop);
fileSizeOop = positive64BitIntegerFor(fileSize);
modDateOop = popRemappableOop();
createDateOop = popRemappableOop();
nameString = popRemappableOop();
results = popRemappableOop()
#endif /* SPURVM */
;
stringPtr = firstIndexableField(nameString);
for (i = 0; i < entryNameSize; i += 1) {
stringPtr[i] = (entryName[i]);
}
storePointerofObjectwithValue(0, results, nameString);
storePointerofObjectwithValue(1, results, createDateOop);
storePointerofObjectwithValue(2, results, modDateOop);
storePointerofObjectwithValue(3, results, (dirFlag
? trueObject()
: falseObject()));
storePointerofObjectwithValue(4, results, fileSizeOop);
return results;
}
/* FilePlugin>>#makeDirEntryName:size:createDate:modDate:isDir:fileSize:posixPermissions:isSymlink: */
#if PharoVM
static sqInt
makeDirEntryNamesizecreateDatemodDateisDirfileSizeposixPermissionsisSymlink(char *entryName, sqInt entryNameSize, sqInt createDate, sqInt modifiedDate, sqInt dirFlag, squeakFileOffsetType fileSize, sqInt posixPermissions, sqInt symlinkFlag)
{
sqInt createDateOop;
sqInt fileSizeOop;
sqInt i;
sqInt modDateOop;
sqInt nameString;
sqInt posixPermissionsOop;
sqInt results;
char *stringPtr;
/* allocate storage for results, remapping newly allocated
oops if required in case GC happens during allocation */
results = instantiateClassindexableSize(classArray(), 7);
#if SPURVM
nameString = instantiateClassindexableSize(classString(), entryNameSize);
createDateOop = positive32BitIntegerFor(createDate);
modDateOop = positive32BitIntegerFor(modifiedDate);
fileSizeOop = positive64BitIntegerFor(fileSize);
posixPermissionsOop = positive32BitIntegerFor(posixPermissions);
#else /* SPURVM */
pushRemappableOop(results);
nameString = instantiateClassindexableSize(classString(), entryNameSize);
pushRemappableOop(nameString);
createDateOop = positive32BitIntegerFor(createDate);
pushRemappableOop(createDateOop);
modDateOop = positive32BitIntegerFor(modifiedDate);
pushRemappableOop(modDateOop);
fileSizeOop = positive64BitIntegerFor(fileSize);
pushRemappableOop(fileSizeOop);
posixPermissionsOop = positive32BitIntegerFor(posixPermissions);
fileSizeOop = popRemappableOop();
modDateOop = popRemappableOop();
createDateOop = popRemappableOop();
nameString = popRemappableOop();
results = popRemappableOop()
#endif /* SPURVM */
;
stringPtr = firstIndexableField(nameString);
for (i = 0; i < entryNameSize; i += 1) {
stringPtr[i] = (entryName[i]);
}
storePointerofObjectwithValue(0, results, nameString);
storePointerofObjectwithValue(1, results, createDateOop);
storePointerofObjectwithValue(2, results, modDateOop);
storePointerofObjectwithValue(3, results, (dirFlag
? trueObject()
: falseObject()));
storePointerofObjectwithValue(4, results, fileSizeOop);
storePointerofObjectwithValue(5, results, posixPermissionsOop);
storePointerofObjectwithValue(6, results, (symlinkFlag
? trueObject()
: falseObject()));
return results;
}
#endif /* PharoVM */
/* The module with the given name was just unloaded.
Make sure we have no dangling references. */
/* FilePlugin>>#moduleUnloaded: */
EXPORT(sqInt)
moduleUnloaded(char *aModuleName)
{
if ((strcmp(aModuleName, "SecurityPlugin")) == 0) {
/* The security plugin just shut down. How odd. */
sCCPfn = (sCDPfn = (sCGFTfn = (sCLPfn = (sCSFTfn = (sDFAfn = (sCDFfn = (sCOFfn = (sCRFfn = (sHFAfn = 0)))))))));
}
return 0;
}
/* Answer the machine address contained in anExternalAddressOop. */
/* FilePlugin>>#pointerFrom: */
static void *
pointerFrom(sqInt pointerByteArray)
{
union {void *address; unsigned char bytes[sizeof(void *)];} addressUnion;
sqInt idx;
unsigned char *ptr;
if (!((isKindOf(pointerByteArray, "ByteArray"))
&& ((stSizeOf(pointerByteArray)) == (sizeof(void *))))) {
return null;
}
ptr = arrayValueOf(pointerByteArray);
idx = 0;
while (idx < (sizeof(void *))) {
addressUnion.bytes[idx] = ptr[idx];
idx += 1;
}
return addressUnion.address;
}
/* Connect to the file with the supplied FILE* and writeFlag.
FILE* must be supplied in a byte object (ByteArray) with the platform
address size.
writeFlag must be a boolean and compatible with the existing file access.
*/
/* FilePlugin>>#primitiveConnectToFile */
EXPORT(sqInt)
primitiveConnectToFile(void)
{
void* cfile;
sqInt cfileOop;
sqInt filePointer;
sqInt writeFlag;
writeFlag = booleanValueOf(stackValue(0));
cfileOop = stackValue(1);
cfile = pointerFrom(cfileOop);
if (!(cfile)) {
return primitiveFailFor(PrimErrBadArgument);
}
filePointer = connectToFilewrite(cfile, writeFlag);
if (!(failed())) {
return popthenPush(3, filePointer);
}
return primitiveFail();
}
/* Connect to the existing file identified by fileDescriptor.
fileDescriptor must be an integer.
writeFlag is aboolean indicating whether to open in read or write mode and
must be compatible with the existing file access.
*/
/* FilePlugin>>#primitiveConnectToFileDescriptor */
EXPORT(sqInt)
primitiveConnectToFileDescriptor(void)
{
int fd;
sqInt fdPointer;
sqInt filePointer;
sqInt writeFlag;
writeFlag = booleanValueOf(stackValue(0));
fdPointer = stackValue(1);
if (!(isIntegerObject(fdPointer))) {
return primitiveFailFor(PrimErrBadArgument);
}
fd = integerValueOf(fdPointer);
filePointer = connectToFdwrite(fd, writeFlag);
if (!(failed())) {
return popthenPush(3, filePointer);
}
return primitiveFail();
}
/* FilePlugin>>#primitiveDirectoryCreate */
EXPORT(sqInt)
primitiveDirectoryCreate(void)
{
sqInt dirName;
char *dirNameIndex;
sqInt dirNameSize;
sqInt okToCreate;
dirName = stackValue(0);
if (!(isBytes(dirName))) {
return primitiveFail();
}
dirNameIndex = firstIndexableField(dirName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
dirNameSize = byteSizeOf(dirName);
if (sCCPfn != 0) {
okToCreate = ((sqInt (*)(char *, sqInt))sCCPfn)(dirNameIndex, dirNameSize);
if (!okToCreate) {
return primitiveFail();
}
}
if (!(dir_Create(dirNameIndex, dirNameSize))) {
return primitiveFail();
}
pop(1);
return 0;
}
/* FilePlugin>>#primitiveDirectoryDelete */
EXPORT(sqInt)
primitiveDirectoryDelete(void)
{
sqInt dirName;
char *dirNameIndex;
sqInt dirNameSize;
sqInt okToDelete;
dirName = stackValue(0);
if (!(isBytes(dirName))) {
return primitiveFail();
}
dirNameIndex = firstIndexableField(dirName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
dirNameSize = byteSizeOf(dirName);
if (sCDPfn != 0) {
okToDelete = ((sqInt (*)(char *, sqInt))sCDPfn)(dirNameIndex, dirNameSize);
if (!okToDelete) {
return primitiveFail();
}
}
if (!(dir_Delete(dirNameIndex, dirNameSize))) {
return primitiveFail();
}
pop(1);
return 0;
}
/* FilePlugin>>#primitiveDirectoryDelimitor */
EXPORT(sqInt)
primitiveDirectoryDelimitor(void)
{
sqInt ascii;
ascii = asciiDirectoryDelimiter();
if ((interpreterProxy->minorVersion()) >= 13) {
popthenPush(1, characterObjectOf(ascii));
}
else {
if ((ascii >= 0)
&& (ascii <= 0xFF)) {
popthenPush(1, fetchPointerofObject(ascii, characterTable()));
}
else {
primitiveFail();
}
}
return 0;
}
/* Two arguments - directory path, and simple file name;
returns an array (see primitiveDirectoryLookup) describing the file or
directory, or nil if it does not exist.
Primitive fails if the outer path does not identify a readable directory.
(This is a lookup-by-name variant of primitiveDirectoryLookup.) */
/* FilePlugin>>#primitiveDirectoryEntry */
EXPORT(sqInt)
primitiveDirectoryEntry(void)
{
sqInt createDate;
sqInt dirFlag;
char entryName[256];
sqInt entryNameSize;
squeakFileOffsetType fileSize;
sqInt modifiedDate;
sqInt okToList;
sqInt pathName;
char *pathNameIndex;
sqInt pathNameSize;
sqInt posixPermissions;
char *reqNameIndex;
sqInt reqNameSize;
sqInt requestedName;
sqInt status;
sqInt symlinkFlag;
createDate = 0;
dirFlag = 0;
entryNameSize = 0;
modifiedDate = 0;
posixPermissions = 0;
symlinkFlag = 0;
requestedName = stackValue(0);
pathName = stackValue(1);
if (!(isBytes(pathName))) {
return primitiveFail();
}
pathNameIndex = firstIndexableField(pathName);
pathNameSize = byteSizeOf(pathName);
reqNameIndex = firstIndexableField(requestedName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
reqNameSize = byteSizeOf(requestedName);
if (sCLPfn != 0) {
okToList = ((sqInt (*)(char *, sqInt))sCLPfn)(pathNameIndex, pathNameSize);
}
else {
okToList = 1;
}
if (okToList) {
# if PharoVM
status = dir_EntryLookup(pathNameIndex, pathNameSize, reqNameIndex, reqNameSize,
entryName, &entryNameSize, &createDate,
&modifiedDate, &dirFlag, &fileSize,
&posixPermissions, &symlinkFlag);
# else /* PharoVM */
status = dir_EntryLookup(pathNameIndex, pathNameSize, reqNameIndex, reqNameSize,
entryName, &entryNameSize, &createDate,
&modifiedDate, &dirFlag, &fileSize);
# endif /* PharoVM */
}
else {
status = DirNoMoreEntries;
}
if (failed()) {
return null;
}
if (status == DirNoMoreEntries) {
/* no entry; return nil */
popthenPush(3, nilObject());
return null;
}
if (status == DirBadPath) {
return primitiveFail();
}
# if PharoVM
popthenPush(3, makeDirEntryNamesizecreateDatemodDateisDirfileSizeposixPermissionsisSymlink(entryName, entryNameSize, createDate, modifiedDate, dirFlag, fileSize, posixPermissions, symlinkFlag));
# else /* PharoVM */
popthenPush(3, makeDirEntryNamesizecreateDatemodDateisDirfileSize(entryName, entryNameSize, createDate, modifiedDate, dirFlag, fileSize));
# endif /* PharoVM */
return 0;
}
/* FilePlugin>>#primitiveDirectoryGetMacTypeAndCreator */
EXPORT(sqInt)
primitiveDirectoryGetMacTypeAndCreator(void)
{
sqInt creatorString;
char *creatorStringIndex;
sqInt fileName;
char *fileNameIndex;
sqInt fileNameSize;
sqInt okToGet;
sqInt typeString;
char *typeStringIndex;
creatorString = stackValue(0);
typeString = stackValue(1);
fileName = stackValue(2);
if (!((isBytes(creatorString))
&& ((byteSizeOf(creatorString)) == 4))) {
return primitiveFail();
}
if (!((isBytes(typeString))
&& ((byteSizeOf(typeString)) == 4))) {
return primitiveFail();
}
if (!(isBytes(fileName))) {
return primitiveFail();
}
creatorStringIndex = firstIndexableField(creatorString);
typeStringIndex = firstIndexableField(typeString);
fileNameIndex = firstIndexableField(fileName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
fileNameSize = byteSizeOf(fileName);
if (sCGFTfn != 0) {
okToGet = ((sqInt (*)(char *, sqInt))sCGFTfn)(fileNameIndex, fileNameSize);
if (!okToGet) {
return primitiveFail();
}
}
if (!(dir_GetMacFileTypeAndCreator(fileNameIndex, fileNameSize, typeStringIndex, creatorStringIndex))) {
return primitiveFail();
}
pop(3);
return 0;
}
/* FilePlugin>>#primitiveDirectoryLookup */
EXPORT(sqInt)
primitiveDirectoryLookup(void)
{
sqInt createDate;
sqInt dirFlag;
char entryName[256];
sqInt entryNameSize;
squeakFileOffsetType fileSize;
sqInt index;
sqInt modifiedDate;
sqInt okToList;
sqInt pathName;
char *pathNameIndex;
sqInt pathNameSize;
sqInt posixPermissions;
sqInt status;
sqInt symlinkFlag;
createDate = 0;
dirFlag = 0;
entryNameSize = 0;
modifiedDate = 0;
posixPermissions = 0;
symlinkFlag = 0;
index = stackIntegerValue(0);
pathName = stackValue(1);
if (!(isBytes(pathName))) {
return primitiveFail();
}
pathNameIndex = firstIndexableField(pathName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
pathNameSize = byteSizeOf(pathName);
if (sCLPfn != 0) {
okToList = ((sqInt (*)(char *, sqInt))sCLPfn)(pathNameIndex, pathNameSize);
}
else {
okToList = 1;
}
if (okToList) {
# if PharoVM
status = dir_Lookup(pathNameIndex, pathNameSize, index,
entryName, &entryNameSize, &createDate,
&modifiedDate, &dirFlag, &fileSize,
&posixPermissions, &symlinkFlag);
# else /* PharoVM */
status = dir_Lookup(pathNameIndex, pathNameSize, index,
entryName, &entryNameSize, &createDate,
&modifiedDate, &dirFlag, &fileSize);
# endif /* PharoVM */
}
else {
status = DirNoMoreEntries;
}
if (failed()) {
return null;
}
if (status == DirNoMoreEntries) {
/* no more entries; return nil */
popthenPush(3, nilObject());
return null;
}
if (status == DirBadPath) {
return primitiveFail();
}
# if PharoVM
popthenPush(3, makeDirEntryNamesizecreateDatemodDateisDirfileSizeposixPermissionsisSymlink(entryName, entryNameSize, createDate, modifiedDate, dirFlag, fileSize, posixPermissions, symlinkFlag));
# else /* PharoVM */
popthenPush(3, makeDirEntryNamesizecreateDatemodDateisDirfileSize(entryName, entryNameSize, createDate, modifiedDate, dirFlag, fileSize));
# endif /* PharoVM */
return 0;
}
/* FilePlugin>>#primitiveDirectorySetMacTypeAndCreator */
EXPORT(sqInt)
primitiveDirectorySetMacTypeAndCreator(void)
{
sqInt creatorString;
char *creatorStringIndex;
sqInt fileName;
char *fileNameIndex;
sqInt fileNameSize;
sqInt okToSet;
sqInt typeString;
char *typeStringIndex;
creatorString = stackValue(0);
typeString = stackValue(1);
fileName = stackValue(2);
if (!((isBytes(creatorString))
&& ((isBytes(typeString))
&& ((isBytes(fileName))
&& (((byteSizeOf(creatorString)) == 4)
&& ((byteSizeOf(typeString)) == 4)))))) {
return primitiveFail();
}
creatorStringIndex = firstIndexableField(creatorString);
typeStringIndex = firstIndexableField(typeString);
fileNameIndex = firstIndexableField(fileName);
/* If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok */
fileNameSize = byteSizeOf(fileName);
if (sCSFTfn != 0) {
okToSet = ((sqInt (*)(char *, sqInt))sCSFTfn)(fileNameIndex, fileNameSize);
if (!okToSet) {
return primitiveFail();
}
}
if (!(dir_SetMacFileTypeAndCreator(fileNameIndex, fileNameSize, typeStringIndex, creatorStringIndex))) {
return primitiveFail();
}
pop(3);
return 0;
}
/* If the security plugin can be loaded, use it to turn off file access