author | ca |
Thu, 23 Oct 2008 08:12:15 +0200 | |
changeset 2047 | 16c159894d3f |
parent 2046 | 0ca98ed5fb80 |
child 2071 | f58e3dcf48b0 |
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 |
||
583 | 14 |
Object subclass:#DirectoryContents |
1105 | 15 |
instanceVariableNames:'directory timeStamp contents' |
957 | 16 |
classVariableNames:'CachedDirectories LockSema ReadersList' |
583 | 17 |
poolDictionaries:'' |
18 |
category:'System-Support' |
|
19 |
! |
|
20 |
||
1093 | 21 |
Object subclass:#DirectoryContentsItem |
1099 | 22 |
instanceVariableNames:'info fileName' |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
23 |
classVariableNames:'CachedRemoteMountPoints CachedRemoteMountPointsTimeStamp' |
1093 | 24 |
poolDictionaries:'' |
25 |
privateIn:DirectoryContents |
|
26 |
! |
|
27 |
||
583 | 28 |
!DirectoryContents class methodsFor:'documentation'! |
29 |
||
30 |
copyright |
|
31 |
" |
|
32 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
33 |
All Rights Reserved |
|
34 |
||
35 |
This software is furnished under a license and may be used |
|
36 |
only in accordance with the terms of that license and with the |
|
37 |
inclusion of the above copyright notice. This software may not |
|
38 |
be provided or otherwise made available to, or used by, any |
|
39 |
other person. No title to or ownership of the software is |
|
40 |
hereby transferred. |
|
41 |
" |
|
42 |
||
43 |
||
44 |
! |
|
45 |
||
46 |
documentation |
|
47 |
" |
|
48 |
DirectoryContents provides a cached view onto a fileDirectory. |
|
49 |
||
50 |
||
51 |
Notice: |
|
52 |
This class is not available in other ST-systems; |
|
53 |
Applications using it may not be portable. |
|
54 |
||
55 |
[author:] |
|
56 |
Claus Atzkern |
|
57 |
||
58 |
[see also:] |
|
59 |
Filename |
|
60 |
FileStream DirectoryStream OperatingSystem |
|
61 |
" |
|
62 |
||
63 |
! ! |
|
64 |
||
957 | 65 |
!DirectoryContents class methodsFor:'initialization'! |
66 |
||
67 |
initialize |
|
68 |
"setup lock-mechanism |
|
69 |
" |
|
70 |
LockSema := RecursionLock new. |
|
71 |
ReadersList := Dictionary new. |
|
72 |
! ! |
|
73 |
||
1093 | 74 |
!DirectoryContents class methodsFor:'instance creation'! |
75 |
||
76 |
new |
|
77 |
^ self basicNew initialize |
|
78 |
! ! |
|
79 |
||
583 | 80 |
!DirectoryContents class methodsFor:'accessing'! |
81 |
||
957 | 82 |
directoryNamed:aDirectory |
583 | 83 |
"returns the DirectoryContents for a directory named |
957 | 84 |
aDirectoryName, aString, nil or Filename |
583 | 85 |
" |
957 | 86 |
|directory contents max lockRead pathName addToList| |
583 | 87 |
|
957 | 88 |
(aDirectory notNil and:[(directory := aDirectory asFilename) exists]) ifFalse:[ |
620 | 89 |
^ nil |
90 |
]. |
|
1105 | 91 |
directory := directory asAbsoluteFilename. |
957 | 92 |
contents := nil. |
620 | 93 |
|
957 | 94 |
LockSema critical:[ |
95 |
CachedDirectories notNil ifTrue:[ |
|
96 |
contents := self directoryAt:directory. |
|
97 |
||
98 |
contents isNil ifTrue:[ |
|
99 |
max := self maxCachedDirectories. |
|
583 | 100 |
|
957 | 101 |
CachedDirectories size > max ifTrue:[ |
102 |
CachedDirectories := CachedDirectories select:[:aDir| |
|
103 |
(aDir size > 32 and:[aDir isObsolete not]) |
|
104 |
] |
|
105 |
]. |
|
106 |
||
107 |
CachedDirectories size > max ifTrue:[ |
|
108 |
CachedDirectories removeFirst |
|
109 |
] |
|
110 |
]. |
|
583 | 111 |
]. |
957 | 112 |
contents isNil ifTrue:[ |
113 |
ReadersList isNil ifTrue:[ReadersList := Dictionary new]. |
|
114 |
pathName := directory pathName. |
|
115 |
lockRead := ReadersList at:pathName ifAbsentPut:[Semaphore forMutualExclusion]. |
|
116 |
] |
|
117 |
]. |
|
118 |
contents notNil ifTrue:[^ contents]. |
|
119 |
addToList := false. |
|
583 | 120 |
|
957 | 121 |
lockRead critical:[ |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
122 |
[ "/ test whether another task got the semaphore and |
957 | 123 |
"/ has read the directory contents |
124 |
(contents := self directoryAt:directory) isNil ifTrue:[ |
|
125 |
"/ read the directory contents |
|
126 |
contents := self new directory:directory. |
|
127 |
"/ only cache if the mod'Time is valid. |
|
128 |
addToList := contents timeStamp notNil. |
|
129 |
] |
|
1022
6a288db5312a
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
974
diff
changeset
|
130 |
] ensure:[ |
957 | 131 |
LockSema critical:[ |
132 |
addToList ifTrue:[ |
|
133 |
CachedDirectories isNil ifTrue:[ |
|
134 |
CachedDirectories := OrderedCollection new |
|
135 |
]. |
|
136 |
CachedDirectories add:contents |
|
137 |
]. |
|
138 |
(lockRead isEmpty and:[ReadersList notNil]) ifTrue:[ |
|
139 |
ReadersList removeKey:pathName ifAbsent:nil |
|
140 |
] |
|
583 | 141 |
] |
142 |
] |
|
957 | 143 |
]. |
620 | 144 |
|
957 | 145 |
^ contents |
583 | 146 |
! ! |
147 |
||
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
148 |
!DirectoryContents class methodsFor:'cache flushing'! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
149 |
|
880 | 150 |
flushCache |
151 |
"flush list of rememebred directory contents" |
|
152 |
||
957 | 153 |
LockSema critical:[ CachedDirectories := nil ]. |
880 | 154 |
|
155 |
" |
|
156 |
self flushCache |
|
157 |
" |
|
158 |
||
159 |
"Created: / 11.2.2000 / 00:13:59 / cg" |
|
160 |
! |
|
161 |
||
1105 | 162 |
flushCachedDirectoryFor:aDirectoryOrString |
958 | 163 |
"remove directory from cache |
164 |
" |
|
1105 | 165 |
|index directory| |
958 | 166 |
|
1105 | 167 |
(CachedDirectories notNil and:[aDirectoryOrString notNil]) ifTrue:[ |
168 |
directory := aDirectoryOrString asFilename. |
|
958 | 169 |
|
1352 | 170 |
(directory isSymbolicLink not |
171 |
and:[ directory isDirectory]) ifTrue:[ |
|
958 | 172 |
LockSema critical:[ |
173 |
CachedDirectories notNil ifTrue:[ |
|
1105 | 174 |
directory := directory asAbsoluteFilename. |
1349 | 175 |
index := CachedDirectories findFirst:[:d | d directory = directory ]. |
958 | 176 |
|
177 |
index ~~ 0 ifTrue:[ |
|
178 |
CachedDirectories removeAtIndex:index. |
|
179 |
] |
|
180 |
] |
|
181 |
] |
|
182 |
] |
|
183 |
]. |
|
184 |
! |
|
185 |
||
695
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
186 |
lowSpaceCleanup |
1575 | 187 |
"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
|
188 |
|
1134 | 189 |
self flushCache |
695
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
190 |
|
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
191 |
" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
192 |
self lowSpaceCleanup |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
193 |
" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
194 |
|
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
195 |
"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
|
196 |
"Modified: / 24.9.1998 / 17:51:15 / cg" |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
197 |
! |
368ddb719976
only cache dir if its mod'time is known (for win32)
Claus Gittinger <cg@exept.de>
parents:
679
diff
changeset
|
198 |
|
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
199 |
preSnapshot |
1919 | 200 |
"flush list of cached directory contents before saving a snapshot" |
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
201 |
|
1349 | 202 |
self flushCache. |
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
203 |
! ! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
204 |
|
583 | 205 |
!DirectoryContents class methodsFor:'constants'! |
206 |
||
207 |
maxCachedDirectories |
|
208 |
"returns number of maximum cached directories |
|
209 |
" |
|
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
210 |
^ 20 |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
211 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
212 |
"Modified: / 25.2.1998 / 19:56:24 / cg" |
583 | 213 |
! ! |
214 |
||
215 |
!DirectoryContents class methodsFor:'private'! |
|
216 |
||
217 |
directoryAt:aFilename |
|
218 |
"checks whether directory already exists and is valid. |
|
219 |
If true the directory is returned otherwise nil |
|
220 |
" |
|
1105 | 221 |
|index directory absoluteFilename| |
583 | 222 |
|
957 | 223 |
directory := nil. |
224 |
||
225 |
LockSema critical:[ |
|
226 |
CachedDirectories notNil ifTrue:[ |
|
1105 | 227 |
absoluteFilename := aFilename asFilename asAbsoluteFilename. |
228 |
index := CachedDirectories findFirst:[:d| d directory = absoluteFilename ]. |
|
583 | 229 |
|
957 | 230 |
index ~~ 0 ifTrue:[ |
231 |
directory := CachedDirectories at:index. |
|
583 | 232 |
|
957 | 233 |
directory isObsolete ifTrue:[ |
234 |
CachedDirectories removeAtIndex:index. |
|
235 |
directory := nil. |
|
236 |
] |
|
237 |
] |
|
583 | 238 |
] |
239 |
]. |
|
957 | 240 |
^ directory |
583 | 241 |
! ! |
242 |
||
243 |
!DirectoryContents class methodsFor:'queries'! |
|
244 |
||
245 |
directoryNamed:aDirectoryName detect:aTwoArgBlock |
|
246 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
247 |
contents of a directory named aDirectoryName, until the block |
|
248 |
returns true. If nothing detected false is returned |
|
249 |
" |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
250 |
|directory dir| |
583 | 251 |
|
252 |
directory := aDirectoryName asFilename. |
|
253 |
||
620 | 254 |
directory exists ifFalse:[ |
255 |
^ false |
|
256 |
]. |
|
257 |
||
583 | 258 |
(dir := self directoryAt:directory) notNil ifTrue:[ |
259 |
dir contentsDo:[:aFile :isDir| |
|
260 |
(aTwoArgBlock value:aFile value:isDir) ifTrue:[^ true] |
|
261 |
]. |
|
262 |
^ false |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
263 |
]. |
583 | 264 |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
265 |
[ |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
266 |
directory directoryContentsDo:[:fn | |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
267 |
|file| |
583 | 268 |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
269 |
file := directory construct:fn. |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
270 |
(aTwoArgBlock value:file value:(file isDirectory)) ifTrue:[ |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
271 |
^ true |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
272 |
] |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
273 |
]. |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
274 |
] on:FileStream openErrorSignal do:[:ex| |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
275 |
"cannot open directory" |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
276 |
^ false |
583 | 277 |
]. |
696
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
278 |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
279 |
|
696
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
280 |
"/ dir := DirectoryStream directoryNamed:(directory pathName). |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
281 |
"/ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
282 |
"/ dir isNil ifFalse:[ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
283 |
"/ [ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
284 |
"/ [dir atEnd] whileFalse:[ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
285 |
"/ (name := dir nextLine) notNil ifTrue:[ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
286 |
"/ name := directory construct:name. |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
287 |
"/ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
288 |
"/ (aTwoArgBlock value:name value:(name isDirectory)) ifTrue:[ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
289 |
"/ ^ true |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
290 |
"/ ] |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
291 |
"/ ] |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
292 |
"/ ]. |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
293 |
"/ ] valueNowOrOnUnwindDo:[ |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
294 |
"/ dir close. |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
295 |
"/ ] |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
296 |
"/ ]. |
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
297 |
|
583 | 298 |
^ false |
696
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
299 |
|
9d1571f69f8e
use fileName>>directoryContents instead of a DirectoryStream.
Claus Gittinger <cg@exept.de>
parents:
695
diff
changeset
|
300 |
"Modified: / 24.9.1998 / 21:14:58 / cg" |
583 | 301 |
! ! |
302 |
||
1102 | 303 |
!DirectoryContents class methodsFor:'utilities'! |
304 |
||
305 |
contentsItemForFileName:aFilenameOrString |
|
306 |
| aFilename directory directoryContents| |
|
307 |
||
308 |
aFilename := aFilenameOrString asFilename. |
|
309 |
directory := aFilename directory. |
|
310 |
directoryContents := self directoryNamed:directory. |
|
1393
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
311 |
directoryContents isNil ifTrue:[ |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
312 |
aFilename exists ifTrue:[ |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
313 |
^ (DirectoryContentsItem new fileName:aFilename) info:aFilename info. |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
314 |
]. |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
315 |
^ nil |
bb7048aaf86a
care for directory without parent (network-dirs on WIN32)
Claus Gittinger <cg@exept.de>
parents:
1352
diff
changeset
|
316 |
]. |
1252
933afe6d5bd6
care for directory being removed
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
317 |
|
1253 | 318 |
directoryContents itemsDo:[:fileItemThere | |
1102 | 319 |
fileItemThere fileName = aFilename ifTrue:[ |
1350 | 320 |
fileItemThere updateInfo. |
1102 | 321 |
^ fileItemThere. |
322 |
] |
|
323 |
]. |
|
1105 | 324 |
^ (DirectoryContentsItem new fileName:aFilename) info:aFilename info. |
1102 | 325 |
|
326 |
" |
|
327 |
DirectoryContents contentsItemForFileName:'/etc/passwd' |
|
1105 | 328 |
DirectoryContents contentsItemForFileName:'/' |
1102 | 329 |
" |
330 |
! ! |
|
331 |
||
583 | 332 |
!DirectoryContents methodsFor:'accessing'! |
333 |
||
334 |
directory |
|
335 |
"returns the directoy name as Filename |
|
336 |
" |
|
337 |
^ directory |
|
338 |
! |
|
339 |
||
340 |
modificationTime |
|
341 |
"get the last modification time of the directory |
|
342 |
" |
|
951 | 343 |
^ directory modificationTime |
344 |
! |
|
345 |
||
346 |
timeStamp |
|
347 |
"get the last timeStamp (when the directory info was read) of the directory |
|
348 |
" |
|
583 | 349 |
^ timeStamp |
350 |
! ! |
|
351 |
||
352 |
!DirectoryContents methodsFor:'enumerating'! |
|
353 |
||
354 |
contentsAndBaseNamesDo:aThreeArgBlock |
|
355 |
"evaluate the block on each file; the argument to the block is the |
|
356 |
filename, the baseName and true in case of a directory |
|
357 |
block arguments: [:fileName :aBaseName :isDirectory| |
|
358 |
" |
|
1350 | 359 |
|
1093 | 360 |
self itemsDo:[:eachItem | |
361 |
aThreeArgBlock |
|
362 |
value:(eachItem fileName) |
|
363 |
value:(eachItem baseName ) |
|
364 |
value:(eachItem isDirectory) |
|
583 | 365 |
]. |
366 |
! |
|
367 |
||
368 |
contentsDo:aTwoArgBlock |
|
369 |
"evaluate the block on each file; the argument to the block is the |
|
370 |
filename and true in case of a directory |
|
371 |
block arguments: [:fileName :isDirectory| |
|
372 |
" |
|
1350 | 373 |
|
1093 | 374 |
self itemsDo:[:eachItem | |
375 |
aTwoArgBlock |
|
376 |
value:(eachItem fileName) |
|
377 |
value:(eachItem isDirectory) |
|
583 | 378 |
]. |
379 |
! |
|
380 |
||
381 |
directoriesAndBasenamesDo:aTwoArgBlock |
|
382 |
"evaluate block on each directory; a Filename and Basename. |
|
383 |
The directories are sorted |
|
384 |
" |
|
1350 | 385 |
|
1093 | 386 |
self itemsDo:[:eachItem | |
387 |
eachItem isDirectory ifTrue:[ |
|
388 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
|
583 | 389 |
] |
390 |
] |
|
391 |
! |
|
392 |
||
393 |
directoriesDo:aOneArgBlock |
|
394 |
"evaluate block on each directory; a Filename. The directories are sorted |
|
395 |
" |
|
1350 | 396 |
|
1093 | 397 |
self itemsDo:[:eachItem | |
398 |
eachItem isDirectory ifTrue:[ |
|
1350 | 399 |
aOneArgBlock value:(eachItem fileName) |
583 | 400 |
] |
1093 | 401 |
]. |
583 | 402 |
! |
403 |
||
404 |
filesAndBasenamesDo:aTwoArgBlock |
|
405 |
"evaluate block on each file; a Filename and a Basename. |
|
406 |
The files are sorted. |
|
407 |
" |
|
1350 | 408 |
|
1093 | 409 |
self itemsDo:[:eachItem | |
1324 | 410 |
eachItem isDirectory ifFalse:[ |
1093 | 411 |
aTwoArgBlock value:(eachItem fileName) value:(eachItem baseName) |
583 | 412 |
] |
413 |
] |
|
414 |
! |
|
415 |
||
416 |
filesDo:aOneArgBlock |
|
417 |
"evaluate block on each file; a Filename. The files are sorted. |
|
418 |
" |
|
1350 | 419 |
|
1093 | 420 |
self itemsDo:[:eachItem | |
1324 | 421 |
eachItem isDirectory ifFalse:[ |
1093 | 422 |
aOneArgBlock value:(eachItem fileName) |
583 | 423 |
] |
424 |
] |
|
1093 | 425 |
! |
426 |
||
427 |
itemsDo:aBlock |
|
428 |
"evaluate the block on each contentsItem, which contains the fileName and type info" |
|
429 |
||
1350 | 430 |
contents do:[:eachItem | |
431 |
aBlock value:eachItem. |
|
432 |
]. |
|
583 | 433 |
! ! |
434 |
||
435 |
!DirectoryContents methodsFor:'instance creation'! |
|
436 |
||
437 |
directory:aFilename |
|
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
438 |
"instance creation; setup attributes" |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
439 |
|
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
440 |
|t dircontents| |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
441 |
|
583 | 442 |
directory := aFilename asFilename. |
1435
5895158e7491
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
1431
diff
changeset
|
443 |
t := Timestamp now. "/ directory modificationTime. |
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
444 |
[ |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
445 |
dircontents := directory directoryContents. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
446 |
dircontents sort. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
447 |
contents := dircontents collect:[:eachBasename | DirectoryContentsItem new fileName:(directory construct:eachBasename)]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
448 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
449 |
"/ dircontents := directory directoryContentsAsFilenames. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
450 |
"/ dircontents sort:[:a :b | a baseName < b baseName]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
451 |
"/ contents := dircontents |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
452 |
"/ collect:[:eachFilename | DirectoryContentsItem new fileName:eachFilename]. |
1155
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
453 |
] on:FileStream openErrorSignal do:[:ex| |
edcf3cfe1e93
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1147
diff
changeset
|
454 |
contents := #(). |
1093 | 455 |
]. |
1101 | 456 |
timeStamp := t. |
1093 | 457 |
! ! |
458 |
||
459 |
!DirectoryContents methodsFor:'printing'! |
|
460 |
||
461 |
printOn:aStream |
|
462 |
aStream nextPutAll:'DirectoryContents of: '. |
|
463 |
directory printOn:aStream. |
|
583 | 464 |
! ! |
465 |
||
466 |
!DirectoryContents methodsFor:'private'! |
|
467 |
||
1093 | 468 |
updateContents |
1350 | 469 |
"ensure that the file-info os present for every item" |
1093 | 470 |
|
471 |
contents do:[:eachItem | |
|
1350 | 472 |
eachItem updateInfo. |
583 | 473 |
]. |
474 |
! ! |
|
475 |
||
476 |
!DirectoryContents methodsFor:'queries'! |
|
477 |
||
478 |
isObsolete |
|
1350 | 479 |
"returns true if the directory contents represented by the receiver is obsolete |
480 |
(i.e. if the fileSystems directory has been changed in the meanwhile) |
|
583 | 481 |
" |
951 | 482 |
|
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
483 |
|mt| |
679 | 484 |
|
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
485 |
directory exists ifFalse:[^ true]. |
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
486 |
timeStamp isNil ifTrue:[^ true]. |
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
487 |
(mt := directory modificationTime) isNil ifTrue:[^ true]. |
1350 | 488 |
"/ ignores milliseconds in the comparison |
974
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
489 |
timeStamp < mt ifTrue:[^ true]. |
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
490 |
timeStamp getSeconds = mt getSeconds ifTrue:[^ true]. |
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
491 |
^ false |
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
492 |
|
c4be514ddd96
fix for windows bug (directory has no modification time)
Claus Gittinger <cg@exept.de>
parents:
958
diff
changeset
|
493 |
"Modified: / 23.8.2001 / 16:50:51 / cg" |
906 | 494 |
! |
495 |
||
496 |
size |
|
497 |
"get number of files including directories in the directory |
|
498 |
" |
|
1093 | 499 |
^ contents size |
904 | 500 |
! ! |
501 |
||
502 |
!DirectoryContents methodsFor:'testing'! |
|
503 |
||
504 |
isEmpty |
|
505 |
"retuirns true if directory is empty |
|
506 |
" |
|
1093 | 507 |
^ contents size == 0 |
583 | 508 |
! |
509 |
||
510 |
notEmpty |
|
511 |
"returns true if directory is not empty |
|
512 |
" |
|
1093 | 513 |
^ contents size ~~ 0 |
514 |
! ! |
|
515 |
||
516 |
!DirectoryContents::DirectoryContentsItem methodsFor:'accessing'! |
|
517 |
||
518 |
baseName |
|
519 |
^ fileName baseName |
|
520 |
! |
|
521 |
||
522 |
fileName |
|
523 |
^ fileName |
|
524 |
! |
|
525 |
||
526 |
fileName:something |
|
527 |
"set the value of the instance variable 'fileName' (automatically generated)" |
|
528 |
||
529 |
fileName := something. |
|
530 |
! |
|
531 |
||
1100 | 532 |
info |
1350 | 533 |
info isNil ifTrue:[ |
534 |
self updateInfo. |
|
535 |
]. |
|
1100 | 536 |
info isSymbol ifTrue:[^ nil]. "/ a remote directory |
537 |
^ info |
|
538 |
! |
|
539 |
||
1099 | 540 |
info:something |
541 |
"set the value of the instance variable 'type' (automatically generated)" |
|
542 |
||
543 |
info := something. |
|
544 |
! |
|
545 |
||
1093 | 546 |
type |
1350 | 547 |
info isNil ifTrue:[ |
548 |
self updateInfo. |
|
549 |
]. |
|
1099 | 550 |
info isSymbol ifTrue:[^ info]. |
551 |
^ info type |
|
1093 | 552 |
! ! |
553 |
||
1350 | 554 |
!DirectoryContents::DirectoryContentsItem methodsFor:'misc'! |
555 |
||
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
556 |
cachedRemoteMountPoints |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
557 |
|mountPoints now| |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
558 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
559 |
mountPoints := CachedRemoteMountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
560 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
561 |
(mountPoints isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
562 |
or:[ CachedRemoteMountPointsTimeStamp isNil |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
563 |
or:[ now := Timestamp now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
564 |
(now - CachedRemoteMountPointsTimeStamp) > 30 |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
565 |
]]) ifTrue:[ |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
566 |
CachedRemoteMountPointsTimeStamp := now. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
567 |
mountPoints := OperatingSystem mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
568 |
mountPoints := mountPoints select:[:mp | mp isRemote]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
569 |
CachedRemoteMountPoints := mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
570 |
]. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
571 |
|
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
572 |
^ mountPoints. |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
573 |
! |
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
574 |
|
1350 | 575 |
updateInfo |
2047 | 576 |
"ensure that the file-info is present |
577 |
DirectoryContents flushCache |
|
578 |
" |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
579 |
|
2047 | 580 |
|mountPoints mountPoint nameString linkName| |
1350 | 581 |
|
582 |
info isNil ifTrue:[ |
|
583 |
nameString := fileName name. |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
584 |
self assert:[fileName isAbsolute]. |
1431
049b89c5bf60
cache mountPoints; faster directoryRead & sort
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
585 |
mountPoints := self cachedRemoteMountPoints. |
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
586 |
info := fileName linkInfo. |
2047 | 587 |
|
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
588 |
(info notNil and:[info isSymbolicLink]) ifTrue:[ |
2047 | 589 |
OperatingSystem isMSWINDOWSlike ifFalse:[ |
590 |
linkName := info path. |
|
2046 | 591 |
|
2047 | 592 |
linkName notNil ifTrue:[ |
593 |
"have to check for both link and link target" |
|
594 |
mountPoint := mountPoints |
|
595 |
detect:[:mInfo | |p| |
|
596 |
p := mInfo mountPointPath. |
|
597 |
((linkName startsWith:p) and:[ linkName startsWith:(p , '/') ]) |
|
598 |
] |
|
599 |
ifNone:nil. |
|
600 |
||
601 |
info := fileName info. "get the info of the link target" |
|
602 |
]. |
|
603 |
]. |
|
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
604 |
] ifFalse:[ |
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
605 |
"have to check for mountPoint only" |
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
606 |
mountPoint := mountPoints |
1468
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
607 |
detect:[:mInfo | mInfo mountPointPath = nameString ] |
476e360d4aeb
#linkInfo now returns the info also for files that are not a symlink
Stefan Vogel <sv@exept.de>
parents:
1435
diff
changeset
|
608 |
ifNone:nil. |
1351
9741c3cbc395
avoid blocking on mounted directories;
Claus Gittinger <cg@exept.de>
parents:
1350
diff
changeset
|
609 |
]. |
2047 | 610 |
mountPoint notNil ifTrue:[ |
1350 | 611 |
info := #remoteDirectory. |
612 |
] ifFalse:[ |
|
613 |
info isNil ifTrue:[ |
|
614 |
"/ broken symbolic link |
|
615 |
info := #symbolicLink. |
|
616 |
] |
|
617 |
]. |
|
618 |
]. |
|
619 |
! ! |
|
620 |
||
1093 | 621 |
!DirectoryContents::DirectoryContentsItem methodsFor:'printing'! |
622 |
||
623 |
printOn:aStream |
|
624 |
aStream nextPutAll:'DirectoryContentsItem for: '. |
|
625 |
fileName printOn:aStream. |
|
626 |
! ! |
|
627 |
||
628 |
!DirectoryContents::DirectoryContentsItem methodsFor:'queries'! |
|
629 |
||
630 |
isDirectory |
|
1099 | 631 |
|t| |
632 |
||
633 |
t := self type. |
|
634 |
^ (t == #directory or:[t == #remoteDirectory]) |
|
1093 | 635 |
! |
636 |
||
637 |
isRemoteDirectory |
|
1099 | 638 |
^ info == #remoteDirectory |
583 | 639 |
! ! |
640 |
||
641 |
!DirectoryContents class methodsFor:'documentation'! |
|
642 |
||
643 |
version |
|
2047 | 644 |
^ '$Header: /cvs/stx/stx/libbasic2/DirectoryContents.st,v 1.44 2008-10-23 06:12:15 ca Exp $' |
583 | 645 |
! ! |
1093 | 646 |
|
957 | 647 |
DirectoryContents initialize! |