-
-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathFileReference.class.st
More file actions
588 lines (468 loc) · 15.5 KB
/
FileReference.class.st
File metadata and controls
588 lines (468 loc) · 15.5 KB
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
"
I combine a filesystem and path, which is sufficient to refer to a concrete file or directory. I provide methods for navigating my filesystem, performing filesystem operations and opening and closing files.
I am the primary mechanism for working with files and directories.
###Examples
```
| working |
working := FileSystem disk workingDirectory.
working files
```
```
| disk |
disk := FileSystem disk.
disk root. ""a reference to the root directory""
disk working. ""a reference to the working directory""
```
"
Class {
#name : 'FileReference',
#superclass : 'AbstractFileReference',
#instVars : [
'filesystem'
],
#category : 'FileSystem-Core-Public',
#package : 'FileSystem-Core',
#tag : 'Public'
}
{ #category : 'cross platform' }
FileReference class >> / aString [
"Answer a reference to the argument resolved against the root of the current disk filesystem."
^ FileSystem disk / aString
]
{ #category : 'instance creation' }
FileReference class >> fileSystem: aFilesystem path: aPath [
^ self new setFileSystem: aFilesystem path: aPath
]
{ #category : 'instance creation' }
FileReference class >> newTempFilePrefix: prefix suffix: suffix [
| tmpDir random fileName |
tmpDir := FileLocator temp asFileReference.
[
random := UUID new asInteger asString.
fileName := prefix , random , suffix.
(tmpDir / fileName) exists ] whileTrue.
^ tmpDir / fileName
]
{ #category : 'navigating' }
FileReference >> , extension [
^ self withPath: self path, extension
]
{ #category : 'comparing' }
FileReference >> = other [
"Two FileReferences are considered equal if they refer to the same file / directory.
As paths can have multiple relative representations, compare the absolute paths."
"Perform the path comparison last as conversion to absolute paths is relatively expensive"
^ self species = other species
and: [self fileSystem = other fileSystem
and: [self absolutePath canonicalize = other absolutePath canonicalize]]
]
{ #category : 'accessing' }
FileReference >> absolutePath [
"Return the absolute of the receiver"
^ self path isRelative
ifFalse: [ self path ]
ifTrue: [ filesystem resolve: self path ]
]
{ #category : 'accessing' }
FileReference >> accessTime [
^ filesystem accessTimeOf: self path
]
{ #category : 'converting' }
FileReference >> asAbsolute [
"Return the receiver as an absolute file reference."
^ self isAbsolute
ifTrue: [ self ]
ifFalse: [ filesystem referenceTo: (filesystem resolve: path) ]
]
{ #category : 'converting' }
FileReference >> asFileLocatorOrReference [
^ self path asFileLocatorOrReference
]
{ #category : 'converting' }
FileReference >> asFileReference [
^ self
]
{ #category : 'converting' }
FileReference >> asPath [
"Answer the receivers path"
^path
]
{ #category : 'streams' }
FileReference >> binaryReadStream [
"Answer a buffered binary read stream on the receiver"
^ ZnBufferedReadStream on: (filesystem binaryReadStreamOn: self path)
]
{ #category : 'streams' }
FileReference >> binaryWriteStream [
"Answer a buffered binary write stream on the receiver"
^ ZnBufferedWriteStream on: (filesystem binaryWriteStreamOn: self path)
]
{ #category : 'delegated' }
FileReference >> canonicalize [
"Answer the receiver with references to the current folder (.) and parent folder (..) removed"
^ self withPath: (self path canonicalizeOnFilesystem: filesystem)
]
{ #category : 'accessing' }
FileReference >> changeTime [
^ filesystem changeTimeOf: self path
]
{ #category : 'comparing' }
FileReference >> containsReference: aReference [
^ aReference fileSystem = filesystem and: [path contains: aReference path]
]
{ #category : 'operations' }
FileReference >> copyTo: aFileReference [
"Copy the receiver and create the argument, a file reference."
"If you want to copy a file from a folder into a given folder, the argument should not be the future parent folder
but a file reference of the future location.
Therefore use
(folder1 / 'foo.txt') copyTo: (folder2 / 'bar.txt')"
self isDirectory
ifTrue: [ aFileReference ensureCreateDirectory ]
ifFalse: [ filesystem copy: path toReference: aFileReference ]
]
{ #category : 'copying' }
FileReference >> copyWithPath: newPath [
^ filesystem referenceTo: newPath
]
{ #category : 'accessing' }
FileReference >> creationTime [
"Answer the receivers creation time.
Note that the interpretation varies by platform."
^ filesystem creationTimeOf: self path
]
{ #category : 'operations' }
FileReference >> delete [
"Deletes the referenced file or directory. If the directory is not empty,
raises an error. Use #deleteAll to delete with the children."
(self isDirectory and:
[ self isSymlink not and:
[ self hasChildren ] ])
ifTrue:[DirectoryIsNotEmpty signalWith: self].
filesystem delete: path
]
{ #category : 'operations' }
FileReference >> deleteIfAbsent: aBlock [
self exists
ifTrue: [ self delete ]
ifFalse: [ aBlock value ]
]
{ #category : 'accessing' }
FileReference >> deviceId [
"Return the device id of the file at aPath"
^ filesystem deviceIdOf: path
]
{ #category : 'displaying' }
FileReference >> displayStringOn: stream [
filesystem printPath: path on: stream
]
{ #category : 'operations' }
FileReference >> ensureCreateDirectory [
"Create if necessary a directory for the receiver."
filesystem ensureCreateDirectory: path
]
{ #category : 'navigating' }
FileReference >> entries [
"Return the entries (meta data - file description) of the direct children of the receiver"
^ self fileSystem entriesAt: self path
]
{ #category : 'accessing' }
FileReference >> entry [
"Return the entry (meta data) describing the receiver."
^ filesystem entryAt: path
]
{ #category : 'testing' }
FileReference >> exists [
"Answer a boolean indicating whether the receiver exists on the file system"
^ filesystem exists: path
]
{ #category : 'accessing' }
FileReference >> fileSystem [
"Return the filesystem to which the receiver belong."
^ filesystem
]
{ #category : 'accessing' }
FileReference >> fullName [
"Return the full path name of the receiver."
^ filesystem stringFromPath: (filesystem resolve: path)
]
{ #category : 'accessing' }
FileReference >> fullPath [
^ self path
]
{ #category : 'accessing' }
FileReference >> gid [
"Return the gid of the file at aPath"
^ filesystem gidOf: path
]
{ #category : 'testing' }
FileReference >> hasChildren [
"Return whether the receiver has any children."
"FileSystem workingDirectory hasChildren"
^ filesystem hasChildren: path
]
{ #category : 'testing' }
FileReference >> hasDirectories [
"Return whether the receiver has children that are directories."
"FileSystem workingDirectory hasDirectories"
^ filesystem hasDirectories: path
]
{ #category : 'testing' }
FileReference >> hasFiles [
"Return whether the receiver has children that are files."
"FileSystem workingDirectory hasFiles"
^ filesystem hasFiles: path
]
{ #category : 'comparing' }
FileReference >> hash [
^ path hash bitXor: filesystem hash
]
{ #category : 'accessing' }
FileReference >> inode [
"Return the inode of the file at aPath"
^ filesystem inodeOf: path
]
{ #category : 'testing' }
FileReference >> isAbsolute [
^ path isAbsolute
]
{ #category : 'testing' }
FileReference >> isBlock [
"Return a boolean indicating whether the File described by aPath is a block device"
^ filesystem isBlock: path
]
{ #category : 'testing' }
FileReference >> isCharacter [
"Return a boolean indicating whether the File described by aPath is character based"
^ filesystem isCharacter: path
]
{ #category : 'testing' }
FileReference >> isDirectory [
^ filesystem isDirectory: path
]
{ #category : 'testing' }
FileReference >> isExecutable [
"Answer a boolean indicating whether the receiver has the executable flag set"
^ filesystem isExecutable: path
]
{ #category : 'testing' }
FileReference >> isFIFO [
"Return a boolean indicating whether the File described by aPath is FIFO (i.e. a pipe)"
^ filesystem isFIFO: path
]
{ #category : 'testing' }
FileReference >> isFile [
^ filesystem isFile: path
]
{ #category : 'testing' }
FileReference >> isHidden [
^ filesystem isHidden: path attributes: nil
]
{ #category : 'testing' }
FileReference >> isReadable [
^ filesystem isReadable: path
]
{ #category : 'testing' }
FileReference >> isRegular [
"Return a boolean indicating whether the File described by aPath is a regular file"
^ filesystem isRegular: path
]
{ #category : 'testing' }
FileReference >> isRelative [
^ path isRelative
]
{ #category : 'testing' }
FileReference >> isRoot [
^ path isRoot
]
{ #category : 'testing' }
FileReference >> isSocket [
"Return a boolean indicating whether the File described by aPath is a socket"
^ filesystem isSocket: path
]
{ #category : 'testing' }
FileReference >> isSymlink [
"Answer a boolean indicating whether the receiver is a symlink"
^ filesystem isSymlink: path
]
{ #category : 'testing' }
FileReference >> isWritable [
^ filesystem isWritable: path
]
{ #category : 'versions' }
FileReference >> lastFileFor: baseFileName extension: extension [
"Assumes a file is named using a version number encoded as '.' followed by digits
preceding the file extension, e.g., games.22.ston
Answer the file name with the largest number.
If a version number is not found, raises an error"
"FileSystem workingDirectory lastFileFor: 'games' extension: 'ston'"
self deprecated: 'Use the class NewFileVersionner' on: '1/1/2026' in: #pharo14.
^ (((FileVersionner from: self) versionNumberWithoutNumberPatternFor: baseFileName extension: extension)
ifNil: [ self error: 'No file with number pattern' ]
ifNotNil: [: version |
version = 0
ifTrue: [ baseFileName, '.', extension ]
ifFalse: [ baseFileName , '.' , version asString , '.' , extension ] ]) asFileReference
]
{ #category : 'accessing' }
FileReference >> modificationTime [
"Returns the last date of modification of self"
^ filesystem modificationTimeOf: self path
]
{ #category : 'operations' }
FileReference >> moveTo: aReference [
"Move the receiver in the location passed as argument.
(FileSystem disk workingDirectory / 'paf' ) ensureCreateFile.
(FileSystem disk workingDirectory / 'fooFolder') ensureCreateDirectory.
(FileSystem disk workingDirectory / 'paf' ) moveTo: (FileSystem disk workingDirectory / 'fooFolder' / 'paf')
Note that the receiver is modified to point to the new location."
| result |
result := self fileSystem
move: self path
to: aReference resolve.
result ifNotNil: [
self setFileSystem: result fileSystem path: result path ]
]
{ #category : 'versions' }
FileReference >> nextNameFor: baseFileName extension: extension [
"Assumes a file name includes a version number encoded as '.' followed by digits
preceding the file extension, e.g., games.22.ston
Increment the version number (of the largest one) and answer the new file name, e.g., games23.ston
If a version number is not found, set the version to 1 and answer a new file name"
self deprecated: 'Use the class NewFileVersionner' on: '1/1/2026' in: #pharo14.
^ ((FileVersionner from: self) versionNumberWithoutNumberPatternFor: baseFileName extension: extension)
ifNil: [ baseFileName, '.1.', extension ]
ifNotNil: [ : version | baseFileName , '.' , (version + 1) asString , '.' , extension ]
]
{ #category : 'versions' }
FileReference >> nextVersion [
"Assumes a file (or folder) name includes a version number encoded as '.' followed by digits
preceding the file extension. Increment the version number and answer the new file name.
If a version number is not found, return just the file"
self deprecated: 'Use NewFileVersionner' on: '1/1/2026' in: #pharo14.
self exists
ifFalse: [ ^ self ].
^ (FileVersionner from: self) nextVersion
]
{ #category : 'accessing' }
FileReference >> numberOfHardLinks [
"Return the number of hard links to the File described by aPath"
^ filesystem numberOfHardLinks: path
]
{ #category : 'streams' }
FileReference >> openWritable: aBoolean [
^ filesystem open: path writable: aBoolean
]
{ #category : 'printing' }
FileReference >> pathString [
"Return the full path name of the receiver."
^ filesystem stringFromPath: (filesystem resolve: path)
]
{ #category : 'accessing' }
FileReference >> permissions [
^ filesystem permissions: self path
]
{ #category : 'operations' }
FileReference >> permissions: permissions [
"Set the receivers mode to anInteger (as defined by chmod())"
^filesystem file: self path posixPermissions: permissions posixPermission
]
{ #category : 'printing' }
FileReference >> printOn: aStream [
filesystem forReferencePrintOn: aStream.
filesystem printPath: path on: aStream
]
{ #category : 'streams' }
FileReference >> readStream [
^ self readStreamEncoded: 'utf8'
]
{ #category : 'operations' }
FileReference >> renameTo: newBasename [
| destinationPath |
destinationPath := self fileSystem
rename: self
to: self parent / newBasename.
destinationPath ifNotNil: [
self
setFileSystem: filesystem
path: destinationPath ].
^ self
]
{ #category : 'accessing' }
FileReference >> resolve [
^ self
]
{ #category : 'resolving' }
FileReference >> resolvePath: anObject [
^ self withPath: (path resolve: anObject)
]
{ #category : 'resolving' }
FileReference >> resolveReference: aReference [
^ (filesystem = aReference fileSystem or: [aReference isRelative])
ifTrue: [filesystem referenceTo: (path resolvePath: aReference path)]
ifFalse: [aReference]
]
{ #category : 'resolving' }
FileReference >> resolveString: aString [
| thePath |
thePath := filesystem pathFromString: aString.
^ filesystem referenceTo: (path resolve: thePath)
]
{ #category : 'initialization' }
FileReference >> setFileSystem: aFilesystem path: aPath [
filesystem := aFilesystem.
path := aPath
]
{ #category : 'accessing' }
FileReference >> size [
^ filesystem sizeOf: path
]
{ #category : 'accessing' }
FileReference >> symlinkEntry [
"Return the symlink entry (meta data) describing the receiver."
^ filesystem symlinkEntryAt: path
]
{ #category : 'accessing' }
FileReference >> symlinkUid: uid gid: gid [
"Set the owner and group of the receiver by numeric id.
An id of nil leaves it unchanged."
^filesystem file: self path symlinkUid: uid gid: gid
]
{ #category : 'accessing' }
FileReference >> targetFileReference [
"Return the target file of the File described by aPath. For a regular file, this is itself, for a symbolic link, it is the file pointed to by the symbolic link"
^ self class fileSystem: filesystem path: self symlinkEntry targetPath
]
{ #category : 'accessing' }
FileReference >> targetPath [
"Return the target file of the File described by aPath. For a regular file, this is itself, for a symbolic link, it is the file pointed to by the symbolic link"
^ self symlinkEntry targetPath
]
{ #category : 'accessing' }
FileReference >> uid [
"Return the gid of the file at aPath"
^ filesystem uidOf: path
]
{ #category : 'accessing' }
FileReference >> uid: uid gid: gid [
"Set the owner and group of the receiver by numeric id.
An id of nil leaves it unchanged."
^filesystem file: self path uid: uid gid: gid
]
{ #category : 'streams' }
FileReference >> unbufferedBinaryWriteStream [
"Answer a binary read/write stream on the receiver"
^ filesystem binaryWriteStreamOn: self path
]
{ #category : 'versions' }
FileReference >> versionNumberFor: basename extension: extension [
"Answer the latest (largest) version number for the specified file.
0 = basename.extension exists, but nothing later.
nil = no file exists"
self deprecated: 'Use NewFileVersionner' on: '1/1/2026' in: #pharo14.
^ (FileVersionner from: self) versionNumberFor: basename extension: extension
]
{ #category : 'streams' }
FileReference >> writeStream [
^ self writeStreamEncoded: 'utf8'
]