author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 9334 | c46a6c457feb |
child 17728 | bbc5fa73dfab |
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 |
|
9332 | 199 |
"Squeak/ST80 compatibility" |
7655 | 200 |
|
201 |
^ self removeDirectory:dirName |
|
202 |
! |
|
203 |
||
204 |
deleteFileNamed:fileName |
|
9332 | 205 |
"Squeak/ST80 compatibility" |
7655 | 206 |
|
207 |
^ self removeFile:fileName |
|
208 |
! |
|
209 |
||
9332 | 210 |
directoryExists:dir |
211 |
"Squeak/ST80 compatibility" |
|
212 |
||
213 |
|f| |
|
214 |
||
215 |
f := self filenameFor:dir. |
|
216 |
^ f exists and:[f isDirectory ]. |
|
217 |
! |
|
218 |
||
7655 | 219 |
directoryNames |
9332 | 220 |
"Squeak/ST80 compatibility" |
7655 | 221 |
|
222 |
^ self directories |
|
223 |
! |
|
224 |
||
9334 | 225 |
fileExists:dir |
226 |
"Squeak/ST80 compatibility" |
|
227 |
||
228 |
|f| |
|
229 |
||
230 |
f := self filenameFor:dir. |
|
231 |
^ f exists and:[f isDirectory not ]. |
|
232 |
! |
|
233 |
||
7655 | 234 |
fileNames |
9332 | 235 |
"Squeak/ST80 compatibility" |
7655 | 236 |
|
237 |
^ self files |
|
238 |
! |
|
239 |
||
240 |
includesKey:name |
|
9332 | 241 |
"Squeak/ST80 compatibility" |
7655 | 242 |
|
243 |
^ self exists:name |
|
244 |
! ! |
|
7305 | 245 |
|
1 | 246 |
!FileDirectory methodsFor:'accessing'! |
247 |
||
248 |
baseName |
|
249 |
"return my baseName |
|
250 |
- thats the directory name without leading parent-dirs" |
|
251 |
||
252 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 253 |
^ pathName asFilename baseName |
1 | 254 |
! |
255 |
||
256 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
257 |
"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
|
258 |
Skips any '.' or '..' entries (UNIX)" |
1 | 259 |
|
260 |
|coll| |
|
261 |
||
262 |
coll := OrderedCollection new. |
|
263 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
264 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
265 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
266 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
267 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
268 |
] |
1 | 269 |
]. |
276 | 270 |
coll sort. |
1 | 271 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
272 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
273 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 274 |
! |
275 |
||
276 |
directories |
|
277 |
"return a collection with all subdirectories in the receiver directory" |
|
278 |
||
279 |
|coll| |
|
280 |
||
281 |
coll := OrderedCollection new. |
|
282 |
self directoriesDo:[:name | |
|
172 | 283 |
coll add:name |
1 | 284 |
]. |
276 | 285 |
coll sort. |
1 | 286 |
^ coll |
287 |
! |
|
288 |
||
605 | 289 |
directoryName |
290 |
"return my directoryName |
|
291 |
- thats the directory name where I'm in" |
|
292 |
||
293 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 294 |
^ pathName asFilename directoryName |
605 | 295 |
! |
296 |
||
1 | 297 |
files |
298 |
"return a collection with all plain files in the receiver directory" |
|
299 |
||
300 |
|coll| |
|
301 |
||
302 |
coll := OrderedCollection new. |
|
303 |
self filesDo:[:name | |
|
172 | 304 |
coll add:name |
1 | 305 |
]. |
276 | 306 |
coll sort. |
307 |
^ coll |
|
605 | 308 |
! |
309 |
||
310 |
pathName |
|
311 |
"return my full pathname" |
|
312 |
||
313 |
lazy ifTrue:[self getFullPathName]. |
|
314 |
^ pathName |
|
315 |
! |
|
316 |
||
317 |
pathName:dirName |
|
318 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
319 |
||
320 |
pathName := dirName. |
|
321 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
322 |
lazy := true |
|
323 |
] ifTrue:[ |
|
324 |
(dirName startsWith:'./') ifFalse:[ |
|
325 |
"/ (dirName includes:$.) ifTrue:[ |
|
326 |
lazy := true |
|
327 |
] |
|
328 |
]. |
|
329 |
^ self |
|
330 |
! ! |
|
331 |
||
332 |
!FileDirectory methodsFor:'basic'! |
|
333 |
||
334 |
createDirectory:newName |
|
335 |
"create a new filedirectory as a subdirectory of myself; |
|
336 |
return true if successful" |
|
337 |
||
338 |
|realName| |
|
339 |
||
340 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
341 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
342 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
343 |
realName := newName copyFrom:2 |
|
344 |
] ifFalse:[ |
|
345 |
realName := newName |
|
346 |
]. |
|
2921 | 347 |
^ (self filenameFor:realName) makeDirectory |
605 | 348 |
] |
349 |
]. |
|
350 |
^ false |
|
351 |
! |
|
352 |
||
353 |
link:oldFileName to:newFileName |
|
354 |
"link oldFileName to newFileName in myself, return true if successful" |
|
355 |
||
356 |
|path1 path2| |
|
357 |
||
358 |
path1 := self pathNameOf:oldFileName. |
|
359 |
path2 := self pathNameOf:newFileName. |
|
360 |
^ OperatingSystem linkFile:path1 to:path2 |
|
361 |
! |
|
362 |
||
363 |
remove:aFileOrDirectoryName |
|
364 |
"remove the file or directory from myself; return true if successful" |
|
365 |
||
2921 | 366 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 367 |
! |
368 |
||
369 |
removeDirectory:dirName |
|
370 |
"remove the directory 'dirName' from myself; return true if successful. |
|
371 |
If the directory is not empty, the containing files/directories are also |
|
372 |
removed." |
|
373 |
||
2921 | 374 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 375 |
! |
376 |
||
377 |
removeFile:fileName |
|
378 |
"remove the file 'fileName' from myself; return true if successful" |
|
379 |
||
2921 | 380 |
^ (self filenameFor:fileName) remove |
605 | 381 |
! |
382 |
||
383 |
renameFile:oldFileName newName:newFileName |
|
384 |
"rename the file; return true if successful" |
|
385 |
||
2921 | 386 |
|f1 f2| |
605 | 387 |
|
2921 | 388 |
f1 := self filenameFor:oldFileName. |
389 |
f2 := self filenameFor:newFileName. |
|
390 |
^ f1 renameTo:f2 |
|
1 | 391 |
! ! |
392 |
||
172 | 393 |
!FileDirectory methodsFor:'converting'! |
394 |
||
395 |
asFilename |
|
396 |
"return myself as a filename" |
|
397 |
||
398 |
^ self pathName asFilename |
|
399 |
! |
|
400 |
||
401 |
asFilename:someFile |
|
402 |
"return a filename for a file named someFile in myself" |
|
403 |
||
276 | 404 |
^ self asFilename construct:someFile |
172 | 405 |
! ! |
406 |
||
605 | 407 |
!FileDirectory methodsFor:'enumerating'! |
408 |
||
409 |
allDirectoriesDo:aBlock |
|
410 |
"evaluate the argument, aBlock for every directory name |
|
411 |
in the directory and in all subdirectories" |
|
412 |
||
413 |
|aStream command line| |
|
414 |
||
415 |
lazy ifTrue:[self getFullPathName]. |
|
416 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
417 |
aStream := PipeStream readingFrom:command. |
|
418 |
aStream isNil ifTrue:[^ nil]. |
|
419 |
[aStream atEnd] whileFalse:[ |
|
420 |
line := aStream nextLine. |
|
421 |
line notNil ifTrue:[ |
|
422 |
(line = '.') ifFalse:[ |
|
423 |
"cut off initial ./" |
|
424 |
line := line copyFrom:3 |
|
425 |
]. |
|
426 |
aBlock value:line |
|
427 |
] |
|
428 |
]. |
|
429 |
aStream close |
|
430 |
! |
|
431 |
||
432 |
allFilesDo:aBlock |
|
433 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
434 |
subdirectories" |
|
435 |
||
436 |
|aStream command line| |
|
437 |
||
438 |
lazy ifTrue:[self getFullPathName]. |
|
439 |
command := 'cd ' , pathName , '; find . -print'. |
|
440 |
aStream := PipeStream readingFrom:command. |
|
441 |
aStream isNil ifTrue:[^ nil]. |
|
442 |
[aStream atEnd] whileFalse:[ |
|
443 |
line := aStream nextLine. |
|
444 |
line notNil ifTrue:[ |
|
445 |
(line = '.') ifFalse:[ |
|
446 |
"cut off initial ./" |
|
447 |
line := line copyFrom:3 |
|
448 |
]. |
|
449 |
aBlock value:line |
|
450 |
] |
|
451 |
]. |
|
452 |
aStream close |
|
453 |
! |
|
454 |
||
455 |
directoriesDo:aBlock |
|
456 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
457 |
||
458 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
459 |
((name ~= '.') and:[name ~= '..']) |
|
460 |
] ifFalse:[ |
|
461 |
false |
|
462 |
] |
|
463 |
] do:aBlock |
|
464 |
! |
|
465 |
||
466 |
do:aBlock |
|
467 |
"evaluate the argument, aBlock for every name in the directory" |
|
468 |
||
469 |
self where:[:name | true] do:aBlock |
|
470 |
! |
|
471 |
||
472 |
filesDo:aBlock |
|
473 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
474 |
||
475 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
476 |
! |
|
477 |
||
478 |
namesDo:aBlock |
|
479 |
"evaluate the argument, aBlock for every name in the directory. |
|
480 |
for ST-80 compatibility" |
|
481 |
||
482 |
self do:aBlock |
|
483 |
! |
|
484 |
||
485 |
where:testBlock do:aBlock |
|
486 |
"evaluate the argument, aBlock for every object in the directory |
|
487 |
for which testBlock evaluates to true." |
|
488 |
||
489 |
|aStream name| |
|
490 |
||
7655 | 491 |
ExternalStream openErrorSignal catch:[ |
492 |
aStream := DirectoryStream directoryNamed:pathName. |
|
493 |
]. |
|
605 | 494 |
aStream isNil ifTrue:[^ nil]. |
495 |
[aStream atEnd] whileFalse:[ |
|
7655 | 496 |
name := aStream nextLine. |
497 |
name notNil ifTrue:[ |
|
498 |
(testBlock value:name) ifTrue:[ |
|
499 |
aBlock value:name |
|
500 |
] |
|
501 |
] |
|
605 | 502 |
]. |
503 |
aStream close |
|
504 |
! ! |
|
505 |
||
506 |
!FileDirectory methodsFor:'more instance creation'! |
|
507 |
||
508 |
directoryNamed:aName |
|
2961 | 509 |
"create & return a new fileDirectory for a subdirectory in myself" |
510 |
||
605 | 511 |
^ self class directoryNamed:aName in:self pathName |
512 |
! ! |
|
513 |
||
514 |
!FileDirectory methodsFor:'printing & storing'! |
|
515 |
||
516 |
printOn:aStream |
|
4392 | 517 |
"append a user printed representation of the receiver to aStream. |
518 |
The format is suitable for a human - not meant to be read back." |
|
605 | 519 |
|
520 |
lazy ifTrue:[self getFullPathName]. |
|
521 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
4392 | 522 |
nextPutAll:pathName; |
523 |
nextPutAll:')' |
|
605 | 524 |
! |
525 |
||
526 |
storeOn:aStream |
|
527 |
"append a printed representation of the receiver to aStream, |
|
528 |
which allows reconstructing it via readFrom:" |
|
529 |
||
530 |
lazy ifTrue:[self getFullPathName]. |
|
531 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
532 |
aStream nextPutAll:pathName. |
|
533 |
aStream nextPut:$) |
|
534 |
! ! |
|
535 |
||
1 | 536 |
!FileDirectory methodsFor:'private'! |
537 |
||
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
538 |
filenameFor:fileName |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
539 |
"return a filename representing the argument, fileName |
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
540 |
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
|
541 |
|
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
542 |
^ pathName asFilename filenameFor:fileName |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
543 |
! |
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
544 |
|
9334 | 545 |
fullNameFor:fileName |
546 |
"return a filename representing the argument, fileName |
|
547 |
either in myself (if the arg is a releative path) or absolute otherwise." |
|
548 |
||
549 |
^ (pathName asFilename filenameFor:fileName) asAbsoluteFilename |
|
550 |
! |
|
551 |
||
1 | 552 |
getFullPathName |
553 |
"make my pathname be a full pathname - i.e. starting at root" |
|
554 |
||
308 | 555 |
|shortPathName| |
1 | 556 |
|
2921 | 557 |
|
159 | 558 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 559 |
lazy := false. |
560 |
^ self |
|
1 | 561 |
]. |
562 |
||
563 |
"since currentDirectory is used very often, cache its path here" |
|
564 |
||
565 |
(pathName = '.') ifTrue:[ |
|
172 | 566 |
PathOfCurrentDirectory notNil ifTrue:[ |
567 |
pathName := PathOfCurrentDirectory. |
|
568 |
lazy := false. |
|
569 |
^ self |
|
570 |
] |
|
1 | 571 |
]. |
572 |
||
573 |
shortPathName := pathName. |
|
574 |
||
308 | 575 |
pathName := OperatingSystem pathNameOf:pathName. |
576 |
lazy := false. |
|
1 | 577 |
|
308 | 578 |
" |
579 |
if it was the current dir, keep name for next query |
|
580 |
" |
|
1 | 581 |
(shortPathName = '.') ifTrue:[ |
308 | 582 |
PathOfCurrentDirectory := pathName |
1 | 583 |
] |
276 | 584 |
! |
585 |
||
586 |
pathNameOf:fileName |
|
2961 | 587 |
"return the pathname for an entry in myself" |
588 |
||
2921 | 589 |
^ (pathName asFilename construct:fileName) osName |
1 | 590 |
! ! |
591 |
||
605 | 592 |
!FileDirectory methodsFor:'queries'! |
1 | 593 |
|
605 | 594 |
accessModeOf:name |
595 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 596 |
|
605 | 597 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 598 |
! |
599 |
||
605 | 600 |
changeAccessModeOf:name to:modeBits |
601 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 602 |
|
605 | 603 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 604 |
! |
605 |
||
606 |
exists |
|
607 |
"return true if this directory exists" |
|
608 |
||
2921 | 609 |
^ (Filename named:pathName) isDirectory |
159 | 610 |
|
611 |
" |
|
612 |
(FileDirectory directoryNamed:'fooBar') exists |
|
613 |
(FileDirectory directoryNamed:'/tmp') exists |
|
614 |
" |
|
1 | 615 |
! |
616 |
||
605 | 617 |
exists:name |
618 |
"return true, if the given name exists in myself" |
|
619 |
||
2921 | 620 |
^ (self filenameFor:name) exists |
605 | 621 |
! |
622 |
||
623 |
id |
|
624 |
"return the directories file-id (inode number)" |
|
625 |
||
2921 | 626 |
^ (Filename named:pathName) id |
605 | 627 |
! |
628 |
||
629 |
infoOf:name |
|
630 |
"return an array filled with file info for the file 'aFileName'; |
|
631 |
return nil if such a file does not exist" |
|
632 |
||
2921 | 633 |
^ (self filenameFor:name) info |
605 | 634 |
! |
635 |
||
636 |
isDirectory:name |
|
637 |
"return true, if the given name is that of a directory in myself" |
|
638 |
||
2921 | 639 |
^ (self filenameFor:name) isDirectory |
605 | 640 |
! |
641 |
||
642 |
isExecutable:name |
|
643 |
"return true, if the given file is executable" |
|
1 | 644 |
|
2921 | 645 |
^ (self filenameFor:name) isExecutable |
1 | 646 |
! |
647 |
||
648 |
isReadable:name |
|
649 |
"return true, if the given file is readable" |
|
650 |
||
2921 | 651 |
^ (self filenameFor:name) isReadable |
1 | 652 |
! |
653 |
||
654 |
isWritable:name |
|
655 |
"return true, if the given file is readable" |
|
656 |
||
2921 | 657 |
^ (self filenameFor:name) isWritable |
1 | 658 |
! |
659 |
||
605 | 660 |
species |
6550 | 661 |
"return the type of collection to be returned by collect, select etc." |
662 |
||
605 | 663 |
^ OrderedCollection |
1 | 664 |
! |
665 |
||
605 | 666 |
timeOfLastChange |
667 |
"return the timeStamp of myself" |
|
1 | 668 |
|
2921 | 669 |
^ (Filename named:pathName) modificationTime |
1 | 670 |
! |
671 |
||
605 | 672 |
timeOfLastChange:name |
673 |
"return the timeStamp of a file in myself" |
|
1 | 674 |
|
2921 | 675 |
^ (self filenameFor:name) modificationTime |
1 | 676 |
! |
677 |
||
605 | 678 |
typeOf:name |
679 |
"return the symbolic type of a file in myself" |
|
1 | 680 |
|
2921 | 681 |
^ (self filenameFor:name) type |
605 | 682 |
! ! |
1 | 683 |
|
5556 | 684 |
!FileDirectory methodsFor:'testing'! |
685 |
||
686 |
isEmpty |
|
687 |
"return true, if the directory is empty; |
|
688 |
redefined since '.' and '..' do not count as entries here." |
|
689 |
||
690 |
self do:[:fName | |
|
691 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
|
692 |
]. |
|
693 |
^ true |
|
694 |
! ! |
|
695 |
||
629 | 696 |
!FileDirectory class methodsFor:'documentation'! |
697 |
||
698 |
version |
|
17711 | 699 |
^ '$Id: FileDirectory.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
629 | 700 |
! ! |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
701 |
|
605 | 702 |
FileDirectory initialize! |