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