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