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