author | Claus Gittinger <cg@exept.de> |
Thu, 18 Sep 1997 22:12:16 +0200 | |
changeset 2951 | 256e90eee7f9 |
parent 2921 | f42da059311e |
child 2961 | a188727d0619 |
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 |
|
1290 | 14 |
instanceVariableNames:'pathName lazy' |
15 |
classVariableNames:'PathOfCurrentDirectory' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Files' |
|
1 | 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 |
documentation |
37 |
" |
|
38 |
FileDirectories represent directories in the underlying host system. |
|
39 |
They provide various methods to create/delete and query for files and/or |
|
202 | 40 |
directories. Also, since FileDirectory inherits from Collection, it |
41 |
provides all enumeration and testing protocol. For example, you can |
|
42 |
loop over the filenames in a directory using 'aFileDirectory do:[:nm | ...]'. |
|
43 |
||
2859 | 44 |
|
172 | 45 |
Notice: |
2921 | 46 |
This class is not available in other ST-systems; |
47 |
in contrast, ST-80 provides a Filename class. |
|
1290 | 48 |
Therefore, Filename will take over much of the functionality in the near |
49 |
future. |
|
50 |
||
2921 | 51 |
This is going to be obsoleted - use instances of Filename if possible. |
276 | 52 |
|
1290 | 53 |
[author:] |
54 |
Claus Gittinger |
|
55 |
||
56 |
[see also:] |
|
57 |
Filename |
|
58 |
FileStream DirectoryStream OperatingSystem |
|
88 | 59 |
" |
60 |
! ! |
|
1 | 61 |
|
2 | 62 |
!FileDirectory class methodsFor:'initialization'! |
63 |
||
64 |
initialize |
|
276 | 65 |
"/ |
66 |
"/ want to know about image restart |
|
67 |
"/ |
|
2 | 68 |
ObjectMemory addDependent:self |
69 |
! |
|
70 |
||
1469 | 71 |
update:something with:aParameter from:changedObject |
276 | 72 |
"/ |
73 |
"/ currentDirectory may be different when restarted |
|
74 |
"/ |
|
2 | 75 |
something == #restarted ifTrue:[ |
1469 | 76 |
PathOfCurrentDirectory := nil |
2 | 77 |
] |
1469 | 78 |
|
79 |
"Created: 15.6.1996 / 15:20:21 / cg" |
|
2 | 80 |
! ! |
81 |
||
1 | 82 |
!FileDirectory class methodsFor:'instance creation'! |
83 |
||
84 |
currentDirectory |
|
85 |
"create and return a new FileDirectory for the current directory" |
|
86 |
||
2951 | 87 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 88 |
^ (self basicNew) pathName:(Filename currentDirectory name) |
88 | 89 |
|
90 |
" |
|
91 |
FileDirectory currentDirectory contents |
|
92 |
FileDirectory currentDirectory files |
|
93 |
FileDirectory currentDirectory isReadable |
|
94 |
FileDirectory currentDirectory pathName |
|
95 |
" |
|
1 | 96 |
! |
97 |
||
605 | 98 |
directoryNamed:name |
99 |
"create and return a new FileDirectory for the directory |
|
100 |
with given pathname" |
|
101 |
||
2951 | 102 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 103 |
name asString = '.' ifTrue:[ |
104 |
Filename currentDirectory name ~= '.' ifTrue:[ |
|
105 |
^ self currentDirectory |
|
106 |
] |
|
107 |
]. |
|
108 |
^ (self basicNew) pathName:(name asFilename constructString:'') |
|
605 | 109 |
|
110 |
" |
|
111 |
(FileDirectory directoryNamed:'..') pathName |
|
112 |
(FileDirectory directoryNamed:'../..') files |
|
113 |
" |
|
114 |
! |
|
115 |
||
1 | 116 |
directoryNamed:name in:aFileDirectory |
117 |
"create and return a new FileDirectory for the directory with given name |
|
118 |
in another FileDirectory" |
|
119 |
||
159 | 120 |
|baseName sep| |
1 | 121 |
|
2951 | 122 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 123 |
|
124 |
"/ OLD: |
|
125 |
"/ sep := OperatingSystem fileSeparator. |
|
126 |
"/ ((name at:1) == sep) ifTrue:[ |
|
127 |
"/ ^ self directoryNamed:name |
|
128 |
"/ ]. |
|
129 |
"/ "/ temporary kludge; this and the previous should |
|
130 |
"/ "/ go into OS isAbsolutePath .. |
|
131 |
"/ |
|
132 |
"/ OperatingSystem isMSDOSlike ifTrue:[ |
|
133 |
"/ ('[a-zA-Z]:*' match:name) ifTrue:[ |
|
134 |
"/ "/ drive spec ... |
|
135 |
"/ ^ self directoryNamed:name |
|
136 |
"/ ] |
|
137 |
"/ ]. |
|
138 |
"/ (aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
|
139 |
"/ baseName := aFileDirectory pathName |
|
140 |
"/ ] ifFalse:[ |
|
141 |
"/ baseName := aFileDirectory |
|
142 |
"/ ]. |
|
143 |
"/ (name = '.') ifTrue:[^ aFileDirectory]. |
|
144 |
"/ |
|
145 |
"/ (baseName = sep asString) ifFalse:[ |
|
146 |
"/ (baseName endsWith:sep) ifFalse:[ |
|
147 |
"/ baseName := baseName copyWith:sep |
|
148 |
"/ ] |
|
149 |
"/ ]. |
|
150 |
"/ ^ self directoryNamed:(baseName , name) |
|
151 |
||
152 |
"/ NEW: |
|
153 |
name asFilename isAbsolute ifTrue:[ |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
154 |
^ self directoryNamed:name |
1 | 155 |
]. |
2921 | 156 |
|
1 | 157 |
(aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
158 |
baseName := aFileDirectory pathName |
1 | 159 |
] ifFalse:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
160 |
baseName := aFileDirectory |
1 | 161 |
]. |
2921 | 162 |
^ (self basicNew) pathName:((Filename named:baseName) constructString:name) |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
163 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
164 |
"Modified: 28.4.1997 / 23:23:51 / cg" |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
165 |
"Modified: 28.4.1997 / 22:34:47 / dq" |
605 | 166 |
! |
167 |
||
168 |
rootDirectory |
|
169 |
"create and return a new FileDirectory for the root directory" |
|
170 |
||
2951 | 171 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 172 |
^ (self basicNew) pathName:(Filename rootDirectory name) |
605 | 173 |
|
174 |
" |
|
175 |
FileDirectory rootDirectory contents |
|
176 |
FileDirectory rootDirectory files |
|
177 |
FileDirectory rootDirectory isReadable |
|
178 |
" |
|
276 | 179 |
! ! |
180 |
||
181 |
!FileDirectory class methodsFor:'private'! |
|
182 |
||
183 |
fullPathNameOf:name in:path |
|
184 |
|sep| |
|
185 |
||
186 |
sep := OperatingSystem fileSeparator asString. |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
187 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
188 |
"/ is it an absolute path ? |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
189 |
|
276 | 190 |
(name startsWith:sep) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
191 |
^ name |
276 | 192 |
]. |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
193 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
194 |
"/ temporary kludge; this and the previous should |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
195 |
"/ go into OS isAbsolutePath .. |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
196 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
197 |
OperatingSystem isMSDOSlike ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
198 |
('[a-zA-Z]:*' match:name) ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
199 |
"/ drive spec ... |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
200 |
^ name |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
201 |
] |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
202 |
]. |
276 | 203 |
^ path , sep , name |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
204 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
205 |
"Modified: 28.4.1997 / 22:06:17 / cg" |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
206 |
"Modified: 28.4.1997 / 22:34:18 / dq" |
1 | 207 |
! ! |
208 |
||
209 |
!FileDirectory methodsFor:'accessing'! |
|
210 |
||
211 |
baseName |
|
212 |
"return my baseName |
|
213 |
- thats the directory name without leading parent-dirs" |
|
214 |
||
215 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 216 |
^ pathName asFilename baseName |
1 | 217 |
! |
218 |
||
219 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
220 |
"return a collection with all files and subdirectories in the receiver. |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
221 |
Skips any '.' or '..' entries (UNIX)" |
1 | 222 |
|
223 |
|coll| |
|
224 |
||
225 |
coll := OrderedCollection new. |
|
226 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
227 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
228 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
229 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
230 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
231 |
] |
1 | 232 |
]. |
276 | 233 |
coll sort. |
1 | 234 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
235 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
236 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 237 |
! |
238 |
||
239 |
directories |
|
240 |
"return a collection with all subdirectories in the receiver directory" |
|
241 |
||
242 |
|coll| |
|
243 |
||
244 |
coll := OrderedCollection new. |
|
245 |
self directoriesDo:[:name | |
|
172 | 246 |
coll add:name |
1 | 247 |
]. |
276 | 248 |
coll sort. |
1 | 249 |
^ coll |
250 |
! |
|
251 |
||
605 | 252 |
directoryName |
253 |
"return my directoryName |
|
254 |
- thats the directory name where I'm in" |
|
255 |
||
256 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 257 |
^ pathName asFilename directoryName |
605 | 258 |
! |
259 |
||
1 | 260 |
files |
261 |
"return a collection with all plain files in the receiver directory" |
|
262 |
||
263 |
|coll| |
|
264 |
||
265 |
coll := OrderedCollection new. |
|
266 |
self filesDo:[:name | |
|
172 | 267 |
coll add:name |
1 | 268 |
]. |
276 | 269 |
coll sort. |
270 |
^ coll |
|
605 | 271 |
! |
272 |
||
273 |
pathName |
|
274 |
"return my full pathname" |
|
275 |
||
276 |
lazy ifTrue:[self getFullPathName]. |
|
277 |
^ pathName |
|
278 |
! |
|
279 |
||
280 |
pathName:dirName |
|
281 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
282 |
||
283 |
pathName := dirName. |
|
284 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
285 |
lazy := true |
|
286 |
] ifTrue:[ |
|
287 |
(dirName startsWith:'./') ifFalse:[ |
|
288 |
"/ (dirName includes:$.) ifTrue:[ |
|
289 |
lazy := true |
|
290 |
] |
|
291 |
]. |
|
292 |
^ self |
|
293 |
" |
|
294 |
(OperatingSystem isDirectory:pathName) ifFalse:[^ nil] |
|
295 |
" |
|
296 |
! ! |
|
297 |
||
298 |
!FileDirectory methodsFor:'basic'! |
|
299 |
||
300 |
createDirectory:newName |
|
301 |
"create a new filedirectory as a subdirectory of myself; |
|
302 |
return true if successful" |
|
303 |
||
304 |
|realName| |
|
305 |
||
306 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
307 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
308 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
309 |
realName := newName copyFrom:2 |
|
310 |
] ifFalse:[ |
|
311 |
realName := newName |
|
312 |
]. |
|
2921 | 313 |
^ (self filenameFor:realName) makeDirectory |
314 |
"/ ^ OperatingSystem createDirectory:(self pathNameOf:realName) |
|
605 | 315 |
] |
316 |
]. |
|
317 |
^ false |
|
318 |
! |
|
319 |
||
320 |
link:oldFileName to:newFileName |
|
321 |
"link oldFileName to newFileName in myself, return true if successful" |
|
322 |
||
323 |
|path1 path2| |
|
324 |
||
325 |
path1 := self pathNameOf:oldFileName. |
|
326 |
path2 := self pathNameOf:newFileName. |
|
327 |
^ OperatingSystem linkFile:path1 to:path2 |
|
328 |
! |
|
329 |
||
330 |
remove:aFileOrDirectoryName |
|
331 |
"remove the file or directory from myself; return true if successful" |
|
332 |
||
2921 | 333 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 334 |
|
2921 | 335 |
"/ |path| |
336 |
"/ |
|
337 |
"/ path := self pathNameOf:aFileOrDirectoryName. |
|
338 |
"/ (OperatingSystem isDirectory:path) ifTrue:[ |
|
339 |
"/ ^ OperatingSystem removeDirectory:path |
|
340 |
"/ ]. |
|
341 |
"/ ^ OperatingSystem removeFile:path |
|
605 | 342 |
! |
343 |
||
344 |
removeDirectory:dirName |
|
345 |
"remove the directory 'dirName' from myself; return true if successful. |
|
346 |
If the directory is not empty, the containing files/directories are also |
|
347 |
removed." |
|
348 |
||
2921 | 349 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 350 |
|
2921 | 351 |
"/ |path| |
352 |
"/ |
|
353 |
"/ path := self pathNameOf:dirName. |
|
354 |
"/ (OperatingSystem removeDirectory:path) ifTrue:[^ true]. |
|
355 |
"/ ^ OperatingSystem recursiveRemoveDirectory:path |
|
605 | 356 |
! |
357 |
||
358 |
removeFile:fileName |
|
359 |
"remove the file 'fileName' from myself; return true if successful" |
|
360 |
||
2921 | 361 |
^ (self filenameFor:fileName) remove |
362 |
"/ ^ OperatingSystem removeFile:(self pathNameOf:fileName). |
|
605 | 363 |
! |
364 |
||
365 |
renameFile:oldFileName newName:newFileName |
|
366 |
"rename the file; return true if successful" |
|
367 |
||
2921 | 368 |
|f1 f2| |
605 | 369 |
|
2921 | 370 |
f1 := self filenameFor:oldFileName. |
371 |
f2 := self filenameFor:newFileName. |
|
372 |
^ f1 renameTo:f2 |
|
1 | 373 |
! ! |
374 |
||
172 | 375 |
!FileDirectory methodsFor:'converting'! |
376 |
||
377 |
asFilename |
|
378 |
"return myself as a filename" |
|
379 |
||
380 |
^ self pathName asFilename |
|
381 |
! |
|
382 |
||
383 |
asFilename:someFile |
|
384 |
"return a filename for a file named someFile in myself" |
|
385 |
||
276 | 386 |
^ self asFilename construct:someFile |
172 | 387 |
! ! |
388 |
||
605 | 389 |
!FileDirectory methodsFor:'enumerating'! |
390 |
||
391 |
allDirectoriesDo:aBlock |
|
392 |
"evaluate the argument, aBlock for every directory name |
|
393 |
in the directory and in all subdirectories" |
|
394 |
||
395 |
|aStream command line| |
|
396 |
||
397 |
lazy ifTrue:[self getFullPathName]. |
|
398 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
399 |
aStream := PipeStream readingFrom:command. |
|
400 |
aStream isNil ifTrue:[^ nil]. |
|
401 |
[aStream atEnd] whileFalse:[ |
|
402 |
line := aStream nextLine. |
|
403 |
line notNil ifTrue:[ |
|
404 |
(line = '.') ifFalse:[ |
|
405 |
"cut off initial ./" |
|
406 |
line := line copyFrom:3 |
|
407 |
]. |
|
408 |
aBlock value:line |
|
409 |
] |
|
410 |
]. |
|
411 |
aStream close |
|
412 |
! |
|
413 |
||
414 |
allFilesDo:aBlock |
|
415 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
416 |
subdirectories" |
|
417 |
||
418 |
|aStream command line| |
|
419 |
||
420 |
lazy ifTrue:[self getFullPathName]. |
|
421 |
command := 'cd ' , pathName , '; find . -print'. |
|
422 |
aStream := PipeStream readingFrom:command. |
|
423 |
aStream isNil ifTrue:[^ nil]. |
|
424 |
[aStream atEnd] whileFalse:[ |
|
425 |
line := aStream nextLine. |
|
426 |
line notNil ifTrue:[ |
|
427 |
(line = '.') ifFalse:[ |
|
428 |
"cut off initial ./" |
|
429 |
line := line copyFrom:3 |
|
430 |
]. |
|
431 |
aBlock value:line |
|
432 |
] |
|
433 |
]. |
|
434 |
aStream close |
|
435 |
! |
|
436 |
||
437 |
directoriesDo:aBlock |
|
438 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
439 |
||
440 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
441 |
((name ~= '.') and:[name ~= '..']) |
|
442 |
] ifFalse:[ |
|
443 |
false |
|
444 |
] |
|
445 |
] do:aBlock |
|
446 |
! |
|
447 |
||
448 |
do:aBlock |
|
449 |
"evaluate the argument, aBlock for every name in the directory" |
|
450 |
||
451 |
self where:[:name | true] do:aBlock |
|
452 |
! |
|
453 |
||
454 |
filesDo:aBlock |
|
455 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
456 |
||
457 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
458 |
! |
|
459 |
||
460 |
namesDo:aBlock |
|
461 |
"evaluate the argument, aBlock for every name in the directory. |
|
462 |
for ST-80 compatibility" |
|
463 |
||
464 |
self do:aBlock |
|
465 |
! |
|
466 |
||
467 |
where:testBlock do:aBlock |
|
468 |
"evaluate the argument, aBlock for every object in the directory |
|
469 |
for which testBlock evaluates to true." |
|
470 |
||
471 |
|aStream name| |
|
472 |
||
473 |
aStream := DirectoryStream directoryNamed:pathName. |
|
474 |
aStream isNil ifTrue:[^ nil]. |
|
475 |
[aStream atEnd] whileFalse:[ |
|
476 |
name := aStream nextLine. |
|
477 |
name notNil ifTrue:[ |
|
478 |
(testBlock value:name) ifTrue:[ |
|
479 |
aBlock value:name |
|
480 |
] |
|
481 |
] |
|
482 |
]. |
|
483 |
aStream close |
|
484 |
! ! |
|
485 |
||
486 |
!FileDirectory methodsFor:'more instance creation'! |
|
487 |
||
488 |
directoryNamed:aName |
|
489 |
^ self class directoryNamed:aName in:self pathName |
|
490 |
! ! |
|
491 |
||
492 |
!FileDirectory methodsFor:'printing & storing'! |
|
493 |
||
494 |
printOn:aStream |
|
495 |
"append a printed representation of the receiver to aStream." |
|
496 |
||
497 |
lazy ifTrue:[self getFullPathName]. |
|
498 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
499 |
nextPutAll:pathName; |
|
500 |
nextPutAll:')' |
|
501 |
! |
|
502 |
||
503 |
storeOn:aStream |
|
504 |
"append a printed representation of the receiver to aStream, |
|
505 |
which allows reconstructing it via readFrom:" |
|
506 |
||
507 |
lazy ifTrue:[self getFullPathName]. |
|
508 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
509 |
aStream nextPutAll:pathName. |
|
510 |
aStream nextPut:$) |
|
511 |
! ! |
|
512 |
||
1 | 513 |
!FileDirectory methodsFor:'private'! |
514 |
||
515 |
getFullPathName |
|
516 |
"make my pathname be a full pathname - i.e. starting at root" |
|
517 |
||
308 | 518 |
|shortPathName| |
1 | 519 |
|
2921 | 520 |
|
159 | 521 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 522 |
lazy := false. |
523 |
^ self |
|
1 | 524 |
]. |
525 |
||
526 |
"since currentDirectory is used very often, cache its path here" |
|
527 |
||
528 |
(pathName = '.') ifTrue:[ |
|
172 | 529 |
PathOfCurrentDirectory notNil ifTrue:[ |
530 |
pathName := PathOfCurrentDirectory. |
|
531 |
lazy := false. |
|
532 |
^ self |
|
533 |
] |
|
1 | 534 |
]. |
535 |
||
536 |
shortPathName := pathName. |
|
537 |
||
308 | 538 |
pathName := OperatingSystem pathNameOf:pathName. |
539 |
lazy := false. |
|
1 | 540 |
|
308 | 541 |
" |
542 |
if it was the current dir, keep name for next query |
|
543 |
" |
|
1 | 544 |
(shortPathName = '.') ifTrue:[ |
308 | 545 |
PathOfCurrentDirectory := pathName |
1 | 546 |
] |
276 | 547 |
! |
548 |
||
2921 | 549 |
filenameFor:fileName |
550 |
|f| |
|
551 |
||
552 |
f := fileName asFilename. |
|
553 |
f isAbsolute ifTrue:[^ f]. |
|
554 |
^ (pathName asFilename construct:fileName) |
|
555 |
! |
|
556 |
||
276 | 557 |
pathNameOf:fileName |
2921 | 558 |
^ (pathName asFilename construct:fileName) osName |
559 |
"/ ^ self class fullPathNameOf:fileName in:pathName |
|
1 | 560 |
! ! |
561 |
||
605 | 562 |
!FileDirectory methodsFor:'queries'! |
1 | 563 |
|
605 | 564 |
accessModeOf:name |
565 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 566 |
|
605 | 567 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 568 |
! |
569 |
||
605 | 570 |
changeAccessModeOf:name to:modeBits |
571 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 572 |
|
605 | 573 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 574 |
! |
575 |
||
576 |
exists |
|
577 |
"return true if this directory exists" |
|
578 |
||
2921 | 579 |
^ (Filename named:pathName) isDirectory |
580 |
"/ ^ OperatingSystem isDirectory:pathName |
|
159 | 581 |
|
582 |
" |
|
583 |
(FileDirectory directoryNamed:'fooBar') exists |
|
584 |
(FileDirectory directoryNamed:'/tmp') exists |
|
585 |
" |
|
1 | 586 |
! |
587 |
||
605 | 588 |
exists:name |
589 |
"return true, if the given name exists in myself" |
|
590 |
||
2921 | 591 |
^ (self filenameFor:name) exists |
592 |
"/ ^ OperatingSystem isValidPath:(self pathNameOf:name) |
|
605 | 593 |
! |
594 |
||
595 |
id |
|
596 |
"return the directories file-id (inode number)" |
|
597 |
||
2921 | 598 |
^ (Filename named:pathName) id |
599 |
"/ ^ OperatingSystem idOf:pathName |
|
605 | 600 |
! |
601 |
||
602 |
infoOf:name |
|
603 |
"return an array filled with file info for the file 'aFileName'; |
|
604 |
return nil if such a file does not exist" |
|
605 |
||
2921 | 606 |
^ (self filenameFor:name) info |
607 |
"/ ^ OperatingSystem infoOf:(self pathNameOf:name) |
|
605 | 608 |
! |
609 |
||
610 |
isDirectory:name |
|
611 |
"return true, if the given name is that of a directory in myself" |
|
612 |
||
2921 | 613 |
^ (self filenameFor:name) isDirectory |
614 |
"/ ^ OperatingSystem isDirectory:(self pathNameOf:name) |
|
605 | 615 |
! |
616 |
||
3 | 617 |
isEmpty |
618 |
"return true, if the directory is empty; |
|
619 |
redefined since '.' and '..' do not count as entries here." |
|
620 |
||
621 |
self do:[:fName | |
|
172 | 622 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
3 | 623 |
]. |
624 |
^ true |
|
625 |
! |
|
626 |
||
605 | 627 |
isExecutable:name |
628 |
"return true, if the given file is executable" |
|
1 | 629 |
|
2921 | 630 |
^ (self filenameFor:name) isExecutable |
631 |
"/ ^ OperatingSystem isExecutable:(self pathNameOf:name) |
|
1 | 632 |
! |
633 |
||
634 |
isReadable:name |
|
635 |
"return true, if the given file is readable" |
|
636 |
||
2921 | 637 |
^ (self filenameFor:name) isReadable |
638 |
"/ ^ OperatingSystem isReadable:(self pathNameOf:name) |
|
1 | 639 |
! |
640 |
||
641 |
isWritable:name |
|
642 |
"return true, if the given file is readable" |
|
643 |
||
2921 | 644 |
^ (self filenameFor:name) isWritable |
645 |
"/ ^ OperatingSystem isWritable:(self pathNameOf:name) |
|
1 | 646 |
! |
647 |
||
605 | 648 |
species |
649 |
^ OrderedCollection |
|
1 | 650 |
! |
651 |
||
605 | 652 |
timeOfLastChange |
653 |
"return the timeStamp of myself" |
|
1 | 654 |
|
2921 | 655 |
^ (Filename named:pathName) modificationTime |
656 |
"/ ^ OperatingSystem timeOfLastChange:pathName |
|
1 | 657 |
! |
658 |
||
605 | 659 |
timeOfLastChange:name |
660 |
"return the timeStamp of a file in myself" |
|
1 | 661 |
|
2921 | 662 |
^ (self filenameFor:name) modificationTime |
663 |
"/ ^ OperatingSystem timeOfLastChange:(self pathNameOf:name) |
|
1 | 664 |
! |
665 |
||
605 | 666 |
typeOf:name |
667 |
"return the symbolic type of a file in myself" |
|
1 | 668 |
|
2921 | 669 |
^ (self filenameFor:name) type |
670 |
"/ ^ OperatingSystem typeOf:(self pathNameOf:name) |
|
605 | 671 |
! ! |
1 | 672 |
|
629 | 673 |
!FileDirectory class methodsFor:'documentation'! |
674 |
||
675 |
version |
|
2951 | 676 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.33 1997-09-18 20:12:15 cg Exp $' |
629 | 677 |
! ! |
605 | 678 |
FileDirectory initialize! |