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 |
|
360
|
23 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.29 1995-07-02 01:06:48 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 |
"
|
360
|
44 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.29 1995-07-02 01:06:48 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 |
|
|
237 |
|pid nm|
|
|
238 |
|
|
239 |
NextTempFilenameIndex isNil ifTrue:[
|
|
240 |
NextTempFilenameIndex := 1.
|
|
241 |
].
|
|
242 |
"
|
|
243 |
the following has to be made OS independent ...
|
|
244 |
"
|
|
245 |
pid := OperatingSystem getProcessId printString.
|
|
246 |
nm := '/tmp/stxtmp_' , pid , '_' , NextTempFilenameIndex printString.
|
|
247 |
NextTempFilenameIndex := NextTempFilenameIndex + 1.
|
|
248 |
^ self named:nm
|
|
249 |
|
|
250 |
"
|
|
251 |
Filename newTemporary
|
|
252 |
Filename newTemporary
|
|
253 |
"
|
|
254 |
!
|
|
255 |
|
216
|
256 |
fromUser
|
|
257 |
"show a box to enter a filename. Return a filename instance or
|
|
258 |
nil (if cancel was pressed)."
|
|
259 |
|
|
260 |
|name|
|
|
261 |
|
|
262 |
name := FileSelectionBox requestFilename.
|
276
|
263 |
name notNil ifTrue:[^ self named:name].
|
216
|
264 |
^ nil
|
276
|
265 |
|
|
266 |
"
|
|
267 |
Filename fromUser
|
|
268 |
"
|
1
|
269 |
! !
|
|
270 |
|
2
|
271 |
!Filename class methodsFor:'queries'!
|
|
272 |
|
|
273 |
separator
|
92
|
274 |
"return the file/directory separator.
|
|
275 |
Usually, this is $/ for unix-like systems
|
|
276 |
and $\ for dos-like ones (there may be more in the future)."
|
2
|
277 |
|
|
278 |
^ OperatingSystem fileSeparator
|
38
|
279 |
|
92
|
280 |
"
|
|
281 |
Filename separator
|
|
282 |
"
|
174
|
283 |
!
|
|
284 |
|
216
|
285 |
parentDirectoryName
|
|
286 |
"return the name used for the parent directory.
|
|
287 |
This is '..' for unix and dos-like systems.
|
|
288 |
(there may be more in the future."
|
|
289 |
|
|
290 |
^ OperatingSystem parentDirectoryName
|
|
291 |
|
|
292 |
"
|
|
293 |
Filename parentDirectoryName
|
|
294 |
"
|
|
295 |
!
|
|
296 |
|
174
|
297 |
isCaseSensitive
|
|
298 |
"return true, if filenames are case sensitive.
|
|
299 |
We ask the OS about this, to be independent here."
|
|
300 |
|
216
|
301 |
^ OperatingSystem caseSensitiveFilenames
|
326
|
302 |
!
|
|
303 |
|
|
304 |
defaultClass
|
|
305 |
"ST-80 compatibility:
|
|
306 |
in ST-80, different subclasses of FIlename are used for different
|
|
307 |
OperatingSYstems; defaultClass is supposed to return an appropriate class.
|
|
308 |
Since in ST/X, there is (currently) only one Filename class, return it here."
|
|
309 |
|
|
310 |
^ self
|
328
|
311 |
!
|
|
312 |
|
|
313 |
localNameStringFrom:aString
|
|
314 |
"ST-80 compatibility.
|
|
315 |
what does this do ? (used in FileNavigator-goody)"
|
|
316 |
|
|
317 |
(aString startsWith:'/') ifTrue:[
|
|
318 |
^ aString copyFrom:2
|
|
319 |
].
|
|
320 |
^ aString
|
|
321 |
!
|
|
322 |
|
|
323 |
volumes
|
|
324 |
"ST-80 compatibility.
|
|
325 |
what does this do ? (used in FileNavigator-goody)"
|
|
326 |
|
356
|
327 |
^ #('/')
|
2
|
328 |
! !
|
|
329 |
|
|
330 |
!Filename methodsFor:'instance creation'!
|
|
331 |
|
249
|
332 |
constructString:subname
|
38
|
333 |
"taking the receiver as a directory name, construct a new
|
249
|
334 |
filenames string for an entry within this directory (i.e. for a file
|
38
|
335 |
or a subdirectory in that directory)."
|
|
336 |
|
276
|
337 |
|sepString|
|
202
|
338 |
|
|
339 |
sepString := self class separator asString.
|
|
340 |
nameString = sepString ifTrue:[
|
159
|
341 |
"I am the root"
|
276
|
342 |
^ sepString , subname
|
85
|
343 |
].
|
276
|
344 |
^ nameString , sepString , subname
|
38
|
345 |
|
85
|
346 |
"
|
249
|
347 |
'/tmp' asFilename constructString:'foo'
|
|
348 |
'/' asFilename constructString:'foo'
|
|
349 |
'/usr/tmp' asFilename constructString:'foo'
|
328
|
350 |
'/foo/bar' asFilename constructString:'baz'
|
249
|
351 |
"
|
|
352 |
!
|
|
353 |
|
|
354 |
construct:subname
|
|
355 |
"taking the receiver as a directory name, construct a new
|
|
356 |
filename for an entry within this directory (i.e. for a file
|
|
357 |
or a subdirectory in that directory)."
|
|
358 |
|
276
|
359 |
^ (self constructString:subname) asFilename
|
249
|
360 |
|
|
361 |
"
|
|
362 |
'/tmp' asFilename construct:'foo'
|
|
363 |
'/' asFilename construct:'foo'
|
|
364 |
'/usr/tmp' asFilename construct:'foo'
|
328
|
365 |
'/foo/bar' asFilename construct:'baz'
|
85
|
366 |
"
|
2
|
367 |
! !
|
|
368 |
|
195
|
369 |
!Filename methodsFor:'misc'!
|
|
370 |
|
|
371 |
, aString
|
|
372 |
"this allows filenames to understand how names are concatenated.
|
308
|
373 |
Returns a string consisting of the receivers name, concatenated
|
195
|
374 |
by aString. Notice this is NOT the same as construct:, which inserts
|
308
|
375 |
a directory delimiter and returns a new fileName instance."
|
195
|
376 |
|
308
|
377 |
^ (nameString , aString asString)
|
195
|
378 |
|
|
379 |
"
|
|
380 |
'Makefile' asFilename , '.bak'
|
|
381 |
'Makefile' asFilename construct:'.bak'
|
|
382 |
"
|
|
383 |
! !
|
|
384 |
|
174
|
385 |
!Filename methodsFor:'comparing'!
|
|
386 |
|
|
387 |
hash
|
|
388 |
"return an integer useful as a hash-key"
|
|
389 |
|
|
390 |
^ nameString hash
|
|
391 |
!
|
|
392 |
|
|
393 |
= aFilename
|
276
|
394 |
"return true, if the argument represents the same filename"
|
|
395 |
|
|
396 |
|str|
|
174
|
397 |
|
|
398 |
self species == aFilename species ifTrue:[
|
276
|
399 |
str := aFilename asString.
|
174
|
400 |
self class isCaseSensitive ifTrue:[
|
276
|
401 |
^ nameString = str
|
174
|
402 |
].
|
276
|
403 |
^ nameString sameAs:str
|
174
|
404 |
].
|
|
405 |
^ false
|
|
406 |
! !
|
|
407 |
|
1
|
408 |
!Filename methodsFor:'converting'!
|
|
409 |
|
|
410 |
asString
|
38
|
411 |
"return the receiver converted to a string"
|
|
412 |
|
68
|
413 |
^ nameString
|
1
|
414 |
!
|
|
415 |
|
|
416 |
asFilename
|
68
|
417 |
"return the receiver converted to a filename."
|
38
|
418 |
|
68
|
419 |
"Thats pretty easy here :-)"
|
1
|
420 |
^ self
|
195
|
421 |
!
|
|
422 |
|
328
|
423 |
asAbsoluteFilename
|
|
424 |
"return the receiver converted to a filename with
|
|
425 |
an absolute pathname."
|
|
426 |
|
|
427 |
^ self pathName asFilename
|
|
428 |
!
|
|
429 |
|
195
|
430 |
makeLegalFilename
|
|
431 |
"convert the receveivers name to be a legal filename.
|
|
432 |
The implementation may change in the future to be more
|
|
433 |
OS specific."
|
|
434 |
|
276
|
435 |
"
|
|
436 |
actually, in Unix spaces are allowed - but it makes life
|
|
437 |
so hard; therefore, replace them by underscores ...
|
|
438 |
"
|
195
|
439 |
nameString replaceAll:(Character space) by:$_.
|
|
440 |
"
|
|
441 |
need more - especially on SYS5.3 type systems,
|
|
442 |
we may want to contract the fileName to 14 characters.
|
|
443 |
"
|
|
444 |
^ self
|
|
445 |
|
|
446 |
"
|
|
447 |
'hello world' asFilename makeLegalFilename
|
|
448 |
"
|
1
|
449 |
! !
|
|
450 |
|
202
|
451 |
!Filename methodsFor:'private accessing'!
|
1
|
452 |
|
68
|
453 |
setName:aString
|
38
|
454 |
"set the filename"
|
|
455 |
|
68
|
456 |
nameString := aString
|
1
|
457 |
! !
|
|
458 |
|
2
|
459 |
!Filename methodsFor:'queries'!
|
|
460 |
|
159
|
461 |
separator
|
|
462 |
"return the directory-separator character (or string)"
|
|
463 |
|
|
464 |
^ self class separator
|
|
465 |
!
|
|
466 |
|
328
|
467 |
directoryName
|
174
|
468 |
"return the directory name part of the file/directory.
|
92
|
469 |
- thats the name of the directory where the file/dir represented by
|
328
|
470 |
the receiver is contained in."
|
|
471 |
|
|
472 |
^ OperatingSystem directoryNameOf:nameString "/ (self pathName)
|
92
|
473 |
|
328
|
474 |
"
|
|
475 |
'/foo/bar/' asFilename directoryName
|
|
476 |
'/foo/bar' asFilename directoryName
|
|
477 |
'.' asFilename directoryName
|
|
478 |
'..' asFilename directoryName
|
|
479 |
'../..' asFilename directoryName
|
|
480 |
"
|
|
481 |
!
|
|
482 |
|
|
483 |
directory
|
|
484 |
"return the directory name part of the file/directory.
|
|
485 |
- thats a filename for the directory where the file/dir represented by
|
|
486 |
the receiver is contained in."
|
|
487 |
|
|
488 |
^ self directoryName asFilename
|
2
|
489 |
|
92
|
490 |
"
|
174
|
491 |
'/foo/bar' asFilename directory
|
|
492 |
'.' asFilename directory
|
|
493 |
'..' asFilename directory
|
|
494 |
'../..' asFilename directory
|
92
|
495 |
"
|
2
|
496 |
!
|
|
497 |
|
308
|
498 |
directoryContents
|
|
499 |
"return the contents of the directory as a collection of strings"
|
|
500 |
|
|
501 |
^ (FileDirectory directoryNamed:self asString) contents
|
|
502 |
|
|
503 |
"
|
|
504 |
'.' asFilename directoryContents
|
|
505 |
"
|
|
506 |
!
|
|
507 |
|
174
|
508 |
tail
|
|
509 |
"the files name without directory prefix. For ST-80 compatiblity."
|
|
510 |
|
|
511 |
^ self baseName
|
|
512 |
!
|
|
513 |
|
2
|
514 |
baseName
|
|
515 |
"return my baseName
|
92
|
516 |
- thats the file/directory name without leading parent-dirs."
|
2
|
517 |
|
328
|
518 |
^ OperatingSystem baseNameOf:nameString "/ (self pathName)
|
68
|
519 |
|
85
|
520 |
"
|
92
|
521 |
'/foo/bar' asFilename baseName
|
|
522 |
'.' asFilename baseName
|
|
523 |
'..' asFilename baseName
|
328
|
524 |
'../..' asFilename baseName
|
|
525 |
'../../libbasic' asFilename baseName
|
|
526 |
'../../libpr' asFilename baseName
|
92
|
527 |
"
|
|
528 |
!
|
|
529 |
|
|
530 |
pathName
|
|
531 |
"return the full pathname of the file represented by the receiver."
|
|
532 |
|
216
|
533 |
|parentName sep|
|
|
534 |
|
|
535 |
sep := self class separator.
|
|
536 |
(nameString startsWith:sep) ifTrue:[
|
|
537 |
parentName := self class parentDirectoryName.
|
357
|
538 |
(nameString findString:parentName) == 0 ifTrue:[
|
|
539 |
^ nameString
|
216
|
540 |
]
|
92
|
541 |
].
|
|
542 |
^ (FileDirectory directoryNamed:nameString) pathName
|
|
543 |
|
|
544 |
"
|
|
545 |
'/foo/bar' asFilename pathName
|
|
546 |
'.' asFilename pathName
|
|
547 |
'../..' asFilename pathName
|
357
|
548 |
'/tmp/../usr' asFilename pathName
|
85
|
549 |
"
|
68
|
550 |
!
|
|
551 |
|
|
552 |
isAbsolute
|
|
553 |
"return true, if the receiver represents an absolute pathname
|
92
|
554 |
(in contrast to one relative to the current directory)."
|
68
|
555 |
|
85
|
556 |
^ (nameString startsWith:self class separator)
|
|
557 |
|
|
558 |
"
|
92
|
559 |
'/foo/bar' asFilename isAbsolute
|
|
560 |
'..' asFilename isAbsolute
|
328
|
561 |
'..' asAbsoluteFilename isAbsolute
|
92
|
562 |
'source/SBrowser.st' asFilename isAbsolute
|
85
|
563 |
"
|
|
564 |
!
|
68
|
565 |
|
159
|
566 |
isRelative
|
|
567 |
"return true, if this name is interpreted relative to some
|
|
568 |
directory (opposite of absolute)"
|
|
569 |
|
|
570 |
^ self isAbsolute not
|
|
571 |
!
|
|
572 |
|
85
|
573 |
isDirectory
|
|
574 |
"return true, if the receiver represents an existing,
|
|
575 |
readable directories pathname."
|
|
576 |
|
|
577 |
^ OperatingSystem isDirectory:nameString
|
|
578 |
|
|
579 |
"
|
|
580 |
'/foo/bar' asFilename isDirectory
|
|
581 |
'/tmp' asFilename isDirectory
|
92
|
582 |
'Makefile' asFilename isDirectory
|
85
|
583 |
"
|
68
|
584 |
!
|
|
585 |
|
|
586 |
exists
|
92
|
587 |
"return true, if such a file exists."
|
68
|
588 |
|
|
589 |
^ OperatingSystem isValidPath:nameString
|
|
590 |
|
85
|
591 |
"
|
92
|
592 |
'/foo/bar' asFilename exists
|
|
593 |
'/tmp' asFilename exists
|
|
594 |
'Makefile' asFilename exists
|
85
|
595 |
"
|
77
|
596 |
!
|
|
597 |
|
|
598 |
isReadable
|
92
|
599 |
"return true, if such a file exists and is readable."
|
77
|
600 |
|
|
601 |
^ OperatingSystem isReadable:nameString
|
|
602 |
|
85
|
603 |
"
|
92
|
604 |
'/foo/bar' asFilename isReadable
|
|
605 |
'/tmp' asFilename isReadable
|
|
606 |
'Makefile' asFilename isReadable
|
85
|
607 |
"
|
77
|
608 |
!
|
|
609 |
|
159
|
610 |
canBeWritten
|
|
611 |
"same as isWritable - for ST-80 compatibility"
|
|
612 |
|
|
613 |
^ self isWritable
|
328
|
614 |
|
|
615 |
"
|
|
616 |
'/foo/bar' asFilename canBeWritten
|
|
617 |
'/tmp' asFilename canBeWritten
|
|
618 |
'Makefile' asFilename canBeWritten
|
|
619 |
"
|
159
|
620 |
!
|
|
621 |
|
77
|
622 |
isWritable
|
92
|
623 |
"return true, if such a file exists and is writable."
|
77
|
624 |
|
|
625 |
^ OperatingSystem isWritable:nameString
|
|
626 |
|
85
|
627 |
"
|
92
|
628 |
'/foo/bar' asFilename isWritable
|
|
629 |
'/tmp' asFilename isWritable
|
|
630 |
'Makefile' asFilename isWritable
|
|
631 |
"
|
|
632 |
!
|
|
633 |
|
|
634 |
isExecutable
|
|
635 |
"return true, if such a file exists and is executable (by Unix's definition).
|
|
636 |
For directories, true is returned if the directory can be entered.
|
|
637 |
See isExecutableProgram for a related check."
|
|
638 |
|
|
639 |
^ OperatingSystem isExecutable:nameString
|
|
640 |
|
|
641 |
"
|
|
642 |
'/foo/bar' asFilename isExecutable
|
|
643 |
'/tmp' asFilename isExecutable
|
|
644 |
'Makefile' asFilename isExecutable
|
|
645 |
'/bin/ls' asFilename isExecutable
|
|
646 |
"
|
|
647 |
!
|
|
648 |
|
|
649 |
isExecutableProgram
|
|
650 |
"return true, if such a file exists and is an executable program.
|
|
651 |
(i.e. for directories, false is returned.)"
|
|
652 |
|
|
653 |
^ (OperatingSystem isExecutable:nameString)
|
|
654 |
and:[(OperatingSystem isDirectory:nameString) not]
|
|
655 |
|
|
656 |
"
|
|
657 |
'/tmp' asFilename isExecutable
|
|
658 |
'/bin/ls' asFilename isExecutable
|
|
659 |
'/tmp' asFilename isExecutableProgram
|
|
660 |
'/bin/ls' asFilename isExecutableProgram
|
|
661 |
"
|
|
662 |
!
|
|
663 |
|
356
|
664 |
isSymbolicLink
|
|
665 |
"return true, if the file represented by the receiver is a symbolic
|
|
666 |
link. Notice that not all OS's support symbolic links; those that do
|
|
667 |
not will always return false."
|
|
668 |
|
|
669 |
^ OperatingSystem isSymbolicLink:nameString
|
|
670 |
|
|
671 |
"
|
|
672 |
'Make.proto' asFilename isSymbolicLink
|
|
673 |
'Makefile' asFilename isSymbolicLink
|
|
674 |
"
|
|
675 |
!
|
|
676 |
|
328
|
677 |
filesMatching:aPattern
|
|
678 |
^ self directoryContents select:[:name | aPattern match:name]
|
|
679 |
|
|
680 |
"
|
|
681 |
Filename currentDirectory filesMatching:'M*'
|
|
682 |
"
|
|
683 |
!
|
|
684 |
|
92
|
685 |
filenameCompletion
|
|
686 |
"try to complete the filename. This method has both a side effect,
|
|
687 |
and a return value:
|
159
|
688 |
it returns a collection of matching filename strings,
|
|
689 |
and sets (as side effect) the receivers filename to the longest common
|
|
690 |
match.
|
92
|
691 |
(i.e. if the size of the returned collection is exactly 1,
|
|
692 |
the completion has succeeded and the receivers name has been changed to
|
|
693 |
that.
|
328
|
694 |
If the returned collection is empty, nothing matched and the receivers
|
|
695 |
names is unchanged.
|
92
|
696 |
If the size of the returned collection is greater than one, the receivers
|
|
697 |
filename-string has been set to the longest common filename-prefix)"
|
|
698 |
|
328
|
699 |
|dir name matching matchLen try allMatching sep parent prefix|
|
216
|
700 |
|
|
701 |
sep := self class separator asString.
|
|
702 |
parent := self class parentDirectoryName.
|
92
|
703 |
|
174
|
704 |
dir := self directory.
|
328
|
705 |
prefix := parent , sep.
|
216
|
706 |
(nameString endsWith:sep) ifTrue:[
|
|
707 |
name := ''
|
|
708 |
] ifFalse:[
|
|
709 |
name := self baseName.
|
|
710 |
].
|
328
|
711 |
[name startsWith:prefix] whileTrue:[
|
|
712 |
self halt.
|
|
713 |
dir := dir directory.
|
|
714 |
name := name copyFrom:(prefix size + 1)
|
|
715 |
].
|
|
716 |
|
|
717 |
dir := dir asString.
|
|
718 |
name = parent ifTrue:[
|
|
719 |
^ dir asFilename filenameCompletion
|
|
720 |
].
|
216
|
721 |
|
92
|
722 |
matching := OrderedCollection new.
|
|
723 |
(FileDirectory directoryNamed:dir) do:[:fileName |
|
216
|
724 |
((fileName ~= '.') and:[fileName ~= parent]) ifTrue:[
|
|
725 |
(fileName startsWith:name) ifTrue:[
|
|
726 |
matching add:fileName
|
|
727 |
]
|
159
|
728 |
]
|
92
|
729 |
].
|
|
730 |
"
|
|
731 |
if there is only one, change my name ...
|
|
732 |
"
|
|
733 |
matching size == 1 ifTrue:[
|
216
|
734 |
dir = sep ifTrue:[
|
159
|
735 |
dir := ''
|
|
736 |
].
|
216
|
737 |
nameString := dir , sep , matching first.
|
159
|
738 |
matching first = name ifTrue:[
|
|
739 |
self isDirectory ifTrue:[
|
|
740 |
nameString := nameString , self class separator asString
|
|
741 |
]
|
|
742 |
]
|
92
|
743 |
] ifFalse:[
|
159
|
744 |
matching size > 1 ifTrue:[
|
|
745 |
"
|
|
746 |
find the longest common prefix
|
|
747 |
"
|
|
748 |
matchLen := name size.
|
|
749 |
matchLen > matching first size ifTrue:[
|
|
750 |
try := name.
|
|
751 |
allMatching := false
|
|
752 |
] ifFalse:[
|
|
753 |
try := matching first copyTo:matchLen.
|
|
754 |
allMatching := true.
|
|
755 |
].
|
92
|
756 |
|
159
|
757 |
[allMatching] whileTrue:[
|
|
758 |
matching do:[:aName |
|
|
759 |
(aName startsWith:try) ifFalse:[
|
|
760 |
allMatching := false
|
|
761 |
]
|
|
762 |
].
|
|
763 |
allMatching ifTrue:[
|
|
764 |
matchLen < matching first size ifTrue:[
|
|
765 |
matchLen := matchLen + 1.
|
|
766 |
try := matching first copyTo:matchLen.
|
|
767 |
] ifFalse:[
|
|
768 |
allMatching := false
|
|
769 |
]
|
|
770 |
] ifFalse:[
|
|
771 |
try := matching first copyTo:matchLen - 1.
|
|
772 |
]
|
|
773 |
].
|
|
774 |
"
|
|
775 |
and set my name to the last full match
|
|
776 |
"
|
216
|
777 |
dir = sep ifTrue:[
|
159
|
778 |
dir := ''
|
|
779 |
].
|
216
|
780 |
nameString := dir , sep , try
|
159
|
781 |
]
|
92
|
782 |
].
|
|
783 |
"
|
|
784 |
return the match-set, so caller can decide what to do
|
|
785 |
(i.e. show the matches, output a warning etc ...)
|
|
786 |
"
|
|
787 |
^ matching
|
|
788 |
|
|
789 |
"
|
|
790 |
'Make' asFilename filenameCompletion;yourself
|
|
791 |
'Makef' asFilename filenameCompletion;yourself
|
|
792 |
'/u' asFilename filenameCompletion
|
328
|
793 |
'../../libpr' asFilename filenameCompletion
|
85
|
794 |
"
|
2
|
795 |
! !
|
|
796 |
|
174
|
797 |
!Filename methodsFor:'file queries'!
|
|
798 |
|
|
799 |
info
|
|
800 |
"return the files info; that is a collection of file attributes,
|
|
801 |
(actually a dictionary) where the keys are #type, #uid, #gid, #size etc.
|
356
|
802 |
The actual amount and detail returned may depend on the OS used.
|
|
803 |
On unix, if you ask for the info of a symbolic link, the target
|
359
|
804 |
files info is returned. (see also: #linkInfo)
|
356
|
805 |
|
|
806 |
On unix, the contents is:
|
|
807 |
id -> the inode number (integer)
|
|
808 |
uid -> the numeric user id of the files owner
|
|
809 |
gid -> the numeric group id of the files owner
|
|
810 |
statusChanged -> the absoluteTime when the files status changed last
|
|
811 |
(i.e. protection change, owner change etc.)
|
|
812 |
accessed -> the absoluteTime when the file was last accessed
|
|
813 |
modified -> the absoluteTime when the file was last modified
|
|
814 |
size -> the size (in bytes) of the file
|
|
815 |
type -> the files type (#regular, #directory, #characterSpecial)
|
|
816 |
mode -> the files access protection bits (rwxrwxrwx mask).
|
|
817 |
|
|
818 |
The minimum returned info (i.e. on all OS's) will consist of at least:
|
|
819 |
modified
|
|
820 |
size
|
|
821 |
type
|
|
822 |
|
|
823 |
Some OS's (VMS) may return more info.
|
|
824 |
|
|
825 |
Dont expect things like uid/gid/mode to be there; write your application
|
|
826 |
to either handle the cases where info-entries are not present,
|
359
|
827 |
or (better) use one of isXXXX query methods. (Be prepared for DOS ...)
|
356
|
828 |
"
|
174
|
829 |
|
|
830 |
^ OperatingSystem infoOf:nameString
|
328
|
831 |
|
|
832 |
"
|
|
833 |
Filename currentDirectory info
|
356
|
834 |
'/dev/null' asFilename info
|
328
|
835 |
'Make.proto' asFilename info
|
356
|
836 |
'source/Point.st' asFilename info
|
359
|
837 |
'source/Point.st' asFilename linkInfo
|
356
|
838 |
'../../libbasic/Point.st' asFilename info
|
|
839 |
"
|
|
840 |
!
|
|
841 |
|
|
842 |
linkInfo
|
|
843 |
"return the files info if its a symbolic link; nil otherwise.
|
|
844 |
On OS's which do not support symbolic links, nil is always returned.
|
359
|
845 |
The information is the same as returned by #info, except that if the
|
|
846 |
receiver represents a symbolic link, the links information
|
|
847 |
is returned
|
|
848 |
(while in this case, #info returns the info of the target file,
|
|
849 |
which is accessed via the symbolic link).
|
356
|
850 |
|
|
851 |
In addition to the normal entries, Unix returns an additional entry:
|
|
852 |
path -> the target files pathname
|
|
853 |
|
|
854 |
See the comment in #info for more details."
|
|
855 |
|
|
856 |
^ OperatingSystem linkInfoOf:nameString
|
|
857 |
|
|
858 |
"
|
|
859 |
Filename currentDirectory linkInfo
|
|
860 |
'/dev/null' asFilename linkInfo
|
|
861 |
'Make.proto' asFilename linkInfo
|
|
862 |
'Make.proto' asFilename linkInfo at:#path
|
|
863 |
'source/Point.st' asFilename linkInfo
|
|
864 |
'../../libbasic/Point.st' asFilename linkInfo
|
328
|
865 |
"
|
|
866 |
!
|
|
867 |
|
|
868 |
dates
|
|
869 |
"return the files modification and access times as an object (currently a dictionary)
|
356
|
870 |
that responds to the at: message with arguments
|
|
871 |
#modified, #accessed or #statusChanged."
|
328
|
872 |
|
|
873 |
|info dates|
|
|
874 |
|
|
875 |
info := OperatingSystem infoOf:nameString.
|
|
876 |
info isNil ifTrue:[
|
|
877 |
info := OperatingSystem linkInfoOf:nameString.
|
|
878 |
info isNil ifTrue:[
|
|
879 |
^ nil
|
|
880 |
]
|
|
881 |
].
|
|
882 |
dates := IdentityDictionary new.
|
|
883 |
dates at:#modified put:(info at:#modified).
|
|
884 |
dates at:#accessed put:(info at:#accessed).
|
|
885 |
dates at:#statusChanged put:(info at:#statusChanged).
|
|
886 |
^ dates
|
|
887 |
|
|
888 |
"
|
|
889 |
Filename currentDirectory dates
|
|
890 |
'../regression' asFilename dates
|
|
891 |
"
|
174
|
892 |
!
|
|
893 |
|
|
894 |
fileSize
|
|
895 |
"return the size of the file in bytes"
|
|
896 |
|
|
897 |
|info|
|
|
898 |
|
|
899 |
info := self info.
|
|
900 |
info isNil ifTrue:[^ nil].
|
|
901 |
^ info at:#size
|
|
902 |
!
|
|
903 |
|
|
904 |
fileType
|
|
905 |
"this returns a string describing the type of contents of
|
|
906 |
the file. This is done using the unix 'file' command,
|
|
907 |
(which usually is configurable by /etc/magic).
|
|
908 |
On non-unix systems, this may return an empty string, not knowning
|
|
909 |
about the contents."
|
|
910 |
|
|
911 |
|stream typeString|
|
|
912 |
|
|
913 |
typeString := ''.
|
|
914 |
stream := PipeStream readingFrom:('file ' , self pathName).
|
|
915 |
stream notNil ifTrue:[
|
|
916 |
typeString := stream contents asString.
|
|
917 |
stream close.
|
|
918 |
typeString := typeString copyFrom:(typeString indexOf:$:) + 1.
|
|
919 |
typeString := typeString withoutSeparators
|
|
920 |
] ifFalse:[
|
|
921 |
"
|
|
922 |
could add some fallback code here, for systems, where no
|
|
923 |
file command is avaliable ...
|
|
924 |
... or at least analyze directory info.
|
|
925 |
"
|
|
926 |
].
|
|
927 |
^ typeString
|
|
928 |
|
|
929 |
"
|
|
930 |
'Makefile' asFilename fileType
|
325
|
931 |
'.' asFilename fileType
|
174
|
932 |
'/dev/null' asFilename fileType
|
|
933 |
'smalltalk.rc' asFilename fileType
|
325
|
934 |
'bitmaps/SBrowser.xbm' asFilename fileType
|
174
|
935 |
"
|
|
936 |
! !
|
|
937 |
|
|
938 |
!Filename methodsFor:'file operations'!
|
1
|
939 |
|
159
|
940 |
delete
|
|
941 |
"remove the file - same as remove, for ST-80 compatibility"
|
|
942 |
|
|
943 |
^ self remove
|
|
944 |
!
|
|
945 |
|
92
|
946 |
remove
|
192
|
947 |
"remove the file - the argument must be convertable to a String.
|
|
948 |
Return true if sucessfull, false if not."
|
92
|
949 |
|
|
950 |
^ OperatingSystem removeFile:nameString
|
|
951 |
|
|
952 |
"
|
|
953 |
(FileStream newFileNamed:'foo') close.
|
|
954 |
'foo' asFilename remove
|
|
955 |
"
|
|
956 |
!
|
|
957 |
|
2
|
958 |
renameTo:newName
|
192
|
959 |
"rename the file - the argument must be convertable to a String.
|
|
960 |
Return true if sucessfull, false if not."
|
2
|
961 |
|
68
|
962 |
^ OperatingSystem renameFile:nameString to:(newName asString)
|
2
|
963 |
|
92
|
964 |
"
|
|
965 |
'/tmp/foo' asFilename renameTo:'/tmp/bar'
|
|
966 |
"
|
2
|
967 |
!
|
|
968 |
|
68
|
969 |
copyTo:newName
|
159
|
970 |
"copy the file - the argument must be convertable to a filename"
|
|
971 |
|
|
972 |
|inStream outStream buffer bufferSize count|
|
1
|
973 |
|
159
|
974 |
bufferSize := 8 * 1024.
|
|
975 |
buffer := ByteArray new:bufferSize.
|
|
976 |
inStream := self readStream.
|
|
977 |
outStream := newName asFilename writeStream.
|
|
978 |
(inStream isNil or:[outStream isNil]) ifTrue:[
|
|
979 |
^ self error:'file copy failed'
|
|
980 |
].
|
|
981 |
|
|
982 |
[inStream atEnd] whileFalse:[
|
|
983 |
count := inStream nextBytes:bufferSize into:buffer.
|
|
984 |
outStream nextPutBytes:count from:buffer.
|
|
985 |
].
|
|
986 |
outStream close.
|
195
|
987 |
inStream close.
|
159
|
988 |
|
|
989 |
"
|
|
990 |
'Makefile' asFilename copyTo:'Makefile.foo'
|
|
991 |
'smalltalk' asFilename copyTo:'/dev/null'
|
|
992 |
"
|
1
|
993 |
!
|
|
994 |
|
174
|
995 |
moveTo:newName
|
|
996 |
"copy the file represented by the receiver, then delete it.
|
195
|
997 |
This is different to renaming in case of cross device moves."
|
174
|
998 |
|
|
999 |
self copyTo:newName.
|
|
1000 |
self remove
|
|
1001 |
!
|
|
1002 |
|
|
1003 |
makeDirectory
|
|
1004 |
"create a directory with the receivers name"
|
|
1005 |
|
|
1006 |
OperatingSystem createDirectory:nameString
|
|
1007 |
!
|
|
1008 |
|
|
1009 |
addAccessRights:aCollection
|
|
1010 |
"add the access rights as specified in aCollection for the file represented
|
|
1011 |
by the receiver. The argument must be a collection of symbols,
|
|
1012 |
such as #readUser, #writeGroup etc."
|
|
1013 |
|
|
1014 |
|access|
|
|
1015 |
|
|
1016 |
access := OperatingSystem accessModeOf:nameString.
|
|
1017 |
aCollection do:[:accessSymbol |
|
|
1018 |
access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol).
|
|
1019 |
].
|
|
1020 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
1021 |
|
|
1022 |
"
|
|
1023 |
'foo' asFilename writeStream close.
|
|
1024 |
'foo' asFilename addAccessRights:#(readUser readGroup readOthers).
|
|
1025 |
'foo' asFilename addAccessRights:#(writeUser writeGroup writeOthers).
|
|
1026 |
'foo' asFilename addAccessRights:#(executeUser executeGroup executeOthers).
|
|
1027 |
"
|
|
1028 |
!
|
|
1029 |
|
|
1030 |
removeAccessRights:aCollection
|
|
1031 |
"remove the access rights as specified in aCollection for the file represented
|
|
1032 |
by the receiver. The argument must be a collection of symbols,
|
|
1033 |
such as #readUser, #writeGroup etc."
|
|
1034 |
|
|
1035 |
|access|
|
|
1036 |
|
|
1037 |
access := OperatingSystem accessModeOf:nameString.
|
|
1038 |
aCollection do:[:accessSymbol |
|
|
1039 |
access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert.
|
|
1040 |
].
|
|
1041 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
1042 |
|
|
1043 |
"
|
|
1044 |
'foo' asFilename writeStream close.
|
|
1045 |
'foo' asFilename removeAccessRights:#(readUser readGroup readOthers).
|
|
1046 |
'foo' asFilename removeAccessRights:#(writeUser writeGroup writeOthers).
|
|
1047 |
'foo' asFilename removeAccessRights:#(executeUser executeGroup executeOthers).
|
|
1048 |
"
|
|
1049 |
!
|
|
1050 |
|
|
1051 |
makeReadableForAll
|
|
1052 |
"make the file readable for all - you must have permission to do so."
|
|
1053 |
|
|
1054 |
self addAccessRights:#(readUser readGroup readOthers)
|
|
1055 |
!
|
|
1056 |
|
|
1057 |
makeReadable
|
|
1058 |
"make the file readable for the owner - you must have permission to do so."
|
|
1059 |
|
|
1060 |
self addAccessRights:#(readUser)
|
|
1061 |
!
|
|
1062 |
|
|
1063 |
makeWritable
|
|
1064 |
"make the file writableable for all - you must have permission to do so."
|
|
1065 |
|
|
1066 |
self addAccessRights:#(writeUser)
|
|
1067 |
!
|
|
1068 |
|
|
1069 |
makeWritableForAll
|
|
1070 |
"make the file writable for all - you must have permission to do so."
|
|
1071 |
|
|
1072 |
self addAccessRights:#(writeUser writeGroup writeOthers)
|
|
1073 |
!
|
|
1074 |
|
|
1075 |
makeUnwritable
|
|
1076 |
"make the file unwritable for all - you must have permission to do so."
|
|
1077 |
|
|
1078 |
self removeAccessRights:#(writeUser writeGroup writeOthers)
|
|
1079 |
! !
|
|
1080 |
|
|
1081 |
!Filename methodsFor:'file utilities'!
|
|
1082 |
|
1
|
1083 |
fileIn
|
2
|
1084 |
"load smalltalk code from the file"
|
|
1085 |
|
|
1086 |
^ self readStream fileIn
|
1
|
1087 |
!
|
|
1088 |
|
174
|
1089 |
edit
|
|
1090 |
"start an editView on the file represented by the receiver"
|
|
1091 |
|
|
1092 |
EditTextView openOn:self asString
|
276
|
1093 |
|
|
1094 |
"
|
|
1095 |
'smalltalk.rc' asFilename edit
|
|
1096 |
"
|
174
|
1097 |
!
|
|
1098 |
|
|
1099 |
contentsOfEntireFile
|
|
1100 |
"return the contents of the file as a string"
|
|
1101 |
|
|
1102 |
|s contents|
|
|
1103 |
|
|
1104 |
s := self readStream.
|
|
1105 |
[
|
|
1106 |
contents := s contents
|
|
1107 |
] valueNowOrOnUnwindDo:[s close].
|
|
1108 |
^ contents
|
|
1109 |
! !
|
|
1110 |
|
|
1111 |
!Filename methodsFor:'file access'!
|
|
1112 |
|
77
|
1113 |
newReadWriteStream
|
325
|
1114 |
"return a stream for read/write the file represented by the receiver.
|
|
1115 |
If the file does not already exist, it is created."
|
77
|
1116 |
|
|
1117 |
^ FileStream newFileNamed:nameString
|
|
1118 |
!
|
|
1119 |
|
|
1120 |
readWriteStream
|
325
|
1121 |
"return a stream for read/write the file represented by the receiver.
|
|
1122 |
If the file does not already exist, nil is returned."
|
77
|
1123 |
|
|
1124 |
^ FileStream oldFileNamed:nameString
|
|
1125 |
!
|
|
1126 |
|
1
|
1127 |
readStream
|
325
|
1128 |
"return a stream for reading from the file represented by the receiver.
|
|
1129 |
If the file does not already exist, nil is returned."
|
2
|
1130 |
|
68
|
1131 |
^ FileStream readonlyFileNamed:nameString
|
325
|
1132 |
|
|
1133 |
"
|
|
1134 |
'/tmp/foo' asFilename readStream
|
|
1135 |
"
|
1
|
1136 |
!
|
|
1137 |
|
|
1138 |
writeStream
|
325
|
1139 |
"return a stream for writing to the file represented by the receiver.
|
|
1140 |
If the file does not already exist, it is created."
|
2
|
1141 |
|
92
|
1142 |
^ FileStream newFileForWritingNamed:nameString
|
325
|
1143 |
|
|
1144 |
"
|
|
1145 |
'/tmp/foo' asFilename writeStream
|
|
1146 |
"
|
92
|
1147 |
!
|
|
1148 |
|
|
1149 |
appendingWriteStream
|
325
|
1150 |
"return a stream for appending to the file represented by the receiver.
|
|
1151 |
If the file does not already exist, it is created."
|
92
|
1152 |
|
|
1153 |
^ FileStream appendingOldFileNamed:nameString
|
1
|
1154 |
! !
|
92
|
1155 |
|
|
1156 |
!Filename methodsFor:'printing & storing'!
|
|
1157 |
|
|
1158 |
storeOn:aStream
|
174
|
1159 |
"append a printed representation of the receiver to aStream,
|
|
1160 |
which allows reconstructing it via readFrom:"
|
|
1161 |
|
92
|
1162 |
aStream nextPut:$(.
|
|
1163 |
nameString storeOn:aStream.
|
|
1164 |
aStream nextPutAll:' asFilename)'
|
|
1165 |
!
|
|
1166 |
|
|
1167 |
printOn:aStream
|
174
|
1168 |
"append a printed representation of the receiver to aStream."
|
|
1169 |
|
92
|
1170 |
aStream nextPutAll:'FileName('''.
|
|
1171 |
nameString printOn:aStream.
|
|
1172 |
aStream nextPutAll:''')'
|
|
1173 |
! !
|