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