/
File.class.st
1242 lines (928 loc) · 33.5 KB
/
File.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
"
I represent a sequential binary File and provide primitives for various file operations.
I am also the entry point of the FilePlugin primitives.
Examples of usage:
""Creating a file""
file := File named: 'asd.txt' asFileReference fullName.
""Opening / closing it""
file open.
file openForAppend.
file close.
""Accessing the file properties""
file size.
file position.
file position: 0.
file seekAbsolute: 10.
file seekRelative: 10.
file atEnd.
""Writing""
file nextPutAll: 'sdd'.
""Reading""
file next: 2.
""Buffered write""
file next: 2 putAll: 'abc' startingAt: 2.
""Buffered read""
buffer := ByteArray new: 5.
file readInto: buffer startingAt: 1 count: 5.
buffer asString.
"
Class {
#name : #File,
#superclass : #Object,
#instVars : [
'name'
],
#classVars : [
'Registry',
'S_IFBLK',
'S_IFCHR',
'S_IFDIR',
'S_IFIFO',
'S_IFLNK',
'S_IFMT',
'S_IFREG',
'S_IFSOCK'
],
#category : #'Files-Core'
}
{ #category : #'primitives-file' }
File class >> atEnd: id [
"Answer true if the file position is at the end of the file."
<primitive: 'primitiveFileAtEnd' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #'primitives-errors' }
File class >> cantAllocateMemory [
^-10
]
{ #category : #'primitives-errors' }
File class >> cantOpenDir [
^-9
]
{ #category : #'primitives-errors' }
File class >> cantReadlink [
^-8
]
{ #category : #'primitives-errors' }
File class >> cantStatPath [
"SecurityPlugin determined that the requested path cannot be accessed."
^-3
]
{ #category : #'primitives-file' }
File class >> close: id [
"Close this file."
<primitive: 'primitiveFileClose' module: 'FilePlugin'>
]
{ #category : #'primitives-file' }
File class >> closed: id [
^ id isNil or: [ (self sizeOrNil: id) isNil ]
]
{ #category : #'primitives-file' }
File class >> connectToFile: filePointer writable: writableFlag [
"Open the file with the supplied FILE* pointer, and return the file ID obtained.
writeableFlag indicates whether to allow write operations and must be compatible with the way the file was opened.
It is the responsibility of the caller to coordinate closing the file."
<primitive: 'primitiveConnectToFile' module: 'FilePlugin' error: error>
error = #'bad argument' ifTrue: [
(filePointer isKindOf: ByteArray) ifFalse:
[ ^self error: 'filePointer must be a ByteArray' ].
(writableFlag isKindOf: Boolean) ifFalse:
[ ^self error: 'writableFlag must be a boolean' ] ].
^ self primitiveFailed
]
{ #category : #'primitives-file' }
File class >> connectToFileDescriptor: fileDescriptor writable: writableFlag [
"Connect to the file with fileDescriptor number, and return the file ID obtained.
writeableFlag indicates whether to allow write operations and must be compatible with the way the file was opened.
It is the responsibility of the caller to coordinate closing the file."
<primitive: 'primitiveConnectToFileDescriptor' module: 'FilePlugin' error: error>
error = #'bad argument' ifTrue: [
fileDescriptor isInteger ifFalse:
[ ^self error: 'fileDescriptor must be an integer' ].
(writableFlag isKindOf: Boolean) ifFalse:
[ ^self error: 'writableFlag must be a boolean' ] ].
^ self primitiveFailed
]
{ #category : #'primitives-errors' }
File class >> corruptValue [
^-7
]
{ #category : #'primitives-path' }
File class >> createDirectory: fullPath [
"Create a directory named by the given path.
Fail if the path is bad or if a file or directory by that name already exists."
<primitive: 'primitiveDirectoryCreate' module: 'FilePlugin'>
^ nil
]
{ #category : #'primitives-encoding' }
File class >> decodePathString: aByteArray [
^ ZnUTF8Encoder default decodeBytes: aByteArray asByteArray
]
{ #category : #'primitives-path' }
File class >> deleteDirectory: fullPath [
"Delete the directory named by the given path.
Fail if the path is bad or if a directory by that name does not exist."
<primitive: 'primitiveDirectoryDelete' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #'primitives-path' }
File class >> deleteFile: fullPathString [
"Delete the supplied UTF8 encoded path. Answer nil on failure."
| utf8Path |
utf8Path := fullPathString utf8Encoded.
self
retryWithGC: [ self primDeleteFile: utf8Path ]
until: [ :result | result notNil ]
forFileNamed: fullPathString.
(self primExists: utf8Path) ifTrue:
[ (CannotDeleteFileException new messageText: 'Could not delete file ' , name,
'. Check the file is not open.') signal ].
]
{ #category : #'primitives-path' }
File class >> delimiter [
"Return the path delimiter for the underlying platform's file system."
<primitive: 'primitiveDirectoryDelimitor' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #'primitives-encoding' }
File class >> encodePathString: aString [
^ ZnUTF8Encoder default encodeString: aString
]
{ #category : #testing }
File class >> exists: aString [
^self primExists: (self encodePathString: aString)
]
{ #category : #attributes }
File class >> file: pathString posixPermissions: anInteger [
"Set the mode of pathString to anInteger (as defined by chmod())"
^self primFile: (self encodePathString: pathString) posixPermissions: anInteger
]
{ #category : #attributes }
File class >> file: pathString symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of pathString by numeric id."
^self primFile: (self encodePathString: pathString)
symlinkUid: (uidInteger ifNil: [ -1 ])
gid: (gidInteger ifNil: [ -1 ])
]
{ #category : #attributes }
File class >> file: pathString uid: uidInteger gid: gidInteger [
"Set the owner and group of pathString by numeric id.
An id of nil leaves it unchanged."
^self primFile: (self encodePathString: pathString)
uid: (uidInteger ifNil: [ -1 ])
gid: (gidInteger ifNil: [ -1 ])
]
{ #category : #attributes }
File class >> fileAttribute: aString number: attributeNumber [
"Answer the attribute identified by attributeNumber for the specified file (aString).
For backward compatibility (on Unix) with FileReference if the file doesn't exist, and the specified path is a (broken) symbolic link, answer the requested attribute for the symbolic link."
^self primFileAttribute: (self encodePathString: aString) number: attributeNumber
]
{ #category : #attributes }
File class >> fileAttributeNumberMap [
"Answer a mapping of attribute number to name for #primFileAttribute:number:"
^#(
#targetName
#mode
#inode
#deviceId
#numberOfHardLinks
#uid
#gid
#size
#accessUnixTime
#modificationUnixTime
#changeUnixTime
#creationUnixTime
#isReadable
#isWritable
#isExecutable
#isSymlink
)
]
{ #category : #attributes }
File class >> fileAttributes: aString mask: attributeMask [
| attributes statAttributes mask |
attributes := self primFileAttributes: (self encodePathString: aString) mask: attributeMask.
mask := attributeMask bitAnd: 2r11.
mask = 1 ifTrue: [ statAttributes := attributes ].
mask = 3 ifTrue: [ statAttributes := attributes at: 1 ].
(statAttributes isNotNil and: [ (statAttributes at: 1) isNotNil ]) ifTrue:
[ statAttributes at: 1 put: (self decodePathString: (statAttributes at: 1)) ].
^attributes
]
{ #category : #'primitives-version' }
File class >> fileAttributesVersionString [
<primitive: 'primitiveVersionString' module: 'FileAttributesPlugin' error: error>
^self primitiveFailed
]
{ #category : #'primitives-file' }
File class >> fileDescriptorIsAvailable: anInteger [
"Answer a boolean indicating whether the supplied file descriptor (anInteger) is available.
A file descriptor is considered available if it is connected to a console / terminal, pipe or a file. cygwin terminals are currently not supported (the pipe is created, but nothing appears in the terminal)"
^ (self fileDescriptorType: anInteger) between: 1 and: 3
]
{ #category : #'primitives-file' }
File class >> fileDescriptorType: fdNum [
"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)"
<primitive: 'primitiveFileDescriptorType' module: 'FilePlugin' error: error>
error = #'bad argument'
ifTrue: [ fdNum isInteger
ifFalse: [ ^ self error: 'File Descriptor must be an integer!' ] ].
^ self primitiveFailed
]
{ #category : #'primitives-file' }
File class >> flush: id [
"On Unix, the FilePlugin uses stdio FILE* structs which maintain their
own internal buffer to minimize write() syscalls. This flushes that buffer.
On Windows this and primSync: do the same thing."
<primitive: 'primitiveFileFlush' module: 'FilePlugin'>
"We can't ignore fflush() failing, because it can fail for any of the
reasons write() can."
self primitiveFailed
]
{ #category : #attributes }
File class >> fromPlatformPath: aByteArray [
"Convert the supplied platform encoded string to the native (UTF8) equivalent"
^self primFromPlatformPath: aByteArray
]
{ #category : #'primitives-errors' }
File class >> getAttributesFailed [
^-4
]
{ #category : #'primitives-path' }
File class >> getMacFile: fileName type: typeString creator: creatorString [
"Get the Macintosh file type and creator info for the file with the given name. Fails if the file does not exist or if the type and creator type arguments are not strings of length 4. This primitive is Mac specific; it is a noop on other platforms."
<primitive: 'primitiveDirectoryGetMacTypeAndCreator' module: 'FilePlugin'>
]
{ #category : #'primitives-file' }
File class >> getPosition: id [
"Get this files current position."
<primitive: 'primitiveFileGetPosition' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #'class initialization' }
File class >> initialize [
SessionManager default registerSystemClassNamed: self name.
self reset.
]
{ #category : #'primitives-errors' }
File class >> invalidArguments [
^-6
]
{ #category : #'primitives-file modes' }
File class >> isBlock: aPath [
"Answer a boolean indicating whether the supplied path is a Block file"
^self modeIsBlock: (self modeOf: aPath)
]
{ #category : #'primitives-file modes' }
File class >> isCharacter: aPath [
"Answer a boolean indicating whether the supplied path is a Character file"
^self modeIsCharacter: (self modeOf: aPath)
]
{ #category : #'primitives-file modes' }
File class >> isDirectory: aPath [
"Answer a boolean indicating whether the supplied path is a Directory"
^[ self modeIsDirectory: (self modeOf: aPath) ]
on: FileDoesNotExistException
do: [ false ].
]
{ #category : #'primitives-file modes' }
File class >> isExecutable: aPath [
"Answer a boolean indicating whether the supplied path is a executable file"
^self fileAttribute: aPath number: 15
]
{ #category : #'primitives-file modes' }
File class >> isFIFO: aPath [
"Answer a boolean indicating whether the supplied path is a FIFO file"
^self modeIsFIFO: (self modeOf: aPath)
]
{ #category : #'primitives-file modes' }
File class >> isFile: aPathString [
"Answer a boolean indicating whether the supplied path is a file, i.e. not a directory"
^[ (self modeIsDirectory: (self modeOf: aPathString)) not ]
on: FileDoesNotExistException
do: [ false ].
]
{ #category : #'primitives-file modes' }
File class >> isReadable: aPath [
"Answer a boolean indicating whether the supplied path is a readable file"
^self fileAttribute: aPath number: 13
]
{ #category : #'primitives-file modes' }
File class >> isRegular: aPath [
"Answer a boolean indicating whether the supplied path is a Regular file"
^self modeIsRegular: (self modeOf: aPath)
]
{ #category : #'primitives-file modes' }
File class >> isSocket: aPath [
"Answer a boolean indicating whether the supplied path is a Socket file"
^self modeIsSocket: (self modeOf: aPath)
]
{ #category : #'primitives-file modes' }
File class >> isSymlink: aPath [
"Answer a boolean indicating whether the supplied path is a Symlink file"
^self fileAttribute: aPath number: 16
]
{ #category : #'primitives-file modes' }
File class >> isWritable: aPath [
"Answer a boolean indicating whether the supplied path is a writable file"
^self fileAttribute: aPath number: 14
]
{ #category : #'primitives-path' }
File class >> lookupDirectory: fullPath filename: fileName [
"Look up <fileName> (a simple file name) in the directory identified by <fullPath> and return an array containing:
<fileName>
<creationTime> (in seconds since the start of the Smalltalk time epoch)
<modificationTime> (in seconds since the start of the Smalltalk time epoch)
<dirFlag> DirFlag is true if the entry is a directory
<fileSize> FileSize the file size in bytes or zero for directories
<posixPermissions> Numeric Notation
<symLinkFlag> seemingly, symLinkFlag is true if the entry is a symLink
On Unix, the empty path denotes '/'.
On Macs and PCs, it is the container of the system volumes."
<primitive: 'primitiveDirectoryEntry' module: 'FilePlugin'>
^ #badDirectoryPath
]
{ #category : #'primitives-path' }
File class >> lookupEntryIn: fullPath index: index [
"Look up the index-th entry of the directory with the given fully-qualified path
(i.e., starting from the root of the file hierarchy) and return an array containing:
<name> <creationTime> <modificationTime> <dirFlag> <fileSize>
The empty string enumerates the top-level files or drives. (For example, on Unix, the empty
path enumerates the contents of '/'. On Macs and PCs, it enumerates the mounted volumes/drives.)
The creation and modification times are in seconds since the start of the Smalltalk time epoch.
DirFlag is true if the entry is a directory. FileSize the file size in bytes or zero for directories.
The primitive returns nil when index is past the end of the directory. It fails if the given path
is bad."
<primitive: 'primitiveDirectoryLookup' module: 'FilePlugin' error: errorCode >
^ #badDirectoryPath
]
{ #category : #'primitives-file modes' }
File class >> modeIsBlock: aMode [
"Answer a boolean indicating whether the supplied mode has the Block type bit set"
^(aMode bitAnd: S_IFMT) = S_IFBLK
]
{ #category : #'primitives-file modes' }
File class >> modeIsCharacter: aMode [
"Answer a boolean indicating whether the supplied mode has the Character type bit set"
^(aMode bitAnd: S_IFMT) = S_IFCHR
]
{ #category : #'primitives-file modes' }
File class >> modeIsDirectory: aMode [
"Answer a boolean indicating whether the supplied mode has the Directory type bit set"
^(aMode bitAnd: S_IFMT) = S_IFDIR
]
{ #category : #'primitives-file modes' }
File class >> modeIsFIFO: aMode [
"Answer a boolean indicating whether the supplied mode has the FIFO type bit set"
^(aMode bitAnd: S_IFMT) = S_IFIFO
]
{ #category : #'primitives-file modes' }
File class >> modeIsRegular: aMode [
"Answer a boolean indicating whether the supplied mode has the Regular type bit set"
^(aMode bitAnd: S_IFMT) = S_IFREG
]
{ #category : #'primitives-file modes' }
File class >> modeIsSocket: aMode [
"Answer a boolean indicating whether the supplied mode has the Socket type bit set"
^(aMode bitAnd: S_IFMT) = S_IFSOCK
]
{ #category : #'primitives-file modes' }
File class >> modeIsSymlink: aMode [
"Answer a boolean indicating whether the supplied mode has the Socket type bit set"
^(aMode bitAnd: S_IFMT) = S_IFLNK
]
{ #category : #'primitives-file modes' }
File class >> modeOf: aPathString [
"Answer the statBuf.st_mode for the supplied path"
^self fileAttribute: aPathString number: 2
]
{ #category : #'file creation' }
File class >> named: fileName [
"Open a file with the given name for reading and writing. If the name has no directory part, then the file will be created in the default directory. If the file already exists, its prior contents may be modified or replaced, but the file will not be truncated on close."
^ self new named: fileName
]
{ #category : #'primitives-file' }
File class >> open: fileName writable: writableFlag [
"Open a file of the given name, and return the file ID obtained.
If writableFlag is true, then
if there is none with this name, then create one
else prepare to overwrite the existing from the beginning
otherwise
if the file exists, open it read-only
else return nil"
<primitive: 'primitiveFileOpen' module: 'FilePlugin'>
^ nil
]
{ #category : #'file creation' }
File class >> openAndTruncateForWriteFileNamed: aName [
^ (self named: aName)
delete;
openForWrite
]
{ #category : #'file creation' }
File class >> openForReadFileNamed: aName [
^ (self named: aName)
openForRead
]
{ #category : #'file creation' }
File class >> openForWriteFileNamed: aName [
^ (self named: aName) openForWrite
]
{ #category : #'primitives-file modes' }
File class >> posixPermissions: aPath [
"Answer the posix permissions for the supplied path"
^(self modeOf: aPath) bitAnd: 8r777
]
{ #category : #'primitives-directory' }
File class >> primClosedir: directoryPointerBytes [
"Close the directory stream associated with directoryPointerBytes.
Caution: do not call this twice on the same externalAddress."
"self primClosedir: (self primOpendir: '/etc')"
"self primClosedir: (self primOpendir: '/no/such/directory')"
<primitive: 'primitiveClosedir' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primClosedir'
]
{ #category : #'primitives-path' }
File class >> primDeleteFile: aFileName [
"Delete the file of the given name.
Return self if the primitive succeeds, nil otherwise."
<primitive: 'primitiveFileDelete' module: 'FilePlugin'>
^ nil
]
{ #category : #'primitives-file attributes' }
File class >> primExists: aByteArray [
"Answer a boolean indicating whether the supplied file exists."
<primitive: 'primitiveFileExists' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: aByteArray.
]
{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray posixPermissions: anInteger [
"Set the mode of pathByateArray to anInteger (as defined by chmod())"
<primitive: 'primitiveChangeMode' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]
{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."
<primitive: 'primitiveSymlinkChangeOwner' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]
{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray uid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."
<primitive: 'primitiveChangeOwner' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]
{ #category : #'primitives-file attributes' }
File class >> primFileAttribute: aByteArray number: attributeNumber [
"Answer a single attribute for the supplied file.
For backward compatibility (on Unix) with FileReference if the file doesn't exist, and the specified path is a (broken) symbolic link, answer the requested attribute for the symbolic link.
stat() information:
1: name
2: mode
3: ino
4: dev
5: nlink
6: uid
7: gid
8: size
9: accessDate
10: modifiedDate
11: changeDate
12: creationDate
access() information
13: is readable
14: is writeable
15: is executable
symbolic link information
16: is symbolic link
"
<primitive: 'primitiveFileAttribute' module: 'FileAttributesPlugin' error: error>
"FilePlugin>>primitiveDirectoryEntry would return the symbolic link attributes if the symbolic link was broken. This was due to the incorrect implementation of attempting to retrieve symbolic link information.
If the old behaviour is required, the logic is:
(error isPrimitiveError and: [attributeNumber ~= 16 and: [error errorCode = self cantStatPath and: [
self platformSupportsSymbolicLinksEgUnix]]]) ifTrue:
[DiskSymlinkDirectoryEntry fileSystem: DiskStore currentFileSystem path: aString asPath]"
error isPrimitiveError ifTrue: [
(attributeNumber = 16 and: [ error errorCode = self unsupportedOperation ]) ifTrue:
"If symlinks aren't supported, answer false"
[ ^false ]].
^self signalError: error for: aByteArray
]
{ #category : #'primitives-file attributes' }
File class >> primFileAttributes: aByteArray mask: attributeMask [
"Answer an array of attributes for the supplied file. The size and contents of the array are determined by the attributeMask:
Bit 0: stat() information
Bit 1: access() information
Bit 2: use lstat() (instead of stat())
On error, answer an error code (Integer).
stat() information:
1: name
2: mode
3: ino
4: dev
5: nlink
6: uid
7: gid
8: size
9: accessDate
10: modifiedDate
11: creationDate
12: Windows File Attributes mask (https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants)
access() information
1: is readable
2: is writeable
3: is executable
"
<primitive: 'primitiveFileAttributes' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: aByteArray
]
{ #category : #private }
File class >> primFileMasks [
"Answer an array of well known masks:
1: S_IFMT
2: S_IFSOCK
3: S_IFLNK
4: S_IFREG
5: S_IFBLK
6: S_IFDIR
7: S_IFCHR
8: S_IFIFO
For more information, see: http://man7.org/linux/man-pages/man2/stat.2.html
"
<primitive: 'primitiveFileMasks' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primFileMasks'
]
{ #category : #'primitives-file attributes' }
File class >> primFromPlatformPath: aByteArray [
"Convert the supplied platform encoded string to the native (UTF8) equivalent"
<primitive: 'primitivePlatToStPath' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primToPlatformPath:'
]
{ #category : #'primitives-windows' }
File class >> primLogicalDrives [
"Answer the windows logical drive mask"
<primitive: 'primitiveLogicalDrives' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primLogicalDrives'
]
{ #category : #'primitives-directory' }
File class >> primOpendir: pathString [
"Answer an ExternalAddress for a directory stream on pathString, or nil if
the directory cannot be opened"
"self primOpendir: '/etc'"
"self primOpendir: '.'"
"self primOpendir: '/no/such/directory'"
<primitive: 'primitiveOpendir' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathString
]
{ #category : #'primitives-directory' }
File class >> primPathMax [
"Answer the VMs PATH_MAX value"
<primitive: 'primitivePathMax' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primPathMax'
]
{ #category : #'primitives-directory' }
File class >> primReaddir: directoryPointerBytes [
"Read the next directory entry from the directory stream associated with
directoryPointerBytes. Answer the name of the entry, ornil for end of directory stream."
"self primReaddir: (self primOpendir: '/etc')"
"self primReaddir: (self primOpendir: '/no/such/directory')"
<primitive: 'primitiveReaddir' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primReaddir:'
]
{ #category : #'primitives-directory' }
File class >> primRewinddir: directoryPointerBytes [
"Rewind the directory stream associated with directoryPointerBytes. Answer
anExternalAddress on success, or nil on failure."
"self primRewinddir: (self primOpendir: '/etc')"
"self primRewinddir: (self primOpendir: '/no/such/directory')"
<primitive: 'primitiveRewinddir' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primRewinddir:'
]
{ #category : #'primitives-file attributes' }
File class >> primToPlatformPath: aByteArray [
"Convert the supplied UTF8 encoded string to the platform encoded equivalent"
<primitive: 'primitiveStToPlatPath' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: 'primToPlatformPath:'
]
{ #category : #'primitives-file' }
File class >> read: id into: byteArray startingAt: startIndex count: count [
"Read up to count bytes of data from this file into the given string or byte array starting at the given index. Answer the number of bytes actually read."
<primitive: 'primitiveFileRead' module: 'FilePlugin'>
(self closed: id)
ifTrue: [ ^ self error: 'File is closed' ].
self error: 'File read failed'
]
{ #category : #registry }
File class >> register: anObject [
^self registry add: anObject
]
{ #category : #registry }
File class >> registry [
^Registry ifNil: [Registry := WeakRegistry new]
]
{ #category : #'primitives-path' }
File class >> rename: oldFileFullName to: newFileFullName [
"Rename the file of the given name to the new name. Fail if there is no file of the old name
or if there is an existing file with the new name."
<primitive: 'primitiveFileRename' module: 'FilePlugin'>
^nil
]
{ #category : #initialization }
File class >> reset [
"Initialise the receiver for the current platform"
| masks |
masks := self primFileMasks.
S_IFMT := masks at: 1.
S_IFSOCK := masks at: 2.
S_IFLNK := masks at: 3.
S_IFREG := masks at: 4.
S_IFBLK := masks at: 5.
S_IFDIR := masks at: 6.
S_IFCHR := masks at: 7.
S_IFIFO := masks at: 8.
]
{ #category : #registry }
File class >> retryWithGC: execBlock until: testBlock forFileNamed: fullName [
"Some platforms, like Windows, will fail if we try to open a file twice.
However, sometimes it may happen that a file was unreferenced and should be garbage-collected.
The purpose of this method is to try to open the file, and if it fails, launch a garbage collection to try to close the unreferenced files.
Then we will retry again to open the file"
| blockValue foundIt |
blockValue := execBlock value.
(testBlock value: blockValue) ifTrue: [ ^ blockValue ].
"Optimization: See if we have a file with the given name. Not really needed"
foundIt := self registry keys "hold on strongly for now"
anySatisfy: [:file| file name sameAs: fullName].
foundIt ifFalse:[^blockValue].
Smalltalk garbageCollectMost.
blockValue := execBlock value.
(testBlock value: blockValue) ifTrue: [ ^ blockValue ].
Smalltalk garbageCollect.
^execBlock value.
]
{ #category : #'primitives-file modes' }
File class >> s_IFBLK [
^ S_IFBLK
]
{ #category : #'primitives-file modes' }
File class >> s_IFCHR [
^ S_IFCHR
]
{ #category : #'primitives-file modes' }
File class >> s_IFDIR [
^ S_IFDIR
]
{ #category : #'primitives-file modes' }
File class >> s_IFIFO [
^ S_IFIFO
]
{ #category : #'primitives-file modes' }
File class >> s_IFLNK [
^ S_IFLNK
]
{ #category : #'primitives-file modes' }
File class >> s_IFMT [
^ S_IFMT
]
{ #category : #'primitives-file modes' }
File class >> s_IFREG [
^ S_IFREG
]
{ #category : #'primitives-file modes' }
File class >> s_IFSOCK [
^ S_IFSOCK
]
{ #category : #'primitives-file' }
File class >> setPosition: id to: anInteger [
"Set this file to the given position."
<primitive: 'primitiveFileSetPosition' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #private }
File class >> signalError: error for: aByteArray [
"Raise the appropriate signal for the supplied error"
| errorNumber pathString |
error ifNil: [ ^self primitiveFailed ].
error isSymbol ifTrue: [ ^self primitiveFailed: error ].
error isPrimitiveError ifFalse: [
"We shouldn't ever get here"
^self primitiveFailed. ].
pathString := self decodePathString: aByteArray asByteArray.
errorNumber := error errorCode.
errorNumber = self stringTooLong ifTrue:
[ ^IllegalFileName signalWith: pathString ].
errorNumber = self cantStatPath ifTrue:
[ ^FileDoesNotExistException signalWith: pathString ].
errorNumber = self getAttributesFailed ifTrue:
[ ^FileDoesNotExistException signalWith: pathString ].
errorNumber = self unsupportedOperation ifTrue:
[ ^FileAttributeNotSupported signalWith: pathString ].
^FileException signalWith: pathString.
]
{ #category : #'primitives-file' }
File class >> sizeOf: id [
"Answer the size of this file."
<primitive: 'primitiveFileSize' module: 'FilePlugin'>
self primitiveFailed
]
{ #category : #'primitives-file' }
File class >> sizeOrNil: id [
"Answer the size of this file."
<primitive: 'primitiveFileSize' module: 'FilePlugin'>
^ nil
]
{ #category : #'class initialization' }
File class >> startUp: isImageStarting [
"Update VM constants on startup"
isImageStarting ifTrue: [ self reset ]