1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Filename
|
159
|
14 |
instanceVariableNames:'nameString'
|
276
|
15 |
classVariableNames:'NextTempFilenameIndex'
|
159
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Support'!
|
1
|
18 |
|
|
19 |
Filename comment:'
|
5
|
20 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
159
|
21 |
All Rights Reserved
|
92
|
22 |
|
441
|
23 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.34 1995-09-23 11:16:55 claus Exp $
|
88
|
24 |
'!
|
1
|
25 |
|
88
|
26 |
!Filename class methodsFor:'documentation'!
|
|
27 |
|
|
28 |
copyright
|
|
29 |
"
|
|
30 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
159
|
31 |
All Rights Reserved
|
1
|
32 |
|
88
|
33 |
This software is furnished under a license and may be used
|
|
34 |
only in accordance with the terms of that license and with the
|
|
35 |
inclusion of the above copyright notice. This software may not
|
|
36 |
be provided or otherwise made available to, or used by, any
|
|
37 |
other person. No title to or ownership of the software is
|
|
38 |
hereby transferred.
|
|
39 |
"
|
|
40 |
!
|
|
41 |
|
|
42 |
version
|
|
43 |
"
|
441
|
44 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.34 1995-09-23 11:16:55 claus Exp $
|
88
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
documentation
|
|
49 |
"
|
|
50 |
Filenames; originally added for ST-80 compatibility, is
|
|
51 |
taking over functionality from other classes (FileDirectory).
|
92
|
52 |
|
|
53 |
Instances of Filename do not nescessarily represent valid or existing
|
|
54 |
files - i.e. it is possible (and useful) to have instances for non-existing
|
|
55 |
files around. In other words: the name-string is not checked automatically
|
|
56 |
for being correct or existing.
|
|
57 |
Thus, it is possible to do queries such as:
|
|
58 |
|
159
|
59 |
'/fee/foo/foe' asFilename exists
|
|
60 |
'/not_existing' asFilename isDirectory
|
|
61 |
'/foo/bar' asFilename isReadable
|
92
|
62 |
|
195
|
63 |
(all of the above examples will probably return false on your machine ;-).
|
276
|
64 |
|
|
65 |
examples:
|
|
66 |
|
|
67 |
'Makefile' asFilename readStream
|
|
68 |
|
|
69 |
'newFile' asFilename writeStream
|
|
70 |
|
|
71 |
Filename newTemporary writeStream
|
88
|
72 |
"
|
360
|
73 |
!
|
|
74 |
|
|
75 |
examples
|
|
76 |
"
|
|
77 |
does a file/directory exist ?:
|
|
78 |
|
|
79 |
|f|
|
|
80 |
|
|
81 |
f := 'foobar' asFilename.
|
|
82 |
^ f exists
|
|
83 |
|
|
84 |
|
|
85 |
is it a directory ?:
|
|
86 |
|
|
87 |
|f|
|
|
88 |
|
|
89 |
f := '/tmp' asFilename.
|
|
90 |
^ f isDirectory.
|
|
91 |
|
|
92 |
|
|
93 |
get the working directory:
|
|
94 |
|
|
95 |
^ Filename defaultDirectory
|
|
96 |
|
|
97 |
|
|
98 |
get a files full pathname
|
|
99 |
(caring for relative names or symbolic links):
|
|
100 |
|
|
101 |
|f|
|
|
102 |
|
|
103 |
f := '..' asFilename.
|
|
104 |
^ f pathName
|
|
105 |
|
|
106 |
|
|
107 |
get a directories directory:
|
|
108 |
|
|
109 |
|f|
|
|
110 |
|
|
111 |
f := Filename defaultDirectory.
|
|
112 |
^ f directory
|
|
113 |
|
|
114 |
|
|
115 |
get a files directory:
|
|
116 |
|
|
117 |
|f|
|
|
118 |
|
|
119 |
f := './smalltalk' asFilename.
|
|
120 |
^ f directory
|
|
121 |
|
|
122 |
|
|
123 |
getting access & modification times:
|
|
124 |
|
|
125 |
|
|
126 |
|f|
|
|
127 |
|
|
128 |
f := '/tmp' asFilename.
|
|
129 |
^ f dates
|
|
130 |
|
|
131 |
access time only:
|
|
132 |
|
|
133 |
|f|
|
|
134 |
|
|
135 |
f := '/tmp' asFilename.
|
|
136 |
^ f dates at:#accessed
|
|
137 |
|
|
138 |
|
|
139 |
getting all information on a file/directory:
|
|
140 |
|
|
141 |
|
|
142 |
|f|
|
|
143 |
|
|
144 |
f := '/tmp' asFilename.
|
|
145 |
^ f info
|
|
146 |
|
|
147 |
|
|
148 |
getting a temporary file (unique name):
|
|
149 |
|
|
150 |
|f|
|
|
151 |
|
|
152 |
f := Filename newTemporary.
|
|
153 |
^ f
|
|
154 |
|
|
155 |
|
|
156 |
creating, writing, reading and removing a temporary file:
|
|
157 |
|
|
158 |
|
|
159 |
|f writeStream readStream|
|
|
160 |
|
|
161 |
f := Filename newTemporary.
|
|
162 |
writeStream := f writeStream.
|
|
163 |
writeStream nextPutAll:'hello world'.
|
|
164 |
writeStream cr.
|
|
165 |
writeStream close.
|
|
166 |
|
|
167 |
'contents (as seen by unix''s cat command:' printNL.
|
|
168 |
OperatingSystem executeCommand:('cat ' , f pathName).
|
|
169 |
|
|
170 |
readStream := f readStream.
|
|
171 |
Transcript showCr:'contents as seen by smalltalk:'.
|
|
172 |
Transcript showCr:(readStream upToEnd).
|
|
173 |
readStream close.
|
|
174 |
|
|
175 |
f delete.
|
|
176 |
|
|
177 |
|
|
178 |
getting a directories contents:
|
|
179 |
|
|
180 |
|f files|
|
|
181 |
|
|
182 |
f := '.' asFilename.
|
|
183 |
files := f directoryContents.
|
|
184 |
Transcript showCr:'the files are:'.
|
|
185 |
Transcript showCr:(files printString).
|
|
186 |
|
|
187 |
|
|
188 |
editing a file:
|
|
189 |
|
|
190 |
|f|
|
|
191 |
|
|
192 |
f := '/tmp/fooBar' asFilename.
|
|
193 |
(f writeStream) nextPutAll:'hello world'; close.
|
|
194 |
|
|
195 |
f edit
|
|
196 |
"
|
88
|
197 |
! !
|
1
|
198 |
|
|
199 |
!Filename class methodsFor:'instance creation'!
|
|
200 |
|
|
201 |
named:aString
|
77
|
202 |
"return a filename for a directory named aString."
|
38
|
203 |
|
68
|
204 |
^ (self basicNew) setName:aString
|
38
|
205 |
|
92
|
206 |
"
|
|
207 |
Filename named:'/tmp/fooBar'
|
|
208 |
"
|
216
|
209 |
!
|
|
210 |
|
276
|
211 |
currentDirectory
|
|
212 |
"return a filename for the current directory"
|
|
213 |
|
|
214 |
^ self named:(FileDirectory currentDirectory pathName)
|
|
215 |
|
|
216 |
"
|
|
217 |
Filename currentDirectory
|
|
218 |
"
|
|
219 |
!
|
|
220 |
|
328
|
221 |
defaultDirectory
|
|
222 |
"ST80 compatibility: same as currentDirectory"
|
|
223 |
|
|
224 |
^ self currentDirectory
|
|
225 |
|
|
226 |
"
|
|
227 |
Filename defaultDirectory
|
|
228 |
"
|
|
229 |
!
|
|
230 |
|
276
|
231 |
newTemporary
|
325
|
232 |
"return a new unique filename - use this for temporary files.
|
|
233 |
The filenames returned are '/tmp/stxtmp_xx_nn' where xx is our
|
|
234 |
unix process id, and nn is a unique number, incremented with every
|
|
235 |
call to this method."
|
276
|
236 |
|
422
|
237 |
|pid tempDir nm|
|
276
|
238 |
|
|
239 |
NextTempFilenameIndex isNil ifTrue:[
|
|
240 |
NextTempFilenameIndex := 1.
|
|
241 |
].
|
422
|
242 |
tempDir := OperatingSystem getEnvironment:'TMPDIR'.
|
|
243 |
tempDir isNil ifTrue:[
|
|
244 |
tempDir := '/tmp'
|
|
245 |
].
|
|
246 |
|
276
|
247 |
"
|
|
248 |
the following has to be made OS independent ...
|
|
249 |
"
|
|
250 |
pid := OperatingSystem getProcessId printString.
|
422
|
251 |
nm := tempDir , '/stxtmp_' , pid , '_' , NextTempFilenameIndex printString.
|
276
|
252 |
NextTempFilenameIndex := NextTempFilenameIndex + 1.
|
|
253 |
^ self named:nm
|
|
254 |
|
|
255 |
"
|
422
|
256 |
Filename newTemporary
|
|
257 |
Filename newTemporary
|
276
|
258 |
"
|
422
|
259 |
|
|
260 |
"Modified: 7.9.1995 / 10:48:31 / claus"
|
276
|
261 |
!
|
|
262 |
|
216
|
263 |
fromUser
|
|
264 |
"show a box to enter a filename. Return a filename instance or
|
|
265 |
nil (if cancel was pressed)."
|
|
266 |
|
|
267 |
|name|
|
|
268 |
|
|
269 |
name := FileSelectionBox requestFilename.
|
276
|
270 |
name notNil ifTrue:[^ self named:name].
|
216
|
271 |
^ nil
|
276
|
272 |
|
|
273 |
"
|
|
274 |
Filename fromUser
|
|
275 |
"
|
1
|
276 |
! !
|
|
277 |
|
2
|
278 |
!Filename class methodsFor:'queries'!
|
|
279 |
|
|
280 |
separator
|
92
|
281 |
"return the file/directory separator.
|
|
282 |
Usually, this is $/ for unix-like systems
|
|
283 |
and $\ for dos-like ones (there may be more in the future)."
|
2
|
284 |
|
|
285 |
^ OperatingSystem fileSeparator
|
38
|
286 |
|
92
|
287 |
"
|
|
288 |
Filename separator
|
|
289 |
"
|
174
|
290 |
!
|
|
291 |
|
422
|
292 |
suffixSeparator
|
|
293 |
"return the filename suffix separator.
|
|
294 |
Usually, this is $. for unix-like and msdos systems
|
|
295 |
(there is currently no knowns system, where this differs)"
|
|
296 |
|
|
297 |
^ $.
|
|
298 |
|
|
299 |
"
|
|
300 |
Filename suffixSeparator
|
|
301 |
"
|
|
302 |
|
|
303 |
"Modified: 7.9.1995 / 11:10:43 / claus"
|
|
304 |
!
|
|
305 |
|
216
|
306 |
parentDirectoryName
|
|
307 |
"return the name used for the parent directory.
|
|
308 |
This is '..' for unix and dos-like systems.
|
|
309 |
(there may be more in the future."
|
|
310 |
|
|
311 |
^ OperatingSystem parentDirectoryName
|
|
312 |
|
|
313 |
"
|
|
314 |
Filename parentDirectoryName
|
|
315 |
"
|
|
316 |
!
|
|
317 |
|
174
|
318 |
isCaseSensitive
|
|
319 |
"return true, if filenames are case sensitive.
|
|
320 |
We ask the OS about this, to be independent here."
|
|
321 |
|
216
|
322 |
^ OperatingSystem caseSensitiveFilenames
|
326
|
323 |
!
|
|
324 |
|
|
325 |
defaultClass
|
|
326 |
"ST-80 compatibility:
|
422
|
327 |
in ST-80, different subclasses of Filename are used for different
|
|
328 |
OperatingSystems; defaultClass is supposed to return an appropriate class.
|
326
|
329 |
Since in ST/X, there is (currently) only one Filename class, return it here."
|
|
330 |
|
|
331 |
^ self
|
328
|
332 |
!
|
|
333 |
|
|
334 |
localNameStringFrom:aString
|
|
335 |
"ST-80 compatibility.
|
422
|
336 |
what does this do ? (used in FileNavigator-goody).
|
|
337 |
GUESS: does it strip off the voulume-character on MSDOS systems ?"
|
328
|
338 |
|
|
339 |
(aString startsWith:'/') ifTrue:[
|
|
340 |
^ aString copyFrom:2
|
|
341 |
].
|
|
342 |
^ aString
|
422
|
343 |
|
|
344 |
"Modified: 7.9.1995 / 10:44:56 / claus"
|
328
|
345 |
!
|
|
346 |
|
|
347 |
volumes
|
|
348 |
"ST-80 compatibility.
|
422
|
349 |
what does this do ? (used in FileNavigator-goody).
|
|
350 |
GUESS: does it return the available drives on MSDOS systems ?"
|
328
|
351 |
|
356
|
352 |
^ #('/')
|
422
|
353 |
|
|
354 |
"Modified: 7.9.1995 / 10:45:25 / claus"
|
440
|
355 |
!
|
|
356 |
|
|
357 |
filenameCompletionFor:aString directory:inDirectory directoriesOnly:directoriesOnly filesOnly:filesOnly ifMultiple:aBlock
|
|
358 |
"perform filename completion on aString in some directory;
|
|
359 |
return the longest matching filename prefix as a string.
|
|
360 |
If directoriesOnly and filesOnly are true, only directories and files
|
|
361 |
are considered respectively. If multiple files match, the exception
|
|
362 |
block aBlock is evaluated with a filename representing the directory
|
|
363 |
(where the match was done) as argument.
|
|
364 |
(this may be different from the inDirectory argument, if aString is absolute
|
|
365 |
or starts with ../)"
|
|
366 |
|
|
367 |
|s f matchSet nMatch name words dir|
|
|
368 |
|
|
369 |
s := aString.
|
|
370 |
"
|
|
371 |
find the last word ...
|
|
372 |
"
|
|
373 |
words := s asCollectionOfWords.
|
|
374 |
words size == 0 ifTrue:[
|
|
375 |
^ aBlock value:'.' asFilename
|
|
376 |
].
|
|
377 |
|
|
378 |
f := words last asFilename.
|
|
379 |
|
|
380 |
matchSet := f filenameCompletionIn:inDirectory.
|
|
381 |
|
|
382 |
dir := f directory.
|
|
383 |
|
|
384 |
directoriesOnly ifTrue:[
|
|
385 |
matchSet := matchSet select:[:aFilename |
|
|
386 |
(dir construct:aFilename) isDirectory
|
|
387 |
].
|
|
388 |
] ifFalse:[
|
|
389 |
filesOnly ifTrue:[
|
|
390 |
matchSet := matchSet select:[:aFilename |
|
|
391 |
(dir construct:aFilename) isDirectory not
|
|
392 |
].
|
|
393 |
]
|
|
394 |
].
|
|
395 |
|
|
396 |
(nMatch := matchSet size) ~~ 1 ifTrue:[
|
|
397 |
"
|
|
398 |
more than one possible completion -
|
|
399 |
"
|
|
400 |
aBlock value:f
|
|
401 |
].
|
|
402 |
"
|
|
403 |
even with more than one possible completion,
|
|
404 |
f's name is now the common prefix
|
|
405 |
"
|
|
406 |
name := f asString.
|
|
407 |
nMatch == 1 ifTrue:[
|
|
408 |
"
|
|
409 |
exactly one possible completion -
|
|
410 |
"
|
|
411 |
f := dir construct:matchSet first.
|
|
412 |
|
|
413 |
directoriesOnly ifFalse:[
|
|
414 |
f isDirectory ifTrue:[
|
|
415 |
(name endsWith:(Filename separator)) ifFalse:[
|
|
416 |
name := name , '/'
|
|
417 |
].
|
|
418 |
].
|
|
419 |
]
|
|
420 |
].
|
|
421 |
|
|
422 |
"
|
|
423 |
construct new contents, by taking
|
|
424 |
last words completion
|
|
425 |
"
|
|
426 |
s := ''.
|
|
427 |
1 to:(words size - 1) do:[:idx |
|
|
428 |
s := s , (words at:idx) , ' '
|
|
429 |
].
|
|
430 |
s := s , name.
|
|
431 |
|
|
432 |
"/ special: if there was no change, and the string represented
|
|
433 |
"/ is a directories name, add a directory separator
|
|
434 |
s = aString ifTrue:[
|
|
435 |
(s endsWith:Filename separator) ifFalse:[
|
|
436 |
s asFilename isDirectory ifTrue:[
|
|
437 |
^ s , Filename separator asString
|
|
438 |
]
|
|
439 |
]
|
|
440 |
].
|
|
441 |
|
|
442 |
^ s
|
2
|
443 |
! !
|
|
444 |
|
|
445 |
!Filename methodsFor:'instance creation'!
|
|
446 |
|
249
|
447 |
constructString:subname
|
38
|
448 |
"taking the receiver as a directory name, construct a new
|
249
|
449 |
filenames string for an entry within this directory (i.e. for a file
|
38
|
450 |
or a subdirectory in that directory)."
|
|
451 |
|
276
|
452 |
|sepString|
|
202
|
453 |
|
|
454 |
sepString := self class separator asString.
|
|
455 |
nameString = sepString ifTrue:[
|
159
|
456 |
"I am the root"
|
276
|
457 |
^ sepString , subname
|
85
|
458 |
].
|
422
|
459 |
^ nameString , sepString , subname asString
|
38
|
460 |
|
85
|
461 |
"
|
249
|
462 |
'/tmp' asFilename constructString:'foo'
|
|
463 |
'/' asFilename constructString:'foo'
|
|
464 |
'/usr/tmp' asFilename constructString:'foo'
|
328
|
465 |
'/foo/bar' asFilename constructString:'baz'
|
249
|
466 |
"
|
422
|
467 |
|
|
468 |
"Modified: 7.9.1995 / 10:15:22 / claus"
|
249
|
469 |
!
|
|
470 |
|
|
471 |
construct:subname
|
|
472 |
"taking the receiver as a directory name, construct a new
|
|
473 |
filename for an entry within this directory (i.e. for a file
|
|
474 |
or a subdirectory in that directory)."
|
|
475 |
|
276
|
476 |
^ (self constructString:subname) asFilename
|
249
|
477 |
|
|
478 |
"
|
|
479 |
'/tmp' asFilename construct:'foo'
|
|
480 |
'/' asFilename construct:'foo'
|
|
481 |
'/usr/tmp' asFilename construct:'foo'
|
328
|
482 |
'/foo/bar' asFilename construct:'baz'
|
85
|
483 |
"
|
2
|
484 |
! !
|
|
485 |
|
195
|
486 |
!Filename methodsFor:'misc'!
|
|
487 |
|
|
488 |
, aString
|
|
489 |
"this allows filenames to understand how names are concatenated.
|
308
|
490 |
Returns a string consisting of the receivers name, concatenated
|
195
|
491 |
by aString. Notice this is NOT the same as construct:, which inserts
|
308
|
492 |
a directory delimiter and returns a new fileName instance."
|
195
|
493 |
|
308
|
494 |
^ (nameString , aString asString)
|
195
|
495 |
|
|
496 |
"
|
|
497 |
'Makefile' asFilename , '.bak'
|
|
498 |
'Makefile' asFilename construct:'.bak'
|
|
499 |
"
|
|
500 |
! !
|
|
501 |
|
174
|
502 |
!Filename methodsFor:'comparing'!
|
|
503 |
|
|
504 |
hash
|
|
505 |
"return an integer useful as a hash-key"
|
|
506 |
|
|
507 |
^ nameString hash
|
|
508 |
!
|
|
509 |
|
|
510 |
= aFilename
|
276
|
511 |
"return true, if the argument represents the same filename"
|
|
512 |
|
|
513 |
|str|
|
174
|
514 |
|
|
515 |
self species == aFilename species ifTrue:[
|
276
|
516 |
str := aFilename asString.
|
174
|
517 |
self class isCaseSensitive ifTrue:[
|
276
|
518 |
^ nameString = str
|
174
|
519 |
].
|
276
|
520 |
^ nameString sameAs:str
|
174
|
521 |
].
|
|
522 |
^ false
|
|
523 |
! !
|
|
524 |
|
1
|
525 |
!Filename methodsFor:'converting'!
|
|
526 |
|
|
527 |
asString
|
38
|
528 |
"return the receiver converted to a string"
|
|
529 |
|
68
|
530 |
^ nameString
|
1
|
531 |
!
|
|
532 |
|
|
533 |
asFilename
|
68
|
534 |
"return the receiver converted to a filename."
|
38
|
535 |
|
68
|
536 |
"Thats pretty easy here :-)"
|
1
|
537 |
^ self
|
195
|
538 |
!
|
|
539 |
|
328
|
540 |
asAbsoluteFilename
|
|
541 |
"return the receiver converted to a filename with
|
|
542 |
an absolute pathname."
|
|
543 |
|
|
544 |
^ self pathName asFilename
|
|
545 |
!
|
|
546 |
|
195
|
547 |
makeLegalFilename
|
|
548 |
"convert the receveivers name to be a legal filename.
|
|
549 |
The implementation may change in the future to be more
|
|
550 |
OS specific."
|
|
551 |
|
276
|
552 |
"
|
|
553 |
actually, in Unix spaces are allowed - but it makes life
|
|
554 |
so hard; therefore, replace them by underscores ...
|
|
555 |
"
|
195
|
556 |
nameString replaceAll:(Character space) by:$_.
|
|
557 |
"
|
|
558 |
need more - especially on SYS5.3 type systems,
|
|
559 |
we may want to contract the fileName to 14 characters.
|
|
560 |
"
|
|
561 |
^ self
|
|
562 |
|
|
563 |
"
|
|
564 |
'hello world' asFilename makeLegalFilename
|
|
565 |
"
|
1
|
566 |
! !
|
|
567 |
|
202
|
568 |
!Filename methodsFor:'private accessing'!
|
1
|
569 |
|
68
|
570 |
setName:aString
|
38
|
571 |
"set the filename"
|
|
572 |
|
68
|
573 |
nameString := aString
|
1
|
574 |
! !
|
|
575 |
|
2
|
576 |
!Filename methodsFor:'queries'!
|
|
577 |
|
159
|
578 |
separator
|
|
579 |
"return the directory-separator character (or string)"
|
|
580 |
|
|
581 |
^ self class separator
|
|
582 |
!
|
|
583 |
|
422
|
584 |
directoryPathName
|
|
585 |
"return the full directory pathname part of the file/directory as a string.
|
|
586 |
- thats the full pathname of the directory where the file/dir represented by
|
|
587 |
the receiver is contained in.
|
|
588 |
See also: directoryName"
|
|
589 |
|
|
590 |
^ OperatingSystem directoryNameOf:(self pathName)
|
|
591 |
|
|
592 |
"
|
|
593 |
'/foo/bar/' asFilename directoryPathName
|
|
594 |
'/foo/bar' asFilename directoryPathName
|
|
595 |
'.' asFilename directoryPathName
|
|
596 |
'..' asFilename directoryPathName
|
|
597 |
'../..' asFilename directoryPathName
|
|
598 |
"
|
|
599 |
|
|
600 |
"Modified: 7.9.1995 / 10:42:13 / claus"
|
|
601 |
!
|
|
602 |
|
328
|
603 |
directoryName
|
422
|
604 |
"return the directory name part of the file/directory as a string.
|
92
|
605 |
- thats the name of the directory where the file/dir represented by
|
422
|
606 |
the receiver is contained in.
|
|
607 |
See also: #directoryPathName"
|
328
|
608 |
|
|
609 |
^ OperatingSystem directoryNameOf:nameString "/ (self pathName)
|
92
|
610 |
|
328
|
611 |
"
|
|
612 |
'/foo/bar/' asFilename directoryName
|
|
613 |
'/foo/bar' asFilename directoryName
|
422
|
614 |
'bitmaps' asFilename directoryName
|
|
615 |
'bitmaps' asFilename directoryPathName
|
328
|
616 |
'.' asFilename directoryName
|
|
617 |
'..' asFilename directoryName
|
|
618 |
'../..' asFilename directoryName
|
422
|
619 |
'../..' asFilename directoryPathName
|
328
|
620 |
"
|
422
|
621 |
|
|
622 |
"Modified: 7.9.1995 / 10:42:03 / claus"
|
328
|
623 |
!
|
|
624 |
|
|
625 |
directory
|
422
|
626 |
"return the directory name part of the file/directory as a filename.
|
328
|
627 |
- thats a filename for the directory where the file/dir represented by
|
|
628 |
the receiver is contained in."
|
|
629 |
|
|
630 |
^ self directoryName asFilename
|
2
|
631 |
|
92
|
632 |
"
|
174
|
633 |
'/foo/bar' asFilename directory
|
|
634 |
'.' asFilename directory
|
|
635 |
'..' asFilename directory
|
|
636 |
'../..' asFilename directory
|
92
|
637 |
"
|
2
|
638 |
!
|
|
639 |
|
308
|
640 |
directoryContents
|
|
641 |
"return the contents of the directory as a collection of strings"
|
|
642 |
|
|
643 |
^ (FileDirectory directoryNamed:self asString) contents
|
|
644 |
|
|
645 |
"
|
|
646 |
'.' asFilename directoryContents
|
|
647 |
"
|
|
648 |
!
|
|
649 |
|
174
|
650 |
tail
|
422
|
651 |
"the files name without directory prefix as a string.
|
|
652 |
An alias for baseName, for ST-80 compatiblity."
|
174
|
653 |
|
|
654 |
^ self baseName
|
|
655 |
!
|
|
656 |
|
2
|
657 |
baseName
|
422
|
658 |
"return my baseName as a string.
|
92
|
659 |
- thats the file/directory name without leading parent-dirs."
|
2
|
660 |
|
328
|
661 |
^ OperatingSystem baseNameOf:nameString "/ (self pathName)
|
68
|
662 |
|
85
|
663 |
"
|
92
|
664 |
'/foo/bar' asFilename baseName
|
|
665 |
'.' asFilename baseName
|
|
666 |
'..' asFilename baseName
|
328
|
667 |
'../..' asFilename baseName
|
|
668 |
'../../libbasic' asFilename baseName
|
|
669 |
'../../libpr' asFilename baseName
|
422
|
670 |
'../../libbasic/Object.st' asFilename baseName
|
92
|
671 |
"
|
|
672 |
!
|
|
673 |
|
422
|
674 |
name
|
|
675 |
"return the name of the file represented by the receiver as a string.
|
|
676 |
This may or may not be a relative name.
|
|
677 |
See also: pathName"
|
|
678 |
|
|
679 |
self isAbsolute ifTrue:[^ self pathName].
|
|
680 |
^ nameString
|
|
681 |
|
|
682 |
"
|
|
683 |
'/foo/bar' asFilename name
|
|
684 |
'/foo/bar' asFilename pathName
|
|
685 |
'.' asFilename name
|
|
686 |
'.' asFilename pathName
|
|
687 |
'../..' asFilename name
|
|
688 |
'../..' asFilename pathName
|
|
689 |
'bitmaps' asFilename name
|
|
690 |
'bitmaps' asFilename pathName
|
|
691 |
'/tmp/../usr' asFilename name
|
|
692 |
'/tmp/../usr' asFilename pathName
|
|
693 |
"
|
|
694 |
|
|
695 |
"Modified: 7.9.1995 / 10:41:14 / claus"
|
|
696 |
!
|
|
697 |
|
92
|
698 |
pathName
|
422
|
699 |
"return the full pathname of the file represented by the receiver,
|
|
700 |
as a string. See also: name"
|
92
|
701 |
|
216
|
702 |
|parentName sep|
|
|
703 |
|
|
704 |
sep := self class separator.
|
|
705 |
(nameString startsWith:sep) ifTrue:[
|
|
706 |
parentName := self class parentDirectoryName.
|
357
|
707 |
(nameString findString:parentName) == 0 ifTrue:[
|
|
708 |
^ nameString
|
216
|
709 |
]
|
92
|
710 |
].
|
|
711 |
^ (FileDirectory directoryNamed:nameString) pathName
|
|
712 |
|
|
713 |
"
|
|
714 |
'/foo/bar' asFilename pathName
|
|
715 |
'.' asFilename pathName
|
|
716 |
'../..' asFilename pathName
|
422
|
717 |
'../..' asFilename name
|
357
|
718 |
'/tmp/../usr' asFilename pathName
|
85
|
719 |
"
|
422
|
720 |
|
|
721 |
"Modified: 7.9.1995 / 10:42:39 / claus"
|
68
|
722 |
!
|
|
723 |
|
|
724 |
isAbsolute
|
|
725 |
"return true, if the receiver represents an absolute pathname
|
92
|
726 |
(in contrast to one relative to the current directory)."
|
68
|
727 |
|
85
|
728 |
^ (nameString startsWith:self class separator)
|
|
729 |
|
|
730 |
"
|
92
|
731 |
'/foo/bar' asFilename isAbsolute
|
|
732 |
'..' asFilename isAbsolute
|
328
|
733 |
'..' asAbsoluteFilename isAbsolute
|
92
|
734 |
'source/SBrowser.st' asFilename isAbsolute
|
85
|
735 |
"
|
|
736 |
!
|
68
|
737 |
|
159
|
738 |
isRelative
|
|
739 |
"return true, if this name is interpreted relative to some
|
|
740 |
directory (opposite of absolute)"
|
|
741 |
|
|
742 |
^ self isAbsolute not
|
|
743 |
!
|
|
744 |
|
85
|
745 |
isDirectory
|
|
746 |
"return true, if the receiver represents an existing,
|
|
747 |
readable directories pathname."
|
|
748 |
|
|
749 |
^ OperatingSystem isDirectory:nameString
|
|
750 |
|
|
751 |
"
|
|
752 |
'/foo/bar' asFilename isDirectory
|
|
753 |
'/tmp' asFilename isDirectory
|
92
|
754 |
'Makefile' asFilename isDirectory
|
85
|
755 |
"
|
68
|
756 |
!
|
|
757 |
|
|
758 |
exists
|
92
|
759 |
"return true, if such a file exists."
|
68
|
760 |
|
|
761 |
^ OperatingSystem isValidPath:nameString
|
|
762 |
|
85
|
763 |
"
|
92
|
764 |
'/foo/bar' asFilename exists
|
|
765 |
'/tmp' asFilename exists
|
|
766 |
'Makefile' asFilename exists
|
85
|
767 |
"
|
77
|
768 |
!
|
|
769 |
|
|
770 |
isReadable
|
92
|
771 |
"return true, if such a file exists and is readable."
|
77
|
772 |
|
|
773 |
^ OperatingSystem isReadable:nameString
|
|
774 |
|
85
|
775 |
"
|
92
|
776 |
'/foo/bar' asFilename isReadable
|
|
777 |
'/tmp' asFilename isReadable
|
|
778 |
'Makefile' asFilename isReadable
|
85
|
779 |
"
|
77
|
780 |
!
|
|
781 |
|
159
|
782 |
canBeWritten
|
|
783 |
"same as isWritable - for ST-80 compatibility"
|
|
784 |
|
|
785 |
^ self isWritable
|
328
|
786 |
|
|
787 |
"
|
|
788 |
'/foo/bar' asFilename canBeWritten
|
|
789 |
'/tmp' asFilename canBeWritten
|
|
790 |
'Makefile' asFilename canBeWritten
|
|
791 |
"
|
159
|
792 |
!
|
|
793 |
|
77
|
794 |
isWritable
|
92
|
795 |
"return true, if such a file exists and is writable."
|
77
|
796 |
|
|
797 |
^ OperatingSystem isWritable:nameString
|
|
798 |
|
85
|
799 |
"
|
92
|
800 |
'/foo/bar' asFilename isWritable
|
|
801 |
'/tmp' asFilename isWritable
|
|
802 |
'Makefile' asFilename isWritable
|
|
803 |
"
|
|
804 |
!
|
|
805 |
|
|
806 |
isExecutable
|
|
807 |
"return true, if such a file exists and is executable (by Unix's definition).
|
|
808 |
For directories, true is returned if the directory can be entered.
|
|
809 |
See isExecutableProgram for a related check."
|
|
810 |
|
|
811 |
^ OperatingSystem isExecutable:nameString
|
|
812 |
|
|
813 |
"
|
|
814 |
'/foo/bar' asFilename isExecutable
|
|
815 |
'/tmp' asFilename isExecutable
|
|
816 |
'Makefile' asFilename isExecutable
|
|
817 |
'/bin/ls' asFilename isExecutable
|
|
818 |
"
|
|
819 |
!
|
|
820 |
|
|
821 |
isExecutableProgram
|
|
822 |
"return true, if such a file exists and is an executable program.
|
|
823 |
(i.e. for directories, false is returned.)"
|
|
824 |
|
|
825 |
^ (OperatingSystem isExecutable:nameString)
|
|
826 |
and:[(OperatingSystem isDirectory:nameString) not]
|
|
827 |
|
|
828 |
"
|
|
829 |
'/tmp' asFilename isExecutable
|
|
830 |
'/bin/ls' asFilename isExecutable
|
|
831 |
'/tmp' asFilename isExecutableProgram
|
|
832 |
'/bin/ls' asFilename isExecutableProgram
|
|
833 |
"
|
|
834 |
!
|
|
835 |
|
356
|
836 |
isSymbolicLink
|
|
837 |
"return true, if the file represented by the receiver is a symbolic
|
|
838 |
link. Notice that not all OS's support symbolic links; those that do
|
|
839 |
not will always return false."
|
|
840 |
|
|
841 |
^ OperatingSystem isSymbolicLink:nameString
|
|
842 |
|
|
843 |
"
|
|
844 |
'Make.proto' asFilename isSymbolicLink
|
|
845 |
'Makefile' asFilename isSymbolicLink
|
|
846 |
"
|
|
847 |
!
|
|
848 |
|
422
|
849 |
prefixAndSuffix
|
|
850 |
"return an array consisting of my prefix and suffix.
|
|
851 |
The suffix is the namepart after the final period character,
|
|
852 |
the prefix everything before, except for the period.
|
|
853 |
(on some systems, the suffix-character may be different from a period).
|
|
854 |
For example, foo.bar.baz has a prefix of 'foo.bar' and a suffix of '.baz'.
|
|
855 |
|
|
856 |
Notice: there is currently no known system which uses other than
|
|
857 |
the period character as suffixCharacter."
|
|
858 |
|
|
859 |
|nm idx|
|
|
860 |
|
|
861 |
nm := self baseName.
|
|
862 |
idx := nm lastIndexOf:(self class suffixSeparator).
|
|
863 |
idx == 0 ifTrue:[
|
|
864 |
^ Array with:nm with:''
|
|
865 |
].
|
|
866 |
^ Array
|
|
867 |
with:(nm copyTo:idx-1)
|
|
868 |
with:(nm copyFrom:idx+1)
|
|
869 |
|
|
870 |
"
|
|
871 |
'abc.st' asFilename prefixAndSuffix
|
|
872 |
'abc' asFilename prefixAndSuffix
|
|
873 |
'a.b.c' asFilename prefixAndSuffix
|
|
874 |
|
|
875 |
|parts|
|
|
876 |
parts := 'Object.st' asFilename prefixAndSuffix.
|
|
877 |
((parts at:1) , '.o') asFilename
|
|
878 |
"
|
|
879 |
|
|
880 |
"Modified: 7.9.1995 / 11:15:42 / claus"
|
|
881 |
!
|
|
882 |
|
|
883 |
suffix
|
|
884 |
"return my suffix.
|
|
885 |
The suffix is the namepart after the final period character,
|
|
886 |
or the empty string, if the name does not contain a period."
|
|
887 |
|
|
888 |
^ self prefixAndSuffix at:2
|
|
889 |
|
|
890 |
"
|
|
891 |
'abc.st' asFilename suffix
|
|
892 |
'abc' asFilename suffix
|
|
893 |
'a.b.c' asFilename suffix
|
|
894 |
"
|
|
895 |
|
|
896 |
"Modified: 7.9.1995 / 11:09:03 / claus"
|
|
897 |
!
|
|
898 |
|
328
|
899 |
filesMatching:aPattern
|
|
900 |
^ self directoryContents select:[:name | aPattern match:name]
|
|
901 |
|
|
902 |
"
|
|
903 |
Filename currentDirectory filesMatching:'M*'
|
|
904 |
"
|
|
905 |
!
|
|
906 |
|
92
|
907 |
filenameCompletion
|
440
|
908 |
"try to complete the recevier filename.
|
|
909 |
This method has both a return value and a side effect on the receiver:
|
|
910 |
it returns a collection of matching filename objects,
|
|
911 |
and leaves changes the receivers filename-string to the longest common
|
|
912 |
match.
|
|
913 |
If none matches, the returned collection is empty and the recevier is unchanged.
|
|
914 |
If there is only one match, the size of the returned collection is exactly 1,
|
|
915 |
containing the fully expanded filename and the receivers name is changed to it."
|
92
|
916 |
|
440
|
917 |
^ self filenameCompletionIn:nil
|
216
|
918 |
|
440
|
919 |
"
|
|
920 |
'mak' asFilename filenameCompletion
|
|
921 |
'Make' asFilename filenameCompletion
|
|
922 |
'Makef' asFilename filenameCompletion;yourself
|
|
923 |
'/u' asFilename filenameCompletion
|
|
924 |
'../../libpr' asFilename inspect filenameCompletion
|
|
925 |
"
|
|
926 |
!
|
92
|
927 |
|
440
|
928 |
filenameCompletionIn:aDirectory
|
|
929 |
"try to complete the recevier filename.
|
|
930 |
This method has both a return value and a side effect on the receiver:
|
|
931 |
it returns a collection of matching filename objects,
|
|
932 |
and leaves changes the receivers filename-string to the longest common
|
|
933 |
match.
|
|
934 |
If none matches, the returned collection is empty and the recevier is unchanged.
|
|
935 |
If there is only one match, the size of the returned collection is exactly 1,
|
|
936 |
containing the fully expanded filename and the receivers name is changed to it."
|
|
937 |
|
|
938 |
|dir baseName matching matchLen try allMatching
|
|
939 |
sepString parentString prefix nMatch|
|
|
940 |
|
|
941 |
sepString := self class separator asString.
|
|
942 |
(nameString endsWith:sepString) ifTrue:[
|
|
943 |
^ #()
|
328
|
944 |
].
|
|
945 |
|
440
|
946 |
parentString := self class parentDirectoryName.
|
|
947 |
baseName := self baseName.
|
|
948 |
baseName ~= nameString ifTrue:[
|
|
949 |
prefix := self directoryName.
|
|
950 |
].
|
|
951 |
|
441
|
952 |
self isAbsolute ifTrue:[
|
440
|
953 |
dir := self directory
|
|
954 |
] ifFalse:[
|
441
|
955 |
aDirectory isNil ifTrue:[
|
|
956 |
dir := self directory
|
|
957 |
] ifFalse:[
|
|
958 |
dir := (aDirectory construct:nameString) directory
|
|
959 |
]
|
328
|
960 |
].
|
216
|
961 |
|
92
|
962 |
matching := OrderedCollection new.
|
440
|
963 |
dir directoryContents do:[:fileName |
|
|
964 |
((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[
|
|
965 |
(fileName startsWith:baseName) ifTrue:[
|
216
|
966 |
matching add:fileName
|
|
967 |
]
|
159
|
968 |
]
|
92
|
969 |
].
|
440
|
970 |
(nMatch := matching size) > 1 ifTrue:[
|
|
971 |
"
|
|
972 |
find the longest common prefix
|
|
973 |
"
|
|
974 |
matchLen := baseName size.
|
|
975 |
matchLen > matching first size ifTrue:[
|
|
976 |
try := baseName.
|
|
977 |
allMatching := false
|
|
978 |
] ifFalse:[
|
|
979 |
try := matching first copyTo:matchLen.
|
|
980 |
allMatching := true.
|
|
981 |
].
|
|
982 |
|
|
983 |
[allMatching] whileTrue:[
|
|
984 |
matching do:[:aName |
|
|
985 |
(aName startsWith:try) ifFalse:[
|
|
986 |
allMatching := false
|
|
987 |
]
|
|
988 |
].
|
|
989 |
allMatching ifTrue:[
|
|
990 |
matchLen < matching first size ifTrue:[
|
|
991 |
matchLen := matchLen + 1.
|
|
992 |
try := matching first copyTo:matchLen.
|
|
993 |
] ifFalse:[
|
|
994 |
allMatching := false
|
|
995 |
]
|
|
996 |
] ifFalse:[
|
|
997 |
try := matching first copyTo:matchLen - 1.
|
|
998 |
]
|
|
999 |
].
|
|
1000 |
"
|
|
1001 |
and set my name to the last full match
|
|
1002 |
"
|
|
1003 |
nameString := try
|
|
1004 |
].
|
|
1005 |
|
92
|
1006 |
"
|
440
|
1007 |
if I had a directory-prefix, change names in collection ...
|
|
1008 |
"
|
|
1009 |
prefix notNil ifTrue:[
|
|
1010 |
prefix = '/' ifTrue:[
|
|
1011 |
"/ avoid introducing double slashes
|
|
1012 |
prefix := ''
|
159
|
1013 |
].
|
440
|
1014 |
matching := matching collect:[:n | prefix , '/' , n].
|
|
1015 |
nMatch == 1 ifTrue:[
|
|
1016 |
nameString := matching first
|
|
1017 |
] ifFalse:[
|
|
1018 |
nMatch > 1 ifTrue:[
|
|
1019 |
nameString := prefix , '/' , nameString
|
159
|
1020 |
]
|
|
1021 |
]
|
92
|
1022 |
] ifFalse:[
|
440
|
1023 |
nMatch == 1 ifTrue:[
|
|
1024 |
nameString := matching first
|
159
|
1025 |
]
|
92
|
1026 |
].
|
440
|
1027 |
|
92
|
1028 |
"
|
|
1029 |
return the match-set, so caller can decide what to do
|
|
1030 |
(i.e. show the matches, output a warning etc ...)
|
|
1031 |
"
|
|
1032 |
^ matching
|
|
1033 |
|
440
|
1034 |
" trivial cases:
|
|
1035 |
|
|
1036 |
'../' asFilename filenameCompletion
|
|
1037 |
'/' asFilename filenameCompletion
|
|
1038 |
'/usr/' asFilename filenameCompletion
|
|
1039 |
|
|
1040 |
'mak' asFilename filenameCompletion
|
|
1041 |
'Make' asFilename filenameCompletion
|
|
1042 |
'Makef' asFilename filenameCompletion
|
|
1043 |
'/u' asFilename filenameCompletion
|
|
1044 |
'../../libpr' asFilename filenameCompletion
|
85
|
1045 |
"
|
2
|
1046 |
! !
|
|
1047 |
|
174
|
1048 |
!Filename methodsFor:'file queries'!
|
|
1049 |
|
|
1050 |
info
|
|
1051 |
"return the files info; that is a collection of file attributes,
|
|
1052 |
(actually a dictionary) where the keys are #type, #uid, #gid, #size etc.
|
356
|
1053 |
The actual amount and detail returned may depend on the OS used.
|
|
1054 |
On unix, if you ask for the info of a symbolic link, the target
|
359
|
1055 |
files info is returned. (see also: #linkInfo)
|
356
|
1056 |
|
|
1057 |
On unix, the contents is:
|
|
1058 |
id -> the inode number (integer)
|
|
1059 |
uid -> the numeric user id of the files owner
|
|
1060 |
gid -> the numeric group id of the files owner
|
|
1061 |
statusChanged -> the absoluteTime when the files status changed last
|
|
1062 |
(i.e. protection change, owner change etc.)
|
|
1063 |
accessed -> the absoluteTime when the file was last accessed
|
|
1064 |
modified -> the absoluteTime when the file was last modified
|
|
1065 |
size -> the size (in bytes) of the file
|
|
1066 |
type -> the files type (#regular, #directory, #characterSpecial)
|
|
1067 |
mode -> the files access protection bits (rwxrwxrwx mask).
|
|
1068 |
|
|
1069 |
The minimum returned info (i.e. on all OS's) will consist of at least:
|
|
1070 |
modified
|
|
1071 |
size
|
|
1072 |
type
|
|
1073 |
|
|
1074 |
Some OS's (VMS) may return more info.
|
|
1075 |
|
|
1076 |
Dont expect things like uid/gid/mode to be there; write your application
|
|
1077 |
to either handle the cases where info-entries are not present,
|
359
|
1078 |
or (better) use one of isXXXX query methods. (Be prepared for DOS ...)
|
356
|
1079 |
"
|
174
|
1080 |
|
|
1081 |
^ OperatingSystem infoOf:nameString
|
328
|
1082 |
|
|
1083 |
"
|
|
1084 |
Filename currentDirectory info
|
356
|
1085 |
'/dev/null' asFilename info
|
328
|
1086 |
'Make.proto' asFilename info
|
356
|
1087 |
'source/Point.st' asFilename info
|
359
|
1088 |
'source/Point.st' asFilename linkInfo
|
356
|
1089 |
'../../libbasic/Point.st' asFilename info
|
|
1090 |
"
|
|
1091 |
!
|
|
1092 |
|
|
1093 |
linkInfo
|
|
1094 |
"return the files info if its a symbolic link; nil otherwise.
|
|
1095 |
On OS's which do not support symbolic links, nil is always returned.
|
359
|
1096 |
The information is the same as returned by #info, except that if the
|
|
1097 |
receiver represents a symbolic link, the links information
|
|
1098 |
is returned
|
|
1099 |
(while in this case, #info returns the info of the target file,
|
|
1100 |
which is accessed via the symbolic link).
|
356
|
1101 |
|
|
1102 |
In addition to the normal entries, Unix returns an additional entry:
|
|
1103 |
path -> the target files pathname
|
|
1104 |
|
|
1105 |
See the comment in #info for more details."
|
|
1106 |
|
|
1107 |
^ OperatingSystem linkInfoOf:nameString
|
|
1108 |
|
|
1109 |
"
|
|
1110 |
Filename currentDirectory linkInfo
|
|
1111 |
'/dev/null' asFilename linkInfo
|
|
1112 |
'Make.proto' asFilename linkInfo
|
|
1113 |
'Make.proto' asFilename linkInfo at:#path
|
|
1114 |
'source/Point.st' asFilename linkInfo
|
|
1115 |
'../../libbasic/Point.st' asFilename linkInfo
|
328
|
1116 |
"
|
|
1117 |
!
|
|
1118 |
|
|
1119 |
dates
|
|
1120 |
"return the files modification and access times as an object (currently a dictionary)
|
356
|
1121 |
that responds to the at: message with arguments
|
|
1122 |
#modified, #accessed or #statusChanged."
|
328
|
1123 |
|
|
1124 |
|info dates|
|
|
1125 |
|
|
1126 |
info := OperatingSystem infoOf:nameString.
|
|
1127 |
info isNil ifTrue:[
|
|
1128 |
info := OperatingSystem linkInfoOf:nameString.
|
|
1129 |
info isNil ifTrue:[
|
|
1130 |
^ nil
|
|
1131 |
]
|
|
1132 |
].
|
|
1133 |
dates := IdentityDictionary new.
|
|
1134 |
dates at:#modified put:(info at:#modified).
|
|
1135 |
dates at:#accessed put:(info at:#accessed).
|
|
1136 |
dates at:#statusChanged put:(info at:#statusChanged).
|
|
1137 |
^ dates
|
|
1138 |
|
|
1139 |
"
|
|
1140 |
Filename currentDirectory dates
|
|
1141 |
'../regression' asFilename dates
|
|
1142 |
"
|
174
|
1143 |
!
|
|
1144 |
|
|
1145 |
fileSize
|
|
1146 |
"return the size of the file in bytes"
|
|
1147 |
|
|
1148 |
|info|
|
|
1149 |
|
|
1150 |
info := self info.
|
|
1151 |
info isNil ifTrue:[^ nil].
|
|
1152 |
^ info at:#size
|
|
1153 |
!
|
|
1154 |
|
|
1155 |
fileType
|
|
1156 |
"this returns a string describing the type of contents of
|
|
1157 |
the file. This is done using the unix 'file' command,
|
|
1158 |
(which usually is configurable by /etc/magic).
|
|
1159 |
On non-unix systems, this may return an empty string, not knowning
|
|
1160 |
about the contents."
|
|
1161 |
|
|
1162 |
|stream typeString|
|
|
1163 |
|
|
1164 |
typeString := ''.
|
|
1165 |
stream := PipeStream readingFrom:('file ' , self pathName).
|
|
1166 |
stream notNil ifTrue:[
|
|
1167 |
typeString := stream contents asString.
|
|
1168 |
stream close.
|
|
1169 |
typeString := typeString copyFrom:(typeString indexOf:$:) + 1.
|
|
1170 |
typeString := typeString withoutSeparators
|
|
1171 |
] ifFalse:[
|
|
1172 |
"
|
|
1173 |
could add some fallback code here, for systems, where no
|
|
1174 |
file command is avaliable ...
|
|
1175 |
... or at least analyze directory info.
|
|
1176 |
"
|
|
1177 |
].
|
|
1178 |
^ typeString
|
|
1179 |
|
|
1180 |
"
|
|
1181 |
'Makefile' asFilename fileType
|
325
|
1182 |
'.' asFilename fileType
|
174
|
1183 |
'/dev/null' asFilename fileType
|
|
1184 |
'smalltalk.rc' asFilename fileType
|
325
|
1185 |
'bitmaps/SBrowser.xbm' asFilename fileType
|
174
|
1186 |
"
|
|
1187 |
! !
|
|
1188 |
|
|
1189 |
!Filename methodsFor:'file operations'!
|
1
|
1190 |
|
159
|
1191 |
delete
|
|
1192 |
"remove the file - same as remove, for ST-80 compatibility"
|
|
1193 |
|
|
1194 |
^ self remove
|
|
1195 |
!
|
|
1196 |
|
92
|
1197 |
remove
|
192
|
1198 |
"remove the file - the argument must be convertable to a String.
|
|
1199 |
Return true if sucessfull, false if not."
|
92
|
1200 |
|
|
1201 |
^ OperatingSystem removeFile:nameString
|
|
1202 |
|
|
1203 |
"
|
|
1204 |
(FileStream newFileNamed:'foo') close.
|
|
1205 |
'foo' asFilename remove
|
|
1206 |
"
|
|
1207 |
!
|
|
1208 |
|
2
|
1209 |
renameTo:newName
|
192
|
1210 |
"rename the file - the argument must be convertable to a String.
|
|
1211 |
Return true if sucessfull, false if not."
|
2
|
1212 |
|
68
|
1213 |
^ OperatingSystem renameFile:nameString to:(newName asString)
|
2
|
1214 |
|
92
|
1215 |
"
|
|
1216 |
'/tmp/foo' asFilename renameTo:'/tmp/bar'
|
|
1217 |
"
|
2
|
1218 |
!
|
|
1219 |
|
68
|
1220 |
copyTo:newName
|
159
|
1221 |
"copy the file - the argument must be convertable to a filename"
|
|
1222 |
|
|
1223 |
|inStream outStream buffer bufferSize count|
|
1
|
1224 |
|
159
|
1225 |
bufferSize := 8 * 1024.
|
|
1226 |
buffer := ByteArray new:bufferSize.
|
|
1227 |
inStream := self readStream.
|
|
1228 |
outStream := newName asFilename writeStream.
|
|
1229 |
(inStream isNil or:[outStream isNil]) ifTrue:[
|
|
1230 |
^ self error:'file copy failed'
|
|
1231 |
].
|
|
1232 |
|
|
1233 |
[inStream atEnd] whileFalse:[
|
|
1234 |
count := inStream nextBytes:bufferSize into:buffer.
|
|
1235 |
outStream nextPutBytes:count from:buffer.
|
|
1236 |
].
|
|
1237 |
outStream close.
|
195
|
1238 |
inStream close.
|
159
|
1239 |
|
|
1240 |
"
|
|
1241 |
'Makefile' asFilename copyTo:'Makefile.foo'
|
|
1242 |
'smalltalk' asFilename copyTo:'/dev/null'
|
|
1243 |
"
|
1
|
1244 |
!
|
|
1245 |
|
174
|
1246 |
moveTo:newName
|
|
1247 |
"copy the file represented by the receiver, then delete it.
|
195
|
1248 |
This is different to renaming in case of cross device moves."
|
174
|
1249 |
|
|
1250 |
self copyTo:newName.
|
|
1251 |
self remove
|
|
1252 |
!
|
|
1253 |
|
|
1254 |
makeDirectory
|
|
1255 |
"create a directory with the receivers name"
|
|
1256 |
|
|
1257 |
OperatingSystem createDirectory:nameString
|
|
1258 |
!
|
|
1259 |
|
|
1260 |
addAccessRights:aCollection
|
|
1261 |
"add the access rights as specified in aCollection for the file represented
|
|
1262 |
by the receiver. The argument must be a collection of symbols,
|
|
1263 |
such as #readUser, #writeGroup etc."
|
|
1264 |
|
|
1265 |
|access|
|
|
1266 |
|
|
1267 |
access := OperatingSystem accessModeOf:nameString.
|
|
1268 |
aCollection do:[:accessSymbol |
|
|
1269 |
access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol).
|
|
1270 |
].
|
|
1271 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
1272 |
|
|
1273 |
"
|
|
1274 |
'foo' asFilename writeStream close.
|
|
1275 |
'foo' asFilename addAccessRights:#(readUser readGroup readOthers).
|
|
1276 |
'foo' asFilename addAccessRights:#(writeUser writeGroup writeOthers).
|
|
1277 |
'foo' asFilename addAccessRights:#(executeUser executeGroup executeOthers).
|
|
1278 |
"
|
|
1279 |
!
|
|
1280 |
|
|
1281 |
removeAccessRights:aCollection
|
|
1282 |
"remove the access rights as specified in aCollection for the file represented
|
|
1283 |
by the receiver. The argument must be a collection of symbols,
|
|
1284 |
such as #readUser, #writeGroup etc."
|
|
1285 |
|
|
1286 |
|access|
|
|
1287 |
|
|
1288 |
access := OperatingSystem accessModeOf:nameString.
|
|
1289 |
aCollection do:[:accessSymbol |
|
|
1290 |
access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert.
|
|
1291 |
].
|
|
1292 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
1293 |
|
|
1294 |
"
|
|
1295 |
'foo' asFilename writeStream close.
|
|
1296 |
'foo' asFilename removeAccessRights:#(readUser readGroup readOthers).
|
|
1297 |
'foo' asFilename removeAccessRights:#(writeUser writeGroup writeOthers).
|
|
1298 |
'foo' asFilename removeAccessRights:#(executeUser executeGroup executeOthers).
|
|
1299 |
"
|
|
1300 |
!
|
|
1301 |
|
|
1302 |
makeReadableForAll
|
|
1303 |
"make the file readable for all - you must have permission to do so."
|
|
1304 |
|
|
1305 |
self addAccessRights:#(readUser readGroup readOthers)
|
|
1306 |
!
|
|
1307 |
|
|
1308 |
makeReadable
|
|
1309 |
"make the file readable for the owner - you must have permission to do so."
|
|
1310 |
|
|
1311 |
self addAccessRights:#(readUser)
|
|
1312 |
!
|
|
1313 |
|
|
1314 |
makeWritable
|
|
1315 |
"make the file writableable for all - you must have permission to do so."
|
|
1316 |
|
|
1317 |
self addAccessRights:#(writeUser)
|
|
1318 |
!
|
|
1319 |
|
|
1320 |
makeWritableForAll
|
|
1321 |
"make the file writable for all - you must have permission to do so."
|
|
1322 |
|
|
1323 |
self addAccessRights:#(writeUser writeGroup writeOthers)
|
|
1324 |
!
|
|
1325 |
|
|
1326 |
makeUnwritable
|
|
1327 |
"make the file unwritable for all - you must have permission to do so."
|
|
1328 |
|
|
1329 |
self removeAccessRights:#(writeUser writeGroup writeOthers)
|
|
1330 |
! !
|
|
1331 |
|
|
1332 |
!Filename methodsFor:'file utilities'!
|
|
1333 |
|
1
|
1334 |
fileIn
|
2
|
1335 |
"load smalltalk code from the file"
|
|
1336 |
|
|
1337 |
^ self readStream fileIn
|
1
|
1338 |
!
|
|
1339 |
|
174
|
1340 |
edit
|
|
1341 |
"start an editView on the file represented by the receiver"
|
|
1342 |
|
|
1343 |
EditTextView openOn:self asString
|
276
|
1344 |
|
|
1345 |
"
|
|
1346 |
'smalltalk.rc' asFilename edit
|
|
1347 |
"
|
174
|
1348 |
!
|
|
1349 |
|
|
1350 |
contentsOfEntireFile
|
|
1351 |
"return the contents of the file as a string"
|
|
1352 |
|
|
1353 |
|s contents|
|
|
1354 |
|
|
1355 |
s := self readStream.
|
|
1356 |
[
|
|
1357 |
contents := s contents
|
|
1358 |
] valueNowOrOnUnwindDo:[s close].
|
|
1359 |
^ contents
|
|
1360 |
! !
|
|
1361 |
|
|
1362 |
!Filename methodsFor:'file access'!
|
|
1363 |
|
77
|
1364 |
newReadWriteStream
|
325
|
1365 |
"return a stream for read/write the file represented by the receiver.
|
|
1366 |
If the file does not already exist, it is created."
|
77
|
1367 |
|
|
1368 |
^ FileStream newFileNamed:nameString
|
|
1369 |
!
|
|
1370 |
|
|
1371 |
readWriteStream
|
325
|
1372 |
"return a stream for read/write the file represented by the receiver.
|
|
1373 |
If the file does not already exist, nil is returned."
|
77
|
1374 |
|
|
1375 |
^ FileStream oldFileNamed:nameString
|
|
1376 |
!
|
|
1377 |
|
1
|
1378 |
readStream
|
325
|
1379 |
"return a stream for reading from the file represented by the receiver.
|
|
1380 |
If the file does not already exist, nil is returned."
|
2
|
1381 |
|
68
|
1382 |
^ FileStream readonlyFileNamed:nameString
|
325
|
1383 |
|
|
1384 |
"
|
|
1385 |
'/tmp/foo' asFilename readStream
|
|
1386 |
"
|
1
|
1387 |
!
|
|
1388 |
|
|
1389 |
writeStream
|
325
|
1390 |
"return a stream for writing to the file represented by the receiver.
|
|
1391 |
If the file does not already exist, it is created."
|
2
|
1392 |
|
92
|
1393 |
^ FileStream newFileForWritingNamed:nameString
|
325
|
1394 |
|
|
1395 |
"
|
|
1396 |
'/tmp/foo' asFilename writeStream
|
|
1397 |
"
|
92
|
1398 |
!
|
|
1399 |
|
|
1400 |
appendingWriteStream
|
325
|
1401 |
"return a stream for appending to the file represented by the receiver.
|
|
1402 |
If the file does not already exist, it is created."
|
92
|
1403 |
|
|
1404 |
^ FileStream appendingOldFileNamed:nameString
|
1
|
1405 |
! !
|
92
|
1406 |
|
|
1407 |
!Filename methodsFor:'printing & storing'!
|
|
1408 |
|
|
1409 |
storeOn:aStream
|
174
|
1410 |
"append a printed representation of the receiver to aStream,
|
|
1411 |
which allows reconstructing it via readFrom:"
|
|
1412 |
|
92
|
1413 |
aStream nextPut:$(.
|
|
1414 |
nameString storeOn:aStream.
|
|
1415 |
aStream nextPutAll:' asFilename)'
|
|
1416 |
!
|
|
1417 |
|
|
1418 |
printOn:aStream
|
174
|
1419 |
"append a printed representation of the receiver to aStream."
|
|
1420 |
|
92
|
1421 |
aStream nextPutAll:'FileName('''.
|
|
1422 |
nameString printOn:aStream.
|
|
1423 |
aStream nextPutAll:''')'
|
|
1424 |
! !
|