author | Claus Gittinger <cg@exept.de> |
Mon, 29 Aug 2011 08:23:13 +0200 | |
changeset 13620 | e38a8ec52cd9 |
parent 13615 | 6fee233c1f8a |
child 13821 | b8f74fc87584 |
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:'' |
|
3203
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
18 |
category:'Obsolete' |
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. |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
48 |
Use Filename instead. |
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; |
09a9b6bde8af
Moved into category 'Obsolete'.
Stefan Vogel <sv@exept.de>
parents:
3201
diff
changeset
|
51 |
in contrast, ST-80 provides a Filename class. |
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 |
||
313 |
readOnlyFileNamed:fn |
|
314 |
"Squeak/ST80 compatibility" |
|
315 |
||
316 |
|f| |
|
317 |
||
318 |
f := self filenameFor:fn. |
|
319 |
^ f readStream |
|
320 |
! |
|
321 |
||
322 |
recursiveDelete |
|
323 |
self asFilename recursiveRemove |
|
7655 | 324 |
! ! |
7305 | 325 |
|
1 | 326 |
!FileDirectory methodsFor:'accessing'! |
327 |
||
328 |
baseName |
|
329 |
"return my baseName |
|
330 |
- thats the directory name without leading parent-dirs" |
|
331 |
||
332 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 333 |
^ pathName asFilename baseName |
1 | 334 |
! |
335 |
||
336 |
contents |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
337 |
"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
|
338 |
Skips any '.' or '..' entries (UNIX)" |
1 | 339 |
|
340 |
|coll| |
|
341 |
||
342 |
coll := OrderedCollection new. |
|
343 |
self do:[:name | |
|
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
344 |
name ~= '.' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
345 |
name ~= '..' ifTrue:[ |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
346 |
coll add:name |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
347 |
] |
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
348 |
] |
1 | 349 |
]. |
276 | 350 |
coll sort. |
1 | 351 |
^ coll |
2708
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
352 |
|
6c4a4f30d540
skip '.' and '..' in #contents
Claus Gittinger <cg@exept.de>
parents:
2590
diff
changeset
|
353 |
"Modified: 20.6.1997 / 17:05:43 / cg" |
1 | 354 |
! |
355 |
||
356 |
directories |
|
357 |
"return a collection with all subdirectories in the receiver directory" |
|
358 |
||
359 |
|coll| |
|
360 |
||
361 |
coll := OrderedCollection new. |
|
362 |
self directoriesDo:[:name | |
|
172 | 363 |
coll add:name |
1 | 364 |
]. |
276 | 365 |
coll sort. |
1 | 366 |
^ coll |
367 |
! |
|
368 |
||
605 | 369 |
directoryName |
370 |
"return my directoryName |
|
371 |
- thats the directory name where I'm in" |
|
372 |
||
373 |
lazy ifTrue:[self getFullPathName]. |
|
2921 | 374 |
^ pathName asFilename directoryName |
605 | 375 |
! |
376 |
||
1 | 377 |
files |
378 |
"return a collection with all plain files in the receiver directory" |
|
379 |
||
380 |
|coll| |
|
381 |
||
382 |
coll := OrderedCollection new. |
|
383 |
self filesDo:[:name | |
|
172 | 384 |
coll add:name |
1 | 385 |
]. |
276 | 386 |
coll sort. |
387 |
^ coll |
|
605 | 388 |
! |
389 |
||
390 |
pathName |
|
391 |
"return my full pathname" |
|
392 |
||
393 |
lazy ifTrue:[self getFullPathName]. |
|
394 |
^ pathName |
|
395 |
! |
|
396 |
||
397 |
pathName:dirName |
|
398 |
"set my pathname; return nil if not a valid path; self otherwise" |
|
399 |
||
400 |
pathName := dirName. |
|
401 |
(dirName startsWith:OperatingSystem fileSeparator) ifFalse:[ |
|
402 |
lazy := true |
|
403 |
] ifTrue:[ |
|
404 |
(dirName startsWith:'./') ifFalse:[ |
|
405 |
"/ (dirName includes:$.) ifTrue:[ |
|
406 |
lazy := true |
|
407 |
] |
|
408 |
]. |
|
409 |
^ self |
|
410 |
! ! |
|
411 |
||
412 |
!FileDirectory methodsFor:'basic'! |
|
413 |
||
414 |
createDirectory:newName |
|
415 |
"create a new filedirectory as a subdirectory of myself; |
|
416 |
return true if successful" |
|
417 |
||
418 |
|realName| |
|
419 |
||
420 |
(newName notNil and:[newName notEmpty]) ifTrue:[ |
|
421 |
(newName ~= '.' and:[newName ~= '..']) ifTrue:[ |
|
422 |
((newName at:1) == OperatingSystem fileSeparator) ifTrue:[ |
|
423 |
realName := newName copyFrom:2 |
|
424 |
] ifFalse:[ |
|
425 |
realName := newName |
|
426 |
]. |
|
2921 | 427 |
^ (self filenameFor:realName) makeDirectory |
605 | 428 |
] |
429 |
]. |
|
430 |
^ false |
|
431 |
! |
|
432 |
||
433 |
link:oldFileName to:newFileName |
|
434 |
"link oldFileName to newFileName in myself, return true if successful" |
|
435 |
||
436 |
|path1 path2| |
|
437 |
||
438 |
path1 := self pathNameOf:oldFileName. |
|
439 |
path2 := self pathNameOf:newFileName. |
|
440 |
^ OperatingSystem linkFile:path1 to:path2 |
|
441 |
! |
|
442 |
||
443 |
remove:aFileOrDirectoryName |
|
444 |
"remove the file or directory from myself; return true if successful" |
|
445 |
||
2921 | 446 |
^ (self filenameFor:aFileOrDirectoryName) remove |
605 | 447 |
! |
448 |
||
449 |
removeDirectory:dirName |
|
450 |
"remove the directory 'dirName' from myself; return true if successful. |
|
451 |
If the directory is not empty, the containing files/directories are also |
|
452 |
removed." |
|
453 |
||
2921 | 454 |
^ (self filenameFor:dirName) recursiveRemove |
605 | 455 |
! |
456 |
||
457 |
removeFile:fileName |
|
458 |
"remove the file 'fileName' from myself; return true if successful" |
|
459 |
||
2921 | 460 |
^ (self filenameFor:fileName) remove |
605 | 461 |
! |
462 |
||
463 |
renameFile:oldFileName newName:newFileName |
|
464 |
"rename the file; return true if successful" |
|
465 |
||
2921 | 466 |
|f1 f2| |
605 | 467 |
|
2921 | 468 |
f1 := self filenameFor:oldFileName. |
469 |
f2 := self filenameFor:newFileName. |
|
470 |
^ f1 renameTo:f2 |
|
1 | 471 |
! ! |
472 |
||
172 | 473 |
!FileDirectory methodsFor:'converting'! |
474 |
||
475 |
asFilename |
|
476 |
"return myself as a filename" |
|
477 |
||
478 |
^ self pathName asFilename |
|
479 |
! |
|
480 |
||
481 |
asFilename:someFile |
|
482 |
"return a filename for a file named someFile in myself" |
|
483 |
||
276 | 484 |
^ self asFilename construct:someFile |
172 | 485 |
! ! |
486 |
||
605 | 487 |
!FileDirectory methodsFor:'enumerating'! |
488 |
||
489 |
allDirectoriesDo:aBlock |
|
490 |
"evaluate the argument, aBlock for every directory name |
|
491 |
in the directory and in all subdirectories" |
|
492 |
||
493 |
|aStream command line| |
|
494 |
||
495 |
lazy ifTrue:[self getFullPathName]. |
|
496 |
command := 'cd ' , pathName , '; find . -type d -print'. |
|
497 |
aStream := PipeStream readingFrom:command. |
|
498 |
aStream isNil ifTrue:[^ nil]. |
|
499 |
[aStream atEnd] whileFalse:[ |
|
500 |
line := aStream nextLine. |
|
501 |
line notNil ifTrue:[ |
|
502 |
(line = '.') ifFalse:[ |
|
503 |
"cut off initial ./" |
|
504 |
line := line copyFrom:3 |
|
505 |
]. |
|
506 |
aBlock value:line |
|
507 |
] |
|
508 |
]. |
|
509 |
aStream close |
|
510 |
! |
|
511 |
||
512 |
allFilesDo:aBlock |
|
513 |
"evaluate the argument, aBlock for every file name in the directory and in all |
|
514 |
subdirectories" |
|
515 |
||
516 |
|aStream command line| |
|
517 |
||
518 |
lazy ifTrue:[self getFullPathName]. |
|
519 |
command := 'cd ' , pathName , '; find . -print'. |
|
520 |
aStream := PipeStream readingFrom:command. |
|
521 |
aStream isNil ifTrue:[^ nil]. |
|
522 |
[aStream atEnd] whileFalse:[ |
|
523 |
line := aStream nextLine. |
|
524 |
line notNil ifTrue:[ |
|
525 |
(line = '.') ifFalse:[ |
|
526 |
"cut off initial ./" |
|
527 |
line := line copyFrom:3 |
|
528 |
]. |
|
529 |
aBlock value:line |
|
530 |
] |
|
531 |
]. |
|
532 |
aStream close |
|
533 |
! |
|
534 |
||
535 |
directoriesDo:aBlock |
|
536 |
"evaluate the argument, aBlock for every subdirectory name in the directory" |
|
537 |
||
538 |
self where:[:name | (self isDirectory:name) ifTrue:[ |
|
539 |
((name ~= '.') and:[name ~= '..']) |
|
540 |
] ifFalse:[ |
|
541 |
false |
|
542 |
] |
|
543 |
] do:aBlock |
|
544 |
! |
|
545 |
||
546 |
do:aBlock |
|
547 |
"evaluate the argument, aBlock for every name in the directory" |
|
548 |
||
549 |
self where:[:name | true] do:aBlock |
|
550 |
! |
|
551 |
||
552 |
filesDo:aBlock |
|
553 |
"evaluate the argument, aBlock for every plain file name in the directory" |
|
554 |
||
555 |
self where:[:name | (self isDirectory:name) not] do:aBlock |
|
556 |
! |
|
557 |
||
558 |
namesDo:aBlock |
|
559 |
"evaluate the argument, aBlock for every name in the directory. |
|
560 |
for ST-80 compatibility" |
|
561 |
||
562 |
self do:aBlock |
|
563 |
! |
|
564 |
||
565 |
where:testBlock do:aBlock |
|
566 |
"evaluate the argument, aBlock for every object in the directory |
|
567 |
for which testBlock evaluates to true." |
|
568 |
||
569 |
|aStream name| |
|
570 |
||
7655 | 571 |
ExternalStream openErrorSignal catch:[ |
572 |
aStream := DirectoryStream directoryNamed:pathName. |
|
573 |
]. |
|
605 | 574 |
aStream isNil ifTrue:[^ nil]. |
575 |
[aStream atEnd] whileFalse:[ |
|
7655 | 576 |
name := aStream nextLine. |
577 |
name notNil ifTrue:[ |
|
578 |
(testBlock value:name) ifTrue:[ |
|
579 |
aBlock value:name |
|
580 |
] |
|
581 |
] |
|
605 | 582 |
]. |
583 |
aStream close |
|
584 |
! ! |
|
585 |
||
586 |
!FileDirectory methodsFor:'more instance creation'! |
|
587 |
||
588 |
directoryNamed:aName |
|
2961 | 589 |
"create & return a new fileDirectory for a subdirectory in myself" |
590 |
||
605 | 591 |
^ self class directoryNamed:aName in:self pathName |
592 |
! ! |
|
593 |
||
594 |
!FileDirectory methodsFor:'printing & storing'! |
|
595 |
||
596 |
printOn:aStream |
|
4392 | 597 |
"append a user printed representation of the receiver to aStream. |
598 |
The format is suitable for a human - not meant to be read back." |
|
605 | 599 |
|
600 |
lazy ifTrue:[self getFullPathName]. |
|
601 |
aStream nextPutAll:'(a FileDirectory pathName:'; |
|
4392 | 602 |
nextPutAll:pathName; |
603 |
nextPutAll:')' |
|
605 | 604 |
! |
605 |
||
606 |
storeOn:aStream |
|
607 |
"append a printed representation of the receiver to aStream, |
|
608 |
which allows reconstructing it via readFrom:" |
|
609 |
||
610 |
lazy ifTrue:[self getFullPathName]. |
|
611 |
aStream nextPutAll:'(FileDirectory directoryNamed:'. |
|
612 |
aStream nextPutAll:pathName. |
|
613 |
aStream nextPut:$) |
|
614 |
! ! |
|
615 |
||
1 | 616 |
!FileDirectory methodsFor:'private'! |
617 |
||
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
618 |
filenameFor:fileName |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
619 |
"return a filename representing the argument, fileName |
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
620 |
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
|
621 |
|
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
622 |
^ pathName asFilename filenameFor:fileName |
3201
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
623 |
! |
4e72e420fdc3
Documentation: FileDirectory is obsolete and works only under Unix.
Stefan Vogel <sv@exept.de>
parents:
2961
diff
changeset
|
624 |
|
9334 | 625 |
fullNameFor:fileName |
626 |
"return a filename representing the argument, fileName |
|
627 |
either in myself (if the arg is a releative path) or absolute otherwise." |
|
628 |
||
629 |
^ (pathName asFilename filenameFor:fileName) asAbsoluteFilename |
|
630 |
! |
|
631 |
||
1 | 632 |
getFullPathName |
633 |
"make my pathname be a full pathname - i.e. starting at root" |
|
634 |
||
308 | 635 |
|shortPathName| |
1 | 636 |
|
2921 | 637 |
|
159 | 638 |
(pathName = OperatingSystem fileSeparator asString) ifTrue:[ |
172 | 639 |
lazy := false. |
640 |
^ self |
|
1 | 641 |
]. |
642 |
||
643 |
"since currentDirectory is used very often, cache its path here" |
|
644 |
||
645 |
(pathName = '.') ifTrue:[ |
|
172 | 646 |
PathOfCurrentDirectory notNil ifTrue:[ |
647 |
pathName := PathOfCurrentDirectory. |
|
648 |
lazy := false. |
|
649 |
^ self |
|
650 |
] |
|
1 | 651 |
]. |
652 |
||
653 |
shortPathName := pathName. |
|
654 |
||
308 | 655 |
pathName := OperatingSystem pathNameOf:pathName. |
656 |
lazy := false. |
|
1 | 657 |
|
308 | 658 |
" |
659 |
if it was the current dir, keep name for next query |
|
660 |
" |
|
1 | 661 |
(shortPathName = '.') ifTrue:[ |
308 | 662 |
PathOfCurrentDirectory := pathName |
1 | 663 |
] |
276 | 664 |
! |
665 |
||
666 |
pathNameOf:fileName |
|
2961 | 667 |
"return the pathname for an entry in myself" |
668 |
||
2921 | 669 |
^ (pathName asFilename construct:fileName) osName |
1 | 670 |
! ! |
671 |
||
605 | 672 |
!FileDirectory methodsFor:'queries'! |
1 | 673 |
|
605 | 674 |
accessModeOf:name |
675 |
"return the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 676 |
|
605 | 677 |
^ OperatingSystem accessModeOf:(self pathNameOf:name) |
1 | 678 |
! |
679 |
||
605 | 680 |
changeAccessModeOf:name to:modeBits |
681 |
"set the access-mode bits (rwxrwxrwx) of a file in myself" |
|
1 | 682 |
|
605 | 683 |
^ OperatingSystem changeAccessModeOf:(self pathNameOf:name) to:modeBits |
1 | 684 |
! |
685 |
||
686 |
exists |
|
687 |
"return true if this directory exists" |
|
688 |
||
2921 | 689 |
^ (Filename named:pathName) isDirectory |
159 | 690 |
|
691 |
" |
|
692 |
(FileDirectory directoryNamed:'fooBar') exists |
|
693 |
(FileDirectory directoryNamed:'/tmp') exists |
|
694 |
" |
|
1 | 695 |
! |
696 |
||
605 | 697 |
exists:name |
698 |
"return true, if the given name exists in myself" |
|
699 |
||
2921 | 700 |
^ (self filenameFor:name) exists |
605 | 701 |
! |
702 |
||
703 |
id |
|
704 |
"return the directories file-id (inode number)" |
|
705 |
||
2921 | 706 |
^ (Filename named:pathName) id |
605 | 707 |
! |
708 |
||
709 |
infoOf:name |
|
710 |
"return an array filled with file info for the file 'aFileName'; |
|
711 |
return nil if such a file does not exist" |
|
712 |
||
2921 | 713 |
^ (self filenameFor:name) info |
605 | 714 |
! |
715 |
||
716 |
isDirectory:name |
|
717 |
"return true, if the given name is that of a directory in myself" |
|
718 |
||
2921 | 719 |
^ (self filenameFor:name) isDirectory |
605 | 720 |
! |
721 |
||
722 |
isExecutable:name |
|
723 |
"return true, if the given file is executable" |
|
1 | 724 |
|
2921 | 725 |
^ (self filenameFor:name) isExecutable |
1 | 726 |
! |
727 |
||
728 |
isReadable:name |
|
729 |
"return true, if the given file is readable" |
|
730 |
||
2921 | 731 |
^ (self filenameFor:name) isReadable |
1 | 732 |
! |
733 |
||
734 |
isWritable:name |
|
735 |
"return true, if the given file is readable" |
|
736 |
||
2921 | 737 |
^ (self filenameFor:name) isWritable |
1 | 738 |
! |
739 |
||
605 | 740 |
species |
6550 | 741 |
"return the type of collection to be returned by collect, select etc." |
742 |
||
605 | 743 |
^ OrderedCollection |
1 | 744 |
! |
745 |
||
605 | 746 |
timeOfLastChange |
747 |
"return the timeStamp of myself" |
|
1 | 748 |
|
2921 | 749 |
^ (Filename named:pathName) modificationTime |
1 | 750 |
! |
751 |
||
605 | 752 |
timeOfLastChange:name |
753 |
"return the timeStamp of a file in myself" |
|
1 | 754 |
|
2921 | 755 |
^ (self filenameFor:name) modificationTime |
1 | 756 |
! |
757 |
||
605 | 758 |
typeOf:name |
759 |
"return the symbolic type of a file in myself" |
|
1 | 760 |
|
2921 | 761 |
^ (self filenameFor:name) type |
605 | 762 |
! ! |
1 | 763 |
|
5556 | 764 |
!FileDirectory methodsFor:'testing'! |
765 |
||
766 |
isEmpty |
|
767 |
"return true, if the directory is empty; |
|
768 |
redefined since '.' and '..' do not count as entries here." |
|
769 |
||
770 |
self do:[:fName | |
|
771 |
((fName ~= '.') and:[fName ~= '..']) ifTrue:[^ false]. |
|
772 |
]. |
|
773 |
^ true |
|
774 |
! ! |
|
775 |
||
12351 | 776 |
!FileDirectory::DirectoryEntry methodsFor:'accessing'! |
777 |
||
778 |
at: index |
|
779 |
"compatibility interface" |
|
780 |
"self halt: 'old-style access to DirectoryEntry'" |
|
781 |
index = 1 ifTrue: [ ^self name ]. |
|
782 |
index = 2 ifTrue: [ ^self creationTime ]. |
|
783 |
index = 3 ifTrue: [ ^self modificationTime ]. |
|
784 |
index = 4 ifTrue:[ ^self isDirectory ]. |
|
785 |
index = 5 ifTrue:[ ^self fileSize ]. |
|
786 |
self error: 'invalid index specified'. |
|
787 |
! |
|
788 |
||
789 |
creationTime |
|
790 |
^ creationTime |
|
791 |
! |
|
792 |
||
793 |
dirFlag |
|
794 |
^ dirFlag |
|
795 |
! |
|
796 |
||
797 |
fileSize |
|
798 |
^ fileSize |
|
799 |
! |
|
800 |
||
801 |
modificationTime |
|
802 |
^ modificationTime |
|
803 |
! |
|
804 |
||
805 |
name |
|
806 |
^ name |
|
807 |
! |
|
808 |
||
809 |
name:nameArg creationTime:creationTimeArg modificationTime:modificationTimeArg dirFlag:dirFlagArg fileSize:fileSizeArg |
|
810 |
name := nameArg. |
|
811 |
creationTime := creationTimeArg. |
|
812 |
modificationTime := modificationTimeArg. |
|
813 |
dirFlag := dirFlagArg. |
|
814 |
fileSize := fileSizeArg. |
|
815 |
! ! |
|
816 |
||
629 | 817 |
!FileDirectory class methodsFor:'documentation'! |
818 |
||
819 |
version |
|
13620
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
820 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.51 2011-08-29 06:23:13 cg Exp $' |
12351 | 821 |
! |
822 |
||
823 |
version_CVS |
|
13620
e38a8ec52cd9
changed: #directoryEntryFor:
Claus Gittinger <cg@exept.de>
parents:
13615
diff
changeset
|
824 |
^ '$Header: /cvs/stx/stx/libbasic/FileDirectory.st,v 1.51 2011-08-29 06:23:13 cg Exp $' |
629 | 825 |
! ! |
6972
0a2f38d186d5
filenameFor: same algorithm found in Filename
Claus Gittinger <cg@exept.de>
parents:
6550
diff
changeset
|
826 |
|
605 | 827 |
FileDirectory initialize! |