author | fm |
Fri, 27 Mar 2009 14:06:24 +0100 | |
changeset 2130 | dcbfec8558f9 |
parent 2083 | fb059bbd81e6 |
child 2133 | a739d816088e |
permissions | -rw-r--r-- |
583 | 1 |
" |
2 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
904 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
583 | 14 |
Object subclass:#DirectoryContents |
2079 | 15 |
instanceVariableNames:'directory timeStamp contents isReadable isRootDirectory accessKey' |
957 | 16 |
classVariableNames:'CachedDirectories LockSema ReadersList' |
583 | 17 |
poolDictionaries:'' |
18 |
category:'System-Support' |
|
19 |
! |
|
20 |
||
1093 | 21 |
Object subclass:#DirectoryContentsItem |
1099 | 22 |
instanceVariableNames:'info fileName' |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
23 |
classVariableNames:'CachedRemoteMountPoints CachedRemoteMountPointsTimeStamp' |
1093 | 24 |
poolDictionaries:'' |
25 |
privateIn:DirectoryContents |
|
26 |
! |
|
27 |
||
583 | 28 |
!DirectoryContents class methodsFor:'documentation'! |
29 |
||
30 |
copyright |
|
31 |
" |
|
32 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
33 |
All Rights Reserved |
|
34 |
||
35 |
This software is furnished under a license and may be used |
|
36 |
only in accordance with the terms of that license and with the |
|
37 |
inclusion of the above copyright notice. This software may not |
|
38 |
be provided or otherwise made available to, or used by, any |
|
39 |
other person. No title to or ownership of the software is |
|
40 |
hereby transferred. |
|
41 |
" |
|
42 |
||
43 |
||
44 |
! |
|
45 |
||
46 |
documentation |
|
47 |
" |
|
48 |
DirectoryContents provides a cached view onto a fileDirectory. |
|
49 |
||
50 |
||
51 |
Notice: |
|
52 |
This class is not available in other ST-systems; |
|
53 |
Applications using it may not be portable. |
|
54 |
||
55 |
[author:] |
|
56 |
Claus Atzkern |
|
57 |
||
58 |
[see also:] |
|
59 |
Filename |
|
60 |
FileStream DirectoryStream OperatingSystem |
|
61 |
" |
|
62 |
||
63 |
! ! |
|
64 |
||
957 | 65 |
!DirectoryContents class methodsFor:'initialization'! |
66 |
||
67 |
initialize |
|
68 |
"setup lock-mechanism |
|
69 |
" |
|
70 |
LockSema := RecursionLock new. |
|
71 |
ReadersList := Dictionary new. |
|
72 |
! ! |
|
73 |
||
1093 | 74 |
!DirectoryContents class methodsFor:'instance creation'! |
75 |
||
76 |
new |
|
77 |
^ self basicNew initialize |
|
78 |
! ! |
|
79 |
||
583 | 80 |
!DirectoryContents class methodsFor:'accessing'! |
81 |
||
2080 | 82 |
cachedDirectoryNamed:aFileOrString |
83 |
"answer the valid cached directory or nil" |
|
84 |
||
85 |
^ self directoryAt:aFileOrString |
|
86 |
! |
|
87 |
||
957 | 88 |
directoryNamed:aDirectory |
583 | 89 |
"returns the DirectoryContents for a directory named |
957 | 90 |
aDirectoryName, aString, nil or Filename |
583 | 91 |
" |
2081 | 92 |
|directory contents lockRead pathName addToList| |
583 | 93 |
|
2078 | 94 |
aDirectory isNil ifTrue:[^ nil]. |
95 |
||
96 |
directory := aDirectory asFilename. |
|
97 |
directory exists ifFalse:[ ^ nil ]. |
|
98 |
||
1105 | 99 |
directory := directory asAbsoluteFilename. |
957 | 100 |
contents := nil. |
620 | 101 |
|
957 | 102 |
LockSema critical:[ |
103 |
CachedDirectories notNil ifTrue:[ |
|
2081 | 104 |
"/ remove all obsolete directories - without doing a system call |
105 |
CachedDirectories := CachedDirectories select:[:aDir| aDir timeStamp notNil ]. |
|
106 |
||
957 | 107 |
contents := self directoryAt:directory. |
108 |
||
109 |
contents isNil ifTrue:[ |
|
2081 | 110 |
|n max| |
2078 | 111 |
|
957 | 112 |
max := self maxCachedDirectories. |
583 | 113 |
|
957 | 114 |
CachedDirectories size > max ifTrue:[ |
2081 | 115 |
"/ remove directories with contents less 64 and not obsolete (system call) |
116 |
CachedDirectories := CachedDirectories |
|
117 |
select:[:aDir| (aDir size > 64 and:[aDir isObsolete not]) ]. |
|
2078 | 118 |
|
2081 | 119 |
n := CachedDirectories size + 2 - max. |
120 |
n > 0 ifTrue:[ |
|
121 |
"/ remove oldest directories |
|
122 |
CachedDirectories removeLast:n. |
|
2078 | 123 |
]. |
957 | 124 |
]. |
125 |
]. |
|
583 | 126 |
]. |
957 | 127 |
contents isNil ifTrue:[ |
128 |
ReadersList isNil ifTrue:[ReadersList := Dictionary new]. |
|
129 |
pathName := directory pathName. |
|
130 |
lockRead := ReadersList at:pathName ifAbsentPut:[Semaphore forMutualExclusion]. |
|
131 |
] |
|
132 |
]. |
|
133 |
contents notNil ifTrue:[^ contents]. |
|
134 |
addToList := false. |
|
583 | 135 |
|
957 | 136 |
lockRead critical:[ |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
137 |
[ "/ test whether another task got the semaphore and |
957 | 138 |
"/ has read the directory contents |
139 |
(contents := self directoryAt:directory) isNil ifTrue:[ |
|
140 |
"/ read the directory contents |
|
141 |
contents := self new directory:directory. |
|
142 |
"/ only cache if the mod'Time is valid. |
|
143 |
addToList := contents timeStamp notNil. |
|
144 |
] |
|
1022
6a288db5312a
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
974
diff
changeset
|
145 |
] ensure:[ |
957 | 146 |
LockSema critical:[ |
147 |
addToList ifTrue:[ |
|
148 |
CachedDirectories isNil ifTrue:[ |
|
149 |
CachedDirectories := OrderedCollection new |
|
150 |
]. |
|
151 |
CachedDirectories add:contents |
|
152 |
]. |
|
153 |
(lockRead isEmpty and:[ReadersList notNil]) ifTrue:[ |
|
154 |
ReadersList removeKey:pathName ifAbsent:nil |
|
155 |
] |
|
583 | 156 |
] |
157 |
] |
|
957 | 158 |
]. |
620 | 159 |
|
957 | 160 |
^ contents |
583 | 161 |
! ! |
162 |
||
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
163 |
!DirectoryContents class methodsFor:'cache flushing'! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
164 |
|
880 | 165 |
flushCache |
166 |
"flush list of rememebred directory contents" |
|
167 |
||
957 | 168 |
LockSema critical:[ CachedDirectories := nil ]. |
880 | 169 |
|
170 |
" |
|
171 |
self flushCache |
|
172 |
" |
|
173 |
||
174 |
"Created: / 11.2.2000 / 00:13:59 / cg" |
|
175 |
! |
|
176 |
||
1105 | 177 |
flushCachedDirectoryFor:aDirectoryOrString |
958 | 178 |
"remove directory from cache |
179 |
" |
|
2130 | 180 |
|directory| |
958 | 181 |
|
2130 | 182 |
aDirectoryOrString isNil ifTrue:[^ self ]. |
958 | 183 |
|
2078 | 184 |
directory := aDirectoryOrString asFilename. |
185 |
directory isSymbolicLink ifTrue:[^ self]. |
|
958 | 186 |
|
2130 | 187 |
self |
188 |
directoryAt:directory |
|
189 |
checkForValidContentsDo:[:aDirectory| false ]. "/ should be removed from cache |
|
958 | 190 |
! |
191 |
||
695
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
192 |
lowSpaceCleanup |
1575 | 193 |
"flush list of remembered directory contents when low on memory" |
695
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
194 |
|
1134 | 195 |
self flushCache |
695
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
196 |
|
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
197 |
" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
198 |
self lowSpaceCleanup |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
199 |
" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
200 |
|
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
201 |
"Created: / 18.2.1998 / 18:17:05 / cg" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
202 |
"Modified: / 24.9.1998 / 17:51:15 / cg" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
203 |
! |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
204 |
|
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
205 |
preSnapshot |
1919 | 206 |
"flush list of cached directory contents before saving a snapshot" |
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
207 |
|
1349 | 208 |
self flushCache. |
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
209 |
! ! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
210 |
|
583 | 211 |
!DirectoryContents class methodsFor:'constants'! |
212 |
||
213 |
maxCachedDirectories |
|
214 |
"returns number of maximum cached directories |
|
215 |
" |
|
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
216 |
^ 20 |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
217 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
218 |
"Modified: / 25.2.1998 / 19:56:24 / cg" |
583 | 219 |
! ! |
220 |
||
221 |
!DirectoryContents class methodsFor:'private'! |
|
222 |
||
2079 | 223 |
accessKeyForDirectory:aDirectoryOrString |
224 |
"computes a fast access key to retrieve the directory in the cache |
|
225 |
" |
|
226 |
|key last| |
|
227 |
||
228 |
aDirectoryOrString isNil ifTrue:[^ nil ]. |
|
229 |
||
230 |
aDirectoryOrString isFilename ifTrue:[ key := aDirectoryOrString nameString ] |
|
231 |
ifFalse:[ key := aDirectoryOrString ]. |
|
232 |
||
233 |
key size > 1 ifTrue:[ |
|
234 |
last := key last. |
|
235 |
(last == $/ or:[last == $\]) ifTrue:[ |
|
236 |
^ key copyWithoutLast:1. |
|
237 |
]. |
|
238 |
]. |
|
239 |
^ key |
|
240 |
! |
|
241 |
||
2078 | 242 |
directoryAt:aFileOrString |
583 | 243 |
"checks whether directory already exists and is valid. |
244 |
If true the directory is returned otherwise nil |
|
245 |
" |
|
2130 | 246 |
^ self |
247 |
directoryAt:aFileOrString |
|
248 |
checkForValidContentsDo:[:aContents| aContents isObsolete not ]. |
|
249 |
! |
|
250 |
||
251 |
directoryAt:aFileOrString checkForValidContentsDo:checkIsValidBlock |
|
252 |
"answer the directoryContents stored under aFileOrString in the cache. |
|
253 |
If the evaluation of the checkIsValidBlock returns false, the contents |
|
254 |
will be removed from the cache and nil is returned." |
|
255 |
||
2079 | 256 |
|index directory file fastKey| |
583 | 257 |
|
2079 | 258 |
fastKey := self accessKeyForDirectory:aFileOrString. |
259 |
fastKey isNil ifTrue:[^ nil ]. "/ the name is nil |
|
2078 | 260 |
|
957 | 261 |
directory := nil. |
262 |
||
263 |
LockSema critical:[ |
|
264 |
CachedDirectories notNil ifTrue:[ |
|
2079 | 265 |
index := CachedDirectories findFirst:[:d| d accessKey = fastKey ]. |
266 |
||
2078 | 267 |
index == 0 ifTrue:[ |
2079 | 268 |
file := aFileOrString asFilename asAbsoluteFilename. |
269 |
index := CachedDirectories findFirst:[:d| d directory = file ]. |
|
2078 | 270 |
]. |
583 | 271 |
|
957 | 272 |
index ~~ 0 ifTrue:[ |
2078 | 273 |
directory := CachedDirectories removeAtIndex:index. |
583 | 274 |
|
2130 | 275 |
(checkIsValidBlock value:directory) ifTrue:[ |
2078 | 276 |
"/ keep the last accessed directory at end |
277 |
CachedDirectories add:directory. |
|
278 |
]. |
|
957 | 279 |
] |
583 | 280 |
] |
281 |
]. |
|
957 | 282 |
^ directory |
583 | 283 |
! ! |
284 |
||
285 |
!DirectoryContents class methodsFor:'queries'! |
|
286 |
||
287 |
directoryNamed:aDirectoryName detect:aTwoArgBlock |
|
288 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
289 |
contents of a directory named aDirectoryName, until the block |
|
290 |
returns true. If nothing detected false is returned |
|
291 |
" |
|
2078 | 292 |
^ self directoryNamed:aDirectoryName detect:aTwoArgBlock onOpenErrorDo:nil |
293 |
! |
|
294 |
||
295 |
directoryNamed:aDirectoryName detect:aTwoArgBlock onOpenErrorDo:exceptionBlock |
|
296 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
297 |
contents of a directory named aDirectoryName, until the block |
|
298 |
returns true. If nothing detected false is returned. |
|
299 |
||
300 |
if the directory cannot be open, the exceptionBlock is processed |
|
301 |
with the filename. |
|
302 |
" |
|
303 |
|directory contents isReadable file| |
|
583 | 304 |
|
305 |
directory := aDirectoryName asFilename. |
|
2078 | 306 |
contents := self directoryAt:directory. |
620 | 307 |
|
2078 | 308 |
contents notNil ifTrue:[ |
309 |
(isReadable := contents isReadable) ifTrue:[ |
|
310 |
contents contentsDo:[:aFile :isDir| |
|
311 |
(aTwoArgBlock value:aFile value:isDir) ifTrue:[^ true] |
|
312 |
]. |
|
313 |
^ false. |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
314 |
]. |
2078 | 315 |
] ifFalse:[ |
316 |
(isReadable := directory exists) ifTrue:[ |
|
317 |
[ |
|
318 |
directory directoryContentsDo:[:fn | |
|
319 |
file := directory construct:fn. |
|
320 |
(aTwoArgBlock value:file value:(file isDirectory)) ifTrue:[ |
|
321 |
^ true |
|
322 |
] |
|
323 |
]. |
|
324 |
] on:FileStream openErrorSignal do:[:ex| |
|
325 |
isReadable := false. |
|
326 |
]. |
|
327 |
]. |
|
583 | 328 |
]. |
2078 | 329 |
(isReadable not and:[exceptionBlock notNil]) ifTrue:[ |
330 |
exceptionBlock value:directory. |
|
331 |
]. |
|
583 | 332 |
^ false |
333 |
! ! |
|
334 |
||
1102 | 335 |
!DirectoryContents class methodsFor:'utilities'! |
336 |
||
337 |
contentsItemForFileName:aFilenameOrString |
|
338 |
| aFilename directory directoryContents| |
|
339 |
||
340 |
aFilename := aFilenameOrString asFilename. |
|
341 |
directory := aFilename directory. |
|
2078 | 342 |
|
343 |
directoryContents := self directoryAt:directory. |
|
344 |
||
345 |
directoryContents notNil ifTrue:[ |
|
346 |
directoryContents itemsDo:[:fileItemThere | |
|
347 |
fileItemThere fileName = aFilename ifTrue:[ |
|
348 |
^ fileItemThere. |
|
349 |
]. |
|
1393
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
350 |
]. |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
351 |
]. |
1252
933afe6d5bd6
care for directory being removed
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
352 |
|
2078 | 353 |
aFilename exists ifFalse:[ |
354 |
directoryContents notNil ifTrue:[ directoryContents beObsolete ]. |
|
355 |
^ nil |
|
1102 | 356 |
]. |
1105 | 357 |
^ (DirectoryContentsItem new fileName:aFilename) info:aFilename info. |
1102 | 358 |
! ! |
359 |
||
583 | 360 |
!DirectoryContents methodsFor:'accessing'! |
361 |
||
2079 | 362 |
accessKey |
363 |
^ accessKey |
|
364 |
! |
|
365 |
||
583 | 366 |
directory |
367 |
"returns the directoy name as Filename |
|
368 |
" |
|
369 |
^ directory |
|
370 |
! |
|
371 |
||
372 |
modificationTime |
|
373 |
"get the last modification time of the directory |
|
374 |
" |
|
2078 | 375 |
|modifyTime| |
376 |
||
2071 | 377 |
timeStamp isNil ifTrue:[^ nil ]. |
378 |
||
2078 | 379 |
isRootDirectory ifTrue:[ |
380 |
directory exists ifTrue:[^ timeStamp ]. |
|
381 |
] ifFalse:[ |
|
382 |
modifyTime := directory modificationTime. |
|
383 |
modifyTime notNil ifTrue:[^ modifyTime ]. |
|
2071 | 384 |
]. |
2078 | 385 |
isReadable := false. |
386 |
self beObsolete. |
|
387 |
^ nil |
|
951 | 388 |
! |
389 |
||
390 |
timeStamp |
|
391 |
"get the last timeStamp (when the directory info was read) of the directory |
|
392 |
" |
|
583 | 393 |
^ timeStamp |
394 |
! ! |
|
395 |
||
396 |
!DirectoryContents methodsFor:'enumerating'! |
|
397 |
||
398 |
contentsAndBaseNamesDo:aThreeArgBlock |
|
399 |
"evaluate the block on each file; the argument to the block is the |
|
400 |
filename, the baseName and true in case of a directory |
|
401 |
block arguments: [:fileName :aBaseName :isDirectory| |
|
402 |
" |
|
1350 | 403 |
|
1093 | 404 |
self itemsDo:[:eachItem | |
405 |
aThreeArgBlock |
|
406 |
value:(eachItem fileName) |
|
407 |
value:(eachItem baseName ) |
|
408 |
value:(eachItem isDirectory) |
|
583 | 409 |
]. |
410 |
! |
|
411 |
||
412 |
contentsDo:aTwoArgBlock |
|
413 |
"evaluate the block on each file; the argument to the block is the |
|
414 |
filename and true in case of a directory |
|
415 |
block arguments: [:fileName :isDirectory| |
|
416 |
" |
|
1350 | 417 |
|
1093 | 418 |
self itemsDo:[:eachItem | |
419 |
aTwoArgBlock |
|
420 |
value:(eachItem fileName) |
|
421 |
value:(eachItem isDirectory) |
|
583 | 422 |
]. |
423 |
! |
|
424 |
||
425 |
directoriesAndBasenamesDo:aTwoArgBlock |
|
426 |
"evaluate block on each directory; a Filename and Basename. |
|
427 |
The directories are sorted |
|
428 |
" |
|
1350 | 429 |
|
1093 | 430 |
self itemsDo:[:eachItem | |
431 |
eachItem isDirectory ifTrue:[ |
|
432 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
|
583 | 433 |
] |
434 |
] |
|
435 |
! |
|
436 |
||
437 |
directoriesDo:aOneArgBlock |
|
438 |
"evaluate block on each directory; a Filename. The directories are sorted |
|
439 |
" |
|
1350 | 440 |
|
1093 | 441 |
self itemsDo:[:eachItem | |
442 |
eachItem isDirectory ifTrue:[ |
|
1350 | 443 |
aOneArgBlock value:(eachItem fileName) |
583 | 444 |
] |
1093 | 445 |
]. |
583 | 446 |
! |
447 |
||
448 |
filesAndBasenamesDo:aTwoArgBlock |
|
449 |
"evaluate block on each file; a Filename and a Basename. |
|
450 |
The files are sorted. |
|
451 |
" |
|
1350 | 452 |
|
1093 | 453 |
self itemsDo:[:eachItem | |
1324 | 454 |
eachItem isDirectory ifFalse:[ |
1093 | 455 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
583 | 456 |
] |
457 |
] |
|
458 |
! |
|
459 |
||
460 |
filesDo:aOneArgBlock |
|
461 |
"evaluate block on each file; a Filename. The files are sorted. |
|
462 |
" |
|
1350 | 463 |
|
1093 | 464 |
self itemsDo:[:eachItem | |
1324 | 465 |
eachItem isDirectory ifFalse:[ |
1093 | 466 |
aOneArgBlock value:(eachItem fileName) |
583 | 467 |
] |
468 |
] |
|
1093 | 469 |
! |
470 |
||
471 |
itemsDo:aBlock |
|
472 |
"evaluate the block on each contentsItem, which contains the fileName and type info" |
|
473 |
||
1350 | 474 |
contents do:[:eachItem | |
475 |
aBlock value:eachItem. |
|
476 |
]. |
|
583 | 477 |
! ! |
478 |
||
479 |
!DirectoryContents methodsFor:'instance creation'! |
|
480 |
||
481 |
directory:aFilename |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
482 |
"instance creation; setup attributes" |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
483 |
|
2082 | 484 |
|time stream bname fitem| |
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
485 |
|
2078 | 486 |
directory := aFilename asFilename. |
487 |
isRootDirectory := directory isRootDirectory. |
|
2079 | 488 |
accessKey := self class accessKeyForDirectory:directory. |
2082 | 489 |
contents := SortedCollection sortBlock:[:a :b| a nameString < b nameString ]. |
490 |
isReadable := false. |
|
491 |
time := Timestamp now. "/ directory modificationTime. |
|
2078 | 492 |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
493 |
[ |
2082 | 494 |
[ |
495 |
stream := DirectoryStream directoryNamed:(directory osNameForDirectoryContents). |
|
496 |
||
497 |
stream notNil ifTrue:[ |
|
498 |
[ (stream atEnd or:[(bname := stream nextLine) isNil]) ] whileFalse:[ |
|
499 |
(bname = '.' or:[bname = '..']) ifFalse:[ |
|
500 |
fitem := DirectoryContentsItem fileName:(directory construct:bname). |
|
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
501 |
|
2082 | 502 |
"/ get info now - cashed under window (apithreads.c) |
2083 | 503 |
fitem updateInfo. |
2082 | 504 |
contents add:fitem. |
505 |
]. |
|
506 |
]. |
|
507 |
isReadable := true. |
|
508 |
]. |
|
509 |
||
510 |
] on:FileStream openErrorSignal do:[:ex| |
|
511 |
isReadable := false. |
|
2078 | 512 |
]. |
2082 | 513 |
] ensure:[ |
514 |
stream notNil ifTrue:[ stream close ]. |
|
515 |
isReadable ifFalse:[ contents := #() ]. |
|
1093 | 516 |
]. |
2082 | 517 |
timeStamp := time. |
1093 | 518 |
! ! |
519 |
||
2078 | 520 |
!DirectoryContents methodsFor:'obsolete'! |
521 |
||
522 |
updateContents |
|
523 |
<resource:#obsolete> |
|
524 |
||
525 |
"ensure that the file-info os present for every item |
|
526 |
** obsolete - access info if required" |
|
527 |
||
528 |
"/ contents do:[:eachItem | eachItem updateInfo ]. |
|
529 |
! ! |
|
530 |
||
1093 | 531 |
!DirectoryContents methodsFor:'printing'! |
532 |
||
533 |
printOn:aStream |
|
534 |
aStream nextPutAll:'DirectoryContents of: '. |
|
2078 | 535 |
aStream nextPutAll:(directory pathName). |
583 | 536 |
! ! |
537 |
||
538 |
!DirectoryContents methodsFor:'queries'! |
|
539 |
||
2078 | 540 |
beObsolete |
541 |
"mark self as obsolete |
|
542 |
clear contents and reset time" |
|
543 |
||
544 |
|saveCont| |
|
545 |
||
546 |
timeStamp := nil. |
|
547 |
saveCont := contents. |
|
548 |
contents := #(). |
|
549 |
||
550 |
saveCont notEmptyOrNil ifTrue:[ |
|
551 |
"/ clear the info - if someone has a reference to the item |
|
552 |
saveCont do:[:eachItem | eachItem resetInfo ]. |
|
553 |
]. |
|
554 |
! |
|
555 |
||
583 | 556 |
isObsolete |
1350 | 557 |
"returns true if the directory contents represented by the receiver is obsolete |
558 |
(i.e. if the fileSystems directory has been changed in the meanwhile) |
|
583 | 559 |
" |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
560 |
|mt| |
679 | 561 |
|
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
562 |
timeStamp isNil ifTrue:[^ true]. |
2071 | 563 |
|
564 |
mt := self modificationTime. |
|
565 |
mt isNil ifTrue:[^ true ]. |
|
566 |
||
567 |
timeStamp < mt ifTrue:[ |
|
568 |
timeStamp getSeconds < mt getSeconds ifTrue:[ |
|
2078 | 569 |
self beObsolete. |
2071 | 570 |
^ true. |
571 |
]. |
|
572 |
]. |
|
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
573 |
^ false |
2071 | 574 |
! |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
575 |
|
2078 | 576 |
isReadable |
577 |
"answer true if the directory is readable |
|
578 |
no open error raised during reading the directory" |
|
579 |
||
580 |
^ isReadable |
|
581 |
! |
|
582 |
||
2071 | 583 |
isRootDirectory |
2078 | 584 |
^ isRootDirectory |
906 | 585 |
! |
586 |
||
587 |
size |
|
588 |
"get number of files including directories in the directory |
|
589 |
" |
|
1093 | 590 |
^ contents size |
904 | 591 |
! ! |
592 |
||
593 |
!DirectoryContents methodsFor:'testing'! |
|
594 |
||
2080 | 595 |
includesIdentical:anItem |
596 |
^ contents includesIdentical:anItem |
|
597 |
! |
|
598 |
||
904 | 599 |
isEmpty |
600 |
"retuirns true if directory is empty |
|
601 |
" |
|
1093 | 602 |
^ contents size == 0 |
583 | 603 |
! |
604 |
||
605 |
notEmpty |
|
606 |
"returns true if directory is not empty |
|
607 |
" |
|
1093 | 608 |
^ contents size ~~ 0 |
609 |
! ! |
|
610 |
||
2082 | 611 |
!DirectoryContents::DirectoryContentsItem class methodsFor:'instance creation'! |
612 |
||
613 |
fileName:aFilename |
|
614 |
^ self new fileName:aFilename. |
|
615 |
! ! |
|
616 |
||
1093 | 617 |
!DirectoryContents::DirectoryContentsItem methodsFor:'accessing'! |
618 |
||
619 |
baseName |
|
620 |
^ fileName baseName |
|
621 |
! |
|
622 |
||
623 |
fileName |
|
624 |
^ fileName |
|
625 |
! |
|
626 |
||
627 |
fileName:something |
|
628 |
"set the value of the instance variable 'fileName' (automatically generated)" |
|
629 |
||
630 |
fileName := something. |
|
631 |
! |
|
632 |
||
1100 | 633 |
info |
1350 | 634 |
info isNil ifTrue:[ |
635 |
self updateInfo. |
|
636 |
]. |
|
1100 | 637 |
info isSymbol ifTrue:[^ nil]. "/ a remote directory |
638 |
^ info |
|
639 |
! |
|
640 |
||
1099 | 641 |
info:something |
642 |
"set the value of the instance variable 'type' (automatically generated)" |
|
643 |
||
644 |
info := something. |
|
645 |
! |
|
646 |
||
2082 | 647 |
nameString |
648 |
"raw access to nameString" |
|
649 |
||
650 |
^ fileName nameString |
|
651 |
! |
|
652 |
||
1093 | 653 |
type |
1350 | 654 |
info isNil ifTrue:[ |
655 |
self updateInfo. |
|
656 |
]. |
|
1099 | 657 |
info isSymbol ifTrue:[^ info]. |
658 |
^ info type |
|
1093 | 659 |
! ! |
660 |
||
1350 | 661 |
!DirectoryContents::DirectoryContentsItem methodsFor:'misc'! |
662 |
||
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
663 |
cachedRemoteMountPoints |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
664 |
|mountPoints now| |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
665 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
666 |
mountPoints := CachedRemoteMountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
667 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
668 |
(mountPoints isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
669 |
or:[ CachedRemoteMountPointsTimeStamp isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
670 |
or:[ now := Timestamp now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
671 |
(now - CachedRemoteMountPointsTimeStamp) > 30 |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
672 |
]]) ifTrue:[ |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
673 |
CachedRemoteMountPointsTimeStamp := now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
674 |
mountPoints := OperatingSystem mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
675 |
mountPoints := mountPoints select:[:mp | mp isRemote]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
676 |
CachedRemoteMountPoints := mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
677 |
]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
678 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
679 |
^ mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
680 |
! |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
681 |
|
2078 | 682 |
resetAttributes |
683 |
"reset the attributes... done if I'am a normal file" |
|
684 |
||
685 |
(info isNil or:[info isSymbol]) ifTrue:[^ self ]. |
|
686 |
||
687 |
self isDirectory ifFalse:[ |
|
688 |
info := nil. |
|
689 |
]. |
|
690 |
! |
|
691 |
||
692 |
resetInfo |
|
693 |
info := nil. |
|
694 |
! |
|
695 |
||
1350 | 696 |
updateInfo |
2047 | 697 |
"ensure that the file-info is present |
698 |
DirectoryContents flushCache |
|
699 |
" |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
700 |
|
2083 | 701 |
|mountPoint nameString linkName| |
1350 | 702 |
|
703 |
info isNil ifTrue:[ |
|
2083 | 704 |
"/ self assert:[fileName isAbsolute]. -> is a windows call |
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
705 |
info := fileName linkInfo. |
2047 | 706 |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
707 |
(info notNil and:[info isSymbolicLink]) ifTrue:[ |
2047 | 708 |
OperatingSystem isMSWINDOWSlike ifFalse:[ |
709 |
linkName := info path. |
|
2046 | 710 |
|
2047 | 711 |
linkName notNil ifTrue:[ |
712 |
"have to check for both link and link target" |
|
2083 | 713 |
mountPoint := self cachedRemoteMountPoints |
2047 | 714 |
detect:[:mInfo | |p| |
715 |
p := mInfo mountPointPath. |
|
716 |
((linkName startsWith:p) and:[ linkName startsWith:(p , '/') ]) |
|
717 |
] |
|
718 |
ifNone:nil. |
|
719 |
||
720 |
info := fileName info. "get the info of the link target" |
|
721 |
]. |
|
722 |
]. |
|
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
723 |
] ifFalse:[ |
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
724 |
"have to check for mountPoint only" |
2083 | 725 |
nameString := fileName name. |
726 |
||
727 |
mountPoint := self cachedRemoteMountPoints |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
728 |
detect:[:mInfo | mInfo mountPointPath = nameString ] |
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
729 |
ifNone:nil. |
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
730 |
]. |
2047 | 731 |
mountPoint notNil ifTrue:[ |
1350 | 732 |
info := #remoteDirectory. |
733 |
] ifFalse:[ |
|
734 |
info isNil ifTrue:[ |
|
735 |
"/ broken symbolic link |
|
736 |
info := #symbolicLink. |
|
737 |
] |
|
738 |
]. |
|
739 |
]. |
|
740 |
! ! |
|
741 |
||
1093 | 742 |
!DirectoryContents::DirectoryContentsItem methodsFor:'printing'! |
743 |
||
744 |
printOn:aStream |
|
745 |
aStream nextPutAll:'DirectoryContentsItem for: '. |
|
746 |
fileName printOn:aStream. |
|
747 |
! ! |
|
748 |
||
749 |
!DirectoryContents::DirectoryContentsItem methodsFor:'queries'! |
|
750 |
||
751 |
isDirectory |
|
1099 | 752 |
|t| |
753 |
||
754 |
t := self type. |
|
755 |
^ (t == #directory or:[t == #remoteDirectory]) |
|
1093 | 756 |
! |
757 |
||
758 |
isRemoteDirectory |
|
2074 | 759 |
^ self type == #remoteDirectory |
760 |
! |
|
761 |
||
2080 | 762 |
isSpecialFile |
763 |
|t| |
|
764 |
||
765 |
t := self type. |
|
766 |
^ (t ~= #directory and:[t ~~ #regular and:[t ~~ #symbolicLink]]) |
|
767 |
! |
|
768 |
||
2074 | 769 |
isSymbolicLink |
770 |
^ self type == #isymbolicLink |
|
583 | 771 |
! ! |
772 |
||
773 |
!DirectoryContents class methodsFor:'documentation'! |
|
774 |
||
775 |
version |
|
2130 | 776 |
^ '$Header: /cvs/stx/stx/libbasic2/DirectoryContents.st,v 1.53 2009-03-27 13:06:24 fm Exp $' |
583 | 777 |
! ! |
1093 | 778 |
|
957 | 779 |
DirectoryContents initialize! |