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