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