author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 18084 | ab5b38bd8f81 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
172 | 3 |
All Rights Reserved |
1 | 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 |
" |
|
5556 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#FileDirectory |
1290 | 15 |
instanceVariableNames:'pathName lazy' |
16 |
classVariableNames:'PathOfCurrentDirectory' |
|
17 |
poolDictionaries:'' |
|
15631 | 18 |
category:'Compatibility-Squeak' |
1 | 19 |
! |
20 |
||
12351 | 21 |
ArrayedCollection subclass:#DirectoryEntry |
22 |
instanceVariableNames:'name creationTime modificationTime dirFlag fileSize' |
|
23 |
classVariableNames:'' |
|
24 |
poolDictionaries:'' |
|
25 |
privateIn:FileDirectory |
|
26 |
! |
|
27 |
||
88 | 28 |
!FileDirectory class methodsFor:'documentation'! |
29 |
||
30 |
copyright |
|
31 |
" |
|
32 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
172 | 33 |
All Rights Reserved |
1 | 34 |
|
88 | 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 |
! |
|
1 | 43 |
|
88 | 44 |
documentation |
45 |
" |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
46 |
Notice: |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
47 |
This class is obsolete and does work only under UNIX. |
15631 | 48 |
Use Filename instead. It is left here for squeak compatibility. |
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
49 |
|
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
50 |
This class is not available in other ST-systems; |
15631 | 51 |
in contrast, VisualWorks provides a Filename class. |
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
52 |
Therefore, Filename has taken over the functionality. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
53 |
|
88 | 54 |
FileDirectories represent directories in the underlying host system. |
55 |
They provide various methods to create/delete and query for files and/or |
|
202 | 56 |
directories. Also, since FileDirectory inherits from Collection, it |
57 |
provides all enumeration and testing protocol. For example, you can |
|
58 |
loop over the filenames in a directory using 'aFileDirectory do:[:nm | ...]'. |
|
59 |
||
1290 | 60 |
[author:] |
61 |
Claus Gittinger |
|
62 |
||
63 |
[see also:] |
|
64 |
Filename |
|
65 |
FileStream DirectoryStream OperatingSystem |
|
88 | 66 |
" |
67 |
! ! |
|
1 | 68 |
|
2 | 69 |
!FileDirectory class methodsFor:'initialization'! |
70 |
||
71 |
initialize |
|
276 | 72 |
"/ |
73 |
"/ want to know about image restart |
|
74 |
"/ |
|
2 | 75 |
ObjectMemory addDependent:self |
76 |
! |
|
77 |
||
1469 | 78 |
update:something with:aParameter from:changedObject |
276 | 79 |
"/ |
80 |
"/ currentDirectory may be different when restarted |
|
81 |
"/ |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
82 |
something == #earlySystemInstallation ifTrue:[ |
1469 | 83 |
PathOfCurrentDirectory := nil |
2 | 84 |
] |
1469 | 85 |
|
3941
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
86 |
"Created: / 15.6.1996 / 15:20:21 / cg" |
574defdaaa40
flush cached current-dir-path earlier.
Claus Gittinger <cg@exept.de>
parents:
3203
diff
changeset
|
87 |
"Modified: / 11.12.1998 / 16:28:40 / cg" |
2 | 88 |
! ! |
89 |
||
1 | 90 |
!FileDirectory class methodsFor:'instance creation'! |
91 |
||
92 |
currentDirectory |
|
93 |
"create and return a new FileDirectory for the current directory" |
|
94 |
||
2951 | 95 |
"/ self obsoleteMethodWarning:'use Filename'. |
13615 | 96 |
^ self on:(Filename currentDirectory name) |
88 | 97 |
|
98 |
" |
|
99 |
FileDirectory currentDirectory contents |
|
100 |
FileDirectory currentDirectory files |
|
101 |
FileDirectory currentDirectory isReadable |
|
102 |
FileDirectory currentDirectory pathName |
|
103 |
" |
|
13615 | 104 |
|
105 |
"Modified: / 20-08-2011 / 12:19:51 / cg" |
|
1 | 106 |
! |
107 |
||
605 | 108 |
directoryNamed:name |
109 |
"create and return a new FileDirectory for the directory |
|
110 |
with given pathname" |
|
111 |
||
2951 | 112 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 113 |
name asString = '.' ifTrue:[ |
13615 | 114 |
Filename currentDirectory name ~= '.' ifTrue:[ |
115 |
^ self currentDirectory |
|
116 |
] |
|
2921 | 117 |
]. |
13615 | 118 |
^ self on:(name asFilename constructString:'') |
605 | 119 |
|
120 |
" |
|
121 |
(FileDirectory directoryNamed:'..') pathName |
|
122 |
(FileDirectory directoryNamed:'../..') files |
|
123 |
" |
|
13615 | 124 |
|
125 |
"Modified: / 20-08-2011 / 12:19:57 / cg" |
|
605 | 126 |
! |
127 |
||
1 | 128 |
directoryNamed:name in:aFileDirectory |
129 |
"create and return a new FileDirectory for the directory with given name |
|
130 |
in another FileDirectory" |
|
131 |
||
2961 | 132 |
|baseName| |
1 | 133 |
|
2951 | 134 |
"/ self obsoleteMethodWarning:'use Filename'. |
2921 | 135 |
|
136 |
name asFilename isAbsolute ifTrue:[ |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
137 |
^ self directoryNamed:name |
1 | 138 |
]. |
2921 | 139 |
|
1 | 140 |
(aFileDirectory isKindOf:FileDirectory) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
141 |
baseName := aFileDirectory pathName |
1 | 142 |
] ifFalse:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
143 |
baseName := aFileDirectory |
1 | 144 |
]. |
13615 | 145 |
^ self on:((Filename named:baseName) constructString:name) |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
146 |
|
13615 | 147 |
"Modified: / 28-04-1997 / 22:34:47 / dq" |
148 |
"Modified: / 20-08-2011 / 12:20:03 / cg" |
|
149 |
! |
|
150 |
||
151 |
on:aDirectoryPathName |
|
152 |
^ (self basicNew) pathName:aDirectoryPathName |
|
153 |
||
154 |
"Created: / 20-08-2011 / 12:19:28 / cg" |
|
605 | 155 |
! |
156 |
||
157 |
rootDirectory |
|
158 |
"create and return a new FileDirectory for the root directory" |
|
159 |
||
2951 | 160 |
"/ self obsoleteMethodWarning:'use Filename'. |
13615 | 161 |
^ self on:(Filename rootDirectory name) |
605 | 162 |
|
163 |
" |
|
164 |
FileDirectory rootDirectory contents |
|
165 |
FileDirectory rootDirectory files |
|
166 |
FileDirectory rootDirectory isReadable |
|
167 |
" |
|
13615 | 168 |
|
169 |
"Modified: / 20-08-2011 / 12:20:09 / cg" |
|
276 | 170 |
! ! |
171 |
||
7305 | 172 |
!FileDirectory class methodsFor:'Compatibility-Squeak'! |
5729 | 173 |
|
7655 | 174 |
default |
175 |
^ (self basicNew) pathName:(Filename defaultDirectory name) |
|
176 |
! |
|
177 |
||
5777 | 178 |
localNameFor:aFileNameString |
179 |
^ aFileNameString asFilename baseName |
|
180 |
! |
|
181 |
||
5729 | 182 |
pathNameDelimiter |
183 |
^ Filename separator |
|
13615 | 184 |
! |
185 |
||
186 |
slash |
|
187 |
^ Filename separator asString |
|
188 |
||
189 |
"Created: / 20-08-2011 / 12:18:27 / cg" |
|
5729 | 190 |
! ! |
191 |
||
276 | 192 |
!FileDirectory class methodsFor:'private'! |
193 |
||
194 |
fullPathNameOf:name in:path |
|
195 |
|sep| |
|
196 |
||
197 |
sep := OperatingSystem fileSeparator asString. |
|
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
198 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
199 |
"/ is it an absolute path ? |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
200 |
|
276 | 201 |
(name startsWith:sep) ifTrue:[ |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
202 |
^ name |
276 | 203 |
]. |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
204 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
205 |
"/ temporary kludge; this and the previous should |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
206 |
"/ go into OS isAbsolutePath .. |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
207 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
208 |
OperatingSystem isMSDOSlike ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
209 |
('[a-zA-Z]:*' match:name) ifTrue:[ |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
210 |
"/ drive spec ... |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
211 |
^ name |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
212 |
] |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
213 |
]. |
276 | 214 |
^ path , sep , name |
2590
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
215 |
|
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
216 |
"Modified: 28.4.1997 / 22:06:17 / cg" |
6855cfa1e628
temporary kludge for drive specifiers in MSDOS fileNames.
Claus Gittinger <cg@exept.de>
parents:
1469
diff
changeset
|
217 |
"Modified: 28.4.1997 / 22:34:18 / dq" |
1 | 218 |
! ! |
219 |
||
7655 | 220 |
!FileDirectory methodsFor:'Compatibility-Squeak'! |
221 |
||
11816 | 222 |
assureExistence |
223 |
self asFilename recursiveMakeDirectory |
|
224 |
! |
|
225 |
||
226 |
containingDirectory |
|
227 |
^ self class |
|
228 |
directoryNamed:(self asFilename directory pathName) |
|
229 |
! |
|
230 |
||
7655 | 231 |
deleteDirectory:dirName |
9332 | 232 |
"Squeak/ST80 compatibility" |
7655 | 233 |
|
234 |
^ self removeDirectory:dirName |
|
235 |
! |
|
236 |
||
237 |
deleteFileNamed:fileName |
|
9332 | 238 |
"Squeak/ST80 compatibility" |
7655 | 239 |
|
240 |
^ self removeFile:fileName |
|
241 |
! |
|
242 |
||
12351 | 243 |
directoryEntryFor:name |
244 |
|info| |
|
245 |
||
246 |
info := self infoOf:name. |
|
13620
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
247 |
info isNil ifTrue:[ |
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
248 |
info := (self filenameFor:name) linkInfo. |
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
249 |
]. |
12351 | 250 |
^ DirectoryEntry new |
251 |
name:name |
|
252 |
creationTime:(info creationTime) |
|
253 |
modificationTime:(info modificationTime) |
|
254 |
dirFlag:(info isDirectory) |
|
255 |
fileSize:(info isDirectory) |
|
256 |
||
257 |
" |
|
258 |
(FileDirectory directoryNamed:'.') entries |
|
259 |
" |
|
13620
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
260 |
|
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
261 |
"Modified: / 29-08-2011 / 08:19:48 / cg" |
12351 | 262 |
! |
263 |
||
9332 | 264 |
directoryExists:dir |
265 |
"Squeak/ST80 compatibility" |
|
266 |
||
267 |
|f| |
|
268 |
||
269 |
f := self filenameFor:dir. |
|
270 |
^ f exists and:[f isDirectory ]. |
|
271 |
! |
|
272 |
||
7655 | 273 |
directoryNames |
9332 | 274 |
"Squeak/ST80 compatibility" |
7655 | 275 |
|
276 |
^ self directories |
|
277 |
! |
|
278 |
||
11816 | 279 |
entries |
12351 | 280 |
^ self contents collect:[:nm | self directoryEntryFor:nm]. |
11816 | 281 |
! |
282 |
||
9334 | 283 |
fileExists:dir |
284 |
"Squeak/ST80 compatibility" |
|
285 |
||
286 |
|f| |
|
287 |
||
288 |
f := self filenameFor:dir. |
|
289 |
^ f exists and:[f isDirectory not ]. |
|
290 |
! |
|
291 |
||
7655 | 292 |
fileNames |
9332 | 293 |
"Squeak/ST80 compatibility" |
7655 | 294 |
|
295 |
^ self files |
|
296 |
! |
|
297 |
||
11816 | 298 |
forceNewFileNamed:fn |
299 |
"Squeak/ST80 compatibility" |
|
300 |
||
301 |
|f| |
|
302 |
||
303 |
f := self filenameFor:fn. |
|
304 |
^ f writeStream |
|
305 |
! |
|
306 |
||
7655 | 307 |
includesKey:name |
9332 | 308 |
"Squeak/ST80 compatibility" |
7655 | 309 |
|
310 |
^ self exists:name |
|
11816 | 311 |
! |
312 |
||
15631 | 313 |
oldFileNamed:fn |
314 |
"Squeak/ST80 compatibility" |
|
315 |
||
316 |
|f| |
|
317 |
||
318 |
f := self filenameFor:fn. |
|
319 |
^ f readStream |
|
320 |
! |
|
321 |
||
11816 | 322 |
readOnlyFileNamed:fn |
323 |
"Squeak/ST80 compatibility" |
|
324 |
||
325 |
|f| |
|
326 |
||
327 |
f := self filenameFor:fn. |
|
328 |
^ f readStream |
|
329 |
! |
|
330 |
||
331 |
recursiveDelete |
|
332 |
self asFilename recursiveRemove |
|
7655 | 333 |
! ! |
7305 | 334 |
|
1 | 335 |
!FileDirectory methodsFor:'accessing'! |
336 |
||
337 |
baseName |
|
338 |
"return my baseName |
|
339 |
- thats the directory name without leading parent-dirs" |
|
340 |
||
341 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 342 |
^ pathName asFilename baseName |
1 | 343 |
! |
344 |
||
345 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
346 |
"return a collection with all files and subdirectories in the receiver. |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
347 |
Skips any '.' or '..' entries (UNIX)" |
1 | 348 |
|
349 |
|coll| |
|
350 |
||
351 |
coll := OrderedCollection new. |
|
352 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
353 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
354 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
355 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
356 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
357 |
] |
1 | 358 |
]. |
276 | 359 |
coll sort. |
1 | 360 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
361 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
362 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 363 |
! |
364 |
||
365 |
directories |
|
366 |
"return a collection with all subdirectories in the receiver directory" |
|
367 |
||
368 |
|coll| |
|
369 |
||
370 |
coll := OrderedCollection new. |
|
371 |
self directoriesDo:[:name | |
|
172 | 372 |
coll add:name |
1 | 373 |
]. |
276 | 374 |
coll sort. |
1 | 375 |
^ coll |
376 |
! |
|
377 |
||
605 | 378 |
directoryName |
379 |
"return my directoryName |
|
380 |
- thats the directory name where I'm in" |
|
381 |
||
382 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 383 |
^ pathName asFilename directoryName |
605 | 384 |
! |
385 |
||
1 | 386 |
files |
387 |
"return a collection with all plain files in the receiver directory" |
|
388 |
||
389 |
|coll| |
|
390 |
||
391 |
coll := OrderedCollection new. |
|
392 |
self filesDo:[:name | |
|
172 | 393 |
coll add:name |
1 | 394 |
]. |
276 | 395 |
coll sort. |
396 |
^ coll |
|
605 | 397 |
! |
398 |
||
399 |
pathName |
|
400 |
"return my full pathname" |
|
401 |
||
402 |
lazy ifTrue:[self getFullPathName]. |
|
403 |
^ pathName |
|
404 |
! |
|
405 |
||
406 |
pathName:dirName |
|
407 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
408 |
||
409 |
pathName := dirName. |
|
410 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
411 |
lazy := true |
|
412 |
] ifTrue:[ |
|
413 |
(dirName startsWith:'./') ifFalse:[ |
|
414 |
"/ (dirName includes:$.) ifTrue:[ |
|
415 |
lazy := true |
|
416 |
] |
|
417 |
]. |
|
418 |
^ self |
|
419 |
! ! |
|
420 |
||
421 |
!FileDirectory methodsFor:'basic'! |
|
422 |
||
423 |
createDirectory:newName |
|
424 |
"create a new filedirectory as a subdirectory of myself; |
|
425 |
return true if successful" |
|
426 |
||
427 |
|realName| |
|
428 |
||
429 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
430 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
431 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
432 |
realName := newName copyFrom:2 |
|
433 |
] ifFalse:[ |
|
434 |
realName := newName |
|
435 |
]. |
|
2921 | 436 |
^ (self filenameFor:realName) makeDirectory |
605 | 437 |
] |
438 |
]. |
|
439 |
^ false |
|
440 |
! |
|
441 |
||
442 |
link:oldFileName to:newFileName |
|
443 |
"link oldFileName to newFileName in myself, return true if successful" |
|
444 |
||
445 |
|path1 path2| |
|
446 |
||
447 |
path1 := self pathNameOf:oldFileName. |
|
448 |
path2 := self pathNameOf:newFileName. |
|
449 |
^ OperatingSystem linkFile:path1 to:path2 |
|
450 |
! |
|
451 |
||
452 |
remove:aFileOrDirectoryName |
|
453 |
"remove the file or directory from myself; return true if successful" |
|
454 |
||
2921 | 455 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 456 |
! |
457 |
||
458 |
removeDirectory:dirName |
|
459 |
"remove the directory 'dirName' from myself; return true if successful. |
|
460 |
If the directory is not empty, the containing files/directories are also |
|
461 |
removed." |
|
462 |
||
2921 | 463 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 464 |
! |
465 |
||
466 |
removeFile:fileName |
|
467 |
"remove the file 'fileName' from myself; return true if successful" |
|
468 |
||
2921 | 469 |
^ (self filenameFor:fileName) remove |
605 | 470 |
! |
471 |
||
472 |
renameFile:oldFileName newName:newFileName |
|
473 |
"rename the file; return true if successful" |
|
474 |
||
2921 | 475 |
|f1 f2| |
605 | 476 |
|
2921 | 477 |
f1 := self filenameFor:oldFileName. |
478 |
f2 := self filenameFor:newFileName. |
|
479 |
^ f1 renameTo:f2 |
|
1 | 480 |
! ! |
481 |
||
172 | 482 |
!FileDirectory methodsFor:'converting'! |
483 |
||
484 |
asFilename |
|
485 |
"return myself as a filename" |
|
486 |
||
487 |
^ self pathName asFilename |
|
488 |
! |
|
489 |
||
490 |
asFilename:someFile |
|
491 |
"return a filename for a file named someFile in myself" |
|
492 |
||
276 | 493 |
^ self asFilename construct:someFile |
172 | 494 |
! ! |
495 |
||
605 | 496 |
!FileDirectory methodsFor:'enumerating'! |
497 |
||
498 |
allDirectoriesDo:aBlock |
|
499 |
"evaluate the argument, aBlock for every directory name |
|
500 |
in the directory and in all subdirectories" |
|
501 |
||
502 |
|aStream command line| |
|
503 |
||
504 |
lazy ifTrue:[self getFullPathName]. |
|
505 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
506 |
aStream := PipeStream readingFrom:command. |
|
507 |
aStream isNil ifTrue:[^ nil]. |
|
508 |
[aStream atEnd] whileFalse:[ |
|
509 |
line := aStream nextLine. |
|
510 |
line notNil ifTrue:[ |
|
511 |
(line = '.') ifFalse:[ |
|
512 |
"cut off initial ./" |
|
513 |
line := line copyFrom:3 |
|
514 |
]. |
|
515 |
aBlock value:line |
|
516 |
] |
|
517 |
]. |
|
518 |
aStream close |
|
519 |
! |
|
520 |
||
521 |
allFilesDo:aBlock |
|
522 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
523 |
subdirectories" |
|
524 |
||
525 |
|aStream command line| |
|
526 |
||
527 |
lazy ifTrue:[self getFullPathName]. |
|
528 |
command := 'cd ' , pathName , '; find . -print'. |
|
529 |
aStream := PipeStream readingFrom:command. |
|
530 |
aStream isNil ifTrue:[^ nil]. |
|
531 |
[aStream atEnd] whileFalse:[ |
|
532 |
line := aStream nextLine. |
|
533 |
line notNil ifTrue:[ |
|
534 |
(line = '.') ifFalse:[ |
|
535 |
"cut off initial ./" |
|
536 |
line := line copyFrom:3 |
|
537 |
]. |
|
538 |
aBlock value:line |
|
539 |
] |
|
540 |
]. |
|
541 |
aStream close |
|
542 |
! |
|
543 |
||
544 |
directoriesDo:aBlock |
|
545 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
546 |
||
547 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
548 |
((name ~= '.') and:[name ~= '..']) |
|
549 |
] ifFalse:[ |
|
550 |
false |
|
551 |
] |
|
552 |
] do:aBlock |
|
553 |
! |
|
554 |
||
555 |
do:aBlock |
|
556 |
"evaluate the argument, aBlock for every name in the directory" |
|
557 |
||
558 |
self where:[:name | true] do:aBlock |
|
559 |
! |
|
560 |
||
561 |
filesDo:aBlock |
|
562 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
563 |
||
564 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
565 |
! |
|
566 |
||
567 |
namesDo:aBlock |
|
568 |
"evaluate the argument, aBlock for every name in the directory. |
|
569 |
for ST-80 compatibility" |
|
570 |
||
571 |
self do:aBlock |
|
572 |
! |
|
573 |
||
574 |
where:testBlock do:aBlock |
|
575 |
"evaluate the argument, aBlock for every object in the directory |
|
576 |
for which testBlock evaluates to true." |
|
577 |
||
578 |
|aStream name| |
|
579 |
||
7655 | 580 |
ExternalStream openErrorSignal catch:[ |
581 |
aStream := DirectoryStream directoryNamed:pathName. |
|
582 |
]. |
|
605 | 583 |
aStream isNil ifTrue:[^ nil]. |
584 |
[aStream atEnd] whileFalse:[ |
|
7655 | 585 |
name := aStream nextLine. |
586 |
name notNil ifTrue:[ |
|
587 |
(testBlock value:name) ifTrue:[ |
|
588 |
aBlock value:name |
|
589 |
] |
|
590 |
] |
|
605 | 591 |
]. |
592 |
aStream close |
|
593 |
! ! |
|
594 |
||
14332
24dcfd3da954
category of: #directoryNamed:
Claus Gittinger <cg@exept.de>
parents:
13821
diff
changeset
|
595 |
!FileDirectory methodsFor:'instance creation'! |
605 | 596 |
|
597 |
directoryNamed:aName |
|
2961 | 598 |
"create & return a new fileDirectory for a subdirectory in myself" |
599 |
||
605 | 600 |
^ self class directoryNamed:aName in:self pathName |
601 |
! ! |
|
602 |
||
603 |
!FileDirectory methodsFor:'printing & storing'! |
|
604 |
||
605 |
printOn:aStream |
|
4392 | 606 |
"append a user printed representation of the receiver to aStream. |
607 |
The format is suitable for a human - not meant to be read back." |
|
605 | 608 |
|
609 |
lazy ifTrue:[self getFullPathName]. |
|
610 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
4392 | 611 |
nextPutAll:pathName; |
612 |
nextPutAll:')' |
|
605 | 613 |
! |
614 |
||
615 |
storeOn:aStream |
|
616 |
"append a printed representation of the receiver to aStream, |
|
617 |
which allows reconstructing it via readFrom:" |
|
618 |
||
619 |
lazy ifTrue:[self getFullPathName]. |
|
620 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
621 |
aStream nextPutAll:pathName. |
|
622 |
aStream nextPut:$) |
|
623 |
! ! |
|
624 |
||
1 | 625 |
!FileDirectory methodsFor:'private'! |
626 |
||
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
627 |
filenameFor:fileName |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
628 |
"return a filename representing the argument, fileName |
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
629 |
either in myself (if the arg is a releative path) or absolute otherwise." |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
630 |
|
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
631 |
^ pathName asFilename filenameFor:fileName |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
632 |
! |
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
633 |
|
9334 | 634 |
fullNameFor:fileName |
635 |
"return a filename representing the argument, fileName |
|
636 |
either in myself (if the arg is a releative path) or absolute otherwise." |
|
637 |
||
638 |
^ (pathName asFilename filenameFor:fileName) asAbsoluteFilename |
|
639 |
! |
|
640 |
||
1 | 641 |
getFullPathName |
642 |
"make my pathname be a full pathname - i.e. starting at root" |
|
643 |
||
308 | 644 |
|shortPathName| |
1 | 645 |
|
2921 | 646 |
|
159 | 647 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 648 |
lazy := false. |
649 |
^ self |
|
1 | 650 |
]. |
651 |
||
652 |
"since currentDirectory is used very often, cache its path here" |
|
653 |
||
654 |
(pathName = '.') ifTrue:[ |
|
172 | 655 |
PathOfCurrentDirectory notNil ifTrue:[ |
656 |
pathName := PathOfCurrentDirectory. |
|
657 |
lazy := false. |
|
658 |
^ self |
|
659 |
] |
|
1 | 660 |
]. |
661 |
||
662 |
shortPathName := pathName. |
|
663 |
||
308 | 664 |
pathName := OperatingSystem pathNameOf:pathName. |
665 |
lazy := false. |
|
1 | 666 |
|
308 | 667 |
" |
668 |
if it was the current dir, keep name for next query |
|
669 |
" |
|
1 | 670 |
(shortPathName = '.') ifTrue:[ |
308 | 671 |
PathOfCurrentDirectory := pathName |
1 | 672 |
] |
276 | 673 |
! |
674 |
||
675 |
pathNameOf:fileName |
|
2961 | 676 |
"return the pathname for an entry in myself" |
677 |
||
2921 | 678 |
^ (pathName asFilename construct:fileName) osName |
1 | 679 |
! ! |
680 |
||
605 | 681 |
!FileDirectory methodsFor:'queries'! |
1 | 682 |
|
605 | 683 |
accessModeOf:name |
684 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 685 |
|
605 | 686 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 687 |
! |
688 |
||
605 | 689 |
changeAccessModeOf:name to:modeBits |
690 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 691 |
|
605 | 692 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 693 |
! |
694 |
||
695 |
exists |
|
696 |
"return true if this directory exists" |
|
697 |
||
2921 | 698 |
^ (Filename named:pathName) isDirectory |
159 | 699 |
|
700 |
" |
|
701 |
(FileDirectory directoryNamed:'fooBar') exists |
|
702 |
(FileDirectory directoryNamed:'/tmp') exists |
|
703 |
" |
|
1 | 704 |
! |
705 |
||
605 | 706 |
exists:name |
707 |
"return true, if the given name exists in myself" |
|
708 |
||
2921 | 709 |
^ (self filenameFor:name) exists |
605 | 710 |
! |
711 |
||
712 |
id |
|
713 |
"return the directories file-id (inode number)" |
|
714 |
||
2921 | 715 |
^ (Filename named:pathName) id |
605 | 716 |
! |
717 |
||
718 |
infoOf:name |
|
719 |
"return an array filled with file info for the file 'aFileName'; |
|
720 |
return nil if such a file does not exist" |
|
721 |
||
2921 | 722 |
^ (self filenameFor:name) info |
605 | 723 |
! |
724 |
||
725 |
isDirectory:name |
|
726 |
"return true, if the given name is that of a directory in myself" |
|
727 |
||
2921 | 728 |
^ (self filenameFor:name) isDirectory |
605 | 729 |
! |
730 |
||
731 |
isExecutable:name |
|
732 |
"return true, if the given file is executable" |
|
1 | 733 |
|
2921 | 734 |
^ (self filenameFor:name) isExecutable |
1 | 735 |
! |
736 |
||
737 |
isReadable:name |
|
738 |
"return true, if the given file is readable" |
|
739 |
||
2921 | 740 |
^ (self filenameFor:name) isReadable |
1 | 741 |
! |
742 |
||
743 |
isWritable:name |
|
744 |
"return true, if the given file is readable" |
|
745 |
||
2921 | 746 |
^ (self filenameFor:name) isWritable |
1 | 747 |
! |
748 |
||
605 | 749 |
species |
6550 | 750 |
"return the type of collection to be returned by collect, select etc." |
751 |
||
605 | 752 |
^ OrderedCollection |
1 | 753 |
! |
754 |
||
605 | 755 |
timeOfLastChange |
756 |
"return the timeStamp of myself" |
|
1 | 757 |
|
2921 | 758 |
^ (Filename named:pathName) modificationTime |
1 | 759 |
! |
760 |
||
605 | 761 |
timeOfLastChange:name |
762 |
"return the timeStamp of a file in myself" |
|
1 | 763 |
|
2921 | 764 |
^ (self filenameFor:name) modificationTime |
1 | 765 |
! |
766 |
||
605 | 767 |
typeOf:name |
768 |
"return the symbolic type of a file in myself" |
|
1 | 769 |
|
2921 | 770 |
^ (self filenameFor:name) type |
605 | 771 |
! ! |
1 | 772 |
|
5556 | 773 |
!FileDirectory methodsFor:'testing'! |
774 |
||
775 |
isEmpty |
|
776 |
"return true, if the directory is empty; |
|
777 |
redefined since '.' and '..' do not count as entries here." |
|
778 |
||
779 |
self do:[:fName | |
|
780 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
|
781 |
]. |
|
782 |
^ true |
|
783 |
! ! |
|
784 |
||
12351 | 785 |
!FileDirectory::DirectoryEntry methodsFor:'accessing'! |
786 |
||
787 |
at: index |
|
788 |
"compatibility interface" |
|
789 |
"self halt: 'old-style access to DirectoryEntry'" |
|
790 |
index = 1 ifTrue: [ ^self name ]. |
|
791 |
index = 2 ifTrue: [ ^self creationTime ]. |
|
792 |
index = 3 ifTrue: [ ^self modificationTime ]. |
|
793 |
index = 4 ifTrue:[ ^self isDirectory ]. |
|
794 |
index = 5 ifTrue:[ ^self fileSize ]. |
|
795 |
self error: 'invalid index specified'. |
|
796 |
! |
|
797 |
||
798 |
creationTime |
|
799 |
^ creationTime |
|
800 |
! |
|
801 |
||
802 |
dirFlag |
|
803 |
^ dirFlag |
|
804 |
! |
|
805 |
||
806 |
fileSize |
|
807 |
^ fileSize |
|
808 |
! |
|
809 |
||
810 |
modificationTime |
|
811 |
^ modificationTime |
|
812 |
! |
|
813 |
||
814 |
name |
|
815 |
^ name |
|
816 |
! |
|
817 |
||
818 |
name:nameArg creationTime:creationTimeArg modificationTime:modificationTimeArg dirFlag:dirFlagArg fileSize:fileSizeArg |
|
819 |
name := nameArg. |
|
820 |
creationTime := creationTimeArg. |
|
821 |
modificationTime := modificationTimeArg. |
|
822 |
dirFlag := dirFlagArg. |
|
823 |
fileSize := fileSizeArg. |
|
824 |
! ! |
|
825 |
||
629 | 826 |
!FileDirectory class methodsFor:'documentation'! |
827 |
||
828 |
version |
|
15631 | 829 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.54 2013-08-16 11:49:12 cg Exp $' |
12351 | 830 |
! |
831 |
||
832 |
version_CVS |
|
15631 | 833 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.54 2013-08-16 11:49:12 cg Exp $' |
629 | 834 |
! ! |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
835 |
|
15631 | 836 |
|
605 | 837 |
FileDirectory initialize! |