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 |
|
276
|
23 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.21 1995-02-22 01:13:28 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 |
"
|
276
|
44 |
$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.21 1995-02-22 01:13:28 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 |
"
|
|
73 |
! !
|
1
|
74 |
|
|
75 |
!Filename class methodsFor:'instance creation'!
|
|
76 |
|
|
77 |
named:aString
|
77
|
78 |
"return a filename for a directory named aString."
|
38
|
79 |
|
68
|
80 |
^ (self basicNew) setName:aString
|
38
|
81 |
|
92
|
82 |
"
|
|
83 |
Filename named:'/tmp/fooBar'
|
|
84 |
"
|
216
|
85 |
!
|
|
86 |
|
276
|
87 |
currentDirectory
|
|
88 |
"return a filename for the current directory"
|
|
89 |
|
|
90 |
^ self named:(FileDirectory currentDirectory pathName)
|
|
91 |
|
|
92 |
"
|
|
93 |
Filename currentDirectory
|
|
94 |
"
|
|
95 |
!
|
|
96 |
|
|
97 |
newTemporary
|
|
98 |
"return a new unique filename - use this for temporary files"
|
|
99 |
|
|
100 |
|pid nm|
|
|
101 |
|
|
102 |
NextTempFilenameIndex isNil ifTrue:[
|
|
103 |
NextTempFilenameIndex := 1.
|
|
104 |
].
|
|
105 |
"
|
|
106 |
the following has to be made OS independent ...
|
|
107 |
"
|
|
108 |
pid := OperatingSystem getProcessId printString.
|
|
109 |
nm := '/tmp/stxtmp_' , pid , '_' , NextTempFilenameIndex printString.
|
|
110 |
NextTempFilenameIndex := NextTempFilenameIndex + 1.
|
|
111 |
^ self named:nm
|
|
112 |
|
|
113 |
"
|
|
114 |
Filename newTemporary
|
|
115 |
Filename newTemporary
|
|
116 |
"
|
|
117 |
!
|
|
118 |
|
216
|
119 |
fromUser
|
|
120 |
"show a box to enter a filename. Return a filename instance or
|
|
121 |
nil (if cancel was pressed)."
|
|
122 |
|
|
123 |
|name|
|
|
124 |
|
|
125 |
name := FileSelectionBox requestFilename.
|
276
|
126 |
name notNil ifTrue:[^ self named:name].
|
216
|
127 |
^ nil
|
276
|
128 |
|
|
129 |
"
|
|
130 |
Filename fromUser
|
|
131 |
"
|
1
|
132 |
! !
|
|
133 |
|
2
|
134 |
!Filename class methodsFor:'queries'!
|
|
135 |
|
|
136 |
separator
|
92
|
137 |
"return the file/directory separator.
|
|
138 |
Usually, this is $/ for unix-like systems
|
|
139 |
and $\ for dos-like ones (there may be more in the future)."
|
2
|
140 |
|
|
141 |
^ OperatingSystem fileSeparator
|
38
|
142 |
|
92
|
143 |
"
|
|
144 |
Filename separator
|
|
145 |
"
|
174
|
146 |
!
|
|
147 |
|
216
|
148 |
parentDirectoryName
|
|
149 |
"return the name used for the parent directory.
|
|
150 |
This is '..' for unix and dos-like systems.
|
|
151 |
(there may be more in the future."
|
|
152 |
|
|
153 |
^ OperatingSystem parentDirectoryName
|
|
154 |
|
|
155 |
"
|
|
156 |
Filename parentDirectoryName
|
|
157 |
"
|
|
158 |
!
|
|
159 |
|
174
|
160 |
isCaseSensitive
|
|
161 |
"return true, if filenames are case sensitive.
|
|
162 |
We ask the OS about this, to be independent here."
|
|
163 |
|
216
|
164 |
^ OperatingSystem caseSensitiveFilenames
|
2
|
165 |
! !
|
|
166 |
|
|
167 |
!Filename methodsFor:'instance creation'!
|
|
168 |
|
249
|
169 |
constructString:subname
|
38
|
170 |
"taking the receiver as a directory name, construct a new
|
249
|
171 |
filenames string for an entry within this directory (i.e. for a file
|
38
|
172 |
or a subdirectory in that directory)."
|
|
173 |
|
276
|
174 |
|sepString|
|
202
|
175 |
|
|
176 |
sepString := self class separator asString.
|
|
177 |
nameString = sepString ifTrue:[
|
159
|
178 |
"I am the root"
|
276
|
179 |
^ sepString , subname
|
85
|
180 |
].
|
276
|
181 |
^ nameString , sepString , subname
|
38
|
182 |
|
85
|
183 |
"
|
249
|
184 |
'/tmp' asFilename constructString:'foo'
|
|
185 |
'/' asFilename constructString:'foo'
|
|
186 |
'/usr/tmp' asFilename constructString:'foo'
|
|
187 |
"
|
|
188 |
!
|
|
189 |
|
|
190 |
construct:subname
|
|
191 |
"taking the receiver as a directory name, construct a new
|
|
192 |
filename for an entry within this directory (i.e. for a file
|
|
193 |
or a subdirectory in that directory)."
|
|
194 |
|
276
|
195 |
^ (self constructString:subname) asFilename
|
249
|
196 |
|
|
197 |
"
|
|
198 |
'/tmp' asFilename construct:'foo'
|
|
199 |
'/' asFilename construct:'foo'
|
|
200 |
'/usr/tmp' asFilename construct:'foo'
|
85
|
201 |
"
|
2
|
202 |
! !
|
|
203 |
|
195
|
204 |
!Filename methodsFor:'misc'!
|
|
205 |
|
|
206 |
, aString
|
|
207 |
"this allows filenames to understand how names are concatenated.
|
213
|
208 |
Returns a new filename consisting of the receivers name, concatenated
|
195
|
209 |
by aString. Notice this is NOT the same as construct:, which inserts
|
|
210 |
a directory delimiter."
|
|
211 |
|
|
212 |
^ (nameString , aString asString) asFilename
|
|
213 |
|
|
214 |
"
|
|
215 |
'Makefile' asFilename , '.bak'
|
|
216 |
'Makefile' asFilename construct:'.bak'
|
|
217 |
"
|
|
218 |
! !
|
|
219 |
|
174
|
220 |
!Filename methodsFor:'comparing'!
|
|
221 |
|
|
222 |
hash
|
|
223 |
"return an integer useful as a hash-key"
|
|
224 |
|
|
225 |
^ nameString hash
|
|
226 |
!
|
|
227 |
|
|
228 |
= aFilename
|
276
|
229 |
"return true, if the argument represents the same filename"
|
|
230 |
|
|
231 |
|str|
|
174
|
232 |
|
|
233 |
self species == aFilename species ifTrue:[
|
276
|
234 |
str := aFilename asString.
|
174
|
235 |
self class isCaseSensitive ifTrue:[
|
276
|
236 |
^ nameString = str
|
174
|
237 |
].
|
276
|
238 |
^ nameString sameAs:str
|
174
|
239 |
].
|
|
240 |
^ false
|
|
241 |
! !
|
|
242 |
|
1
|
243 |
!Filename methodsFor:'converting'!
|
|
244 |
|
|
245 |
asString
|
38
|
246 |
"return the receiver converted to a string"
|
|
247 |
|
68
|
248 |
^ nameString
|
1
|
249 |
!
|
|
250 |
|
|
251 |
asFilename
|
68
|
252 |
"return the receiver converted to a filename."
|
38
|
253 |
|
68
|
254 |
"Thats pretty easy here :-)"
|
1
|
255 |
^ self
|
195
|
256 |
!
|
|
257 |
|
|
258 |
makeLegalFilename
|
|
259 |
"convert the receveivers name to be a legal filename.
|
|
260 |
The implementation may change in the future to be more
|
|
261 |
OS specific."
|
|
262 |
|
276
|
263 |
"
|
|
264 |
actually, in Unix spaces are allowed - but it makes life
|
|
265 |
so hard; therefore, replace them by underscores ...
|
|
266 |
"
|
195
|
267 |
nameString replaceAll:(Character space) by:$_.
|
|
268 |
"
|
|
269 |
need more - especially on SYS5.3 type systems,
|
|
270 |
we may want to contract the fileName to 14 characters.
|
|
271 |
"
|
|
272 |
^ self
|
|
273 |
|
|
274 |
"
|
|
275 |
'hello world' asFilename makeLegalFilename
|
|
276 |
"
|
1
|
277 |
! !
|
|
278 |
|
202
|
279 |
!Filename methodsFor:'private accessing'!
|
1
|
280 |
|
68
|
281 |
setName:aString
|
38
|
282 |
"set the filename"
|
|
283 |
|
68
|
284 |
nameString := aString
|
1
|
285 |
! !
|
|
286 |
|
2
|
287 |
!Filename methodsFor:'queries'!
|
|
288 |
|
159
|
289 |
separator
|
|
290 |
"return the directory-separator character (or string)"
|
|
291 |
|
|
292 |
^ self class separator
|
|
293 |
!
|
|
294 |
|
174
|
295 |
directory
|
|
296 |
"return the directory name part of the file/directory.
|
92
|
297 |
- thats the name of the directory where the file/dir represented by
|
174
|
298 |
the receiver is contained in.
|
|
299 |
This method used to be called 'directoryName' but has been renamed
|
|
300 |
for ST-80 compatibility."
|
92
|
301 |
|
216
|
302 |
^ OperatingSystem directoryNameOf:(self pathName)
|
2
|
303 |
|
92
|
304 |
"
|
174
|
305 |
'/foo/bar' asFilename directory
|
|
306 |
'.' asFilename directory
|
|
307 |
'..' asFilename directory
|
|
308 |
'../..' asFilename directory
|
92
|
309 |
"
|
2
|
310 |
!
|
|
311 |
|
174
|
312 |
tail
|
|
313 |
"the files name without directory prefix. For ST-80 compatiblity."
|
|
314 |
|
|
315 |
^ self baseName
|
|
316 |
!
|
|
317 |
|
2
|
318 |
baseName
|
|
319 |
"return my baseName
|
92
|
320 |
- thats the file/directory name without leading parent-dirs."
|
2
|
321 |
|
216
|
322 |
^ OperatingSystem baseNameOf:(self pathName)
|
68
|
323 |
|
85
|
324 |
"
|
92
|
325 |
'/foo/bar' asFilename baseName
|
|
326 |
'.' asFilename baseName
|
|
327 |
'..' asFilename baseName
|
|
328 |
'../..' asFilename baseName
|
|
329 |
"
|
|
330 |
!
|
|
331 |
|
|
332 |
pathName
|
|
333 |
"return the full pathname of the file represented by the receiver."
|
|
334 |
|
216
|
335 |
|parentName sep|
|
|
336 |
|
|
337 |
sep := self class separator.
|
|
338 |
(nameString startsWith:sep) ifTrue:[
|
|
339 |
parentName := self class parentDirectoryName.
|
|
340 |
(nameString endsWith:parentName) ifFalse:[
|
|
341 |
(nameString endsWith:parentName , sep asString) ifFalse:[
|
|
342 |
^ nameString
|
|
343 |
]
|
|
344 |
]
|
92
|
345 |
].
|
|
346 |
^ (FileDirectory directoryNamed:nameString) pathName
|
|
347 |
|
|
348 |
"
|
|
349 |
'/foo/bar' asFilename pathName
|
|
350 |
'.' asFilename pathName
|
|
351 |
'../..' asFilename pathName
|
85
|
352 |
"
|
68
|
353 |
!
|
|
354 |
|
|
355 |
isAbsolute
|
|
356 |
"return true, if the receiver represents an absolute pathname
|
92
|
357 |
(in contrast to one relative to the current directory)."
|
68
|
358 |
|
85
|
359 |
^ (nameString startsWith:self class separator)
|
|
360 |
|
|
361 |
"
|
92
|
362 |
'/foo/bar' asFilename isAbsolute
|
|
363 |
'..' asFilename isAbsolute
|
|
364 |
'source/SBrowser.st' asFilename isAbsolute
|
85
|
365 |
"
|
|
366 |
!
|
68
|
367 |
|
159
|
368 |
isRelative
|
|
369 |
"return true, if this name is interpreted relative to some
|
|
370 |
directory (opposite of absolute)"
|
|
371 |
|
|
372 |
^ self isAbsolute not
|
|
373 |
!
|
|
374 |
|
85
|
375 |
isDirectory
|
|
376 |
"return true, if the receiver represents an existing,
|
|
377 |
readable directories pathname."
|
|
378 |
|
|
379 |
^ OperatingSystem isDirectory:nameString
|
|
380 |
|
|
381 |
"
|
|
382 |
'/foo/bar' asFilename isDirectory
|
|
383 |
'/tmp' asFilename isDirectory
|
92
|
384 |
'Makefile' asFilename isDirectory
|
85
|
385 |
"
|
68
|
386 |
!
|
|
387 |
|
|
388 |
exists
|
92
|
389 |
"return true, if such a file exists."
|
68
|
390 |
|
|
391 |
^ OperatingSystem isValidPath:nameString
|
|
392 |
|
85
|
393 |
"
|
92
|
394 |
'/foo/bar' asFilename exists
|
|
395 |
'/tmp' asFilename exists
|
|
396 |
'Makefile' asFilename exists
|
85
|
397 |
"
|
77
|
398 |
!
|
|
399 |
|
|
400 |
isReadable
|
92
|
401 |
"return true, if such a file exists and is readable."
|
77
|
402 |
|
|
403 |
^ OperatingSystem isReadable:nameString
|
|
404 |
|
85
|
405 |
"
|
92
|
406 |
'/foo/bar' asFilename isReadable
|
|
407 |
'/tmp' asFilename isReadable
|
|
408 |
'Makefile' asFilename isReadable
|
85
|
409 |
"
|
77
|
410 |
!
|
|
411 |
|
159
|
412 |
canBeWritten
|
|
413 |
"same as isWritable - for ST-80 compatibility"
|
|
414 |
|
|
415 |
^ self isWritable
|
|
416 |
!
|
|
417 |
|
77
|
418 |
isWritable
|
92
|
419 |
"return true, if such a file exists and is writable."
|
77
|
420 |
|
|
421 |
^ OperatingSystem isWritable:nameString
|
|
422 |
|
85
|
423 |
"
|
92
|
424 |
'/foo/bar' asFilename isWritable
|
|
425 |
'/tmp' asFilename isWritable
|
|
426 |
'Makefile' asFilename isWritable
|
|
427 |
"
|
|
428 |
!
|
|
429 |
|
|
430 |
isExecutable
|
|
431 |
"return true, if such a file exists and is executable (by Unix's definition).
|
|
432 |
For directories, true is returned if the directory can be entered.
|
|
433 |
See isExecutableProgram for a related check."
|
|
434 |
|
|
435 |
^ OperatingSystem isExecutable:nameString
|
|
436 |
|
|
437 |
"
|
|
438 |
'/foo/bar' asFilename isExecutable
|
|
439 |
'/tmp' asFilename isExecutable
|
|
440 |
'Makefile' asFilename isExecutable
|
|
441 |
'/bin/ls' asFilename isExecutable
|
|
442 |
"
|
|
443 |
!
|
|
444 |
|
|
445 |
isExecutableProgram
|
|
446 |
"return true, if such a file exists and is an executable program.
|
|
447 |
(i.e. for directories, false is returned.)"
|
|
448 |
|
|
449 |
^ (OperatingSystem isExecutable:nameString)
|
|
450 |
and:[(OperatingSystem isDirectory:nameString) not]
|
|
451 |
|
|
452 |
"
|
|
453 |
'/tmp' asFilename isExecutable
|
|
454 |
'/bin/ls' asFilename isExecutable
|
|
455 |
'/tmp' asFilename isExecutableProgram
|
|
456 |
'/bin/ls' asFilename isExecutableProgram
|
|
457 |
"
|
|
458 |
!
|
|
459 |
|
|
460 |
filenameCompletion
|
|
461 |
"try to complete the filename. This method has both a side effect,
|
|
462 |
and a return value:
|
159
|
463 |
it returns a collection of matching filename strings,
|
|
464 |
and sets (as side effect) the receivers filename to the longest common
|
|
465 |
match.
|
92
|
466 |
(i.e. if the size of the returned collection is exactly 1,
|
|
467 |
the completion has succeeded and the receivers name has been changed to
|
|
468 |
that.
|
132
|
469 |
If the returned collection is empty, nothing matched.
|
92
|
470 |
If the size of the returned collection is greater than one, the receivers
|
|
471 |
filename-string has been set to the longest common filename-prefix)"
|
|
472 |
|
216
|
473 |
|dir name matching matchLen try allMatching sep parent|
|
|
474 |
|
|
475 |
sep := self class separator asString.
|
|
476 |
parent := self class parentDirectoryName.
|
92
|
477 |
|
174
|
478 |
dir := self directory.
|
216
|
479 |
(nameString endsWith:sep) ifTrue:[
|
|
480 |
name := ''
|
|
481 |
] ifFalse:[
|
|
482 |
name := self baseName.
|
|
483 |
].
|
|
484 |
name = parent ifTrue:[^ dir asFilename filenameCompletion].
|
|
485 |
|
92
|
486 |
matching := OrderedCollection new.
|
|
487 |
(FileDirectory directoryNamed:dir) do:[:fileName |
|
216
|
488 |
((fileName ~= '.') and:[fileName ~= parent]) ifTrue:[
|
|
489 |
(fileName startsWith:name) ifTrue:[
|
|
490 |
matching add:fileName
|
|
491 |
]
|
159
|
492 |
]
|
92
|
493 |
].
|
|
494 |
"
|
|
495 |
if there is only one, change my name ...
|
|
496 |
"
|
|
497 |
matching size == 1 ifTrue:[
|
216
|
498 |
dir = sep ifTrue:[
|
159
|
499 |
dir := ''
|
|
500 |
].
|
216
|
501 |
nameString := dir , sep , matching first.
|
159
|
502 |
matching first = name ifTrue:[
|
|
503 |
self isDirectory ifTrue:[
|
|
504 |
nameString := nameString , self class separator asString
|
|
505 |
]
|
|
506 |
]
|
92
|
507 |
] ifFalse:[
|
159
|
508 |
matching size > 1 ifTrue:[
|
|
509 |
"
|
|
510 |
find the longest common prefix
|
|
511 |
"
|
|
512 |
matchLen := name size.
|
|
513 |
matchLen > matching first size ifTrue:[
|
|
514 |
try := name.
|
|
515 |
allMatching := false
|
|
516 |
] ifFalse:[
|
|
517 |
try := matching first copyTo:matchLen.
|
|
518 |
allMatching := true.
|
|
519 |
].
|
92
|
520 |
|
159
|
521 |
[allMatching] whileTrue:[
|
|
522 |
matching do:[:aName |
|
|
523 |
(aName startsWith:try) ifFalse:[
|
|
524 |
allMatching := false
|
|
525 |
]
|
|
526 |
].
|
|
527 |
allMatching ifTrue:[
|
|
528 |
matchLen < matching first size ifTrue:[
|
|
529 |
matchLen := matchLen + 1.
|
|
530 |
try := matching first copyTo:matchLen.
|
|
531 |
] ifFalse:[
|
|
532 |
allMatching := false
|
|
533 |
]
|
|
534 |
] ifFalse:[
|
|
535 |
try := matching first copyTo:matchLen - 1.
|
|
536 |
]
|
|
537 |
].
|
|
538 |
"
|
|
539 |
and set my name to the last full match
|
|
540 |
"
|
216
|
541 |
dir = sep ifTrue:[
|
159
|
542 |
dir := ''
|
|
543 |
].
|
216
|
544 |
nameString := dir , sep , try
|
159
|
545 |
]
|
92
|
546 |
].
|
|
547 |
"
|
|
548 |
return the match-set, so caller can decide what to do
|
|
549 |
(i.e. show the matches, output a warning etc ...)
|
|
550 |
"
|
|
551 |
^ matching
|
|
552 |
|
|
553 |
"
|
|
554 |
'Make' asFilename filenameCompletion;yourself
|
|
555 |
'Makef' asFilename filenameCompletion;yourself
|
|
556 |
'/u' asFilename filenameCompletion
|
85
|
557 |
"
|
2
|
558 |
! !
|
|
559 |
|
174
|
560 |
!Filename methodsFor:'file queries'!
|
|
561 |
|
|
562 |
info
|
|
563 |
"return the files info; that is a collection of file attributes,
|
|
564 |
(actually a dictionary) where the keys are #type, #uid, #gid, #size etc.
|
|
565 |
The actual amount and detail returned may depend on the OS used."
|
|
566 |
|
|
567 |
^ OperatingSystem infoOf:nameString
|
|
568 |
!
|
|
569 |
|
|
570 |
fileSize
|
|
571 |
"return the size of the file in bytes"
|
|
572 |
|
|
573 |
|info|
|
|
574 |
|
|
575 |
info := self info.
|
|
576 |
info isNil ifTrue:[^ nil].
|
|
577 |
^ info at:#size
|
|
578 |
!
|
|
579 |
|
|
580 |
fileType
|
|
581 |
"this returns a string describing the type of contents of
|
|
582 |
the file. This is done using the unix 'file' command,
|
|
583 |
(which usually is configurable by /etc/magic).
|
|
584 |
On non-unix systems, this may return an empty string, not knowning
|
|
585 |
about the contents."
|
|
586 |
|
|
587 |
|stream typeString|
|
|
588 |
|
|
589 |
typeString := ''.
|
|
590 |
stream := PipeStream readingFrom:('file ' , self pathName).
|
|
591 |
stream notNil ifTrue:[
|
|
592 |
typeString := stream contents asString.
|
|
593 |
stream close.
|
|
594 |
typeString := typeString copyFrom:(typeString indexOf:$:) + 1.
|
|
595 |
typeString := typeString withoutSeparators
|
|
596 |
] ifFalse:[
|
|
597 |
"
|
|
598 |
could add some fallback code here, for systems, where no
|
|
599 |
file command is avaliable ...
|
|
600 |
... or at least analyze directory info.
|
|
601 |
"
|
|
602 |
].
|
|
603 |
^ typeString
|
|
604 |
|
|
605 |
"
|
|
606 |
'Makefile' asFilename fileType
|
|
607 |
'.' asFilename fileType
|
|
608 |
'/dev/null' asFilename fileType
|
|
609 |
'smalltalk.rc' asFilename fileType
|
|
610 |
"
|
|
611 |
! !
|
|
612 |
|
|
613 |
!Filename methodsFor:'file operations'!
|
1
|
614 |
|
159
|
615 |
delete
|
|
616 |
"remove the file - same as remove, for ST-80 compatibility"
|
|
617 |
|
|
618 |
^ self remove
|
|
619 |
!
|
|
620 |
|
92
|
621 |
remove
|
192
|
622 |
"remove the file - the argument must be convertable to a String.
|
|
623 |
Return true if sucessfull, false if not."
|
92
|
624 |
|
|
625 |
^ OperatingSystem removeFile:nameString
|
|
626 |
|
|
627 |
"
|
|
628 |
(FileStream newFileNamed:'foo') close.
|
|
629 |
'foo' asFilename remove
|
|
630 |
"
|
|
631 |
!
|
|
632 |
|
2
|
633 |
renameTo:newName
|
192
|
634 |
"rename the file - the argument must be convertable to a String.
|
|
635 |
Return true if sucessfull, false if not."
|
2
|
636 |
|
68
|
637 |
^ OperatingSystem renameFile:nameString to:(newName asString)
|
2
|
638 |
|
92
|
639 |
"
|
|
640 |
'/tmp/foo' asFilename renameTo:'/tmp/bar'
|
|
641 |
"
|
2
|
642 |
!
|
|
643 |
|
68
|
644 |
copyTo:newName
|
159
|
645 |
"copy the file - the argument must be convertable to a filename"
|
|
646 |
|
|
647 |
|inStream outStream buffer bufferSize count|
|
1
|
648 |
|
159
|
649 |
bufferSize := 8 * 1024.
|
|
650 |
buffer := ByteArray new:bufferSize.
|
|
651 |
inStream := self readStream.
|
|
652 |
outStream := newName asFilename writeStream.
|
|
653 |
(inStream isNil or:[outStream isNil]) ifTrue:[
|
|
654 |
^ self error:'file copy failed'
|
|
655 |
].
|
|
656 |
|
|
657 |
[inStream atEnd] whileFalse:[
|
|
658 |
count := inStream nextBytes:bufferSize into:buffer.
|
|
659 |
outStream nextPutBytes:count from:buffer.
|
|
660 |
].
|
|
661 |
outStream close.
|
195
|
662 |
inStream close.
|
159
|
663 |
|
|
664 |
"
|
|
665 |
'Makefile' asFilename copyTo:'Makefile.foo'
|
|
666 |
'smalltalk' asFilename copyTo:'/dev/null'
|
|
667 |
"
|
1
|
668 |
!
|
|
669 |
|
174
|
670 |
moveTo:newName
|
|
671 |
"copy the file represented by the receiver, then delete it.
|
195
|
672 |
This is different to renaming in case of cross device moves."
|
174
|
673 |
|
|
674 |
self copyTo:newName.
|
|
675 |
self remove
|
|
676 |
!
|
|
677 |
|
|
678 |
makeDirectory
|
|
679 |
"create a directory with the receivers name"
|
|
680 |
|
|
681 |
OperatingSystem createDirectory:nameString
|
|
682 |
!
|
|
683 |
|
|
684 |
addAccessRights:aCollection
|
|
685 |
"add the access rights as specified in aCollection for the file represented
|
|
686 |
by the receiver. The argument must be a collection of symbols,
|
|
687 |
such as #readUser, #writeGroup etc."
|
|
688 |
|
|
689 |
|access|
|
|
690 |
|
|
691 |
access := OperatingSystem accessModeOf:nameString.
|
|
692 |
aCollection do:[:accessSymbol |
|
|
693 |
access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol).
|
|
694 |
].
|
|
695 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
696 |
|
|
697 |
"
|
|
698 |
'foo' asFilename writeStream close.
|
|
699 |
'foo' asFilename addAccessRights:#(readUser readGroup readOthers).
|
|
700 |
'foo' asFilename addAccessRights:#(writeUser writeGroup writeOthers).
|
|
701 |
'foo' asFilename addAccessRights:#(executeUser executeGroup executeOthers).
|
|
702 |
"
|
|
703 |
!
|
|
704 |
|
|
705 |
removeAccessRights:aCollection
|
|
706 |
"remove the access rights as specified in aCollection for the file represented
|
|
707 |
by the receiver. The argument must be a collection of symbols,
|
|
708 |
such as #readUser, #writeGroup etc."
|
|
709 |
|
|
710 |
|access|
|
|
711 |
|
|
712 |
access := OperatingSystem accessModeOf:nameString.
|
|
713 |
aCollection do:[:accessSymbol |
|
|
714 |
access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert.
|
|
715 |
].
|
|
716 |
OperatingSystem changeAccessModeOf:nameString to:access
|
|
717 |
|
|
718 |
"
|
|
719 |
'foo' asFilename writeStream close.
|
|
720 |
'foo' asFilename removeAccessRights:#(readUser readGroup readOthers).
|
|
721 |
'foo' asFilename removeAccessRights:#(writeUser writeGroup writeOthers).
|
|
722 |
'foo' asFilename removeAccessRights:#(executeUser executeGroup executeOthers).
|
|
723 |
"
|
|
724 |
!
|
|
725 |
|
|
726 |
makeReadableForAll
|
|
727 |
"make the file readable for all - you must have permission to do so."
|
|
728 |
|
|
729 |
self addAccessRights:#(readUser readGroup readOthers)
|
|
730 |
!
|
|
731 |
|
|
732 |
makeReadable
|
|
733 |
"make the file readable for the owner - you must have permission to do so."
|
|
734 |
|
|
735 |
self addAccessRights:#(readUser)
|
|
736 |
!
|
|
737 |
|
|
738 |
makeWritable
|
|
739 |
"make the file writableable for all - you must have permission to do so."
|
|
740 |
|
|
741 |
self addAccessRights:#(writeUser)
|
|
742 |
!
|
|
743 |
|
|
744 |
makeWritableForAll
|
|
745 |
"make the file writable for all - you must have permission to do so."
|
|
746 |
|
|
747 |
self addAccessRights:#(writeUser writeGroup writeOthers)
|
|
748 |
!
|
|
749 |
|
|
750 |
makeUnwritable
|
|
751 |
"make the file unwritable for all - you must have permission to do so."
|
|
752 |
|
|
753 |
self removeAccessRights:#(writeUser writeGroup writeOthers)
|
|
754 |
! !
|
|
755 |
|
|
756 |
!Filename methodsFor:'file utilities'!
|
|
757 |
|
1
|
758 |
fileIn
|
2
|
759 |
"load smalltalk code from the file"
|
|
760 |
|
|
761 |
^ self readStream fileIn
|
1
|
762 |
!
|
|
763 |
|
174
|
764 |
edit
|
|
765 |
"start an editView on the file represented by the receiver"
|
|
766 |
|
|
767 |
EditTextView openOn:self asString
|
276
|
768 |
|
|
769 |
"
|
|
770 |
'smalltalk.rc' asFilename edit
|
|
771 |
"
|
174
|
772 |
!
|
|
773 |
|
|
774 |
contentsOfEntireFile
|
|
775 |
"return the contents of the file as a string"
|
|
776 |
|
|
777 |
|s contents|
|
|
778 |
|
|
779 |
s := self readStream.
|
|
780 |
[
|
|
781 |
contents := s contents
|
|
782 |
] valueNowOrOnUnwindDo:[s close].
|
|
783 |
^ contents
|
|
784 |
! !
|
|
785 |
|
|
786 |
!Filename methodsFor:'file access'!
|
|
787 |
|
77
|
788 |
newReadWriteStream
|
|
789 |
"return a stream for read/write"
|
|
790 |
|
|
791 |
^ FileStream newFileNamed:nameString
|
|
792 |
!
|
|
793 |
|
|
794 |
readWriteStream
|
|
795 |
"return a stream for read/write"
|
|
796 |
|
|
797 |
^ FileStream oldFileNamed:nameString
|
|
798 |
!
|
|
799 |
|
1
|
800 |
readStream
|
2
|
801 |
"return a stream for reading"
|
|
802 |
|
68
|
803 |
^ FileStream readonlyFileNamed:nameString
|
1
|
804 |
!
|
|
805 |
|
|
806 |
writeStream
|
2
|
807 |
"return a stream for writing"
|
|
808 |
|
92
|
809 |
^ FileStream newFileForWritingNamed:nameString
|
|
810 |
!
|
|
811 |
|
|
812 |
appendingWriteStream
|
|
813 |
"return a stream for appending"
|
|
814 |
|
|
815 |
^ FileStream appendingOldFileNamed:nameString
|
1
|
816 |
! !
|
92
|
817 |
|
|
818 |
!Filename methodsFor:'printing & storing'!
|
|
819 |
|
|
820 |
storeOn:aStream
|
174
|
821 |
"append a printed representation of the receiver to aStream,
|
|
822 |
which allows reconstructing it via readFrom:"
|
|
823 |
|
92
|
824 |
aStream nextPut:$(.
|
|
825 |
nameString storeOn:aStream.
|
|
826 |
aStream nextPutAll:' asFilename)'
|
|
827 |
!
|
|
828 |
|
|
829 |
printOn:aStream
|
174
|
830 |
"append a printed representation of the receiver to aStream."
|
|
831 |
|
92
|
832 |
aStream nextPutAll:'FileName('''.
|
|
833 |
nameString printOn:aStream.
|
|
834 |
aStream nextPutAll:''')'
|
|
835 |
! !
|
|
836 |
|