-
Notifications
You must be signed in to change notification settings - Fork 71
/
FilePlugin.class.st
892 lines (789 loc) · 33.2 KB
/
FilePlugin.class.st
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
"
Provide access to the host machine file system. Requires both the Cross platform support files from platforms - Cross - plugins - FilePlugin (or some suitable replacement) and the platform specific fils from platforms - {your platform} - plugins - FilePlugin.
"
Class {
#name : #FilePlugin,
#superclass : #InterpreterPlugin,
#classVars : [
'DirBadPath',
'DirEntryFound',
'DirNoMoreEntries'
],
#category : #'VMMaker-Plugins'
}
{ #category : #translation }
FilePlugin class >> declareCVarsIn: aCCodeGenerator [
aCCodeGenerator addHeaderFile: '"pharovm/imageAccess.h"'.
aCCodeGenerator addHeaderFile: '"FilePlugin.h"'
]
{ #category : #translation }
FilePlugin class >> hasHeaderFile [
"If there is a single intrinsic header file to be associated with the plugin, here is where you want to flag"
^true
]
{ #category : #'class initialization' }
FilePlugin class >> initialize [
"FilePlugin initialize"
DirEntryFound := 0.
DirNoMoreEntries := 1.
DirBadPath := 2.
]
{ #category : #translation }
FilePlugin class >> requiresCrossPlatformFiles [
"this plugin requires cross platform files in order to work"
^true
]
{ #category : #translation }
FilePlugin class >> requiresPlatformFiles [
"this plugin requires platform specific files in order to work"
^true
]
{ #category : #simulation }
FilePlugin class >> simulatorClass [
^FilePluginSimulator
]
{ #category : #private }
FilePlugin >> connectToFd: fd write: writeFlag [
"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."
| file fileOop |
<var: 'file' type: #'SQFile *'>
<var: 'fd' type: #int>
fileOop := interpreterProxy instantiateClass: interpreterProxy classByteArray indexableSize: self fileRecordSize.
file := self fileValueOf: fileOop.
interpreterProxy failed ifFalse:
[self sqConnect: file ToFile: fd Descriptor: writeFlag].
^fileOop
]
{ #category : #private }
FilePlugin >> connectToFile: cfile write: writeFlag [
"Open the FILE* as file. Answer the file oop.
writeFlag must be compatible with the existing file access."
| file fileOop |
<var: 'file' type: #'SQFile *'>
<var: 'cfile' type: #'void *'>
fileOop := interpreterProxy instantiateClass: interpreterProxy classByteArray indexableSize: self fileRecordSize.
file := self fileValueOf: fileOop.
interpreterProxy failed ifFalse:
[self sqConnect: file To: cfile File: writeFlag].
^fileOop
]
{ #category : #'directory primitives' }
FilePlugin >> dir_Delimitor [
<doNotGenerate>
^FileSystem disk delimiter asInteger
]
{ #category : #'file primitives' }
FilePlugin >> fileOpenName: nameIndex size: nameSize write: writeFlag [
"Open the named file. Answer the file oop."
| file fileOop |
<var: #file type: 'SQFile *'>
<var: 'nameIndex' type: 'char *'>
<export: true>
fileOop := interpreterProxy instantiateClass: interpreterProxy classByteArray indexableSize: self fileRecordSize.
file := self fileValueOf: fileOop.
interpreterProxy failed
ifFalse: [self cCode: 'sqFileOpen(file, nameIndex, nameSize, writeFlag)' inSmalltalk: [file]].
^ fileOop
]
{ #category : #'file primitives' }
FilePlugin >> fileOpenNewName: nameIndex size: nameSize [
"Open the new named file, possibly checking security. Answer the file oop."
| file fileOop |
<var: #file type: 'SQFile *'>
<var: 'nameIndex' type: 'char *'>
<export: true>
fileOop := interpreterProxy instantiateClass: interpreterProxy classByteArray indexableSize: self fileRecordSize.
file := self fileValueOf: fileOop.
interpreterProxy failed
ifFalse: [| exists |
exists := false.
self cCode: 'sqFileOpenNew(file, nameIndex, nameSize, &exists)' inSmalltalk: [file].
(interpreterProxy failed
and: [exists])
ifTrue: [interpreterProxy primitiveFailFor: PrimErrInappropriate]].
^ fileOop
]
{ #category : #'file primitives' }
FilePlugin >> fileRecordSize [
"Return the size of a Smalltalk file record in bytes."
<static: false>
^self sizeof: #SQFile
]
{ #category : #'file primitives' }
FilePlugin >> fileValueOf: objectPointer [
"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."
<returnTypeC: #'SQFile *'>
<static: false>
(((interpreterProxy isBytes: objectPointer) and:
[(interpreterProxy byteSizeOf: objectPointer) = self fileRecordSize]))
ifFalse:[interpreterProxy primitiveFail. ^nil].
^interpreterProxy firstIndexableField: objectPointer
]
{ #category : #'initialize-release' }
FilePlugin >> initialiseModule [
<export: true>
^self sqFileInit
]
{ #category : #'directory primitives' }
FilePlugin >> makeDirEntryName: entryName size: entryNameSize
createDate: createDate modDate: modifiedDate
isDir: dirFlag fileSize: fileSize [
<var: 'entryName' type: #'char *'>
<var: 'fileSize' type: #squeakFileOffsetType>
| modDateOop createDateOop nameString results stringPtr fileSizeOop |
<var: 'stringPtr' type: #'char *'>
"allocate storage for results, remapping newly allocated
oops if required in case GC happens during allocation"
results := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 5.
self remapOop: results in:
[nameString := interpreterProxy instantiateClass: interpreterProxy classString indexableSize: entryNameSize.
self remapOop: nameString in:
[createDateOop := interpreterProxy positive32BitIntegerFor: createDate.
self remapOop: createDateOop in:
[modDateOop := interpreterProxy positive32BitIntegerFor: modifiedDate.
self remapOop: modDateOop in:
[fileSizeOop := interpreterProxy positive64BitIntegerFor: fileSize]]]].
"copy name into Smalltalk string"
stringPtr := interpreterProxy firstIndexableField: nameString.
0 to: entryNameSize - 1 do:
[ :i |
self cCode: [stringPtr at: i put: (entryName at: i)]
inSmalltalk: [interpreterProxy storeByte: i ofObject: nameString withValue: (entryName at: i+1) asciiValue]].
interpreterProxy
storePointer: 0 ofObject: results withValue: nameString;
storePointer: 1 ofObject: results withValue: createDateOop;
storePointer: 2 ofObject: results withValue: modDateOop;
storePointer: 3 ofObject: results withValue: (dirFlag
ifTrue: [interpreterProxy trueObject]
ifFalse: [interpreterProxy falseObject]);
storePointer: 4 ofObject: results withValue: fileSizeOop.
^results
]
{ #category : #'directory primitives' }
FilePlugin >> makeDirEntryName: entryName size: entryNameSize createDate: createDate modDate: modifiedDate isDir: dirFlag fileSize: fileSize posixPermissions: posixPermissions isSymlink: symlinkFlag [
<var: 'entryName' type: #'char *'>
<var: 'fileSize' type: #squeakFileOffsetType>
<option: #PharoVM>
| modDateOop createDateOop nameString results stringPtr posixPermissionsOop fileSizeOop |
<var: 'stringPtr' type: #'char *'>
"allocate storage for results, remapping newly allocated
oops if required in case GC happens during allocation"
results := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 7.
self remapOop: results in:
[nameString := interpreterProxy instantiateClass: interpreterProxy classString indexableSize: entryNameSize.
self remapOop: nameString in:
[createDateOop := interpreterProxy positive32BitIntegerFor: createDate.
self remapOop: createDateOop in:
[modDateOop := interpreterProxy positive32BitIntegerFor: modifiedDate.
self remapOop: modDateOop in:
[fileSizeOop := interpreterProxy positive64BitIntegerFor: fileSize.
self remapOop: fileSizeOop in:
[posixPermissionsOop := interpreterProxy positive32BitIntegerFor: posixPermissions]]]]].
"copy name into Smalltalk string"
stringPtr := interpreterProxy firstIndexableField: nameString.
0 to: entryNameSize - 1 do:
[ :i |
self cCode: [stringPtr at: i put: (entryName at: i)]
inSmalltalk: [interpreterProxy storeByte: i ofObject: nameString withValue: (entryName at: i+1) asciiValue]].
interpreterProxy
storePointer: 0 ofObject: results withValue: nameString;
storePointer: 1 ofObject: results withValue: createDateOop;
storePointer: 2 ofObject: results withValue: modDateOop;
storePointer: 3 ofObject: results withValue: (dirFlag
ifTrue: [interpreterProxy trueObject]
ifFalse: [interpreterProxy falseObject]);
storePointer: 4 ofObject: results withValue: fileSizeOop;
storePointer: 5 ofObject: results withValue: posixPermissionsOop;
storePointer: 6 ofObject: results withValue: (symlinkFlag
ifTrue: [interpreterProxy trueObject]
ifFalse: [interpreterProxy falseObject]).
^results
]
{ #category : #private }
FilePlugin >> pointerFrom: pointerByteArray [
"Answer the machine address contained in anExternalAddressOop."
| ptr addressUnion idx |
<returnTypeC: #'void *'>
<var: 'ptr' type: #'unsigned char *'>
<var: 'addressUnion' type: #'union {void *address; unsigned char bytes[sizeof(void *)];}'>
((interpreterProxy is: pointerByteArray KindOf: 'ByteArray') and:
[(interpreterProxy stSizeOf: pointerByteArray) = self sizeOfPointer])
ifFalse: [^interpreterProxy primitiveFailFor: PrimErrBadArgument].
ptr := interpreterProxy arrayValueOf: pointerByteArray.
interpreterProxy failed ifTrue: [^nil].
idx := 0.
[idx < self sizeOfPointer] whileTrue:
[self cCode: 'addressUnion.bytes[idx] = ptr[idx]'.
idx := idx + 1].
^ self cCode: 'addressUnion.address' inSmalltalk: [addressUnion]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveConnectToFile [
"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."
| writeFlag cfileOop cfile filePointer |
<var: 'cfile' type: #'void *'>
<export: true>
writeFlag := interpreterProxy booleanValueOf: (interpreterProxy stackValue: 0).
cfileOop := interpreterProxy stackValue: 1.
cfile := self pointerFrom: cfileOop.
interpreterProxy failed ifTrue: [
"Ensure that the appropriate failure code has been set"
^interpreterProxy primitiveFailFor: PrimErrBadArgument].
filePointer := self connectToFile: cfile write: writeFlag.
interpreterProxy failed ifFalse:
[interpreterProxy methodReturnValue: filePointer]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveConnectToFileDescriptor [
"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."
| writeFlag fdPointer fd filePointer |
<var: 'fd' type: #int>
<export: true>
writeFlag := interpreterProxy booleanValueOf: (interpreterProxy stackValue: 0).
fdPointer := interpreterProxy stackValue: 1.
(interpreterProxy isIntegerObject: fdPointer)
ifFalse: [^ interpreterProxy primitiveFailFor: PrimErrBadArgument].
fd := interpreterProxy integerValueOf: fdPointer.
interpreterProxy failed ifTrue: [
"Ensure that the appropriate failure code has been set"
^interpreterProxy primitiveFailFor: PrimErrBadArgument].
filePointer := self connectToFd: fd write: writeFlag.
interpreterProxy failed ifFalse:
[interpreterProxy methodReturnValue: filePointer]
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryCreate [
| dirName dirNameIndex dirNameSize okToCreate |
<var: #dirNameIndex type: #'char *'>
<export: true>
dirName := interpreterProxy stackValue: 0.
(interpreterProxy isBytes: dirName) ifFalse:
[^interpreterProxy primitiveFail].
dirNameIndex := interpreterProxy firstIndexableField: dirName.
dirNameSize := interpreterProxy byteSizeOf: dirName.
(self dir_Create: dirNameIndex _: dirNameSize) ifFalse:
[^interpreterProxy primitiveFail].
interpreterProxy pop: 1
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryDelete [
| dirName dirNameIndex dirNameSize |
<var: #dirNameIndex type: 'char *'>
<export: true>
dirName := interpreterProxy stackValue: 0.
(interpreterProxy isBytes: dirName)
ifFalse: [^interpreterProxy primitiveFail].
dirNameIndex := interpreterProxy firstIndexableField: dirName.
dirNameSize := interpreterProxy byteSizeOf: dirName.
(self
cCode: 'dir_Delete(dirNameIndex, dirNameSize)'
inSmalltalk: [false])
ifFalse: [^interpreterProxy primitiveFail].
interpreterProxy pop: 1
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryDelimitor [
<export: true>
interpreterProxy minorVersion >= 13
ifTrue:
[ interpreterProxy
pop: 1
thenPush: (interpreterProxy characterObjectOf: self dir_Delimitor)]
ifFalse:
[ interpreterProxy primitiveFail]
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryEntry [
"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.)"
| requestedName pathName pathNameIndex pathNameSize status entryName entryNameSize createDate modifiedDate dirFlag posixPermissions symlinkFlag fileSize reqNameIndex reqNameSize |
<var: 'entryName' declareC: 'char entryName[256]'>
<var: 'pathNameIndex' type: 'char *'>
<var: 'reqNameIndex' type: 'char *'>
<var: 'fileSize' type: 'squeakFileOffsetType'>
<export: true>
requestedName := interpreterProxy stackValue: 0.
pathName := interpreterProxy stackValue: 1.
(interpreterProxy isBytes: pathName) ifFalse:
[^interpreterProxy primitiveFail].
"Outbound string parameters"
pathNameIndex := interpreterProxy firstIndexableField: pathName.
pathNameSize := interpreterProxy byteSizeOf: pathName.
reqNameIndex := interpreterProxy firstIndexableField: requestedName.
reqNameSize := interpreterProxy byteSizeOf: requestedName.
self cCode: '' inSmalltalk:
[entryName := ByteString new: 256.
entryNameSize := createDate := modifiedDate := dirFlag := fileSize := posixPermissions := symlinkFlag := nil].
status := self dir_EntryLookup: pathNameIndex _: pathNameSize
_: reqNameIndex _: reqNameSize
_: entryName _: (self addressOf: entryNameSize put: [:v| entryNameSize := v])
_: (self addressOf: createDate put: [:v| createDate := v])
_: (self addressOf: modifiedDate put: [:v| modifiedDate := v])
_: (self addressOf: dirFlag put: [:v| dirFlag := v])
_: (self addressOf: fileSize put: [:v| fileSize := v])
_: (self addressOf: posixPermissions put: [:v| posixPermissions := v])
_: (self addressOf: symlinkFlag put: [:v| symlinkFlag := v]).
interpreterProxy failed ifTrue:
[^nil].
status = DirNoMoreEntries ifTrue: "no entry; return nil"
[interpreterProxy "pop pathName, index, rcvr"
pop: 3 thenPush: interpreterProxy nilObject.
^nil].
status = DirBadPath ifTrue:
[^interpreterProxy primitiveFail]."bad path"
interpreterProxy
pop: 3 "pop pathName, index, rcvr"
thenPush:
(self
makeDirEntryName: entryName
size: entryNameSize
createDate: createDate
modDate: modifiedDate
isDir: dirFlag
fileSize: fileSize
posixPermissions: posixPermissions
isSymlink: symlinkFlag)
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryGetMacTypeAndCreator [
| creatorString typeString fileName creatorStringIndex typeStringIndex fileNameIndex fileNameSize |
<var: 'creatorStringIndex' type: 'char *'>
<var: 'typeStringIndex' type: 'char *'>
<var: 'fileNameIndex' type: 'char *'>
<export: true>
creatorString := interpreterProxy stackValue: 0.
typeString := interpreterProxy stackValue: 1.
fileName := interpreterProxy stackValue: 2.
((interpreterProxy isBytes: creatorString)
and: [(interpreterProxy byteSizeOf: creatorString) = 4])
ifFalse: [^interpreterProxy primitiveFail].
((interpreterProxy isBytes: typeString)
and: [(interpreterProxy byteSizeOf: typeString) = 4])
ifFalse: [^interpreterProxy primitiveFail].
(interpreterProxy isBytes: fileName)
ifFalse: [^interpreterProxy primitiveFail].
creatorStringIndex := interpreterProxy firstIndexableField: creatorString.
typeStringIndex := interpreterProxy firstIndexableField: typeString.
fileNameIndex := interpreterProxy firstIndexableField: fileName.
fileNameSize := interpreterProxy byteSizeOf: fileName.
(self
cCode: 'dir_GetMacFileTypeAndCreator(fileNameIndex, fileNameSize, typeStringIndex, creatorStringIndex)'
inSmalltalk: [true])
ifFalse: [^interpreterProxy primitiveFail].
interpreterProxy pop: 3
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectoryLookup [
| index pathName pathNameIndex pathNameSize status entryName entryNameSize createDate modifiedDate dirFlag symlinkFlag posixPermissions fileSize |
<var: 'entryName' declareC: 'char entryName[256]'>
<var: 'pathNameIndex' type: 'char *'>
<var: 'fileSize' type: 'squeakFileOffsetType'>
<export: true>
index := interpreterProxy stackIntegerValue: 0.
pathName := interpreterProxy stackValue: 1.
(interpreterProxy isBytes: pathName)
ifFalse: [^interpreterProxy primitiveFail].
pathNameIndex := interpreterProxy firstIndexableField: pathName.
pathNameSize := interpreterProxy byteSizeOf: pathName.
self cCode: '' inSmalltalk:
[entryName := ByteString new: 256.
entryNameSize := createDate := modifiedDate := dirFlag := fileSize := posixPermissions := symlinkFlag := nil].
"If the security plugin can be loaded, use it to check for permission.
If not, assume it's ok"
status := self dir_Lookup: pathNameIndex _: pathNameSize
_: index
_: entryName _: (self addressOf: entryNameSize put: [:v| entryNameSize := v])
_: (self addressOf: createDate put: [:v| createDate := v])
_: (self addressOf: modifiedDate put: [:v| modifiedDate := v])
_: (self addressOf: dirFlag put: [:v| dirFlag := v])
_: (self addressOf: fileSize put: [:v| fileSize := v])
_: (self addressOf: posixPermissions put: [:v| posixPermissions := v])
_: (self addressOf: symlinkFlag put: [:v| symlinkFlag := v]).
interpreterProxy failed ifTrue:
[^nil].
status = DirNoMoreEntries ifTrue: "no more entries; return nil"
[interpreterProxy "pop pathName, index, rcvr"
pop: 3 thenPush: interpreterProxy nilObject.
^nil].
status = DirBadPath ifTrue:
[^interpreterProxy primitiveFail]."bad path"
interpreterProxy
pop: 3 "pop pathName, index, rcvr"
thenPush:
(self
cppIf: PharoVM
ifTrue:
[self
makeDirEntryName: entryName
size: entryNameSize
createDate: createDate
modDate: modifiedDate
isDir: dirFlag
fileSize: fileSize
posixPermissions: posixPermissions
isSymlink: symlinkFlag]
ifFalse:
[self
makeDirEntryName: entryName
size: entryNameSize
createDate: createDate
modDate: modifiedDate
isDir: dirFlag
fileSize: fileSize])
]
{ #category : #'directory primitives' }
FilePlugin >> primitiveDirectorySetMacTypeAndCreator [
| creatorString typeString fileName creatorStringIndex typeStringIndex fileNameIndex fileNameSize |
<var: 'creatorStringIndex' type: 'char *'>
<var: 'typeStringIndex' type: 'char *'>
<var: 'fileNameIndex' type: 'char *'>
<export: true>
creatorString := interpreterProxy stackValue: 0.
typeString := interpreterProxy stackValue: 1.
fileName := interpreterProxy stackValue: 2.
((interpreterProxy isBytes: creatorString)
and: [(interpreterProxy isBytes: typeString)
and: [(interpreterProxy isBytes: fileName)
and: [(interpreterProxy byteSizeOf: creatorString) = 4
and: [(interpreterProxy byteSizeOf: typeString) = 4]]]]) ifFalse:
[^interpreterProxy primitiveFail].
creatorStringIndex := interpreterProxy firstIndexableField: creatorString.
typeStringIndex := interpreterProxy firstIndexableField: typeString.
fileNameIndex := interpreterProxy firstIndexableField: fileName.
fileNameSize := interpreterProxy byteSizeOf: fileName.
(self
cCode: 'dir_SetMacFileTypeAndCreator(fileNameIndex, fileNameSize, typeStringIndex, creatorStringIndex)'
inSmalltalk: [true]) ifFalse:
[^interpreterProxy primitiveFail].
interpreterProxy pop: 3
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileAtEnd [
| file atEnd |
<export: true>
<var: 'file' type: 'SQFile *'>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed
ifFalse: [atEnd := self sqFileAtEnd: file].
interpreterProxy failed
ifFalse: [interpreterProxy methodReturnBool: atEnd]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileClose [
| file |
<export: true>
<var: 'file' type: 'SQFile *'>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifFalse: [ self sqFileClose: file ].
interpreterProxy failed ifFalse: [ interpreterProxy pop: 1 "pop file; leave rcvr on stack" ].
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileDelete [
| namePointer nameIndex nameSize okToDelete |
<var: 'nameIndex' type: 'char *'>
<export: true>
namePointer := interpreterProxy stackValue: 0.
(interpreterProxy isBytes: namePointer)
ifFalse: [^ interpreterProxy primitiveFail].
nameIndex := interpreterProxy firstIndexableField: namePointer.
nameSize := interpreterProxy byteSizeOf: namePointer.
self
sqFileDeleteName: nameIndex
Size: nameSize.
interpreterProxy failed
ifFalse: [interpreterProxy pop: 1]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileDescriptorType [
| fileType fd fdPointer |
"Allow to test if the standard input/output files are from a console or not
Return values:
* -1 - Error
* 0 - no console (windows only)
* 1 - normal terminal (unix terminal / windows console)
* 2 - pipe
* 3 - file
* 4 - cygwin terminal (windows only)"
<var: 'fd' type: #int>
<export: true>
fdPointer := interpreterProxy stackValue: 0.
(interpreterProxy isIntegerObject: fdPointer)
ifFalse: [^ interpreterProxy primitiveFailFor: PrimErrBadArgument].
fd := interpreterProxy integerValueOf: fdPointer.
interpreterProxy failed ifTrue: [
"Ensure that the appropriate failure code has been set"
^interpreterProxy primitiveFailFor: PrimErrBadArgument].
fileType := self
cCode: 'sqFileDescriptorType(fd)'
inSmalltalk: [1].
interpreterProxy methodReturnInteger: fileType
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileFlush [
| file |
<var: 'file' type: 'SQFile *'>
<export: true>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifFalse:[self sqFileFlush: file].
interpreterProxy failed ifFalse: [interpreterProxy pop: 1].
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileGetPosition [
| file position |
<var: 'file' type: 'SQFile *'>
<var: 'position' type: 'squeakFileOffsetType'>
<export: true>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifFalse: [position := self sqFileGetPosition: file].
interpreterProxy failed ifFalse: [
interpreterProxy methodReturnValue: (interpreterProxy positive64BitIntegerFor: position)].
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileOpen [
| writeFlag namePointer filePointer nameIndex nameSize |
<var: 'nameIndex' type: 'char *'>
<export: true>
writeFlag := interpreterProxy
booleanValueOf: (interpreterProxy stackValue: 0).
namePointer := interpreterProxy stackValue: 1.
(interpreterProxy isBytes: namePointer)
ifFalse: [^ interpreterProxy primitiveFail].
nameIndex := interpreterProxy firstIndexableField: namePointer.
nameSize := interpreterProxy byteSizeOf: namePointer.
filePointer := self fileOpenName: nameIndex size: nameSize write: writeFlag.
interpreterProxy failed
ifFalse: [interpreterProxy methodReturnValue: filePointer]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileOpenNew [
| namePointer filePointer nameIndex nameSize |
<var: 'nameIndex' type: 'char *'>
<export: true>
namePointer := interpreterProxy stackValue: 0.
(interpreterProxy isBytes: namePointer)
ifFalse: [^ interpreterProxy primitiveFail].
nameIndex := interpreterProxy firstIndexableField: namePointer.
nameSize := interpreterProxy byteSizeOf: namePointer.
filePointer := self fileOpenNewName: nameIndex size: nameSize.
interpreterProxy failed
ifFalse: [
interpreterProxy methodReturnValue: filePointer]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileRead [
<export: true>
| count startIndex array file slotSize elementSize bytesRead |
count := interpreterProxy positiveMachineIntegerValueOf:
(interpreterProxy stackValue: 0).
startIndex := interpreterProxy positiveMachineIntegerValueOf:
(interpreterProxy stackValue: 1).
array := interpreterProxy stackValue: 2.
file := self fileValueOf: (interpreterProxy stackValue: 3).
(interpreterProxy failed or: [
(interpreterProxy isWordsOrBytes: array) not ])
ifTrue: [ interpreterProxy primitiveFailFor: PrimErrBadArgument ]
ifFalse: [
slotSize := interpreterProxy slotSizeOf: array.
(startIndex >= 1 and: [ startIndex + count - 1 <= slotSize ])
ifTrue: [
elementSize := slotSize = 0
ifTrue: [ 1 ]
ifFalse: [
(interpreterProxy byteSizeOf: array) // slotSize ].
bytesRead := self
sqFile: file
Read: count * elementSize
Into: (interpreterProxy
cCoerce:
(interpreterProxy firstIndexableField: array)
to: #'char *')
At: startIndex - 1 * elementSize.
interpreterProxy failed ifFalse: [
interpreterProxy methodReturnValue:
(interpreterProxy integerObjectOf: bytesRead // elementSize) ] ]
ifFalse: [ interpreterProxy primitiveFailFor: PrimErrBadIndex ] ] "buffer can be any indexable words or bytes object except CompiledMethod"
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileRename [
| oldNamePointer newNamePointer oldNameIndex oldNameSize newNameIndex newNameSize okToRename |
<var: 'oldNameIndex' type: 'char *'>
<var: 'newNameIndex' type: 'char *'>
<export: true>
newNamePointer := interpreterProxy stackValue: 0.
oldNamePointer := interpreterProxy stackValue: 1.
((interpreterProxy isBytes: newNamePointer)
and: [interpreterProxy isBytes: oldNamePointer])
ifFalse: [^interpreterProxy primitiveFail].
newNameIndex := interpreterProxy firstIndexableField: newNamePointer.
newNameSize := interpreterProxy byteSizeOf: newNamePointer.
oldNameIndex := interpreterProxy firstIndexableField: oldNamePointer.
oldNameSize := interpreterProxy byteSizeOf: oldNamePointer.
self
sqFileRenameOld: oldNameIndex Size: oldNameSize
New: newNameIndex Size: newNameSize.
interpreterProxy failed
ifFalse: [interpreterProxy pop: 2]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileSetPosition [
| newPosition file |
<var: 'file' type: 'SQFile *'>
<var: 'newPosition' type: 'squeakFileOffsetType'>
<export: true>
(interpreterProxy byteSizeOf: (interpreterProxy stackValue: 0)) > (self sizeof: #squeakFileOffsetType) ifTrue:
[^interpreterProxy primitiveFail].
newPosition := interpreterProxy positive64BitValueOf: (interpreterProxy stackValue: 0).
file := self fileValueOf: (interpreterProxy stackValue: 1).
interpreterProxy failed ifFalse:
[self sqFile: file SetPosition: newPosition ].
interpreterProxy failed ifFalse:
[interpreterProxy pop: 2] "pop position, file; leave rcvr on stack"
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileSize [
| file size |
<var: 'file' type: 'SQFile *'>
<var: 'size' type: 'squeakFileOffsetType'>
<export: true>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifFalse:[size := self sqFileSize: file].
interpreterProxy failed ifFalse: [
interpreterProxy methodReturnValue: (interpreterProxy positive64BitIntegerFor: size)].
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileStdioHandles [
"Answer an Array of file handles for standard in, standard out and standard error,
with nil in entries that are unvailable, e.g. because the platform does not provide
standard error, etc. Fail if an error occurs determining the stdio handles,
if the security plugin denies access or if memory runs out."
| fileRecords result validMask |
<export: true>
<var: 'fileRecords' declareC: 'SQFile fileRecords[3]'>
self cCode: '' inSmalltalk: [fileRecords := Array new: 3].
validMask := self sqFileStdioHandlesInto: fileRecords.
validMask < 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: validMask].
result := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 3.
result = nil ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
interpreterProxy pushRemappableOop: result.
0 to: 2 do:
[:index|
(validMask bitAnd: (1 << index)) ~= 0 ifTrue:
[result := interpreterProxy instantiateClass: interpreterProxy classByteArray indexableSize: self fileRecordSize.
result = nil ifTrue:
[interpreterProxy popRemappableOop.
^interpreterProxy primitiveFailFor: PrimErrNoMemory].
interpreterProxy storePointer: index ofObject: interpreterProxy topRemappableOop withValue: result.
self
cCode:
[self memcpy: (interpreterProxy firstIndexableField: result)
_: (self addressOf: (fileRecords at: index))
_: self fileRecordSize]
inSmalltalk:
[(interpreterProxy firstIndexableField: result)
unitSize: interpreterProxy wordSize;
at: 0 put: (fileRecords at: index + 1)]]].
result := interpreterProxy popRemappableOop.
interpreterProxy methodReturnValue: result
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileSync [
| file |
<var: 'file' type: 'SQFile *'>
<export: true>
file := self fileValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifFalse:[self sqFileSync: file].
interpreterProxy failed ifFalse: [interpreterProxy pop: 1].
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileTruncate [
"ftruncate is not an ansi function so we have a macro to point to a suitable platform implementation"
| truncatePosition file |
<var: 'file' type: #'SQFile *'>
<var: 'truncatePosition' type: #squeakFileOffsetType>
<export: true>
(interpreterProxy isIntegerObject: (interpreterProxy stackValue: 0)) ifFalse:
[(interpreterProxy byteSizeOf: (interpreterProxy stackValue: 0)) > (self sizeof: #squeakFileOffsetType) ifTrue:
[^interpreterProxy primitiveFail]].
truncatePosition := interpreterProxy positive64BitValueOf: (interpreterProxy stackValue: 0).
file := self fileValueOf: (interpreterProxy stackValue: 1).
interpreterProxy failed ifFalse:
[self sqFile: file Truncate: truncatePosition].
interpreterProxy failed ifFalse:
[interpreterProxy pop: 2 "pop position, file; leave rcvr on stack"]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveFileWrite [
| count startIndex array file slotSize elementSize bytesWritten |
<var: 'file' type: 'SQFile *'>
<var: 'count' type: 'size_t'>
<var: 'startIndex' type: 'size_t'>
<var: 'slotSize' type: #'size_t'>
<var: 'elementSize' type: #'size_t'>
<export: true>
count := interpreterProxy positiveMachineIntegerValueOf: (interpreterProxy stackValue: 0).
startIndex := interpreterProxy positiveMachineIntegerValueOf: (interpreterProxy stackValue: 1).
array := interpreterProxy stackValue: 2.
file := self fileValueOf: (interpreterProxy stackValue: 3).
(interpreterProxy failed
"buffer can be any indexable words or bytes object except CompiledMethod"
or: [(interpreterProxy isWordsOrBytes: array) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
slotSize := interpreterProxy slotSizeOf: array.
(startIndex >= 1 and: [startIndex + count - 1 <= slotSize]) ifFalse:
[^interpreterProxy primitiveFailFor: PrimErrBadIndex].
"Note: adjust startIndex for zero-origin byte indexing"
elementSize := slotSize = 0
ifTrue: [1]
ifFalse: [(interpreterProxy byteSizeOf: array) // slotSize].
bytesWritten := self
sqFile: file
Write: count * elementSize
From: (interpreterProxy cCoerce: (interpreterProxy firstIndexableField: array) to: #'char *')
At: startIndex - 1 * elementSize.
interpreterProxy failed ifFalse:
[interpreterProxy methodReturnValue: (interpreterProxy integerObjectOf: bytesWritten // elementSize)]
]
{ #category : #'file primitives' }
FilePlugin >> primitiveWaitForDataWithSemaphore [
"This primitive set a semaphore passed as a semaphoreIndex to be signalled when there is data in the File / Pipe, and it will not block"
| semaphoreIndex file |
<var: 'file' type: 'SQFile *'>
<export: true>
semaphoreIndex := interpreterProxy integerValueOf: (interpreterProxy stackValue: 0).
file := self fileValueOf: (interpreterProxy stackValue: 1).
interpreterProxy failed ifFalse:
[self waitForData: file onSemaphoreIndex: semaphoreIndex ].
interpreterProxy failed ifFalse:
[interpreterProxy pop: 2] "pop position, file; leave rcvr on stack"
]
{ #category : #'file primitives' }
FilePlugin >> setMacFile: fileName Type: typeString AndCreator: creatorString [
"Exported entry point for the VM. Needed for image saving only and no-op on anything but Macs."
<export: true>
<var: #fileName type: 'char *'>
<var: #typeString type: 'char *'>
<var: #creatorString type: 'char *'>
^self cCode: 'dir_SetMacFileTypeAndCreator(fileName, strlen(fileName), typeString, creatorString)'.
]
{ #category : #'initialize-release' }
FilePlugin >> shutdownModule [
<export: true>
^self cCode: 'sqFileShutdown()' inSmalltalk:[true]
]
{ #category : #private }
FilePlugin >> sizeOfPointer [
"Return the size of a pointer (address size)"
<inline: #always>
^self sizeof: #'void *'
]