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