-
Notifications
You must be signed in to change notification settings - Fork 64
/
FileAttributesPlugin.class.st
1098 lines (915 loc) · 36.7 KB
/
FileAttributesPlugin.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
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
"
FileAttributesPlugin defines the named primitives used by the FileAttributes.
For information on stat(), see http://man7.org/linux/man-pages/man2/stat.2.html
For information on access(), see http://man7.org/linux/man-pages/man2/access.2.html
For information on design considerations, see FileAttributes class comments.
Public interface:
The set of primitives available to the image are located in the 'file primitives' protocol.
Return codes:
Methods called within FileAttributesPlugin will return 0 on success, or one of the codes contained in the 'errors' protocol on failure.
Public primitives should never return 0. See each primitive for the success return types. Failure codes are returned as an integer, or integer wrapped in an array (see FileAttributes class comments for further discussion).
"
Class {
#name : #FileAttributesPlugin,
#superclass : #InterpreterPlugin,
#instVars : [
'hasSecurityPlugin',
'sCOFfn',
'sCLPfn'
],
#category : #FileAttributesPlugin
}
{ #category : #translation }
FileAttributesPlugin class >> declareCVarsIn: cg [
self declareC: #('sCLPfn' 'sCOFfn')
as: #'void *'
in: cg.
"Assume the security plugin can be loaded until proven otherwise"
cg var: 'hasSecurityPlugin' declareC: 'int hasSecurityPlugin = 1'.
cg addHeaderFile: '<errno.h>'.
cg addHeaderFile: '<limits.h>'.
cg addHeaderFile: '<sys/types.h>'.
cg addHeaderFile: '<unistd.h>'.
cg addHeaderFile: '<dirent.h>
#if _WIN32 || _WIN64
# include <windows.h>
# include <winbase.h>
# define FAIL() { return -1; }
# include "sqWin32File.h"
# if !defined(PATH_MAX) /* work around bug in 64-bit cygwin; sigh :-( */
# define PATH_MAX 4096
# endif
# define HAVE_CHMOD 0
# define HAVE_CHOWN 0
#else
#define HAVE_CHMOD 1
#define HAVE_CHOWN 1
#endif'.
cg addHeaderFile: '"faCommon.h"'.
cg addHeaderFile: '<sys/stat.h>
#if !defined(HAVE_LSTAT) && !defined(_WIN32) && !defined(_WIN64)
# define HAVE_LSTAT 1
#endif'.
cg addHeaderFile: '<unistd.h>
/* AKG 2018 - ', self moduleName, '.c translated from class ', self name, ' */'
]
{ #category : #testing }
FileAttributesPlugin class >> installedModuleVersionString [
"Answer the version string for the plugin which is actually running in the VM"
"self installedModuleVersionString"
<primitive: 'primitiveVersionString' module: 'FileAttributesPlugin'>
^ nil
]
{ #category : #translation }
FileAttributesPlugin class >> isStructType: typeName [
^#('struct stat' 'struct dirent' 'osdir') includes: typeName
]
{ #category : #translation }
FileAttributesPlugin class >> moduleName [
^ 'FileAttributesPlugin'
]
{ #category : #testing }
FileAttributesPlugin class >> primFileAttributeOf: pathString number: attributeNumber [
"Answer a single file attribute.
pathString is the path to the file
attributeNumber identifies which attribute to return:
1 - 12: stat(): name, mode, ino, dev, nlink, uid, gid, size, accessDate, modifiedDate, changeDate, creationDate
13 - 15: access(): R_OK, W_OK, X_OK
16: isSymlink
On error, fail with an error code containing the appropriate OS error code."
"(1 to: 16) collect: [:i| self primFileAttributeOf: '.' number: i]"
"(1 to: 16) collect: [:i| self primFileAttributeOf: 'THIS HAS A GOOD CHANCE OF FAILING' number: i]"
<primitive: 'primitiveFileAttribute' module: 'FileAttributesPlugin' error: ec>
^self primitiveFailed
]
{ #category : #testing }
FileAttributesPlugin class >> primFileAttributes: pathString attributeNumber: attributeNumber [
"Answer a single file attribute.
primFileAttributes: aString attributeNumber: attributeNumber
pathString is the path to the file
attributeNumber identifies which attribute to return:
1 - 12: stat(): name, mode, ino, dev, nlink, uid, gid, size, accessDate, modifiedDate, changeDate, creationDate
13 - 15: access(): R_OK, W_OK, X_OK
16: isSymlink
On error, fail with an error code containing the appropriate OS error code."
"(1 to: 16) collect: [:i| self installedFileAttributeFor: '.' numbered: i]"
"(1 to: 16) collect: [:i| self installedFileAttributeFor: 'THIS HAS A GOOD CHANCE OF FAILING' numbered: i]"
<primitive: 'primitiveFileAttribute' module: 'FileAttributesPlugin' error: ec>
^self primitiveFailed
]
{ #category : #simulation }
FileAttributesPlugin class >> simulatorClass [
^FileAttributesPluginSimulator
]
{ #category : #testing }
FileAttributesPlugin class >> versionString [
"Answer version string for this class. If the installed module is up to date, it
will match this version string."
"self versionString"
^ self new versionString
]
{ #category : #'private - directory' }
FileAttributesPlugin >> addressObjectFor: aMachineAddress [
"Answer an ExternalAddress object which represents aMachineAddress"
| addressOop addressOopArrayPointer addressUnion idx |
<var: 'aMachineAddress' type: #'void *'>
<var: 'addressUnion' type: 'union {void *address; unsigned char bytes[sizeof(void *)];}'>
<var: 'addressOopArrayPointer' type: #'unsigned char *'>
self touch: addressUnion.
addressOop := interpreterProxy
instantiateClass: interpreterProxy classByteArray
indexableSize: self sizeOfPointer.
addressOop ifNil:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
addressOopArrayPointer := interpreterProxy arrayValueOf: addressOop.
self cCode: 'addressUnion.address = aMachineAddress'.
idx := 0.
[idx < self sizeOfPointer] whileTrue:
[self cCode: 'addressOopArrayPointer[idx] = addressUnion.bytes[idx]'.
idx := idx + 1].
^ addressOop
]
{ #category : #private }
FileAttributesPlugin >> attributeArray: attributeArrayPtr for: faPath mask: attributeMask [
"Create the attributes array for the specified file (faPath) and set attributeArrayPtr.
Which attributes are retrieved are specified in attributeMask.
On error, set the error in interpreterProxy and answer the appropriate status (some callers check the status, others interpreterProxy)"
| status getAccess getStats getLinkStats attributeArray accessArray resultOop |
<var: 'faPath' type: #'fapath *'>
<var: 'attributeArrayPtr' type: #'sqInt *'>
"Determine which attributes to retrieve"
getStats := attributeMask anyMask: 1.
getAccess := attributeMask anyMask: 2.
(getStats or: [getAccess]) ifFalse:
["No information has been requested, which doesn't make sense"
interpreterProxy primitiveFailForOSError: self invalidArguments.
^self invalidArguments].
getLinkStats := attributeMask anyMask: 4.
getStats ifTrue:
[ attributeArray := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 13.
attributeArray ifNil:
[interpreterProxy primitiveFailFor: PrimErrNoMemory.
^self interpreterError].
self remapOop: attributeArray in:
[status := self faFileStatAttributes: faPath _: getLinkStats _: attributeArray].
status ~= 0 ifTrue:
[^status].
"Set resultOop in case only stat attributes have been requested"
resultOop := attributeArray ].
getAccess ifTrue:
[self remapOop: attributeArray in:
[accessArray := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 3].
accessArray ifNil:
[interpreterProxy primitiveFailFor: PrimErrNoMemory.
self interpreterError].
self faAccessAttributes: faPath _: accessArray _: 0.
interpreterProxy failed ifTrue:
[^self interpreterError].
"Set resultOop in case only access attributes have been requested"
resultOop := accessArray ].
(getStats and: [getAccess]) ifTrue:
[self remapOop: #(attributeArray accessArray) in:
[resultOop := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 2].
resultOop ifNil:
[interpreterProxy primitiveFailFor: PrimErrNoMemory.
self interpreterError].
interpreterProxy
storePointer: 0 ofObject: resultOop withValue: attributeArray;
storePointer: 1 ofObject: resultOop withValue: accessArray
].
attributeArrayPtr at: 0 put: resultOop.
^self faSuccess
]
{ #category : #'errors / status' }
FileAttributesPlugin >> badSessionId [
"The supplied session Id doesn't match the current VM session Id"
^-17
]
{ #category : #'private - directory' }
FileAttributesPlugin >> canOpenDirectoryStreamFor: aPathCString length: length [
"Answer non-zero if security permits the directory to be listed."
"FIXME: This function has not been tested. -dtl"
<var: 'aPathCString' type: #'char *'>
"If the security plugin can be loaded, use it to check .
If not, assume it's ok"
"The hasSecurityPlugin flag is set to 1 by default"
(hasSecurityPlugin = 0) ifTrue: [^ true].
sCLPfn ~= 0
ifTrue:
[^ self cCode: ' ((int (*) (char *, int)) sCLPfn)(aPathCString, length)'
inSmalltalk: [true]]
ifFalse:
[hasSecurityPlugin := 0. "Reset the flag so we do not try again next time"
^ true]
]
{ #category : #'private - file' }
FileAttributesPlugin >> canStatFilePath: aPathCString length: length [
"Answer non-zero if security permits the a stat() call on the file path. Allow a
stat() call only on files which we are permitted to open."
"FIXME: This function has not been tested. -dtl"
<var: 'aPathCString' type: #'char *'>
"If the security plugin can be loaded, use it to check .
If not, assume it's ok"
"The hasSecurityPlugin flag is set to 1 by default"
(hasSecurityPlugin = 0) ifTrue: [^ true].
sCOFfn ~= 0
ifTrue:
[^ self cCode: ' ((int (*) (char *, int, int)) sCOFfn)(aPathCString, length, 0)'
inSmalltalk: [true]]
ifFalse:
[hasSecurityPlugin := 0. "Reset the flag so we do not try again next time"
^ true]
]
{ #category : #'errors / status' }
FileAttributesPlugin >> cantAllocateMemory [
<inline: #always>
^-10
]
{ #category : #'errors / status' }
FileAttributesPlugin >> cantOpenDir [
<inline: #always>
^-9
]
{ #category : #'errors / status' }
FileAttributesPlugin >> cantReadlink [
<inline: #always>
^-8
]
{ #category : #'errors / status' }
FileAttributesPlugin >> cantStatPath [
"SecurityPlugin determined that the requested path cannot be accessed."
<inline: #always>
^-3
]
{ #category : #'private - windows' }
FileAttributesPlugin >> convertWinToSqueakTime: st [
"Convert the supplied Windows SYSTEMTIME to Squeak time"
<option: #_WIN32>
| dy secs nDaysPerMonth |
<returnTypeC: #'sqLong'>
<var: 'st' type: #'SYSTEMTIME'>
<var: 'dy' type: #'sqLong'>
<var: 'secs' type: #'sqLong'>
<var: 'nDaysPerMonth' declareC: 'static sqLong nDaysPerMonth[14] = {
0, 0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334, 365 }'>
self touch: nDaysPerMonth.
"Squeak epoch is Jan 1, 1901"
"compute delta year"
dy := (self cCode: 'st.wYear') - 1901.
secs := (dy * 365 * 24 * 60 * 60) "base seconds"
+ ((dy bitShift: -2) * 24 * 60 * 60). "seconds of leap years"
"check if month > 2 and current year is a leap year"
[ (self cCode: 'st.wMonth') > 2 and: [ (dy bitAnd: 16r0003) = 16r0003 ]] ifTrue: [
"add one day"
secs := secs + (24 * 60 * 60) ].
"add the days from the beginning of the year"
secs := secs + (self cCode: '(nDaysPerMonth[st.wMonth] + st.wDay - 1) * 24 * 60 * 60').
"add the hours, minutes, and seconds"
secs := secs + (self cCode: 'st.wSecond + 60*(st.wMinute + 60*st.wHour)').
^secs
]
{ #category : #'errors / status' }
FileAttributesPlugin >> corruptValue [
<inline: #always>
^-7
]
{ #category : #private }
FileAttributesPlugin >> faPathMax [
"Answer the value of the platform support's definition of FA_PATH_MAX, defined in
platforms/{unix.win32,etc}/plugins/FileAttributesPlugin/faSupport.h"
<inline: #always>
^#FA_PATH_MAX
]
{ #category : #'errors / status' }
FileAttributesPlugin >> faSuccess [
^0
]
{ #category : #'private - file' }
FileAttributesPlugin >> fileExecutableFlag [
<inline: #always>
^ self cCode: [#X_OK] inSmalltalk: [1]
]
{ #category : #'private - file' }
FileAttributesPlugin >> fileOKFlag [
<inline: #always>
^ self cCode: [#F_OK] inSmalltalk: [0]
]
{ #category : #'private - file' }
FileAttributesPlugin >> fileReadableFlag [
<inline: #always>
^ self cCode: [#R_OK] inSmalltalk: [4]
]
{ #category : #'private - file' }
FileAttributesPlugin >> fileWriteableFlag [
<inline: #always>
^ self cCode: [#W_OK] inSmalltalk: [2]
]
{ #category : #'errors / status' }
FileAttributesPlugin >> getAttributesFailed [
<inline: #always>
^-4
]
{ #category : #'initialize-release' }
FileAttributesPlugin >> initialiseModule [
<export: true>
sCOFfn := interpreterProxy ioLoadFunction: 'secCanOpenFileOfSizeWritable' From: 'SecurityPlugin'.
sCLPfn := interpreterProxy ioLoadFunction: 'secCanListPathOfSize' From: 'SecurityPlugin'.
^true
]
{ #category : #'errors / status' }
FileAttributesPlugin >> interpreterError [
"The actual error code is stored by the interpreterProxy"
<inline: #always>
^-15
]
{ #category : #'errors / status' }
FileAttributesPlugin >> invalidArguments [
<inline: #always>
^-6
]
{ #category : #'errors / status' }
FileAttributesPlugin >> invalidRequest [
<inline: #always>
^-11
]
{ #category : #'errors / status' }
FileAttributesPlugin >> noMoreData [
<inline: #always>
^1
]
{ #category : #'private - directory' }
FileAttributesPlugin >> objectFromStruct: aMachineAddress size: structSize [
"Answer a ByteArray object which copies the structure at aMachineAddress"
| addressOop addressOopArrayPointer |
<var: 'aMachineAddress' type: #'void *'>
<var: 'addressOopArrayPointer' type: #'unsigned char *'>
addressOop := interpreterProxy
instantiateClass: interpreterProxy classByteArray
indexableSize: structSize.
addressOop ifNil:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
addressOopArrayPointer := interpreterProxy arrayValueOf: addressOop.
self memcpy: addressOopArrayPointer _: aMachineAddress _: structSize.
^ addressOop
]
{ #category : #'private - file' }
FileAttributesPlugin >> pathOop: pathNameOop toBuffer: cPathName maxLen: maxLen [
"Copy the supplied path name string object to the supplied c string buffer"
| len sPtr |
<var: 'cPathName' type: #'char *'>
<var: 'sPtr' type: #'char *'>
<returnTypeC: #'int'>
len := interpreterProxy stSizeOf: pathNameOop.
(len >= maxLen) ifTrue: [^self stringTooLong].
"Copy pathName to the new string"
sPtr := interpreterProxy arrayValueOf: pathNameOop.
((self canStatFilePath: sPtr length: len) = 0) ifTrue: [^self cantStatPath].
self memcpy: cPathName _: sPtr _: len.
cPathName at: len put: 0.
^0.
]
{ #category : #'private - directory' }
FileAttributesPlugin >> pointerFrom: directoryPointerBytes [
"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: directoryPointerBytes KindOf: 'ByteArray') and:
[(interpreterProxy stSizeOf: directoryPointerBytes) = self sizeOfPointer])
ifFalse: [^ nil].
ptr := interpreterProxy arrayValueOf: directoryPointerBytes.
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' }
FileAttributesPlugin >> primitiveChangeMode [
"Set the mode of the supplied file using chmod()."
| fileNameOop newMode status faPath |
<export: true>
<var: 'newMode' type: #'sqInt'>
<var: 'faPath' type: #'fapath'>
fileNameOop := interpreterProxy stackObjectValue: 1.
newMode := interpreterProxy stackIntegerValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileNameOop) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cppIf: #HAVE_CHMOD ifTrue: [
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileNameOop.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
status := self chmod: (self faGetPlatPath: (self addressOf: faPath)) _: newMode.
status ~= 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: (self cCode: 'errno')].
^interpreterProxy methodReturnValue: interpreterProxy nilObject.
].
^interpreterProxy primitiveFailForOSError: self unsupportedOperation.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveChangeOwner [
"Set the owner of the supplied file using chown()."
| fileNameOop ownerId groupId faPath status |
<export: true>
<var: 'faPath' type: #'fapath'>
fileNameOop := interpreterProxy stackObjectValue: 2.
ownerId := interpreterProxy stackIntegerValue: 1.
groupId := interpreterProxy stackIntegerValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileNameOop) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cppIf: #HAVE_CHOWN ifTrue: [
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileNameOop.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
status := self chown: (self faGetPlatPath: (self addressOf: faPath)) _: ownerId _: groupId.
status ~= 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: (self cCode: 'errno')].
^interpreterProxy methodReturnValue: interpreterProxy nilObject.
].
^interpreterProxy primitiveFailForOSError: self unsupportedOperation.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveClosedir [
"Close the directory stream for dirPointerOop. Answer dirPointerOop on success.
Raise PrimErrBadArgument if the parameter is not a ByteArray length size(void *).
If closedir() returns an error raise PrimitiveOSError."
| dirPointerOop faPathPtr faPath result |
<export: true>
<var: 'faPath' type: #'fapath *'>
<var: 'faPathPtr' type: #'FAPathPtr *'>
dirPointerOop := interpreterProxy stackValue: 0.
faPathPtr := self structFromObject: dirPointerOop
size: (self cCode: 'sizeof(FAPathPtr)' inSmalltalk: [self sizeOfFaPathPtr]).
faPathPtr = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPathPtr := FAPathPtr fromArray: faPathPtr plugin: self].
(self faValidateSessionId: faPathPtr sessionId) ifFalse:
[^interpreterProxy primitiveFailForOSError: self badSessionId].
faPath := faPathPtr faPath.
result := self faCloseDirectory: faPath.
self faInvalidateSessionId: (self cCode: '&faPathPtr->sessionId' inSmalltalk: [faPathPtr]).
result = 0 ifFalse:
[^interpreterProxy primitiveFailForOSError: result].
self free: faPath.
interpreterProxy methodReturnValue: dirPointerOop
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveFileAttribute [
"Answer a single file attribute.
primFileAttributes: aString attributeNumber: attributeNumber
aString is the path to the file
attributeNumber identifies which attribute to return:
1 - 12: stat(): name, mode, ino, dev, nlink, uid, gid, size, accessDate, modifiedDate, changeDate, creationDate
13 - 15: access(): R_OK, W_OK, X_OK
16: isSymlink
On error, answer a single element array containing the appropriate error code."
| fileName attributeNumber resultOop faPath |
<export: true>
<var: 'faPath' type: #'fapath'>
fileName := interpreterProxy stackObjectValue: 1.
attributeNumber := interpreterProxy stackIntegerValue: 0.
(interpreterProxy failed
or: [(attributeNumber between: 1 and: 16) not
or: [(interpreterProxy isBytes: fileName) not]]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileName.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
resultOop := self faFileAttribute: (self addressOf: faPath) _: attributeNumber.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
resultOop = 0
ifTrue: ["It shouldn't be possible to get here"
interpreterProxy primitiveFailForOSError: self unexpectedError]
ifFalse: [interpreterProxy methodReturnValue: resultOop]
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveFileAttributes [
"Answer an array of file attributes.
primFileAttributes: aString mask: attributeMask
aString is the path to the file
attributeMask indicates which attributes to retrieve:
bit 0 - get stat() attributes
bit 1 - get access() attributes
bit 2 - use lstat() instead of stat()
On error answer the appropriate error code (Integer)"
| fileName attributeMask faPath status resultOop |
<export: true>
<var: 'faPath' type: #'fapath'>
fileName := interpreterProxy stackObjectValue: 1.
attributeMask := interpreterProxy stackIntegerValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileName) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileName.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
status := self attributeArray: (self addressOf: resultOop put: [ :val | resultOop := val])
for: (self addressOf: faPath)
mask: attributeMask.
status ~= 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: status].
^interpreterProxy methodReturnValue: resultOop
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveFileExists [
"Check for existence of a file with a call to access(), or the appropriate platform function."
<export: true>
| fileNameOop faPath |
<var: 'faPath' type: #'fapath'>
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
fileNameOop := interpreterProxy stackValue: 0.
(interpreterProxy isBytes: fileNameOop) ifFalse:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self faSetStPathOop: (self addressOf: faPath) _: fileNameOop.
interpreterProxy failed ifFalse:
[^interpreterProxy methodReturnBool: (self faExists: (self addressOf: faPath))]
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveFileMasks [
"Answer an array of well known file masks"
| masks |
<export: true>
masks := interpreterProxy instantiateClass: (interpreterProxy classArray) indexableSize: 8.
masks ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
interpreterProxy
storePointer: 0
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFMT] inSmalltalk: [16rF000])).
self cppIf: (self defined: #_WIN32) not
ifTrue: [
interpreterProxy
storePointer: 1
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFSOCK] inSmalltalk: [16rC000])).
interpreterProxy
storePointer: 2
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFLNK] inSmalltalk: [16rA000]))].
interpreterProxy
storePointer: 3
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFREG] inSmalltalk: [16r8000])).
interpreterProxy
storePointer: 4
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFBLK] inSmalltalk: [16r6000])).
interpreterProxy
storePointer: 5
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFDIR] inSmalltalk: [16r4000])).
interpreterProxy
storePointer: 6
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFCHR] inSmalltalk: [16r2000])).
interpreterProxy
storePointer: 7
ofObject: masks
withValue: (interpreterProxy positive32BitIntegerFor: (self cCode: [#S_IFIFO] inSmalltalk: [16r1000])).
interpreterProxy pop: 1 thenPush: masks
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveLogicalDrives [
"Answer the logical drive mask on windows"
| mask |
<export: true>
<var: 'mask' type: #'unsigned int'>
self cppIf: (self defined: #_WIN32)
ifTrue:
[
mask := self GetLogicalDrives.
mask ~= 0 ifTrue:
[^interpreterProxy pop: 1 thenPush: (interpreterProxy positive32BitIntegerFor: mask)]].
interpreterProxy primitiveFail
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveOpendir [
"Open the supplied directory and answer the first entry and directory pointer.
If the directory is empty, answer nil as the first entry.
If the directory can't be opened, answer an error (cantOpenDir)"
"self primOpendir: '/etc'"
| dirName faPath faPathPtr dirOop status resultOop |
<export: true>
<var: 'faPath' type: #'fapath *'>
<var: 'faPathPtr' type: #'FAPathPtr'>
"Process the parameters"
dirName := interpreterProxy stackObjectValue: 0.
(interpreterProxy isBytes: dirName) ifFalse:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
"Allocate and initialise faPath"
faPath := self cCode: '(fapath *) calloc(1, sizeof(fapath))'
inSmalltalk: [self simulatedFaPath].
faPath = nil ifTrue: [^interpreterProxy primitiveFailForOSError: self cantAllocateMemory].
self faSetStDirOop: faPath _: dirName.
interpreterProxy failed ifTrue: [^interpreterProxy primitiveFailureCode].
(self canOpenDirectoryStreamFor: (self faGetStPath: faPath) length: (self faGetStPathLen: faPath)) ifFalse: [
self free: faPath.
^interpreterProxy primitiveFailForOSError: self cantOpenDir].
"Open the directory and process the first entry"
status := self faOpenDirectory: faPath.
status = self noMoreData ifTrue: [
self free: faPath.
^interpreterProxy methodReturnValue: interpreterProxy nilObject].
status < 0 ifTrue: [
self free: faPath.
^interpreterProxy primitiveFailForOSError: status].
resultOop := self processDirectory: faPath.
interpreterProxy failed ifTrue: [
self free: faPath.
^interpreterProxy primitiveFailureCode ].
"Set the faPathPtr"
self cCode: '' inSmalltalk: [faPathPtr := FAPathPtr plugin: self].
self faInitSessionId: (self cCode: '&faPathPtr.sessionId' inSmalltalk: [faPathPtr]).
faPathPtr faPath: faPath.
self remapOop: resultOop in:
[ dirOop := self objectFromStruct: (self addressOf: faPathPtr) size: self sizeOfFaPathPtr ].
^interpreterProxy
storePointer: 2 ofObject: resultOop withValue: dirOop;
methodReturnValue: resultOop.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitivePathMax [
"Answer the value of FA_PATH_MAX (PATH_MAX) for the current VM"
<export: true>
^interpreterProxy methodReturnInteger: self faPathMax
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitivePlatToStPath [
"Convert the supplied file name (platform encoded) to the St UTF8 encoded byte array"
| fileName faPath resultOop byteArrayPtr |
<export: true>
<var: 'faPath' type: #'fapath'>
<var: 'byteArrayPtr' type: #'unsigned char *'>
fileName := interpreterProxy stackObjectValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileName) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetPlatPathOop: (self addressOf: faPath) _: fileName.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
resultOop := interpreterProxy
instantiateClass: interpreterProxy classByteArray
indexableSize: (self faGetStPathLen: (self addressOf: faPath)).
resultOop ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
byteArrayPtr := interpreterProxy arrayValueOf: resultOop.
self memcpy: byteArrayPtr
_: (self faGetStPath: (self addressOf: faPath))
_: (self faGetStPathLen: (self addressOf: faPath)).
^interpreterProxy methodReturnValue: resultOop.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveReaddir [
"Get the next entry in the directory stream. Answer the name of the entry, or
nil for the end of the directory stream.
Arguments:
- directoryPointer (ByteArray)"
| dirPointerOop faPathPtr faPath resultArray status |
<export: true>
<var: 'faPath' type: #'fapath *'>
<var: 'faPathPtr' type: #'FAPathPtr *'>
dirPointerOop := interpreterProxy stackValue: 0.
faPathPtr := self structFromObject: dirPointerOop
size: (self cCode: 'sizeof(FAPathPtr)' inSmalltalk: [self sizeOfFaPathPtr]).
faPathPtr = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPathPtr := FAPathPtr fromArray: faPathPtr plugin: self].
(self faValidateSessionId: faPathPtr sessionId) ifFalse:
[^interpreterProxy primitiveFailForOSError: self badSessionId].
faPath := faPathPtr faPath.
status := self faReadDirectory: faPath.
status = self noMoreData ifTrue:
[^interpreterProxy methodReturnValue: interpreterProxy nilObject].
status < 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: status].
resultArray := self processDirectory: faPath.
"no need to check the status of #processDirectory: as it will have flagged an error with interpreterProxy"
^interpreterProxy methodReturnValue: resultArray.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveRewinddir [
"Set directoryStream to first entry. Answer dirPointerOop."
| dirPointerOop faPathPtr faPath status resultOop |
<export: true>
<var: 'faPath' type: #'fapath *'>
<var: 'faPathPtr' type: #'FAPathPtr *'>
dirPointerOop := interpreterProxy stackValue: 0.
faPathPtr := self structFromObject: dirPointerOop
size: (self cCode: 'sizeof(FAPathPtr)' inSmalltalk: [self sizeOfFaPathPtr]).
faPathPtr = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPathPtr := FAPathPtr fromArray: faPathPtr plugin: self].
(self faValidateSessionId: faPathPtr sessionId) ifFalse:
[^interpreterProxy primitiveFailForOSError: self badSessionId].
faPath := faPathPtr faPath.
status := self faRewindDirectory: faPath.
status < 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: status].
resultOop := self processDirectory: faPath.
"no need to check the status of #processDirectory: as it will have flagged an error with interpreterProxy"
^interpreterProxy methodReturnValue: resultOop.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveStToPlatPath [
"Convert the supplied file name (UTF8 encoded) to the platform encoded byte array"
| fileName faPath resultOop byteArrayPtr |
<export: true>
<var: 'faPath' type: #'fapath'>
<var: 'byteArrayPtr' type: #'unsigned char *'>
fileName := interpreterProxy stackObjectValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileName) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileName.
interpreterProxy failed ifTrue:
[^interpreterProxy primitiveFailureCode].
resultOop := interpreterProxy
instantiateClass: interpreterProxy classByteArray
indexableSize: (self faGetPlatPathByteCount: (self addressOf: faPath)).
resultOop ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
byteArrayPtr := interpreterProxy arrayValueOf: resultOop.
self memcpy: byteArrayPtr
_: (self faGetPlatPath: (self addressOf: faPath))
_: (self faGetPlatPathByteCount: (self addressOf: faPath)).
^interpreterProxy methodReturnValue: resultOop.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveSymlinkChangeOwner [
"Set the owner of the supplied file using chown()."
| fileNameOop ownerId groupId faPath status |
<export: true>
<var: 'faPath' type: #'fapath'>
fileNameOop := interpreterProxy stackObjectValue: 2.
ownerId := interpreterProxy stackIntegerValue: 1.
groupId := interpreterProxy stackIntegerValue: 0.
(interpreterProxy failed
or: [(interpreterProxy isBytes: fileNameOop) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].
self cppIf: #HAVE_CHOWN ifTrue: [
self cCode: '' inSmalltalk: [faPath := self simulatedFaPath].
self faSetStPathOop: (self addressOf: faPath) _: fileNameOop.
interpreterProxy failed ifTrue: [^interpreterProxy primitiveFailureCode].
status := self lchown: (self faGetPlatPath: (self addressOf: faPath)) _: ownerId _: groupId.
status ~= 0 ifTrue:
[^interpreterProxy primitiveFailForOSError: (self cCode: 'errno')].
^interpreterProxy methodReturnValue: interpreterProxy nilObject.
].
^interpreterProxy primitiveFailForOSError: self unsupportedOperation.
]
{ #category : #'file primitives' }
FileAttributesPlugin >> primitiveVersionString [
"Answer a string containing the version string for this plugin."
<export: true>
interpreterProxy pop: 1 thenPush: (self stringFromCString: self versionString)
]
{ #category : #private }
FileAttributesPlugin >> processDirectory: faPath [
"The supplied faPath contains the full path to the current entry while iterating over a directory.
Convert the file name to an object, get the attributes and answer the resulting array."
| status entryName attributeArray resultArray |
<var: 'faPath' type: #'fapath *'>
status := self faCharToByteArray: (self faGetStFile: faPath)
_: (self addressOf: entryName put: [:val | entryName := val]).
status ~= 0 ifTrue:
[ ^interpreterProxy primitiveFailForOSError: status].
status := self attributeArray: (self addressOf: attributeArray put: [ :val | attributeArray := val ]) for: faPath mask: 1.
"If the stat() fails, still return the filename, just no attributes"
status ~= 0 ifTrue:
[status = self cantStatPath ifTrue:
[attributeArray := interpreterProxy nilObject]
ifFalse:
[^interpreterProxy primitiveFailForOSError: status]].
"resultArray: entryName, attributeArray, dirPtrOop"
self remapOop: #(entryName attributeArray)
in: [resultArray := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 3].
resultArray ifNil:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
interpreterProxy
storePointer: 0 ofObject: resultArray withValue: entryName;
storePointer: 1 ofObject: resultArray withValue: attributeArray.
^resultArray
]
{ #category : #'private - file' }
FileAttributesPlugin >> readLink: cPathName into: cLinkPtr maxLength: maxLength [
"Get the target filename of the supplied symbolic link."
| len |
<var: 'cPathName' type: #'char *'>
<var: 'cLinkPtr' type: #'char *'>
<var: 'maxLength' type: #'size_t'>
<var: 'len' type: #sqInt> "len must be signed so that -1 can be communicated as an error."
len := self cppIf: (self defined: #_WIN32)
ifTrue: [-1]
ifFalse: [self read: cPathName li: cLinkPtr nk: maxLength].
len < 0 ifTrue:
[self cppIf: (self defined: #'INDEBUG') ifTrue:
[self cCode: 'fprintf(stderr, "FileAttributesPlugin: unable to readlink(): %d, errno=%d\n", len, errno)'].
^self cantReadlink].
cLinkPtr at: len put: 0.
^len
]
{ #category : #private }
FileAttributesPlugin >> sizeOfFaPath [
"Answer the size of fapath"
^self cCode: 'sizeof(fapath)'
inSmalltalk: [6].
]
{ #category : #private }
FileAttributesPlugin >> sizeOfFaPathPtr [
"Answer the size of fapathptr.
The simulation uses a two element array."
^self cCode: 'sizeof(FAPathPtr)'
inSmalltalk: [2].
]