author | Claus Gittinger <cg@exept.de> |
Wed, 17 Aug 2011 09:47:30 +0200 | |
changeset 2627 | 6d4adf17cfd6 |
parent 2499 | 9a5dcca2130a |
child 2745 | 7fb1a2c5483d |
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 |
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
166 |
"flush list of remembered directory contents" |
880 | 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. |
|
2133 | 278 |
] ifFalse:[ |
279 |
"/ validation block answers false - return nil and removed from cache |
|
280 |
directory := nil. |
|
2078 | 281 |
]. |
2133 | 282 |
]. |
283 |
]. |
|
583 | 284 |
]. |
957 | 285 |
^ directory |
583 | 286 |
! ! |
287 |
||
288 |
!DirectoryContents class methodsFor:'queries'! |
|
289 |
||
290 |
directoryNamed:aDirectoryName detect:aTwoArgBlock |
|
291 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
292 |
contents of a directory named aDirectoryName, until the block |
|
293 |
returns true. If nothing detected false is returned |
|
294 |
" |
|
2078 | 295 |
^ self directoryNamed:aDirectoryName detect:aTwoArgBlock onOpenErrorDo:nil |
296 |
! |
|
297 |
||
298 |
directoryNamed:aDirectoryName detect:aTwoArgBlock onOpenErrorDo:exceptionBlock |
|
299 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
300 |
contents of a directory named aDirectoryName, until the block |
|
301 |
returns true. If nothing detected false is returned. |
|
302 |
||
303 |
if the directory cannot be open, the exceptionBlock is processed |
|
304 |
with the filename. |
|
305 |
" |
|
306 |
|directory contents isReadable file| |
|
583 | 307 |
|
308 |
directory := aDirectoryName asFilename. |
|
2078 | 309 |
contents := self directoryAt:directory. |
620 | 310 |
|
2078 | 311 |
contents notNil ifTrue:[ |
312 |
(isReadable := contents isReadable) ifTrue:[ |
|
313 |
contents contentsDo:[:aFile :isDir| |
|
314 |
(aTwoArgBlock value:aFile value:isDir) ifTrue:[^ true] |
|
315 |
]. |
|
316 |
^ false. |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
317 |
]. |
2078 | 318 |
] ifFalse:[ |
319 |
(isReadable := directory exists) ifTrue:[ |
|
320 |
[ |
|
321 |
directory directoryContentsDo:[:fn | |
|
322 |
file := directory construct:fn. |
|
323 |
(aTwoArgBlock value:file value:(file isDirectory)) ifTrue:[ |
|
324 |
^ true |
|
325 |
] |
|
326 |
]. |
|
327 |
] on:FileStream openErrorSignal do:[:ex| |
|
328 |
isReadable := false. |
|
329 |
]. |
|
330 |
]. |
|
583 | 331 |
]. |
2078 | 332 |
(isReadable not and:[exceptionBlock notNil]) ifTrue:[ |
333 |
exceptionBlock value:directory. |
|
334 |
]. |
|
583 | 335 |
^ false |
336 |
! ! |
|
337 |
||
1102 | 338 |
!DirectoryContents class methodsFor:'utilities'! |
339 |
||
340 |
contentsItemForFileName:aFilenameOrString |
|
341 |
| aFilename directory directoryContents| |
|
342 |
||
343 |
aFilename := aFilenameOrString asFilename. |
|
344 |
directory := aFilename directory. |
|
2078 | 345 |
|
346 |
directoryContents := self directoryAt:directory. |
|
347 |
||
348 |
directoryContents notNil ifTrue:[ |
|
349 |
directoryContents itemsDo:[:fileItemThere | |
|
350 |
fileItemThere fileName = aFilename ifTrue:[ |
|
351 |
^ fileItemThere. |
|
352 |
]. |
|
1393
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
353 |
]. |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
354 |
]. |
1252
933afe6d5bd6
care for directory being removed
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
355 |
|
2078 | 356 |
aFilename exists ifFalse:[ |
357 |
directoryContents notNil ifTrue:[ directoryContents beObsolete ]. |
|
358 |
^ nil |
|
1102 | 359 |
]. |
1105 | 360 |
^ (DirectoryContentsItem new fileName:aFilename) info:aFilename info. |
1102 | 361 |
! ! |
362 |
||
583 | 363 |
!DirectoryContents methodsFor:'accessing'! |
364 |
||
2079 | 365 |
accessKey |
366 |
^ accessKey |
|
367 |
! |
|
368 |
||
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
369 |
beObsolete |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
370 |
"mark self as obsolete |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
371 |
clear contents and reset time" |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
372 |
|
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
373 |
|saveCont| |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
374 |
|
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
375 |
timeStamp := nil. |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
376 |
saveCont := contents. |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
377 |
contents := #(). |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
378 |
|
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
379 |
saveCont notEmptyOrNil ifTrue:[ |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
380 |
"/ clear the info - if someone has a reference to the item |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
381 |
saveCont do:[:eachItem | eachItem resetInfo ]. |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
382 |
]. |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
383 |
! |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
384 |
|
583 | 385 |
directory |
386 |
"returns the directoy name as Filename |
|
387 |
" |
|
388 |
^ directory |
|
389 |
! |
|
390 |
||
391 |
modificationTime |
|
2499 | 392 |
"get the last modification time of the directory. |
393 |
Note that sometimes a root directory does not return |
|
394 |
a valid modification time - so do not cache it." |
|
395 |
||
2078 | 396 |
|modifyTime| |
397 |
||
2499 | 398 |
timeStamp isNil ifTrue:[^ nil]. |
2071 | 399 |
|
2499 | 400 |
modifyTime := directory modificationTime. |
401 |
modifyTime notNil ifTrue:[ |
|
402 |
^ modifyTime |
|
403 |
]. |
|
404 |
||
405 |
isRootDirectory ifFalse:[ |
|
406 |
"if it is not a root directory, assume that we have no access" |
|
407 |
isReadable := false. |
|
408 |
self beObsolete. |
|
2071 | 409 |
]. |
2078 | 410 |
^ nil |
951 | 411 |
! |
412 |
||
413 |
timeStamp |
|
414 |
"get the last timeStamp (when the directory info was read) of the directory |
|
415 |
" |
|
583 | 416 |
^ timeStamp |
417 |
! ! |
|
418 |
||
419 |
!DirectoryContents methodsFor:'enumerating'! |
|
420 |
||
421 |
contentsAndBaseNamesDo:aThreeArgBlock |
|
422 |
"evaluate the block on each file; the argument to the block is the |
|
423 |
filename, the baseName and true in case of a directory |
|
424 |
block arguments: [:fileName :aBaseName :isDirectory| |
|
425 |
" |
|
1350 | 426 |
|
1093 | 427 |
self itemsDo:[:eachItem | |
428 |
aThreeArgBlock |
|
429 |
value:(eachItem fileName) |
|
430 |
value:(eachItem baseName ) |
|
431 |
value:(eachItem isDirectory) |
|
583 | 432 |
]. |
433 |
! |
|
434 |
||
435 |
contentsDo:aTwoArgBlock |
|
436 |
"evaluate the block on each file; the argument to the block is the |
|
437 |
filename and true in case of a directory |
|
438 |
block arguments: [:fileName :isDirectory| |
|
439 |
" |
|
1350 | 440 |
|
1093 | 441 |
self itemsDo:[:eachItem | |
442 |
aTwoArgBlock |
|
443 |
value:(eachItem fileName) |
|
444 |
value:(eachItem isDirectory) |
|
583 | 445 |
]. |
446 |
! |
|
447 |
||
448 |
directoriesAndBasenamesDo:aTwoArgBlock |
|
449 |
"evaluate block on each directory; a Filename and Basename. |
|
450 |
The directories are sorted |
|
451 |
" |
|
1350 | 452 |
|
1093 | 453 |
self itemsDo:[:eachItem | |
454 |
eachItem isDirectory ifTrue:[ |
|
455 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
|
583 | 456 |
] |
457 |
] |
|
458 |
! |
|
459 |
||
460 |
directoriesDo:aOneArgBlock |
|
461 |
"evaluate block on each directory; a Filename. The directories are sorted |
|
462 |
" |
|
1350 | 463 |
|
1093 | 464 |
self itemsDo:[:eachItem | |
465 |
eachItem isDirectory ifTrue:[ |
|
1350 | 466 |
aOneArgBlock value:(eachItem fileName) |
583 | 467 |
] |
1093 | 468 |
]. |
583 | 469 |
! |
470 |
||
471 |
filesAndBasenamesDo:aTwoArgBlock |
|
472 |
"evaluate block on each file; a Filename and a Basename. |
|
473 |
The files are sorted. |
|
474 |
" |
|
1350 | 475 |
|
1093 | 476 |
self itemsDo:[:eachItem | |
1324 | 477 |
eachItem isDirectory ifFalse:[ |
1093 | 478 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
583 | 479 |
] |
480 |
] |
|
481 |
! |
|
482 |
||
483 |
filesDo:aOneArgBlock |
|
484 |
"evaluate block on each file; a Filename. The files are sorted. |
|
485 |
" |
|
1350 | 486 |
|
1093 | 487 |
self itemsDo:[:eachItem | |
1324 | 488 |
eachItem isDirectory ifFalse:[ |
1093 | 489 |
aOneArgBlock value:(eachItem fileName) |
583 | 490 |
] |
491 |
] |
|
1093 | 492 |
! |
493 |
||
494 |
itemsDo:aBlock |
|
495 |
"evaluate the block on each contentsItem, which contains the fileName and type info" |
|
496 |
||
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
497 |
contents do:aBlock. |
583 | 498 |
! ! |
499 |
||
500 |
!DirectoryContents methodsFor:'instance creation'! |
|
501 |
||
502 |
directory:aFilename |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
503 |
"instance creation; setup attributes" |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
504 |
|
2082 | 505 |
|time stream bname fitem| |
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
506 |
|
2078 | 507 |
directory := aFilename asFilename. |
508 |
isRootDirectory := directory isRootDirectory. |
|
2079 | 509 |
accessKey := self class accessKeyForDirectory:directory. |
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
510 |
contents := OrderedCollection new. |
2082 | 511 |
isReadable := false. |
512 |
time := Timestamp now. "/ directory modificationTime. |
|
2078 | 513 |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
514 |
[ |
2082 | 515 |
[ |
516 |
stream := DirectoryStream directoryNamed:(directory osNameForDirectoryContents). |
|
517 |
||
518 |
stream notNil ifTrue:[ |
|
519 |
[ (stream atEnd or:[(bname := stream nextLine) isNil]) ] whileFalse:[ |
|
520 |
(bname = '.' or:[bname = '..']) ifFalse:[ |
|
521 |
fitem := DirectoryContentsItem fileName:(directory construct:bname). |
|
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
522 |
|
2082 | 523 |
"/ get info now - cashed under window (apithreads.c) |
2083 | 524 |
fitem updateInfo. |
2082 | 525 |
contents add:fitem. |
526 |
]. |
|
527 |
]. |
|
528 |
isReadable := true. |
|
529 |
]. |
|
530 |
] on:FileStream openErrorSignal do:[:ex| |
|
531 |
isReadable := false. |
|
2078 | 532 |
]. |
2082 | 533 |
] ensure:[ |
534 |
stream notNil ifTrue:[ stream close ]. |
|
535 |
isReadable ifFalse:[ contents := #() ]. |
|
1093 | 536 |
]. |
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
537 |
contents sort:[:a :b| a nameString < b nameString ]. |
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
538 |
contents := contents asArray. |
2082 | 539 |
timeStamp := time. |
1093 | 540 |
! ! |
541 |
||
2078 | 542 |
!DirectoryContents methodsFor:'obsolete'! |
543 |
||
544 |
updateContents |
|
545 |
<resource:#obsolete> |
|
546 |
||
547 |
"ensure that the file-info os present for every item |
|
548 |
** obsolete - access info if required" |
|
549 |
||
550 |
"/ contents do:[:eachItem | eachItem updateInfo ]. |
|
551 |
! ! |
|
552 |
||
1093 | 553 |
!DirectoryContents methodsFor:'printing'! |
554 |
||
555 |
printOn:aStream |
|
556 |
aStream nextPutAll:'DirectoryContents of: '. |
|
2078 | 557 |
aStream nextPutAll:(directory pathName). |
583 | 558 |
! ! |
559 |
||
560 |
!DirectoryContents methodsFor:'queries'! |
|
561 |
||
562 |
isObsolete |
|
1350 | 563 |
"returns true if the directory contents represented by the receiver is obsolete |
564 |
(i.e. if the fileSystems directory has been changed in the meanwhile) |
|
583 | 565 |
" |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
566 |
|mt| |
679 | 567 |
|
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
568 |
timeStamp isNil ifTrue:[^ true]. |
2071 | 569 |
|
570 |
mt := self modificationTime. |
|
571 |
mt isNil ifTrue:[^ true ]. |
|
572 |
||
2499 | 573 |
"we compare only seconds, since the modification time is not as (millisecond) accurate as the timestamp" |
574 |
timeStamp getSeconds < mt getSeconds ifTrue:[ |
|
575 |
self beObsolete. |
|
576 |
^ true. |
|
2071 | 577 |
]. |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
578 |
^ false |
2071 | 579 |
! |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
580 |
|
2078 | 581 |
isReadable |
582 |
"answer true if the directory is readable |
|
583 |
no open error raised during reading the directory" |
|
584 |
||
585 |
^ isReadable |
|
586 |
! |
|
587 |
||
2071 | 588 |
isRootDirectory |
2078 | 589 |
^ isRootDirectory |
906 | 590 |
! |
591 |
||
592 |
size |
|
593 |
"get number of files including directories in the directory |
|
594 |
" |
|
1093 | 595 |
^ contents size |
904 | 596 |
! ! |
597 |
||
598 |
!DirectoryContents methodsFor:'testing'! |
|
599 |
||
2080 | 600 |
includesIdentical:anItem |
601 |
^ contents includesIdentical:anItem |
|
602 |
! |
|
603 |
||
904 | 604 |
isEmpty |
2421
7d70e8269bec
comment/format in: #isObsolete
Claus Gittinger <cg@exept.de>
parents:
2412
diff
changeset
|
605 |
"returns true if directory is empty |
904 | 606 |
" |
1093 | 607 |
^ contents size == 0 |
583 | 608 |
! |
609 |
||
2133 | 610 |
isEmptyOrNil |
2627 | 611 |
"return true if I am nil or an empty collection" |
612 |
||
2133 | 613 |
^ contents size == 0 |
2627 | 614 |
|
615 |
"Modified (comment): / 17-08-2011 / 09:30:15 / cg" |
|
2133 | 616 |
! |
617 |
||
583 | 618 |
notEmpty |
619 |
"returns true if directory is not empty |
|
620 |
" |
|
1093 | 621 |
^ contents size ~~ 0 |
2133 | 622 |
! |
623 |
||
624 |
notEmptyOrNil |
|
625 |
"returns true if directory is not empty |
|
626 |
" |
|
627 |
^ contents size ~~ 0 |
|
1093 | 628 |
! ! |
629 |
||
2082 | 630 |
!DirectoryContents::DirectoryContentsItem class methodsFor:'instance creation'! |
631 |
||
632 |
fileName:aFilename |
|
633 |
^ self new fileName:aFilename. |
|
634 |
! ! |
|
635 |
||
1093 | 636 |
!DirectoryContents::DirectoryContentsItem methodsFor:'accessing'! |
637 |
||
638 |
baseName |
|
639 |
^ fileName baseName |
|
640 |
! |
|
641 |
||
642 |
fileName |
|
643 |
^ fileName |
|
644 |
! |
|
645 |
||
646 |
fileName:something |
|
647 |
"set the value of the instance variable 'fileName' (automatically generated)" |
|
648 |
||
649 |
fileName := something. |
|
650 |
! |
|
651 |
||
1100 | 652 |
info |
1350 | 653 |
info isNil ifTrue:[ |
654 |
self updateInfo. |
|
655 |
]. |
|
1100 | 656 |
info isSymbol ifTrue:[^ nil]. "/ a remote directory |
657 |
^ info |
|
658 |
! |
|
659 |
||
1099 | 660 |
info:something |
661 |
"set the value of the instance variable 'type' (automatically generated)" |
|
662 |
||
663 |
info := something. |
|
664 |
! |
|
665 |
||
2082 | 666 |
nameString |
667 |
"raw access to nameString" |
|
668 |
||
669 |
^ fileName nameString |
|
670 |
! |
|
671 |
||
1093 | 672 |
type |
1350 | 673 |
info isNil ifTrue:[ |
674 |
self updateInfo. |
|
675 |
]. |
|
1099 | 676 |
info isSymbol ifTrue:[^ info]. |
677 |
^ info type |
|
1093 | 678 |
! ! |
679 |
||
1350 | 680 |
!DirectoryContents::DirectoryContentsItem methodsFor:'misc'! |
681 |
||
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
682 |
cachedRemoteMountPoints |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
683 |
|mountPoints now| |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
684 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
685 |
mountPoints := CachedRemoteMountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
686 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
687 |
(mountPoints isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
688 |
or:[ CachedRemoteMountPointsTimeStamp isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
689 |
or:[ now := Timestamp now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
690 |
(now - CachedRemoteMountPointsTimeStamp) > 30 |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
691 |
]]) ifTrue:[ |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
692 |
CachedRemoteMountPointsTimeStamp := now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
693 |
mountPoints := OperatingSystem mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
694 |
mountPoints := mountPoints select:[:mp | mp isRemote]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
695 |
CachedRemoteMountPoints := mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
696 |
]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
697 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
698 |
^ mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
699 |
! |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
700 |
|
2078 | 701 |
resetAttributes |
702 |
"reset the attributes... done if I'am a normal file" |
|
703 |
||
704 |
(info isNil or:[info isSymbol]) ifTrue:[^ self ]. |
|
705 |
||
706 |
self isDirectory ifFalse:[ |
|
707 |
info := nil. |
|
708 |
]. |
|
709 |
! |
|
710 |
||
711 |
resetInfo |
|
712 |
info := nil. |
|
713 |
! |
|
714 |
||
1350 | 715 |
updateInfo |
2047 | 716 |
"ensure that the file-info is present |
717 |
DirectoryContents flushCache |
|
718 |
" |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
719 |
|
2083 | 720 |
|mountPoint nameString linkName| |
1350 | 721 |
|
722 |
info isNil ifTrue:[ |
|
2083 | 723 |
"/ 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
|
724 |
info := fileName linkInfo. |
2047 | 725 |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
726 |
(info notNil and:[info isSymbolicLink]) ifTrue:[ |
2047 | 727 |
OperatingSystem isMSWINDOWSlike ifFalse:[ |
728 |
linkName := info path. |
|
2046 | 729 |
|
2047 | 730 |
linkName notNil ifTrue:[ |
731 |
"have to check for both link and link target" |
|
2083 | 732 |
mountPoint := self cachedRemoteMountPoints |
2047 | 733 |
detect:[:mInfo | |p| |
734 |
p := mInfo mountPointPath. |
|
735 |
((linkName startsWith:p) and:[ linkName startsWith:(p , '/') ]) |
|
736 |
] |
|
737 |
ifNone:nil. |
|
738 |
||
739 |
info := fileName info. "get the info of the link target" |
|
740 |
]. |
|
741 |
]. |
|
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
742 |
] 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
|
743 |
"have to check for mountPoint only" |
2083 | 744 |
nameString := fileName name. |
745 |
||
746 |
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
|
747 |
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
|
748 |
ifNone:nil. |
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
749 |
]. |
2047 | 750 |
mountPoint notNil ifTrue:[ |
1350 | 751 |
info := #remoteDirectory. |
752 |
] ifFalse:[ |
|
753 |
info isNil ifTrue:[ |
|
754 |
"/ broken symbolic link |
|
755 |
info := #symbolicLink. |
|
756 |
] |
|
757 |
]. |
|
758 |
]. |
|
759 |
! ! |
|
760 |
||
1093 | 761 |
!DirectoryContents::DirectoryContentsItem methodsFor:'printing'! |
762 |
||
763 |
printOn:aStream |
|
764 |
aStream nextPutAll:'DirectoryContentsItem for: '. |
|
765 |
fileName printOn:aStream. |
|
766 |
! ! |
|
767 |
||
768 |
!DirectoryContents::DirectoryContentsItem methodsFor:'queries'! |
|
769 |
||
770 |
isDirectory |
|
1099 | 771 |
|t| |
772 |
||
773 |
t := self type. |
|
774 |
^ (t == #directory or:[t == #remoteDirectory]) |
|
1093 | 775 |
! |
776 |
||
777 |
isRemoteDirectory |
|
2074 | 778 |
^ self type == #remoteDirectory |
779 |
! |
|
780 |
||
2080 | 781 |
isSpecialFile |
2412 | 782 |
|type| |
783 |
||
784 |
type := self type. |
|
2080 | 785 |
|
2412 | 786 |
^ (type ~~ #directory |
787 |
and:[type ~~ #remoteDirectory |
|
788 |
and:[type ~~ #regular |
|
789 |
and:[type ~~ #symbolicLink |
|
790 |
]]]) |
|
2080 | 791 |
! |
792 |
||
2074 | 793 |
isSymbolicLink |
2412 | 794 |
^ self type == #symbolicLink |
583 | 795 |
! ! |
796 |
||
797 |
!DirectoryContents class methodsFor:'documentation'! |
|
798 |
||
799 |
version |
|
2627 | 800 |
^ '$Header: /cvs/stx/stx/libbasic2/DirectoryContents.st,v 1.58 2011-08-17 07:47:30 cg Exp $' |
2412 | 801 |
! |
802 |
||
803 |
version_CVS |
|
2627 | 804 |
^ '$Header: /cvs/stx/stx/libbasic2/DirectoryContents.st,v 1.58 2011-08-17 07:47:30 cg Exp $' |
583 | 805 |
! ! |
1093 | 806 |
|
957 | 807 |
DirectoryContents initialize! |