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