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