author | Claus Gittinger <cg@exept.de> |
Thu, 09 Oct 2003 23:19:41 +0200 | |
changeset 7655 | a0f603816b20 |
parent 7305 | 260bd0e93817 |
child 9332 | 01b7123072b7 |
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 |
||
5556 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Collection subclass:#FileDirectory |
1290 | 16 |
instanceVariableNames:'pathName lazy' |
17 |
classVariableNames:'PathOfCurrentDirectory' |
|
18 |
poolDictionaries:'' |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
19 |
category:'Obsolete' |
1 | 20 |
! |
21 |
||
88 | 22 |
!FileDirectory class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
172 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
40 |
Notice: |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
41 |
This class is obsolete and does work only under UNIX. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
42 |
Use Filename instead. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
43 |
|
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
44 |
This class is not available in other ST-systems; |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
45 |
in contrast, ST-80 provides a Filename class. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
46 |
Therefore, Filename has taken over the functionality. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
47 |
|
88 | 48 |
FileDirectories represent directories in the underlying host system. |
49 |
They provide various methods to create/delete and query for files and/or |
|
202 | 50 |
directories. Also, since FileDirectory inherits from Collection, it |
51 |
provides all enumeration and testing protocol. For example, you can |
|
52 |
loop over the filenames in a directory using 'aFileDirectory do:[:nm | ...]'. |
|
53 |
||
1290 | 54 |
[author:] |
55 |
Claus Gittinger |
|
56 |
||
57 |
[see also:] |
|
58 |
Filename |
|
59 |
FileStream DirectoryStream OperatingSystem |
|
88 | 60 |
" |
61 |
! ! |
|
1 | 62 |
|
2 | 63 |
!FileDirectory class methodsFor:'initialization'! |
64 |
||
65 |
initialize |
|
276 | 66 |
"/ |
67 |
"/ want to know about image restart |
|
68 |
"/ |
|
2 | 69 |
ObjectMemory addDependent:self |
70 |
! |
|
71 |
||
1469 | 72 |
update:something with:aParameter from:changedObject |
276 | 73 |
"/ |
74 |
"/ currentDirectory may be different when restarted |
|
75 |
"/ |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
76 |
something == #earlySystemInstallation ifTrue:[ |
1469 | 77 |
PathOfCurrentDirectory := nil |
2 | 78 |
] |
1469 | 79 |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
80 |
"Created: / 15.6.1996 / 15:20:21 / cg" |
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
81 |
"Modified: / 11.12.1998 / 16:28:40 / cg" |
2 | 82 |
! ! |
83 |
||
1 | 84 |
!FileDirectory class methodsFor:'instance creation'! |
85 |
||
86 |
currentDirectory |
|
87 |
"create and return a new FileDirectory for the current directory" |
|
88 |
||
2951 | 89 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 90 |
^ (self basicNew) pathName:(Filename currentDirectory name) |
88 | 91 |
|
92 |
" |
|
93 |
FileDirectory currentDirectory contents |
|
94 |
FileDirectory currentDirectory files |
|
95 |
FileDirectory currentDirectory isReadable |
|
96 |
FileDirectory currentDirectory pathName |
|
97 |
" |
|
1 | 98 |
! |
99 |
||
605 | 100 |
directoryNamed:name |
101 |
"create and return a new FileDirectory for the directory |
|
102 |
with given pathname" |
|
103 |
||
2951 | 104 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 105 |
name asString = '.' ifTrue:[ |
106 |
Filename currentDirectory name ~= '.' ifTrue:[ |
|
107 |
^ self currentDirectory |
|
108 |
] |
|
109 |
]. |
|
110 |
^ (self basicNew) pathName:(name asFilename constructString:'') |
|
605 | 111 |
|
112 |
" |
|
113 |
(FileDirectory directoryNamed:'..') pathName |
|
114 |
(FileDirectory directoryNamed:'../..') files |
|
115 |
" |
|
116 |
! |
|
117 |
||
1 | 118 |
directoryNamed:name in:aFileDirectory |
119 |
"create and return a new FileDirectory for the directory with given name |
|
120 |
in another FileDirectory" |
|
121 |
||
2961 | 122 |
|baseName| |
1 | 123 |
|
2951 | 124 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 125 |
|
126 |
name asFilename isAbsolute ifTrue:[ |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
127 |
^ self directoryNamed:name |
1 | 128 |
]. |
2921 | 129 |
|
1 | 130 |
(aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
131 |
baseName := aFileDirectory pathName |
1 | 132 |
] ifFalse:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
133 |
baseName := aFileDirectory |
1 | 134 |
]. |
2921 | 135 |
^ (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
|
136 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
137 |
"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
|
138 |
"Modified: 28.4.1997 / 22:34:47 / dq" |
605 | 139 |
! |
140 |
||
141 |
rootDirectory |
|
142 |
"create and return a new FileDirectory for the root directory" |
|
143 |
||
2951 | 144 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 145 |
^ (self basicNew) pathName:(Filename rootDirectory name) |
605 | 146 |
|
147 |
" |
|
148 |
FileDirectory rootDirectory contents |
|
149 |
FileDirectory rootDirectory files |
|
150 |
FileDirectory rootDirectory isReadable |
|
151 |
" |
|
276 | 152 |
! ! |
153 |
||
7305 | 154 |
!FileDirectory class methodsFor:'Compatibility-Squeak'! |
5729 | 155 |
|
7655 | 156 |
default |
157 |
^ (self basicNew) pathName:(Filename defaultDirectory name) |
|
158 |
! |
|
159 |
||
5777 | 160 |
localNameFor:aFileNameString |
161 |
^ aFileNameString asFilename baseName |
|
162 |
! |
|
163 |
||
5729 | 164 |
pathNameDelimiter |
165 |
^ Filename separator |
|
166 |
! ! |
|
167 |
||
276 | 168 |
!FileDirectory class methodsFor:'private'! |
169 |
||
170 |
fullPathNameOf:name in:path |
|
171 |
|sep| |
|
172 |
||
173 |
sep := OperatingSystem fileSeparator asString. |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
174 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
175 |
"/ is it an absolute path ? |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
176 |
|
276 | 177 |
(name startsWith:sep) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
178 |
^ name |
276 | 179 |
]. |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
180 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
181 |
"/ 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
|
182 |
"/ go into OS isAbsolutePath .. |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
183 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
184 |
OperatingSystem isMSDOSlike ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
185 |
('[a-zA-Z]:*' match:name) ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
186 |
"/ drive spec ... |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
187 |
^ name |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
188 |
] |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
189 |
]. |
276 | 190 |
^ path , sep , name |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
191 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
192 |
"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
|
193 |
"Modified: 28.4.1997 / 22:34:18 / dq" |
1 | 194 |
! ! |
195 |
||
7655 | 196 |
!FileDirectory methodsFor:'Compatibility-Squeak'! |
197 |
||
198 |
deleteDirectory:dirName |
|
199 |
"ST80 compatibility" |
|
200 |
||
201 |
^ self removeDirectory:dirName |
|
202 |
! |
|
203 |
||
204 |
deleteFileNamed:fileName |
|
205 |
"ST80 compatibility" |
|
206 |
||
207 |
^ self removeFile:fileName |
|
208 |
! |
|
209 |
||
210 |
directoryNames |
|
211 |
"ST80 compatibility" |
|
212 |
||
213 |
^ self directories |
|
214 |
! |
|
215 |
||
216 |
fileNames |
|
217 |
"ST80 compatibility" |
|
218 |
||
219 |
^ self files |
|
220 |
! |
|
221 |
||
222 |
includesKey:name |
|
223 |
"ST80 compatibility" |
|
224 |
||
225 |
^ self exists:name |
|
226 |
! ! |
|
7305 | 227 |
|
1 | 228 |
!FileDirectory methodsFor:'accessing'! |
229 |
||
230 |
baseName |
|
231 |
"return my baseName |
|
232 |
- thats the directory name without leading parent-dirs" |
|
233 |
||
234 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 235 |
^ pathName asFilename baseName |
1 | 236 |
! |
237 |
||
238 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
239 |
"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
|
240 |
Skips any '.' or '..' entries (UNIX)" |
1 | 241 |
|
242 |
|coll| |
|
243 |
||
244 |
coll := OrderedCollection new. |
|
245 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
246 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
247 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
248 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
249 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
250 |
] |
1 | 251 |
]. |
276 | 252 |
coll sort. |
1 | 253 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
254 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
255 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 256 |
! |
257 |
||
258 |
directories |
|
259 |
"return a collection with all subdirectories in the receiver directory" |
|
260 |
||
261 |
|coll| |
|
262 |
||
263 |
coll := OrderedCollection new. |
|
264 |
self directoriesDo:[:name | |
|
172 | 265 |
coll add:name |
1 | 266 |
]. |
276 | 267 |
coll sort. |
1 | 268 |
^ coll |
269 |
! |
|
270 |
||
605 | 271 |
directoryName |
272 |
"return my directoryName |
|
273 |
- thats the directory name where I'm in" |
|
274 |
||
275 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 276 |
^ pathName asFilename directoryName |
605 | 277 |
! |
278 |
||
1 | 279 |
files |
280 |
"return a collection with all plain files in the receiver directory" |
|
281 |
||
282 |
|coll| |
|
283 |
||
284 |
coll := OrderedCollection new. |
|
285 |
self filesDo:[:name | |
|
172 | 286 |
coll add:name |
1 | 287 |
]. |
276 | 288 |
coll sort. |
289 |
^ coll |
|
605 | 290 |
! |
291 |
||
292 |
pathName |
|
293 |
"return my full pathname" |
|
294 |
||
295 |
lazy ifTrue:[self getFullPathName]. |
|
296 |
^ pathName |
|
297 |
! |
|
298 |
||
299 |
pathName:dirName |
|
300 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
301 |
||
302 |
pathName := dirName. |
|
303 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
304 |
lazy := true |
|
305 |
] ifTrue:[ |
|
306 |
(dirName startsWith:'./') ifFalse:[ |
|
307 |
"/ (dirName includes:$.) ifTrue:[ |
|
308 |
lazy := true |
|
309 |
] |
|
310 |
]. |
|
311 |
^ self |
|
312 |
! ! |
|
313 |
||
314 |
!FileDirectory methodsFor:'basic'! |
|
315 |
||
316 |
createDirectory:newName |
|
317 |
"create a new filedirectory as a subdirectory of myself; |
|
318 |
return true if successful" |
|
319 |
||
320 |
|realName| |
|
321 |
||
322 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
323 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
324 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
325 |
realName := newName copyFrom:2 |
|
326 |
] ifFalse:[ |
|
327 |
realName := newName |
|
328 |
]. |
|
2921 | 329 |
^ (self filenameFor:realName) makeDirectory |
605 | 330 |
] |
331 |
]. |
|
332 |
^ false |
|
333 |
! |
|
334 |
||
335 |
link:oldFileName to:newFileName |
|
336 |
"link oldFileName to newFileName in myself, return true if successful" |
|
337 |
||
338 |
|path1 path2| |
|
339 |
||
340 |
path1 := self pathNameOf:oldFileName. |
|
341 |
path2 := self pathNameOf:newFileName. |
|
342 |
^ OperatingSystem linkFile:path1 to:path2 |
|
343 |
! |
|
344 |
||
345 |
remove:aFileOrDirectoryName |
|
346 |
"remove the file or directory from myself; return true if successful" |
|
347 |
||
2921 | 348 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 349 |
! |
350 |
||
351 |
removeDirectory:dirName |
|
352 |
"remove the directory 'dirName' from myself; return true if successful. |
|
353 |
If the directory is not empty, the containing files/directories are also |
|
354 |
removed." |
|
355 |
||
2921 | 356 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 357 |
! |
358 |
||
359 |
removeFile:fileName |
|
360 |
"remove the file 'fileName' from myself; return true if successful" |
|
361 |
||
2921 | 362 |
^ (self filenameFor:fileName) remove |
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 |
||
7655 | 473 |
ExternalStream openErrorSignal catch:[ |
474 |
aStream := DirectoryStream directoryNamed:pathName. |
|
475 |
]. |
|
605 | 476 |
aStream isNil ifTrue:[^ nil]. |
477 |
[aStream atEnd] whileFalse:[ |
|
7655 | 478 |
name := aStream nextLine. |
479 |
name notNil ifTrue:[ |
|
480 |
(testBlock value:name) ifTrue:[ |
|
481 |
aBlock value:name |
|
482 |
] |
|
483 |
] |
|
605 | 484 |
]. |
485 |
aStream close |
|
486 |
! ! |
|
487 |
||
488 |
!FileDirectory methodsFor:'more instance creation'! |
|
489 |
||
490 |
directoryNamed:aName |
|
2961 | 491 |
"create & return a new fileDirectory for a subdirectory in myself" |
492 |
||
605 | 493 |
^ self class directoryNamed:aName in:self pathName |
494 |
! ! |
|
495 |
||
496 |
!FileDirectory methodsFor:'printing & storing'! |
|
497 |
||
498 |
printOn:aStream |
|
4392 | 499 |
"append a user printed representation of the receiver to aStream. |
500 |
The format is suitable for a human - not meant to be read back." |
|
605 | 501 |
|
502 |
lazy ifTrue:[self getFullPathName]. |
|
503 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
4392 | 504 |
nextPutAll:pathName; |
505 |
nextPutAll:')' |
|
605 | 506 |
! |
507 |
||
508 |
storeOn:aStream |
|
509 |
"append a printed representation of the receiver to aStream, |
|
510 |
which allows reconstructing it via readFrom:" |
|
511 |
||
512 |
lazy ifTrue:[self getFullPathName]. |
|
513 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
514 |
aStream nextPutAll:pathName. |
|
515 |
aStream nextPut:$) |
|
516 |
! ! |
|
517 |
||
1 | 518 |
!FileDirectory methodsFor:'private'! |
519 |
||
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
520 |
filenameFor:fileName |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
521 |
"return a filename representing the argument, fileName |
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
522 |
either in myself (if the arg is a releative path) or absolute otherwise." |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
523 |
|
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
524 |
^ pathName asFilename filenameFor:fileName |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
525 |
! |
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
526 |
|
1 | 527 |
getFullPathName |
528 |
"make my pathname be a full pathname - i.e. starting at root" |
|
529 |
||
308 | 530 |
|shortPathName| |
1 | 531 |
|
2921 | 532 |
|
159 | 533 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 534 |
lazy := false. |
535 |
^ self |
|
1 | 536 |
]. |
537 |
||
538 |
"since currentDirectory is used very often, cache its path here" |
|
539 |
||
540 |
(pathName = '.') ifTrue:[ |
|
172 | 541 |
PathOfCurrentDirectory notNil ifTrue:[ |
542 |
pathName := PathOfCurrentDirectory. |
|
543 |
lazy := false. |
|
544 |
^ self |
|
545 |
] |
|
1 | 546 |
]. |
547 |
||
548 |
shortPathName := pathName. |
|
549 |
||
308 | 550 |
pathName := OperatingSystem pathNameOf:pathName. |
551 |
lazy := false. |
|
1 | 552 |
|
308 | 553 |
" |
554 |
if it was the current dir, keep name for next query |
|
555 |
" |
|
1 | 556 |
(shortPathName = '.') ifTrue:[ |
308 | 557 |
PathOfCurrentDirectory := pathName |
1 | 558 |
] |
276 | 559 |
! |
560 |
||
561 |
pathNameOf:fileName |
|
2961 | 562 |
"return the pathname for an entry in myself" |
563 |
||
2921 | 564 |
^ (pathName asFilename construct:fileName) osName |
1 | 565 |
! ! |
566 |
||
605 | 567 |
!FileDirectory methodsFor:'queries'! |
1 | 568 |
|
605 | 569 |
accessModeOf:name |
570 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 571 |
|
605 | 572 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 573 |
! |
574 |
||
605 | 575 |
changeAccessModeOf:name to:modeBits |
576 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 577 |
|
605 | 578 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 579 |
! |
580 |
||
581 |
exists |
|
582 |
"return true if this directory exists" |
|
583 |
||
2921 | 584 |
^ (Filename named:pathName) isDirectory |
159 | 585 |
|
586 |
" |
|
587 |
(FileDirectory directoryNamed:'fooBar') exists |
|
588 |
(FileDirectory directoryNamed:'/tmp') exists |
|
589 |
" |
|
1 | 590 |
! |
591 |
||
605 | 592 |
exists:name |
593 |
"return true, if the given name exists in myself" |
|
594 |
||
2921 | 595 |
^ (self filenameFor:name) exists |
605 | 596 |
! |
597 |
||
598 |
id |
|
599 |
"return the directories file-id (inode number)" |
|
600 |
||
2921 | 601 |
^ (Filename named:pathName) id |
605 | 602 |
! |
603 |
||
604 |
infoOf:name |
|
605 |
"return an array filled with file info for the file 'aFileName'; |
|
606 |
return nil if such a file does not exist" |
|
607 |
||
2921 | 608 |
^ (self filenameFor:name) info |
605 | 609 |
! |
610 |
||
611 |
isDirectory:name |
|
612 |
"return true, if the given name is that of a directory in myself" |
|
613 |
||
2921 | 614 |
^ (self filenameFor:name) isDirectory |
605 | 615 |
! |
616 |
||
617 |
isExecutable:name |
|
618 |
"return true, if the given file is executable" |
|
1 | 619 |
|
2921 | 620 |
^ (self filenameFor:name) isExecutable |
1 | 621 |
! |
622 |
||
623 |
isReadable:name |
|
624 |
"return true, if the given file is readable" |
|
625 |
||
2921 | 626 |
^ (self filenameFor:name) isReadable |
1 | 627 |
! |
628 |
||
629 |
isWritable:name |
|
630 |
"return true, if the given file is readable" |
|
631 |
||
2921 | 632 |
^ (self filenameFor:name) isWritable |
1 | 633 |
! |
634 |
||
605 | 635 |
species |
6550 | 636 |
"return the type of collection to be returned by collect, select etc." |
637 |
||
605 | 638 |
^ OrderedCollection |
1 | 639 |
! |
640 |
||
605 | 641 |
timeOfLastChange |
642 |
"return the timeStamp of myself" |
|
1 | 643 |
|
2921 | 644 |
^ (Filename named:pathName) modificationTime |
1 | 645 |
! |
646 |
||
605 | 647 |
timeOfLastChange:name |
648 |
"return the timeStamp of a file in myself" |
|
1 | 649 |
|
2921 | 650 |
^ (self filenameFor:name) modificationTime |
1 | 651 |
! |
652 |
||
605 | 653 |
typeOf:name |
654 |
"return the symbolic type of a file in myself" |
|
1 | 655 |
|
2921 | 656 |
^ (self filenameFor:name) type |
605 | 657 |
! ! |
1 | 658 |
|
5556 | 659 |
!FileDirectory methodsFor:'testing'! |
660 |
||
661 |
isEmpty |
|
662 |
"return true, if the directory is empty; |
|
663 |
redefined since '.' and '..' do not count as entries here." |
|
664 |
||
665 |
self do:[:fName | |
|
666 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
|
667 |
]. |
|
668 |
^ true |
|
669 |
! ! |
|
670 |
||
629 | 671 |
!FileDirectory class methodsFor:'documentation'! |
672 |
||
673 |
version |
|
7655 | 674 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.45 2003-10-09 21:19:41 cg Exp $' |
629 | 675 |
! ! |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
676 |
|
605 | 677 |
FileDirectory initialize! |