author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:28:41 +0100 | |
changeset 530 | 07d0bce293c9 |
parent 430 | 9f14534e3dd2 |
child 605 | 8b17f96bf05a |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
172 | 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 |
Collection subclass:#FileDirectory |
|
14 |
instanceVariableNames:'pathName lazy' |
|
31 | 15 |
classVariableNames:'PathOfCurrentDirectory' |
1 | 16 |
poolDictionaries:'' |
17 |
category:'Collections-Files' |
|
18 |
! |
|
19 |
||
88 | 20 |
!FileDirectory class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
172 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
version |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
430
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.24 1995-11-11 15:23:00 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
FileDirectories represent directories in the underlying host system. |
|
43 |
They provide various methods to create/delete and query for files and/or |
|
202 | 44 |
directories. Also, since FileDirectory inherits from Collection, it |
45 |
provides all enumeration and testing protocol. For example, you can |
|
46 |
loop over the filenames in a directory using 'aFileDirectory do:[:nm | ...]'. |
|
47 |
||
172 | 48 |
Notice: |
49 |
This class is not available in other ST-systems, while for example, |
|
50 |
ST-80 provides a Filename class. |
|
202 | 51 |
Therefore, Filename will take over much of the functionality in the near |
52 |
future. |
|
276 | 53 |
|
172 | 54 |
Use instances of Filename if possible. |
88 | 55 |
" |
56 |
! ! |
|
1 | 57 |
|
2 | 58 |
!FileDirectory class methodsFor:'initialization'! |
59 |
||
60 |
initialize |
|
276 | 61 |
"/ |
62 |
"/ want to know about image restart |
|
63 |
"/ |
|
2 | 64 |
ObjectMemory addDependent:self |
65 |
! |
|
66 |
||
67 |
update:something |
|
276 | 68 |
"/ |
69 |
"/ currentDirectory may be different when restarted |
|
70 |
"/ |
|
2 | 71 |
something == #restarted ifTrue:[ |
172 | 72 |
PathOfCurrentDirectory := nil |
2 | 73 |
] |
74 |
! ! |
|
75 |
||
1 | 76 |
!FileDirectory class methodsFor:'instance creation'! |
77 |
||
276 | 78 |
directoryNamed:name |
79 |
"create and return a new FileDirectory for the directory |
|
80 |
with given pathname" |
|
81 |
||
82 |
^ (self basicNew) pathName:name |
|
83 |
||
84 |
" |
|
85 |
(FileDirectory directoryNamed:'..') pathName |
|
86 |
(FileDirectory directoryNamed:'../..') files |
|
87 |
" |
|
88 |
! |
|
89 |
||
1 | 90 |
rootDirectory |
91 |
"create and return a new FileDirectory for the root directory" |
|
92 |
||
276 | 93 |
^ self directoryNamed:(OperatingSystem fileSeparator asString) |
88 | 94 |
|
95 |
" |
|
96 |
FileDirectory rootDirectory contents |
|
97 |
FileDirectory rootDirectory files |
|
98 |
FileDirectory rootDirectory isReadable |
|
99 |
" |
|
1 | 100 |
! |
101 |
||
102 |
currentDirectory |
|
103 |
"create and return a new FileDirectory for the current directory" |
|
104 |
||
276 | 105 |
^ self directoryNamed:'.' |
88 | 106 |
|
107 |
" |
|
108 |
FileDirectory currentDirectory contents |
|
109 |
FileDirectory currentDirectory files |
|
110 |
FileDirectory currentDirectory isReadable |
|
111 |
FileDirectory currentDirectory pathName |
|
112 |
" |
|
1 | 113 |
! |
114 |
||
115 |
directoryNamed:name in:aFileDirectory |
|
116 |
"create and return a new FileDirectory for the directory with given name |
|
117 |
in another FileDirectory" |
|
118 |
||
159 | 119 |
|baseName sep| |
1 | 120 |
|
159 | 121 |
sep := OperatingSystem fileSeparator. |
122 |
((name at:1) == sep) ifTrue:[ |
|
172 | 123 |
^ self directoryNamed:name |
1 | 124 |
]. |
125 |
(aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
|
172 | 126 |
baseName := aFileDirectory pathName |
1 | 127 |
] ifFalse:[ |
172 | 128 |
baseName := aFileDirectory |
1 | 129 |
]. |
130 |
" |
|
131 |
(name = '..') ifTrue:[ |
|
172 | 132 |
^ (self basicNew) pathName:(OperatingSystem directoryNameOf:baseName) |
1 | 133 |
]. |
134 |
" |
|
135 |
(name = '.') ifTrue:[^ aFileDirectory]. |
|
136 |
||
159 | 137 |
(baseName = sep asString) ifFalse:[ |
172 | 138 |
(baseName endsWith:sep) ifFalse:[ |
139 |
baseName := baseName copyWith:sep |
|
140 |
] |
|
1 | 141 |
]. |
276 | 142 |
^ self directoryNamed:(baseName , name) |
143 |
! ! |
|
144 |
||
145 |
!FileDirectory class methodsFor:'private'! |
|
146 |
||
147 |
fullPathNameOf:name in:path |
|
148 |
|sep| |
|
149 |
||
150 |
sep := OperatingSystem fileSeparator asString. |
|
151 |
(name startsWith:sep) ifTrue:[ |
|
152 |
^ name |
|
153 |
]. |
|
154 |
^ path , sep , name |
|
1 | 155 |
! ! |
156 |
||
157 |
!FileDirectory methodsFor:'accessing'! |
|
158 |
||
159 |
baseName |
|
160 |
"return my baseName |
|
161 |
- thats the directory name without leading parent-dirs" |
|
162 |
||
163 |
lazy ifTrue:[self getFullPathName]. |
|
164 |
^ OperatingSystem baseNameOf:pathName |
|
165 |
! |
|
166 |
||
167 |
directoryName |
|
168 |
"return my directoryName |
|
169 |
- thats the directory name where I'm in" |
|
170 |
||
171 |
lazy ifTrue:[self getFullPathName]. |
|
172 |
^ OperatingSystem directoryNameOf:pathName |
|
173 |
! |
|
174 |
||
175 |
pathName |
|
176 |
"return my full pathname" |
|
177 |
||
178 |
lazy ifTrue:[self getFullPathName]. |
|
179 |
^ pathName |
|
180 |
! |
|
181 |
||
182 |
pathName:dirName |
|
183 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
184 |
||
185 |
pathName := dirName. |
|
159 | 186 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
172 | 187 |
lazy := true |
1 | 188 |
] ifTrue:[ |
362 | 189 |
(dirName startsWith:'./') ifFalse:[ |
190 |
"/ (dirName includes:$.) ifTrue:[ |
|
172 | 191 |
lazy := true |
192 |
] |
|
1 | 193 |
]. |
194 |
^ self |
|
195 |
" |
|
196 |
(OperatingSystem isDirectory:pathName) ifFalse:[^ nil] |
|
197 |
" |
|
198 |
! |
|
199 |
||
200 |
contents |
|
201 |
"return a collection with all files and subdirectories in the receiver" |
|
202 |
||
203 |
|coll| |
|
204 |
||
205 |
coll := OrderedCollection new. |
|
206 |
self do:[:name | |
|
172 | 207 |
coll add:name |
1 | 208 |
]. |
276 | 209 |
coll sort. |
1 | 210 |
^ coll |
211 |
! |
|
212 |
||
213 |
directories |
|
214 |
"return a collection with all subdirectories in the receiver directory" |
|
215 |
||
216 |
|coll| |
|
217 |
||
218 |
coll := OrderedCollection new. |
|
219 |
self directoriesDo:[:name | |
|
172 | 220 |
coll add:name |
1 | 221 |
]. |
276 | 222 |
coll sort. |
1 | 223 |
^ coll |
224 |
! |
|
225 |
||
226 |
files |
|
227 |
"return a collection with all plain files in the receiver directory" |
|
228 |
||
229 |
|coll| |
|
230 |
||
231 |
coll := OrderedCollection new. |
|
232 |
self filesDo:[:name | |
|
172 | 233 |
coll add:name |
1 | 234 |
]. |
276 | 235 |
coll sort. |
236 |
^ coll |
|
1 | 237 |
! ! |
238 |
||
172 | 239 |
!FileDirectory methodsFor:'converting'! |
240 |
||
241 |
asFilename |
|
242 |
"return myself as a filename" |
|
243 |
||
244 |
^ self pathName asFilename |
|
245 |
! |
|
246 |
||
247 |
asFilename:someFile |
|
248 |
"return a filename for a file named someFile in myself" |
|
249 |
||
276 | 250 |
^ self asFilename construct:someFile |
172 | 251 |
! ! |
252 |
||
1 | 253 |
!FileDirectory methodsFor:'private'! |
254 |
||
255 |
getFullPathName |
|
256 |
"make my pathname be a full pathname - i.e. starting at root" |
|
257 |
||
308 | 258 |
|shortPathName| |
1 | 259 |
|
159 | 260 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 261 |
lazy := false. |
262 |
^ self |
|
1 | 263 |
]. |
264 |
||
265 |
"since currentDirectory is used very often, cache its path here" |
|
266 |
||
267 |
(pathName = '.') ifTrue:[ |
|
172 | 268 |
PathOfCurrentDirectory notNil ifTrue:[ |
269 |
pathName := PathOfCurrentDirectory. |
|
270 |
lazy := false. |
|
271 |
^ self |
|
272 |
] |
|
1 | 273 |
]. |
274 |
||
275 |
shortPathName := pathName. |
|
276 |
||
308 | 277 |
pathName := OperatingSystem pathNameOf:pathName. |
278 |
lazy := false. |
|
1 | 279 |
|
308 | 280 |
" |
281 |
if it was the current dir, keep name for next query |
|
282 |
" |
|
1 | 283 |
(shortPathName = '.') ifTrue:[ |
308 | 284 |
PathOfCurrentDirectory := pathName |
1 | 285 |
] |
276 | 286 |
! |
287 |
||
288 |
pathNameOf:fileName |
|
289 |
^ self class fullPathNameOf:fileName in:pathName |
|
1 | 290 |
! ! |
291 |
||
292 |
!FileDirectory methodsFor:'basic'! |
|
293 |
||
294 |
createDirectory:newName |
|
295 |
"create a new filedirectory as a subdirectory of myself; |
|
296 |
return true if successful" |
|
297 |
||
298 |
|realName| |
|
299 |
||
430 | 300 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
301 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
172 | 302 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
303 |
realName := newName copyFrom:2 |
|
304 |
] ifFalse:[ |
|
305 |
realName := newName |
|
306 |
]. |
|
276 | 307 |
^ OperatingSystem createDirectory:(self pathNameOf:realName) |
172 | 308 |
] |
1 | 309 |
]. |
310 |
^ false |
|
311 |
! |
|
312 |
||
313 |
removeFile:fileName |
|
314 |
"remove the file 'fileName' from myself; return true if successful" |
|
315 |
||
276 | 316 |
^ OperatingSystem removeFile:(self pathNameOf:fileName). |
1 | 317 |
! |
318 |
||
319 |
removeDirectory:dirName |
|
19 | 320 |
"remove the directory 'dirName' from myself; return true if successful. |
20 | 321 |
If the directory is not empty, the containing files/directories are also |
19 | 322 |
removed." |
323 |
||
159 | 324 |
|path| |
1 | 325 |
|
276 | 326 |
path := self pathNameOf:dirName. |
159 | 327 |
(OperatingSystem removeDirectory:path) ifTrue:[^ true]. |
328 |
^ OperatingSystem recursiveRemoveDirectory:path |
|
1 | 329 |
! |
330 |
||
331 |
remove:aFileOrDirectoryName |
|
332 |
"remove the file or directory from myself; return true if successful" |
|
333 |
||
334 |
|path| |
|
335 |
||
276 | 336 |
path := self pathNameOf:aFileOrDirectoryName. |
1 | 337 |
(OperatingSystem isDirectory:path) ifTrue:[ |
172 | 338 |
^ OperatingSystem removeDirectory:path |
1 | 339 |
]. |
340 |
^ OperatingSystem removeFile:path |
|
341 |
! |
|
342 |
||
343 |
link:oldFileName to:newFileName |
|
344 |
"link oldFileName to newFileName in myself, return true if successful" |
|
345 |
||
346 |
|path1 path2| |
|
347 |
||
276 | 348 |
path1 := self pathNameOf:oldFileName. |
349 |
path2 := self pathNameOf:newFileName. |
|
54 | 350 |
^ OperatingSystem linkFile:path1 to:path2 |
1 | 351 |
! |
352 |
||
353 |
renameFile:oldFileName newName:newFileName |
|
354 |
"rename the file; return true if successful" |
|
355 |
||
356 |
|path1 path2| |
|
357 |
||
276 | 358 |
path1 := self pathNameOf:oldFileName. |
359 |
path2 := self pathNameOf:newFileName. |
|
159 | 360 |
^ OperatingSystem renameFile:path1 to:path2 |
361 |
! ! |
|
362 |
||
1 | 363 |
!FileDirectory methodsFor:'queries'! |
364 |
||
314 | 365 |
species |
366 |
^ OrderedCollection |
|
367 |
! |
|
368 |
||
1 | 369 |
id |
370 |
"return the directories file-id (inode number)" |
|
371 |
||
372 |
^ OperatingSystem idOf:pathName |
|
373 |
! |
|
374 |
||
375 |
exists |
|
376 |
"return true if this directory exists" |
|
377 |
||
378 |
^ OperatingSystem isDirectory:pathName |
|
159 | 379 |
|
380 |
" |
|
381 |
(FileDirectory directoryNamed:'fooBar') exists |
|
382 |
(FileDirectory directoryNamed:'/tmp') exists |
|
383 |
" |
|
1 | 384 |
! |
385 |
||
3 | 386 |
isEmpty |
387 |
"return true, if the directory is empty; |
|
388 |
redefined since '.' and '..' do not count as entries here." |
|
389 |
||
390 |
self do:[:fName | |
|
172 | 391 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
3 | 392 |
]. |
393 |
^ true |
|
394 |
! |
|
395 |
||
1 | 396 |
infoOf:name |
397 |
"return an array filled with file info for the file 'aFileName'; |
|
398 |
return nil if such a file does not exist" |
|
399 |
||
276 | 400 |
^ OperatingSystem infoOf:(self pathNameOf:name) |
1 | 401 |
! |
402 |
||
403 |
timeOfLastChange:name |
|
404 |
"return the timeStamp of a file in myself" |
|
405 |
||
276 | 406 |
^ OperatingSystem timeOfLastChange:(self pathNameOf:name) |
1 | 407 |
! |
408 |
||
409 |
timeOfLastChange |
|
410 |
"return the timeStamp of myself" |
|
411 |
||
412 |
^ OperatingSystem timeOfLastChange:pathName |
|
413 |
! |
|
414 |
||
159 | 415 |
accessModeOf:name |
1 | 416 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
417 |
||
276 | 418 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 419 |
! |
420 |
||
159 | 421 |
changeAccessModeOf:name to:modeBits |
1 | 422 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
423 |
||
276 | 424 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 425 |
! |
426 |
||
159 | 427 |
typeOf:name |
1 | 428 |
"return the symbolic type of a file in myself" |
429 |
||
276 | 430 |
^ OperatingSystem typeOf:(self pathNameOf:name) |
1 | 431 |
! |
432 |
||
92 | 433 |
exists:name |
434 |
"return true, if the given name exists in myself" |
|
435 |
||
276 | 436 |
^ OperatingSystem isValidPath:(self pathNameOf:name) |
92 | 437 |
! |
438 |
||
1 | 439 |
isDirectory:name |
440 |
"return true, if the given name is that of a directory in myself" |
|
441 |
||
276 | 442 |
^ OperatingSystem isDirectory:(self pathNameOf:name) |
1 | 443 |
! |
444 |
||
445 |
isReadable:name |
|
446 |
"return true, if the given file is readable" |
|
447 |
||
276 | 448 |
^ OperatingSystem isReadable:(self pathNameOf:name) |
1 | 449 |
! |
450 |
||
451 |
isWritable:name |
|
452 |
"return true, if the given file is readable" |
|
453 |
||
276 | 454 |
^ OperatingSystem isWritable:(self pathNameOf:name) |
1 | 455 |
! |
456 |
||
457 |
isExecutable:name |
|
458 |
"return true, if the given file is executable" |
|
459 |
||
276 | 460 |
^ OperatingSystem isExecutable:(self pathNameOf:name) |
1 | 461 |
! ! |
462 |
||
463 |
!FileDirectory methodsFor:'printing & storing'! |
|
464 |
||
172 | 465 |
printOn:aStream |
466 |
"append a printed representation of the receiver to aStream." |
|
467 |
||
1 | 468 |
lazy ifTrue:[self getFullPathName]. |
172 | 469 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
470 |
nextPutAll:pathName; |
|
471 |
nextPutAll:')' |
|
1 | 472 |
! |
473 |
||
474 |
storeOn:aStream |
|
172 | 475 |
"append a printed representation of the receiver to aStream, |
476 |
which allows reconstructing it via readFrom:" |
|
477 |
||
1 | 478 |
lazy ifTrue:[self getFullPathName]. |
479 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
480 |
aStream nextPutAll:pathName. |
|
481 |
aStream nextPut:$) |
|
482 |
! ! |
|
483 |
||
484 |
!FileDirectory methodsFor:'more instance creation'! |
|
485 |
||
486 |
directoryNamed:aName |
|
487 |
^ self class directoryNamed:aName in:self pathName |
|
488 |
! ! |
|
489 |
||
490 |
!FileDirectory methodsFor:'enumerating'! |
|
491 |
||
492 |
where:testBlock do:aBlock |
|
493 |
"evaluate the argument, aBlock for every object in the directory |
|
494 |
for which testBlock evaluates to true." |
|
495 |
||
496 |
|aStream name| |
|
497 |
||
498 |
aStream := DirectoryStream directoryNamed:pathName. |
|
499 |
aStream isNil ifTrue:[^ nil]. |
|
500 |
[aStream atEnd] whileFalse:[ |
|
172 | 501 |
name := aStream nextLine. |
502 |
name notNil ifTrue:[ |
|
503 |
(testBlock value:name) ifTrue:[ |
|
504 |
aBlock value:name |
|
505 |
] |
|
506 |
] |
|
1 | 507 |
]. |
508 |
aStream close |
|
509 |
! |
|
510 |
||
511 |
do:aBlock |
|
512 |
"evaluate the argument, aBlock for every name in the directory" |
|
513 |
||
514 |
self where:[:name | true] do:aBlock |
|
515 |
! |
|
516 |
||
517 |
namesDo:aBlock |
|
518 |
"evaluate the argument, aBlock for every name in the directory. |
|
519 |
for ST-80 compatibility" |
|
520 |
||
521 |
self do:aBlock |
|
522 |
! |
|
523 |
||
524 |
filesDo:aBlock |
|
525 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
526 |
||
527 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
528 |
! |
|
529 |
||
530 |
directoriesDo:aBlock |
|
531 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
532 |
||
533 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
172 | 534 |
((name ~= '.') and:[name ~= '..']) |
535 |
] ifFalse:[ |
|
536 |
false |
|
537 |
] |
|
538 |
] do:aBlock |
|
1 | 539 |
! |
540 |
||
541 |
allFilesDo:aBlock |
|
542 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
543 |
subdirectories" |
|
544 |
||
545 |
|aStream command line| |
|
546 |
||
547 |
lazy ifTrue:[self getFullPathName]. |
|
548 |
command := 'cd ' , pathName , '; find . -print'. |
|
549 |
aStream := PipeStream readingFrom:command. |
|
550 |
aStream isNil ifTrue:[^ nil]. |
|
551 |
[aStream atEnd] whileFalse:[ |
|
172 | 552 |
line := aStream nextLine. |
553 |
line notNil ifTrue:[ |
|
554 |
(line = '.') ifFalse:[ |
|
555 |
"cut off initial ./" |
|
556 |
line := line copyFrom:3 |
|
557 |
]. |
|
558 |
aBlock value:line |
|
559 |
] |
|
1 | 560 |
]. |
561 |
aStream close |
|
562 |
! |
|
563 |
||
564 |
allDirectoriesDo:aBlock |
|
565 |
"evaluate the argument, aBlock for every directory name |
|
566 |
in the directory and in all subdirectories" |
|
567 |
||
568 |
|aStream command line| |
|
569 |
||
570 |
lazy ifTrue:[self getFullPathName]. |
|
571 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
572 |
aStream := PipeStream readingFrom:command. |
|
573 |
aStream isNil ifTrue:[^ nil]. |
|
574 |
[aStream atEnd] whileFalse:[ |
|
172 | 575 |
line := aStream nextLine. |
576 |
line notNil ifTrue:[ |
|
577 |
(line = '.') ifFalse:[ |
|
578 |
"cut off initial ./" |
|
579 |
line := line copyFrom:3 |
|
580 |
]. |
|
581 |
aBlock value:line |
|
582 |
] |
|
1 | 583 |
]. |
584 |
aStream close |
|
585 |
! ! |