author | Claus Gittinger <cg@exept.de> |
Mon, 12 Oct 2009 19:26:08 +0200 | |
changeset 12213 | 73371949b807 |
parent 11816 | 7d1d476d74cc |
child 12351 | 7e0772ce0947 |
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 |
" |
|
5556 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#FileDirectory |
1290 | 15 |
instanceVariableNames:'pathName lazy' |
16 |
classVariableNames:'PathOfCurrentDirectory' |
|
17 |
poolDictionaries:'' |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
18 |
category:'Obsolete' |
1 | 19 |
! |
20 |
||
88 | 21 |
!FileDirectory class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
172 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
39 |
Notice: |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
40 |
This class is obsolete and does work only under UNIX. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
41 |
Use Filename instead. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
42 |
|
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
43 |
This class is not available in other ST-systems; |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
44 |
in contrast, ST-80 provides a Filename class. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
45 |
Therefore, Filename has taken over the functionality. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
46 |
|
88 | 47 |
FileDirectories represent directories in the underlying host system. |
48 |
They provide various methods to create/delete and query for files and/or |
|
202 | 49 |
directories. Also, since FileDirectory inherits from Collection, it |
50 |
provides all enumeration and testing protocol. For example, you can |
|
51 |
loop over the filenames in a directory using 'aFileDirectory do:[:nm | ...]'. |
|
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 |
"/ |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
75 |
something == #earlySystemInstallation ifTrue:[ |
1469 | 76 |
PathOfCurrentDirectory := nil |
2 | 77 |
] |
1469 | 78 |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
79 |
"Created: / 15.6.1996 / 15:20:21 / cg" |
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
80 |
"Modified: / 11.12.1998 / 16:28:40 / cg" |
2 | 81 |
! ! |
82 |
||
1 | 83 |
!FileDirectory class methodsFor:'instance creation'! |
84 |
||
85 |
currentDirectory |
|
86 |
"create and return a new FileDirectory for the current directory" |
|
87 |
||
2951 | 88 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 89 |
^ (self basicNew) pathName:(Filename currentDirectory name) |
88 | 90 |
|
91 |
" |
|
92 |
FileDirectory currentDirectory contents |
|
93 |
FileDirectory currentDirectory files |
|
94 |
FileDirectory currentDirectory isReadable |
|
95 |
FileDirectory currentDirectory pathName |
|
96 |
" |
|
1 | 97 |
! |
98 |
||
605 | 99 |
directoryNamed:name |
100 |
"create and return a new FileDirectory for the directory |
|
101 |
with given pathname" |
|
102 |
||
2951 | 103 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 104 |
name asString = '.' ifTrue:[ |
105 |
Filename currentDirectory name ~= '.' ifTrue:[ |
|
106 |
^ self currentDirectory |
|
107 |
] |
|
108 |
]. |
|
109 |
^ (self basicNew) pathName:(name asFilename constructString:'') |
|
605 | 110 |
|
111 |
" |
|
112 |
(FileDirectory directoryNamed:'..') pathName |
|
113 |
(FileDirectory directoryNamed:'../..') files |
|
114 |
" |
|
115 |
! |
|
116 |
||
1 | 117 |
directoryNamed:name in:aFileDirectory |
118 |
"create and return a new FileDirectory for the directory with given name |
|
119 |
in another FileDirectory" |
|
120 |
||
2961 | 121 |
|baseName| |
1 | 122 |
|
2951 | 123 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 124 |
|
125 |
name asFilename isAbsolute ifTrue:[ |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
126 |
^ self directoryNamed:name |
1 | 127 |
]. |
2921 | 128 |
|
1 | 129 |
(aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
130 |
baseName := aFileDirectory pathName |
1 | 131 |
] ifFalse:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
132 |
baseName := aFileDirectory |
1 | 133 |
]. |
2921 | 134 |
^ (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
|
135 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
136 |
"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
|
137 |
"Modified: 28.4.1997 / 22:34:47 / dq" |
605 | 138 |
! |
139 |
||
140 |
rootDirectory |
|
141 |
"create and return a new FileDirectory for the root directory" |
|
142 |
||
2951 | 143 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 144 |
^ (self basicNew) pathName:(Filename rootDirectory name) |
605 | 145 |
|
146 |
" |
|
147 |
FileDirectory rootDirectory contents |
|
148 |
FileDirectory rootDirectory files |
|
149 |
FileDirectory rootDirectory isReadable |
|
150 |
" |
|
276 | 151 |
! ! |
152 |
||
7305 | 153 |
!FileDirectory class methodsFor:'Compatibility-Squeak'! |
5729 | 154 |
|
7655 | 155 |
default |
156 |
^ (self basicNew) pathName:(Filename defaultDirectory name) |
|
157 |
! |
|
158 |
||
5777 | 159 |
localNameFor:aFileNameString |
160 |
^ aFileNameString asFilename baseName |
|
161 |
! |
|
162 |
||
5729 | 163 |
pathNameDelimiter |
164 |
^ Filename separator |
|
165 |
! ! |
|
166 |
||
276 | 167 |
!FileDirectory class methodsFor:'private'! |
168 |
||
169 |
fullPathNameOf:name in:path |
|
170 |
|sep| |
|
171 |
||
172 |
sep := OperatingSystem fileSeparator asString. |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
173 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
174 |
"/ is it an absolute path ? |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
175 |
|
276 | 176 |
(name startsWith:sep) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
177 |
^ name |
276 | 178 |
]. |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
179 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
180 |
"/ 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
|
181 |
"/ go into OS isAbsolutePath .. |
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 |
OperatingSystem isMSDOSlike ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
184 |
('[a-zA-Z]:*' match:name) ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
185 |
"/ drive spec ... |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
186 |
^ name |
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 |
]. |
276 | 189 |
^ path , sep , name |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
190 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
191 |
"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
|
192 |
"Modified: 28.4.1997 / 22:34:18 / dq" |
1 | 193 |
! ! |
194 |
||
7655 | 195 |
!FileDirectory methodsFor:'Compatibility-Squeak'! |
196 |
||
11816 | 197 |
assureExistence |
198 |
self asFilename recursiveMakeDirectory |
|
199 |
! |
|
200 |
||
201 |
containingDirectory |
|
202 |
^ self class |
|
203 |
directoryNamed:(self asFilename directory pathName) |
|
204 |
! |
|
205 |
||
7655 | 206 |
deleteDirectory:dirName |
9332 | 207 |
"Squeak/ST80 compatibility" |
7655 | 208 |
|
209 |
^ self removeDirectory:dirName |
|
210 |
! |
|
211 |
||
212 |
deleteFileNamed:fileName |
|
9332 | 213 |
"Squeak/ST80 compatibility" |
7655 | 214 |
|
215 |
^ self removeFile:fileName |
|
216 |
! |
|
217 |
||
9332 | 218 |
directoryExists:dir |
219 |
"Squeak/ST80 compatibility" |
|
220 |
||
221 |
|f| |
|
222 |
||
223 |
f := self filenameFor:dir. |
|
224 |
^ f exists and:[f isDirectory ]. |
|
225 |
! |
|
226 |
||
7655 | 227 |
directoryNames |
9332 | 228 |
"Squeak/ST80 compatibility" |
7655 | 229 |
|
230 |
^ self directories |
|
231 |
! |
|
232 |
||
11816 | 233 |
entries |
234 |
^ self contents |
|
235 |
! |
|
236 |
||
9334 | 237 |
fileExists:dir |
238 |
"Squeak/ST80 compatibility" |
|
239 |
||
240 |
|f| |
|
241 |
||
242 |
f := self filenameFor:dir. |
|
243 |
^ f exists and:[f isDirectory not ]. |
|
244 |
! |
|
245 |
||
7655 | 246 |
fileNames |
9332 | 247 |
"Squeak/ST80 compatibility" |
7655 | 248 |
|
249 |
^ self files |
|
250 |
! |
|
251 |
||
11816 | 252 |
forceNewFileNamed:fn |
253 |
"Squeak/ST80 compatibility" |
|
254 |
||
255 |
|f| |
|
256 |
||
257 |
f := self filenameFor:fn. |
|
258 |
^ f writeStream |
|
259 |
! |
|
260 |
||
7655 | 261 |
includesKey:name |
9332 | 262 |
"Squeak/ST80 compatibility" |
7655 | 263 |
|
264 |
^ self exists:name |
|
11816 | 265 |
! |
266 |
||
267 |
readOnlyFileNamed:fn |
|
268 |
"Squeak/ST80 compatibility" |
|
269 |
||
270 |
|f| |
|
271 |
||
272 |
f := self filenameFor:fn. |
|
273 |
^ f readStream |
|
274 |
! |
|
275 |
||
276 |
recursiveDelete |
|
277 |
self asFilename recursiveRemove |
|
7655 | 278 |
! ! |
7305 | 279 |
|
1 | 280 |
!FileDirectory methodsFor:'accessing'! |
281 |
||
282 |
baseName |
|
283 |
"return my baseName |
|
284 |
- thats the directory name without leading parent-dirs" |
|
285 |
||
286 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 287 |
^ pathName asFilename baseName |
1 | 288 |
! |
289 |
||
290 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
291 |
"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
|
292 |
Skips any '.' or '..' entries (UNIX)" |
1 | 293 |
|
294 |
|coll| |
|
295 |
||
296 |
coll := OrderedCollection new. |
|
297 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
298 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
299 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
300 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
301 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
302 |
] |
1 | 303 |
]. |
276 | 304 |
coll sort. |
1 | 305 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
306 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
307 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 308 |
! |
309 |
||
310 |
directories |
|
311 |
"return a collection with all subdirectories in the receiver directory" |
|
312 |
||
313 |
|coll| |
|
314 |
||
315 |
coll := OrderedCollection new. |
|
316 |
self directoriesDo:[:name | |
|
172 | 317 |
coll add:name |
1 | 318 |
]. |
276 | 319 |
coll sort. |
1 | 320 |
^ coll |
321 |
! |
|
322 |
||
605 | 323 |
directoryName |
324 |
"return my directoryName |
|
325 |
- thats the directory name where I'm in" |
|
326 |
||
327 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 328 |
^ pathName asFilename directoryName |
605 | 329 |
! |
330 |
||
1 | 331 |
files |
332 |
"return a collection with all plain files in the receiver directory" |
|
333 |
||
334 |
|coll| |
|
335 |
||
336 |
coll := OrderedCollection new. |
|
337 |
self filesDo:[:name | |
|
172 | 338 |
coll add:name |
1 | 339 |
]. |
276 | 340 |
coll sort. |
341 |
^ coll |
|
605 | 342 |
! |
343 |
||
344 |
pathName |
|
345 |
"return my full pathname" |
|
346 |
||
347 |
lazy ifTrue:[self getFullPathName]. |
|
348 |
^ pathName |
|
349 |
! |
|
350 |
||
351 |
pathName:dirName |
|
352 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
353 |
||
354 |
pathName := dirName. |
|
355 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
356 |
lazy := true |
|
357 |
] ifTrue:[ |
|
358 |
(dirName startsWith:'./') ifFalse:[ |
|
359 |
"/ (dirName includes:$.) ifTrue:[ |
|
360 |
lazy := true |
|
361 |
] |
|
362 |
]. |
|
363 |
^ self |
|
364 |
! ! |
|
365 |
||
366 |
!FileDirectory methodsFor:'basic'! |
|
367 |
||
368 |
createDirectory:newName |
|
369 |
"create a new filedirectory as a subdirectory of myself; |
|
370 |
return true if successful" |
|
371 |
||
372 |
|realName| |
|
373 |
||
374 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
375 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
376 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
377 |
realName := newName copyFrom:2 |
|
378 |
] ifFalse:[ |
|
379 |
realName := newName |
|
380 |
]. |
|
2921 | 381 |
^ (self filenameFor:realName) makeDirectory |
605 | 382 |
] |
383 |
]. |
|
384 |
^ false |
|
385 |
! |
|
386 |
||
387 |
link:oldFileName to:newFileName |
|
388 |
"link oldFileName to newFileName in myself, return true if successful" |
|
389 |
||
390 |
|path1 path2| |
|
391 |
||
392 |
path1 := self pathNameOf:oldFileName. |
|
393 |
path2 := self pathNameOf:newFileName. |
|
394 |
^ OperatingSystem linkFile:path1 to:path2 |
|
395 |
! |
|
396 |
||
397 |
remove:aFileOrDirectoryName |
|
398 |
"remove the file or directory from myself; return true if successful" |
|
399 |
||
2921 | 400 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 401 |
! |
402 |
||
403 |
removeDirectory:dirName |
|
404 |
"remove the directory 'dirName' from myself; return true if successful. |
|
405 |
If the directory is not empty, the containing files/directories are also |
|
406 |
removed." |
|
407 |
||
2921 | 408 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 409 |
! |
410 |
||
411 |
removeFile:fileName |
|
412 |
"remove the file 'fileName' from myself; return true if successful" |
|
413 |
||
2921 | 414 |
^ (self filenameFor:fileName) remove |
605 | 415 |
! |
416 |
||
417 |
renameFile:oldFileName newName:newFileName |
|
418 |
"rename the file; return true if successful" |
|
419 |
||
2921 | 420 |
|f1 f2| |
605 | 421 |
|
2921 | 422 |
f1 := self filenameFor:oldFileName. |
423 |
f2 := self filenameFor:newFileName. |
|
424 |
^ f1 renameTo:f2 |
|
1 | 425 |
! ! |
426 |
||
172 | 427 |
!FileDirectory methodsFor:'converting'! |
428 |
||
429 |
asFilename |
|
430 |
"return myself as a filename" |
|
431 |
||
432 |
^ self pathName asFilename |
|
433 |
! |
|
434 |
||
435 |
asFilename:someFile |
|
436 |
"return a filename for a file named someFile in myself" |
|
437 |
||
276 | 438 |
^ self asFilename construct:someFile |
172 | 439 |
! ! |
440 |
||
605 | 441 |
!FileDirectory methodsFor:'enumerating'! |
442 |
||
443 |
allDirectoriesDo:aBlock |
|
444 |
"evaluate the argument, aBlock for every directory name |
|
445 |
in the directory and in all subdirectories" |
|
446 |
||
447 |
|aStream command line| |
|
448 |
||
449 |
lazy ifTrue:[self getFullPathName]. |
|
450 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
451 |
aStream := PipeStream readingFrom:command. |
|
452 |
aStream isNil ifTrue:[^ nil]. |
|
453 |
[aStream atEnd] whileFalse:[ |
|
454 |
line := aStream nextLine. |
|
455 |
line notNil ifTrue:[ |
|
456 |
(line = '.') ifFalse:[ |
|
457 |
"cut off initial ./" |
|
458 |
line := line copyFrom:3 |
|
459 |
]. |
|
460 |
aBlock value:line |
|
461 |
] |
|
462 |
]. |
|
463 |
aStream close |
|
464 |
! |
|
465 |
||
466 |
allFilesDo:aBlock |
|
467 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
468 |
subdirectories" |
|
469 |
||
470 |
|aStream command line| |
|
471 |
||
472 |
lazy ifTrue:[self getFullPathName]. |
|
473 |
command := 'cd ' , pathName , '; find . -print'. |
|
474 |
aStream := PipeStream readingFrom:command. |
|
475 |
aStream isNil ifTrue:[^ nil]. |
|
476 |
[aStream atEnd] whileFalse:[ |
|
477 |
line := aStream nextLine. |
|
478 |
line notNil ifTrue:[ |
|
479 |
(line = '.') ifFalse:[ |
|
480 |
"cut off initial ./" |
|
481 |
line := line copyFrom:3 |
|
482 |
]. |
|
483 |
aBlock value:line |
|
484 |
] |
|
485 |
]. |
|
486 |
aStream close |
|
487 |
! |
|
488 |
||
489 |
directoriesDo:aBlock |
|
490 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
491 |
||
492 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
493 |
((name ~= '.') and:[name ~= '..']) |
|
494 |
] ifFalse:[ |
|
495 |
false |
|
496 |
] |
|
497 |
] do:aBlock |
|
498 |
! |
|
499 |
||
500 |
do:aBlock |
|
501 |
"evaluate the argument, aBlock for every name in the directory" |
|
502 |
||
503 |
self where:[:name | true] do:aBlock |
|
504 |
! |
|
505 |
||
506 |
filesDo:aBlock |
|
507 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
508 |
||
509 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
510 |
! |
|
511 |
||
512 |
namesDo:aBlock |
|
513 |
"evaluate the argument, aBlock for every name in the directory. |
|
514 |
for ST-80 compatibility" |
|
515 |
||
516 |
self do:aBlock |
|
517 |
! |
|
518 |
||
519 |
where:testBlock do:aBlock |
|
520 |
"evaluate the argument, aBlock for every object in the directory |
|
521 |
for which testBlock evaluates to true." |
|
522 |
||
523 |
|aStream name| |
|
524 |
||
7655 | 525 |
ExternalStream openErrorSignal catch:[ |
526 |
aStream := DirectoryStream directoryNamed:pathName. |
|
527 |
]. |
|
605 | 528 |
aStream isNil ifTrue:[^ nil]. |
529 |
[aStream atEnd] whileFalse:[ |
|
7655 | 530 |
name := aStream nextLine. |
531 |
name notNil ifTrue:[ |
|
532 |
(testBlock value:name) ifTrue:[ |
|
533 |
aBlock value:name |
|
534 |
] |
|
535 |
] |
|
605 | 536 |
]. |
537 |
aStream close |
|
538 |
! ! |
|
539 |
||
540 |
!FileDirectory methodsFor:'more instance creation'! |
|
541 |
||
542 |
directoryNamed:aName |
|
2961 | 543 |
"create & return a new fileDirectory for a subdirectory in myself" |
544 |
||
605 | 545 |
^ self class directoryNamed:aName in:self pathName |
546 |
! ! |
|
547 |
||
548 |
!FileDirectory methodsFor:'printing & storing'! |
|
549 |
||
550 |
printOn:aStream |
|
4392 | 551 |
"append a user printed representation of the receiver to aStream. |
552 |
The format is suitable for a human - not meant to be read back." |
|
605 | 553 |
|
554 |
lazy ifTrue:[self getFullPathName]. |
|
555 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
4392 | 556 |
nextPutAll:pathName; |
557 |
nextPutAll:')' |
|
605 | 558 |
! |
559 |
||
560 |
storeOn:aStream |
|
561 |
"append a printed representation of the receiver to aStream, |
|
562 |
which allows reconstructing it via readFrom:" |
|
563 |
||
564 |
lazy ifTrue:[self getFullPathName]. |
|
565 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
566 |
aStream nextPutAll:pathName. |
|
567 |
aStream nextPut:$) |
|
568 |
! ! |
|
569 |
||
1 | 570 |
!FileDirectory methodsFor:'private'! |
571 |
||
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
572 |
filenameFor:fileName |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
573 |
"return a filename representing the argument, fileName |
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
574 |
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
|
575 |
|
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
576 |
^ pathName asFilename filenameFor:fileName |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
577 |
! |
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
578 |
|
9334 | 579 |
fullNameFor:fileName |
580 |
"return a filename representing the argument, fileName |
|
581 |
either in myself (if the arg is a releative path) or absolute otherwise." |
|
582 |
||
583 |
^ (pathName asFilename filenameFor:fileName) asAbsoluteFilename |
|
584 |
! |
|
585 |
||
1 | 586 |
getFullPathName |
587 |
"make my pathname be a full pathname - i.e. starting at root" |
|
588 |
||
308 | 589 |
|shortPathName| |
1 | 590 |
|
2921 | 591 |
|
159 | 592 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 593 |
lazy := false. |
594 |
^ self |
|
1 | 595 |
]. |
596 |
||
597 |
"since currentDirectory is used very often, cache its path here" |
|
598 |
||
599 |
(pathName = '.') ifTrue:[ |
|
172 | 600 |
PathOfCurrentDirectory notNil ifTrue:[ |
601 |
pathName := PathOfCurrentDirectory. |
|
602 |
lazy := false. |
|
603 |
^ self |
|
604 |
] |
|
1 | 605 |
]. |
606 |
||
607 |
shortPathName := pathName. |
|
608 |
||
308 | 609 |
pathName := OperatingSystem pathNameOf:pathName. |
610 |
lazy := false. |
|
1 | 611 |
|
308 | 612 |
" |
613 |
if it was the current dir, keep name for next query |
|
614 |
" |
|
1 | 615 |
(shortPathName = '.') ifTrue:[ |
308 | 616 |
PathOfCurrentDirectory := pathName |
1 | 617 |
] |
276 | 618 |
! |
619 |
||
620 |
pathNameOf:fileName |
|
2961 | 621 |
"return the pathname for an entry in myself" |
622 |
||
2921 | 623 |
^ (pathName asFilename construct:fileName) osName |
1 | 624 |
! ! |
625 |
||
605 | 626 |
!FileDirectory methodsFor:'queries'! |
1 | 627 |
|
605 | 628 |
accessModeOf:name |
629 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 630 |
|
605 | 631 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 632 |
! |
633 |
||
605 | 634 |
changeAccessModeOf:name to:modeBits |
635 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 636 |
|
605 | 637 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 638 |
! |
639 |
||
640 |
exists |
|
641 |
"return true if this directory exists" |
|
642 |
||
2921 | 643 |
^ (Filename named:pathName) isDirectory |
159 | 644 |
|
645 |
" |
|
646 |
(FileDirectory directoryNamed:'fooBar') exists |
|
647 |
(FileDirectory directoryNamed:'/tmp') exists |
|
648 |
" |
|
1 | 649 |
! |
650 |
||
605 | 651 |
exists:name |
652 |
"return true, if the given name exists in myself" |
|
653 |
||
2921 | 654 |
^ (self filenameFor:name) exists |
605 | 655 |
! |
656 |
||
657 |
id |
|
658 |
"return the directories file-id (inode number)" |
|
659 |
||
2921 | 660 |
^ (Filename named:pathName) id |
605 | 661 |
! |
662 |
||
663 |
infoOf:name |
|
664 |
"return an array filled with file info for the file 'aFileName'; |
|
665 |
return nil if such a file does not exist" |
|
666 |
||
2921 | 667 |
^ (self filenameFor:name) info |
605 | 668 |
! |
669 |
||
670 |
isDirectory:name |
|
671 |
"return true, if the given name is that of a directory in myself" |
|
672 |
||
2921 | 673 |
^ (self filenameFor:name) isDirectory |
605 | 674 |
! |
675 |
||
676 |
isExecutable:name |
|
677 |
"return true, if the given file is executable" |
|
1 | 678 |
|
2921 | 679 |
^ (self filenameFor:name) isExecutable |
1 | 680 |
! |
681 |
||
682 |
isReadable:name |
|
683 |
"return true, if the given file is readable" |
|
684 |
||
2921 | 685 |
^ (self filenameFor:name) isReadable |
1 | 686 |
! |
687 |
||
688 |
isWritable:name |
|
689 |
"return true, if the given file is readable" |
|
690 |
||
2921 | 691 |
^ (self filenameFor:name) isWritable |
1 | 692 |
! |
693 |
||
605 | 694 |
species |
6550 | 695 |
"return the type of collection to be returned by collect, select etc." |
696 |
||
605 | 697 |
^ OrderedCollection |
1 | 698 |
! |
699 |
||
605 | 700 |
timeOfLastChange |
701 |
"return the timeStamp of myself" |
|
1 | 702 |
|
2921 | 703 |
^ (Filename named:pathName) modificationTime |
1 | 704 |
! |
705 |
||
605 | 706 |
timeOfLastChange:name |
707 |
"return the timeStamp of a file in myself" |
|
1 | 708 |
|
2921 | 709 |
^ (self filenameFor:name) modificationTime |
1 | 710 |
! |
711 |
||
605 | 712 |
typeOf:name |
713 |
"return the symbolic type of a file in myself" |
|
1 | 714 |
|
2921 | 715 |
^ (self filenameFor:name) type |
605 | 716 |
! ! |
1 | 717 |
|
5556 | 718 |
!FileDirectory methodsFor:'testing'! |
719 |
||
720 |
isEmpty |
|
721 |
"return true, if the directory is empty; |
|
722 |
redefined since '.' and '..' do not count as entries here." |
|
723 |
||
724 |
self do:[:fName | |
|
725 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
|
726 |
]. |
|
727 |
^ true |
|
728 |
! ! |
|
729 |
||
629 | 730 |
!FileDirectory class methodsFor:'documentation'! |
731 |
||
732 |
version |
|
11816 | 733 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.48 2009-07-30 08:00:27 cg Exp $' |
629 | 734 |
! ! |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
735 |
|
605 | 736 |
FileDirectory initialize! |