author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:21:27 +0100 | |
changeset 607 | a9a526c51233 |
parent 545 | 32adce698074 |
child 615 | e9d0e782206d |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
159 | 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 |
" |
|
12 |
||
13 |
Object subclass:#Filename |
|
159 | 14 |
instanceVariableNames:'nameString' |
276 | 15 |
classVariableNames:'NextTempFilenameIndex' |
159 | 16 |
poolDictionaries:'' |
17 |
category:'System-Support'! |
|
1 | 18 |
|
88 | 19 |
!Filename class methodsFor:'documentation'! |
20 |
||
21 |
copyright |
|
22 |
" |
|
23 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
159 | 24 |
All Rights Reserved |
1 | 25 |
|
88 | 26 |
This software is furnished under a license and may be used |
27 |
only in accordance with the terms of that license and with the |
|
28 |
inclusion of the above copyright notice. This software may not |
|
29 |
be provided or otherwise made available to, or used by, any |
|
30 |
other person. No title to or ownership of the software is |
|
31 |
hereby transferred. |
|
32 |
" |
|
33 |
! |
|
34 |
||
35 |
version |
|
545
32adce698074
dont try remove it it does not exist
Claus Gittinger <cg@exept.de>
parents:
542
diff
changeset
|
36 |
^ '$Header: /cvs/stx/stx/libbasic/Filename.st,v 1.41 1995-11-14 21:42:42 cg Exp $' |
88 | 37 |
! |
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Filenames; originally added for ST-80 compatibility, is |
|
42 |
taking over functionality from other classes (FileDirectory). |
|
92 | 43 |
|
44 |
Instances of Filename do not nescessarily represent valid or existing |
|
45 |
files - i.e. it is possible (and useful) to have instances for non-existing |
|
46 |
files around. In other words: the name-string is not checked automatically |
|
47 |
for being correct or existing. |
|
48 |
Thus, it is possible to do queries such as: |
|
49 |
||
159 | 50 |
'/fee/foo/foe' asFilename exists |
51 |
'/not_existing' asFilename isDirectory |
|
52 |
'/foo/bar' asFilename isReadable |
|
92 | 53 |
|
195 | 54 |
(all of the above examples will probably return false on your machine ;-). |
276 | 55 |
|
56 |
examples: |
|
57 |
||
58 |
'Makefile' asFilename readStream |
|
59 |
||
60 |
'newFile' asFilename writeStream |
|
61 |
||
62 |
Filename newTemporary writeStream |
|
88 | 63 |
" |
360 | 64 |
! |
65 |
||
66 |
examples |
|
67 |
" |
|
68 |
does a file/directory exist ?: |
|
69 |
||
70 |
|f| |
|
71 |
||
72 |
f := 'foobar' asFilename. |
|
73 |
^ f exists |
|
74 |
||
75 |
||
76 |
is it a directory ?: |
|
77 |
||
78 |
|f| |
|
79 |
||
80 |
f := '/tmp' asFilename. |
|
81 |
^ f isDirectory. |
|
82 |
||
83 |
||
84 |
get the working directory: |
|
85 |
||
86 |
^ Filename defaultDirectory |
|
87 |
||
88 |
||
89 |
get a files full pathname |
|
90 |
(caring for relative names or symbolic links): |
|
91 |
||
92 |
|f| |
|
93 |
||
94 |
f := '..' asFilename. |
|
95 |
^ f pathName |
|
96 |
||
97 |
||
98 |
get a directories directory: |
|
99 |
||
100 |
|f| |
|
101 |
||
102 |
f := Filename defaultDirectory. |
|
103 |
^ f directory |
|
104 |
||
105 |
||
106 |
get a files directory: |
|
107 |
||
108 |
|f| |
|
109 |
||
110 |
f := './smalltalk' asFilename. |
|
111 |
^ f directory |
|
112 |
||
113 |
||
114 |
getting access & modification times: |
|
115 |
||
116 |
||
117 |
|f| |
|
118 |
||
119 |
f := '/tmp' asFilename. |
|
120 |
^ f dates |
|
121 |
||
122 |
access time only: |
|
123 |
||
124 |
|f| |
|
125 |
||
126 |
f := '/tmp' asFilename. |
|
127 |
^ f dates at:#accessed |
|
128 |
||
129 |
||
130 |
getting all information on a file/directory: |
|
131 |
||
132 |
||
133 |
|f| |
|
134 |
||
135 |
f := '/tmp' asFilename. |
|
136 |
^ f info |
|
137 |
||
138 |
||
139 |
getting a temporary file (unique name): |
|
140 |
||
141 |
|f| |
|
142 |
||
143 |
f := Filename newTemporary. |
|
144 |
^ f |
|
145 |
||
146 |
||
147 |
creating, writing, reading and removing a temporary file: |
|
148 |
||
149 |
||
150 |
|f writeStream readStream| |
|
151 |
||
152 |
f := Filename newTemporary. |
|
153 |
writeStream := f writeStream. |
|
154 |
writeStream nextPutAll:'hello world'. |
|
155 |
writeStream cr. |
|
156 |
writeStream close. |
|
157 |
||
158 |
'contents (as seen by unix''s cat command:' printNL. |
|
159 |
OperatingSystem executeCommand:('cat ' , f pathName). |
|
160 |
||
161 |
readStream := f readStream. |
|
162 |
Transcript showCr:'contents as seen by smalltalk:'. |
|
163 |
Transcript showCr:(readStream upToEnd). |
|
164 |
readStream close. |
|
165 |
||
166 |
f delete. |
|
167 |
||
168 |
||
169 |
getting a directories contents: |
|
170 |
||
171 |
|f files| |
|
172 |
||
173 |
f := '.' asFilename. |
|
174 |
files := f directoryContents. |
|
175 |
Transcript showCr:'the files are:'. |
|
176 |
Transcript showCr:(files printString). |
|
177 |
||
178 |
||
179 |
editing a file: |
|
180 |
||
181 |
|f| |
|
182 |
||
183 |
f := '/tmp/fooBar' asFilename. |
|
184 |
(f writeStream) nextPutAll:'hello world'; close. |
|
185 |
||
186 |
f edit |
|
187 |
" |
|
88 | 188 |
! ! |
1 | 189 |
|
190 |
!Filename class methodsFor:'instance creation'! |
|
191 |
||
192 |
named:aString |
|
542 | 193 |
"return a filename for a directory named aString. |
194 |
This is the same as 'aString asFilename'." |
|
38 | 195 |
|
68 | 196 |
^ (self basicNew) setName:aString |
38 | 197 |
|
92 | 198 |
" |
199 |
Filename named:'/tmp/fooBar' |
|
200 |
" |
|
216 | 201 |
! |
202 |
||
276 | 203 |
currentDirectory |
204 |
"return a filename for the current directory" |
|
205 |
||
206 |
^ self named:(FileDirectory currentDirectory pathName) |
|
207 |
||
208 |
" |
|
209 |
Filename currentDirectory |
|
210 |
" |
|
211 |
! |
|
212 |
||
328 | 213 |
defaultDirectory |
214 |
"ST80 compatibility: same as currentDirectory" |
|
215 |
||
216 |
^ self currentDirectory |
|
217 |
||
218 |
" |
|
219 |
Filename defaultDirectory |
|
220 |
" |
|
221 |
! |
|
222 |
||
542 | 223 |
defaultDirectoryName |
224 |
"ST80 compatibility: return the defaultDirectories name (as a string)" |
|
225 |
||
226 |
^ self defaultDirectory name |
|
227 |
||
228 |
" |
|
229 |
Filename defaultDirectoryName |
|
230 |
" |
|
231 |
! |
|
232 |
||
276 | 233 |
newTemporary |
325 | 234 |
"return a new unique filename - use this for temporary files. |
235 |
The filenames returned are '/tmp/stxtmp_xx_nn' where xx is our |
|
236 |
unix process id, and nn is a unique number, incremented with every |
|
483
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
237 |
call to this method. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
238 |
If any of the environment variables ST_TMPDIR or TMPDIR is set, |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
239 |
its value defines the temp directory." |
276 | 240 |
|
483
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
241 |
|tempDir| |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
242 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
243 |
tempDir := OperatingSystem getEnvironment:'ST_TMPDIR'. |
422 | 244 |
tempDir isNil ifTrue:[ |
483
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
245 |
tempDir := OperatingSystem getEnvironment:'TMPDIR'. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
246 |
tempDir isNil ifTrue:[ |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
247 |
tempDir := '/tmp' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
248 |
]. |
422 | 249 |
]. |
483
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
250 |
^ self newTemporaryIn:tempDir |
276 | 251 |
|
252 |
" |
|
422 | 253 |
Filename newTemporary |
254 |
Filename newTemporary |
|
276 | 255 |
" |
422 | 256 |
|
257 |
"Modified: 7.9.1995 / 10:48:31 / claus" |
|
483
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
258 |
"Modified: 4.11.1995 / 20:08:51 / cg" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
259 |
! |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
260 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
261 |
newTemporaryIn:aDirectoryPrefix |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
262 |
"return a new unique filename - use this for temporary files. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
263 |
The filenames returned are in aDirectoryPrefix and named 'stxtmp_xx_nn', |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
264 |
where xx is our unix process id, and nn is a unique number, incremented |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
265 |
with every call to this method. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
266 |
Notice: only a filename object is created and returned - no physical |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
267 |
file is created by this method (i.e. you have to send #writeStream or |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
268 |
whatever to it in order to really create something). |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
269 |
See also: #newTemporary which looks for a good temp directory." |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
270 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
271 |
|pid nm| |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
272 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
273 |
NextTempFilenameIndex isNil ifTrue:[ |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
274 |
NextTempFilenameIndex := 1. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
275 |
]. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
276 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
277 |
" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
278 |
the following has to be made OS independent ... |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
279 |
" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
280 |
pid := OperatingSystem getProcessId printString. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
281 |
nm := 'stxtmp_' , pid , '_' , NextTempFilenameIndex printString. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
282 |
NextTempFilenameIndex := NextTempFilenameIndex + 1. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
283 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
284 |
(aDirectoryPrefix isNil or:[aDirectoryPrefix asString isEmpty]) ifFalse:[ |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
285 |
nm := aDirectoryPrefix asFilename construct:nm |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
286 |
] ifTrue:[ |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
287 |
nm := nm asFilename |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
288 |
]. |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
289 |
^ nm |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
290 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
291 |
"temp files in '/tmp': |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
292 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
293 |
Filename newTemporary |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
294 |
Filename newTemporaryIn:'/tmp' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
295 |
Filename newTemporaryIn:'/tmp' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
296 |
" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
297 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
298 |
"temp files somewhere (not recommended): |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
299 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
300 |
Filename newTemporaryIn:'/usr/tmp' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
301 |
Filename newTemporaryIn:'/' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
302 |
" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
303 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
304 |
"a local temp file: |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
305 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
306 |
Filename newTemporaryIn:'' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
307 |
Filename newTemporaryIn:nil |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
308 |
Filename newTemporaryIn:'.' |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
309 |
Filename newTemporaryIn:('source' asFilename) |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
310 |
" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
311 |
|
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
312 |
"Modified: 7.9.1995 / 10:48:31 / claus" |
f36d0167b819
newTemporyary and newTemporaryIn:
Claus Gittinger <cg@exept.de>
parents:
473
diff
changeset
|
313 |
"Modified: 4.11.1995 / 20:26:23 / cg" |
276 | 314 |
! |
315 |
||
216 | 316 |
fromUser |
317 |
"show a box to enter a filename. Return a filename instance or |
|
318 |
nil (if cancel was pressed)." |
|
319 |
||
320 |
|name| |
|
321 |
||
322 |
name := FileSelectionBox requestFilename. |
|
276 | 323 |
name notNil ifTrue:[^ self named:name]. |
216 | 324 |
^ nil |
276 | 325 |
|
326 |
" |
|
327 |
Filename fromUser |
|
328 |
" |
|
1 | 329 |
! ! |
330 |
||
2 | 331 |
!Filename class methodsFor:'queries'! |
332 |
||
333 |
separator |
|
92 | 334 |
"return the file/directory separator. |
335 |
Usually, this is $/ for unix-like systems |
|
336 |
and $\ for dos-like ones (there may be more in the future)." |
|
2 | 337 |
|
338 |
^ OperatingSystem fileSeparator |
|
38 | 339 |
|
92 | 340 |
" |
341 |
Filename separator |
|
342 |
" |
|
174 | 343 |
! |
344 |
||
422 | 345 |
suffixSeparator |
346 |
"return the filename suffix separator. |
|
347 |
Usually, this is $. for unix-like and msdos systems |
|
348 |
(there is currently no knowns system, where this differs)" |
|
349 |
||
350 |
^ $. |
|
351 |
||
352 |
" |
|
353 |
Filename suffixSeparator |
|
354 |
" |
|
355 |
||
356 |
"Modified: 7.9.1995 / 11:10:43 / claus" |
|
357 |
! |
|
358 |
||
216 | 359 |
parentDirectoryName |
360 |
"return the name used for the parent directory. |
|
361 |
This is '..' for unix and dos-like systems. |
|
362 |
(there may be more in the future." |
|
363 |
||
364 |
^ OperatingSystem parentDirectoryName |
|
365 |
||
366 |
" |
|
367 |
Filename parentDirectoryName |
|
368 |
" |
|
369 |
! |
|
370 |
||
174 | 371 |
isCaseSensitive |
372 |
"return true, if filenames are case sensitive. |
|
373 |
We ask the OS about this, to be independent here." |
|
374 |
||
216 | 375 |
^ OperatingSystem caseSensitiveFilenames |
326 | 376 |
! |
377 |
||
378 |
defaultClass |
|
379 |
"ST-80 compatibility: |
|
422 | 380 |
in ST-80, different subclasses of Filename are used for different |
381 |
OperatingSystems; defaultClass is supposed to return an appropriate class. |
|
326 | 382 |
Since in ST/X, there is (currently) only one Filename class, return it here." |
383 |
||
384 |
^ self |
|
328 | 385 |
! |
386 |
||
387 |
localNameStringFrom:aString |
|
388 |
"ST-80 compatibility. |
|
422 | 389 |
what does this do ? (used in FileNavigator-goody). |
390 |
GUESS: does it strip off the voulume-character on MSDOS systems ?" |
|
328 | 391 |
|
392 |
(aString startsWith:'/') ifTrue:[ |
|
393 |
^ aString copyFrom:2 |
|
394 |
]. |
|
395 |
^ aString |
|
422 | 396 |
|
397 |
"Modified: 7.9.1995 / 10:44:56 / claus" |
|
328 | 398 |
! |
399 |
||
400 |
volumes |
|
401 |
"ST-80 compatibility. |
|
422 | 402 |
what does this do ? (used in FileNavigator-goody). |
403 |
GUESS: does it return the available drives on MSDOS systems ?" |
|
328 | 404 |
|
356 | 405 |
^ #('/') |
422 | 406 |
|
407 |
"Modified: 7.9.1995 / 10:45:25 / claus" |
|
440 | 408 |
! |
409 |
||
410 |
filenameCompletionFor:aString directory:inDirectory directoriesOnly:directoriesOnly filesOnly:filesOnly ifMultiple:aBlock |
|
411 |
"perform filename completion on aString in some directory; |
|
412 |
return the longest matching filename prefix as a string. |
|
413 |
If directoriesOnly and filesOnly are true, only directories and files |
|
414 |
are considered respectively. If multiple files match, the exception |
|
415 |
block aBlock is evaluated with a filename representing the directory |
|
416 |
(where the match was done) as argument. |
|
417 |
(this may be different from the inDirectory argument, if aString is absolute |
|
418 |
or starts with ../)" |
|
419 |
||
420 |
|s f matchSet nMatch name words dir| |
|
421 |
||
422 |
s := aString. |
|
423 |
" |
|
424 |
find the last word ... |
|
425 |
" |
|
426 |
words := s asCollectionOfWords. |
|
427 |
words size == 0 ifTrue:[ |
|
473
f92507d781d2
filenameCompletion: bad return value when called with empty string
Claus Gittinger <cg@exept.de>
parents:
441
diff
changeset
|
428 |
aBlock value:'.' asFilename. |
f92507d781d2
filenameCompletion: bad return value when called with empty string
Claus Gittinger <cg@exept.de>
parents:
441
diff
changeset
|
429 |
^ '' |
440 | 430 |
]. |
431 |
||
432 |
f := words last asFilename. |
|
433 |
||
434 |
matchSet := f filenameCompletionIn:inDirectory. |
|
435 |
||
436 |
dir := f directory. |
|
437 |
||
438 |
directoriesOnly ifTrue:[ |
|
439 |
matchSet := matchSet select:[:aFilename | |
|
440 |
(dir construct:aFilename) isDirectory |
|
441 |
]. |
|
442 |
] ifFalse:[ |
|
443 |
filesOnly ifTrue:[ |
|
444 |
matchSet := matchSet select:[:aFilename | |
|
445 |
(dir construct:aFilename) isDirectory not |
|
446 |
]. |
|
447 |
] |
|
448 |
]. |
|
449 |
||
450 |
(nMatch := matchSet size) ~~ 1 ifTrue:[ |
|
451 |
" |
|
452 |
more than one possible completion - |
|
453 |
" |
|
454 |
aBlock value:f |
|
455 |
]. |
|
456 |
" |
|
457 |
even with more than one possible completion, |
|
458 |
f's name is now the common prefix |
|
459 |
" |
|
460 |
name := f asString. |
|
461 |
nMatch == 1 ifTrue:[ |
|
462 |
" |
|
463 |
exactly one possible completion - |
|
464 |
" |
|
465 |
f := dir construct:matchSet first. |
|
466 |
||
467 |
directoriesOnly ifFalse:[ |
|
468 |
f isDirectory ifTrue:[ |
|
469 |
(name endsWith:(Filename separator)) ifFalse:[ |
|
470 |
name := name , '/' |
|
471 |
]. |
|
472 |
]. |
|
473 |
] |
|
474 |
]. |
|
475 |
||
476 |
" |
|
477 |
construct new contents, by taking |
|
478 |
last words completion |
|
479 |
" |
|
480 |
s := ''. |
|
481 |
1 to:(words size - 1) do:[:idx | |
|
482 |
s := s , (words at:idx) , ' ' |
|
483 |
]. |
|
484 |
s := s , name. |
|
485 |
||
486 |
"/ special: if there was no change, and the string represented |
|
487 |
"/ is a directories name, add a directory separator |
|
488 |
s = aString ifTrue:[ |
|
489 |
(s endsWith:Filename separator) ifFalse:[ |
|
490 |
s asFilename isDirectory ifTrue:[ |
|
491 |
^ s , Filename separator asString |
|
492 |
] |
|
493 |
] |
|
494 |
]. |
|
495 |
||
496 |
^ s |
|
2 | 497 |
! ! |
498 |
||
542 | 499 |
!Filename methodsFor:'error handling'! |
500 |
||
501 |
reportError:string with:filename |
|
502 |
^ OperatingSystem errorSignal |
|
503 |
raiseRequestWith:filename |
|
504 |
errorString:string |
|
505 |
! |
|
506 |
||
507 |
fileNotFoundError:filename |
|
508 |
^ OperatingSystem fileNotFoundErrorSignal |
|
509 |
raiseRequestWith:filename |
|
510 |
errorString:('file not found: ' , filename asString) |
|
511 |
! |
|
512 |
||
513 |
fileCreationError:filename |
|
514 |
^ OperatingSystem accessDeniedErrorSignal |
|
515 |
raiseRequestWith:filename |
|
516 |
errorString:('cannot create/write file: ' , filename asString) |
|
517 |
! |
|
518 |
||
519 |
accessDeniedError:filename |
|
520 |
^ OperatingSystem accessDeniedErrorSignal |
|
521 |
raiseRequestWith:filename |
|
522 |
errorString:('access denied: ' , filename asString) |
|
523 |
! |
|
524 |
||
525 |
removeError:filename |
|
526 |
^ OperatingSystem accessDeniedErrorSignal |
|
527 |
raiseRequestWith:filename |
|
528 |
errorString:('cannot remove: ' , filename asString) |
|
529 |
! ! |
|
530 |
||
2 | 531 |
!Filename methodsFor:'instance creation'! |
532 |
||
249 | 533 |
constructString:subname |
38 | 534 |
"taking the receiver as a directory name, construct a new |
249 | 535 |
filenames string for an entry within this directory (i.e. for a file |
38 | 536 |
or a subdirectory in that directory)." |
537 |
||
276 | 538 |
|sepString| |
202 | 539 |
|
540 |
sepString := self class separator asString. |
|
541 |
nameString = sepString ifTrue:[ |
|
159 | 542 |
"I am the root" |
276 | 543 |
^ sepString , subname |
85 | 544 |
]. |
422 | 545 |
^ nameString , sepString , subname asString |
38 | 546 |
|
85 | 547 |
" |
249 | 548 |
'/tmp' asFilename constructString:'foo' |
549 |
'/' asFilename constructString:'foo' |
|
550 |
'/usr/tmp' asFilename constructString:'foo' |
|
328 | 551 |
'/foo/bar' asFilename constructString:'baz' |
249 | 552 |
" |
422 | 553 |
|
554 |
"Modified: 7.9.1995 / 10:15:22 / claus" |
|
249 | 555 |
! |
556 |
||
557 |
construct:subname |
|
558 |
"taking the receiver as a directory name, construct a new |
|
559 |
filename for an entry within this directory (i.e. for a file |
|
560 |
or a subdirectory in that directory)." |
|
561 |
||
276 | 562 |
^ (self constructString:subname) asFilename |
249 | 563 |
|
564 |
" |
|
565 |
'/tmp' asFilename construct:'foo' |
|
566 |
'/' asFilename construct:'foo' |
|
567 |
'/usr/tmp' asFilename construct:'foo' |
|
328 | 568 |
'/foo/bar' asFilename construct:'baz' |
85 | 569 |
" |
2 | 570 |
! ! |
571 |
||
195 | 572 |
!Filename methodsFor:'misc'! |
573 |
||
574 |
, aString |
|
575 |
"this allows filenames to understand how names are concatenated. |
|
308 | 576 |
Returns a string consisting of the receivers name, concatenated |
195 | 577 |
by aString. Notice this is NOT the same as construct:, which inserts |
308 | 578 |
a directory delimiter and returns a new fileName instance." |
195 | 579 |
|
308 | 580 |
^ (nameString , aString asString) |
195 | 581 |
|
582 |
" |
|
583 |
'Makefile' asFilename , '.bak' |
|
584 |
'Makefile' asFilename construct:'.bak' |
|
585 |
" |
|
586 |
! ! |
|
587 |
||
174 | 588 |
!Filename methodsFor:'comparing'! |
589 |
||
590 |
hash |
|
591 |
"return an integer useful as a hash-key" |
|
592 |
||
593 |
^ nameString hash |
|
594 |
! |
|
595 |
||
596 |
= aFilename |
|
276 | 597 |
"return true, if the argument represents the same filename" |
598 |
||
599 |
|str| |
|
174 | 600 |
|
601 |
self species == aFilename species ifTrue:[ |
|
276 | 602 |
str := aFilename asString. |
174 | 603 |
self class isCaseSensitive ifTrue:[ |
276 | 604 |
^ nameString = str |
174 | 605 |
]. |
276 | 606 |
^ nameString sameAs:str |
174 | 607 |
]. |
608 |
^ false |
|
609 |
! ! |
|
610 |
||
1 | 611 |
!Filename methodsFor:'converting'! |
612 |
||
613 |
asString |
|
38 | 614 |
"return the receiver converted to a string" |
615 |
||
68 | 616 |
^ nameString |
1 | 617 |
! |
618 |
||
619 |
asFilename |
|
68 | 620 |
"return the receiver converted to a filename." |
38 | 621 |
|
68 | 622 |
"Thats pretty easy here :-)" |
1 | 623 |
^ self |
195 | 624 |
! |
625 |
||
328 | 626 |
asAbsoluteFilename |
627 |
"return the receiver converted to a filename with |
|
628 |
an absolute pathname." |
|
629 |
||
630 |
^ self pathName asFilename |
|
631 |
! |
|
632 |
||
195 | 633 |
makeLegalFilename |
634 |
"convert the receveivers name to be a legal filename. |
|
635 |
The implementation may change in the future to be more |
|
636 |
OS specific." |
|
637 |
||
276 | 638 |
" |
639 |
actually, in Unix spaces are allowed - but it makes life |
|
640 |
so hard; therefore, replace them by underscores ... |
|
641 |
" |
|
195 | 642 |
nameString replaceAll:(Character space) by:$_. |
643 |
" |
|
644 |
need more - especially on SYS5.3 type systems, |
|
645 |
we may want to contract the fileName to 14 characters. |
|
646 |
" |
|
647 |
^ self |
|
648 |
||
649 |
" |
|
650 |
'hello world' asFilename makeLegalFilename |
|
651 |
" |
|
1 | 652 |
! ! |
653 |
||
202 | 654 |
!Filename methodsFor:'private accessing'! |
1 | 655 |
|
68 | 656 |
setName:aString |
38 | 657 |
"set the filename" |
658 |
||
68 | 659 |
nameString := aString |
1 | 660 |
! ! |
661 |
||
2 | 662 |
!Filename methodsFor:'queries'! |
663 |
||
159 | 664 |
separator |
665 |
"return the directory-separator character (or string)" |
|
666 |
||
667 |
^ self class separator |
|
668 |
! |
|
669 |
||
422 | 670 |
directoryPathName |
671 |
"return the full directory pathname part of the file/directory as a string. |
|
672 |
- thats the full pathname of the directory where the file/dir represented by |
|
673 |
the receiver is contained in. |
|
674 |
See also: directoryName" |
|
675 |
||
676 |
^ OperatingSystem directoryNameOf:(self pathName) |
|
677 |
||
678 |
" |
|
679 |
'/foo/bar/' asFilename directoryPathName |
|
680 |
'/foo/bar' asFilename directoryPathName |
|
681 |
'.' asFilename directoryPathName |
|
682 |
'..' asFilename directoryPathName |
|
683 |
'../..' asFilename directoryPathName |
|
684 |
" |
|
685 |
||
686 |
"Modified: 7.9.1995 / 10:42:13 / claus" |
|
687 |
! |
|
688 |
||
328 | 689 |
directoryName |
422 | 690 |
"return the directory name part of the file/directory as a string. |
92 | 691 |
- thats the name of the directory where the file/dir represented by |
422 | 692 |
the receiver is contained in. |
693 |
See also: #directoryPathName" |
|
328 | 694 |
|
695 |
^ OperatingSystem directoryNameOf:nameString "/ (self pathName) |
|
92 | 696 |
|
328 | 697 |
" |
698 |
'/foo/bar/' asFilename directoryName |
|
699 |
'/foo/bar' asFilename directoryName |
|
422 | 700 |
'bitmaps' asFilename directoryName |
701 |
'bitmaps' asFilename directoryPathName |
|
328 | 702 |
'.' asFilename directoryName |
703 |
'..' asFilename directoryName |
|
704 |
'../..' asFilename directoryName |
|
422 | 705 |
'../..' asFilename directoryPathName |
328 | 706 |
" |
422 | 707 |
|
708 |
"Modified: 7.9.1995 / 10:42:03 / claus" |
|
328 | 709 |
! |
710 |
||
711 |
directory |
|
422 | 712 |
"return the directory name part of the file/directory as a filename. |
328 | 713 |
- thats a filename for the directory where the file/dir represented by |
714 |
the receiver is contained in." |
|
715 |
||
716 |
^ self directoryName asFilename |
|
2 | 717 |
|
92 | 718 |
" |
174 | 719 |
'/foo/bar' asFilename directory |
720 |
'.' asFilename directory |
|
721 |
'..' asFilename directory |
|
722 |
'../..' asFilename directory |
|
92 | 723 |
" |
2 | 724 |
! |
725 |
||
308 | 726 |
directoryContents |
727 |
"return the contents of the directory as a collection of strings" |
|
728 |
||
729 |
^ (FileDirectory directoryNamed:self asString) contents |
|
730 |
||
731 |
" |
|
732 |
'.' asFilename directoryContents |
|
733 |
" |
|
734 |
! |
|
735 |
||
174 | 736 |
tail |
422 | 737 |
"the files name without directory prefix as a string. |
738 |
An alias for baseName, for ST-80 compatiblity." |
|
174 | 739 |
|
740 |
^ self baseName |
|
741 |
! |
|
742 |
||
2 | 743 |
baseName |
422 | 744 |
"return my baseName as a string. |
92 | 745 |
- thats the file/directory name without leading parent-dirs." |
2 | 746 |
|
328 | 747 |
^ OperatingSystem baseNameOf:nameString "/ (self pathName) |
68 | 748 |
|
85 | 749 |
" |
92 | 750 |
'/foo/bar' asFilename baseName |
751 |
'.' asFilename baseName |
|
752 |
'..' asFilename baseName |
|
328 | 753 |
'../..' asFilename baseName |
754 |
'../../libbasic' asFilename baseName |
|
755 |
'../../libpr' asFilename baseName |
|
422 | 756 |
'../../libbasic/Object.st' asFilename baseName |
92 | 757 |
" |
758 |
! |
|
759 |
||
422 | 760 |
name |
761 |
"return the name of the file represented by the receiver as a string. |
|
762 |
This may or may not be a relative name. |
|
763 |
See also: pathName" |
|
764 |
||
765 |
self isAbsolute ifTrue:[^ self pathName]. |
|
766 |
^ nameString |
|
767 |
||
768 |
" |
|
769 |
'/foo/bar' asFilename name |
|
770 |
'/foo/bar' asFilename pathName |
|
771 |
'.' asFilename name |
|
772 |
'.' asFilename pathName |
|
773 |
'../..' asFilename name |
|
774 |
'../..' asFilename pathName |
|
775 |
'bitmaps' asFilename name |
|
776 |
'bitmaps' asFilename pathName |
|
777 |
'/tmp/../usr' asFilename name |
|
778 |
'/tmp/../usr' asFilename pathName |
|
779 |
" |
|
780 |
||
781 |
"Modified: 7.9.1995 / 10:41:14 / claus" |
|
782 |
! |
|
783 |
||
92 | 784 |
pathName |
422 | 785 |
"return the full pathname of the file represented by the receiver, |
786 |
as a string. See also: name" |
|
92 | 787 |
|
216 | 788 |
|parentName sep| |
789 |
||
790 |
sep := self class separator. |
|
791 |
(nameString startsWith:sep) ifTrue:[ |
|
792 |
parentName := self class parentDirectoryName. |
|
357 | 793 |
(nameString findString:parentName) == 0 ifTrue:[ |
794 |
^ nameString |
|
216 | 795 |
] |
92 | 796 |
]. |
797 |
^ (FileDirectory directoryNamed:nameString) pathName |
|
798 |
||
799 |
" |
|
800 |
'/foo/bar' asFilename pathName |
|
801 |
'.' asFilename pathName |
|
802 |
'../..' asFilename pathName |
|
422 | 803 |
'../..' asFilename name |
357 | 804 |
'/tmp/../usr' asFilename pathName |
85 | 805 |
" |
422 | 806 |
|
807 |
"Modified: 7.9.1995 / 10:42:39 / claus" |
|
68 | 808 |
! |
809 |
||
810 |
isAbsolute |
|
811 |
"return true, if the receiver represents an absolute pathname |
|
92 | 812 |
(in contrast to one relative to the current directory)." |
68 | 813 |
|
85 | 814 |
^ (nameString startsWith:self class separator) |
815 |
||
816 |
" |
|
92 | 817 |
'/foo/bar' asFilename isAbsolute |
818 |
'..' asFilename isAbsolute |
|
328 | 819 |
'..' asAbsoluteFilename isAbsolute |
92 | 820 |
'source/SBrowser.st' asFilename isAbsolute |
85 | 821 |
" |
822 |
! |
|
68 | 823 |
|
159 | 824 |
isRelative |
825 |
"return true, if this name is interpreted relative to some |
|
826 |
directory (opposite of absolute)" |
|
827 |
||
828 |
^ self isAbsolute not |
|
829 |
! |
|
830 |
||
85 | 831 |
isDirectory |
832 |
"return true, if the receiver represents an existing, |
|
833 |
readable directories pathname." |
|
834 |
||
835 |
^ OperatingSystem isDirectory:nameString |
|
836 |
||
837 |
" |
|
838 |
'/foo/bar' asFilename isDirectory |
|
839 |
'/tmp' asFilename isDirectory |
|
92 | 840 |
'Makefile' asFilename isDirectory |
85 | 841 |
" |
68 | 842 |
! |
843 |
||
844 |
exists |
|
92 | 845 |
"return true, if such a file exists." |
68 | 846 |
|
847 |
^ OperatingSystem isValidPath:nameString |
|
848 |
||
85 | 849 |
" |
92 | 850 |
'/foo/bar' asFilename exists |
851 |
'/tmp' asFilename exists |
|
852 |
'Makefile' asFilename exists |
|
85 | 853 |
" |
77 | 854 |
! |
855 |
||
856 |
isReadable |
|
92 | 857 |
"return true, if such a file exists and is readable." |
77 | 858 |
|
859 |
^ OperatingSystem isReadable:nameString |
|
860 |
||
85 | 861 |
" |
92 | 862 |
'/foo/bar' asFilename isReadable |
863 |
'/tmp' asFilename isReadable |
|
864 |
'Makefile' asFilename isReadable |
|
85 | 865 |
" |
77 | 866 |
! |
867 |
||
159 | 868 |
canBeWritten |
869 |
"same as isWritable - for ST-80 compatibility" |
|
870 |
||
871 |
^ self isWritable |
|
328 | 872 |
|
873 |
" |
|
874 |
'/foo/bar' asFilename canBeWritten |
|
875 |
'/tmp' asFilename canBeWritten |
|
876 |
'Makefile' asFilename canBeWritten |
|
877 |
" |
|
159 | 878 |
! |
879 |
||
77 | 880 |
isWritable |
92 | 881 |
"return true, if such a file exists and is writable." |
77 | 882 |
|
883 |
^ OperatingSystem isWritable:nameString |
|
884 |
||
85 | 885 |
" |
92 | 886 |
'/foo/bar' asFilename isWritable |
887 |
'/tmp' asFilename isWritable |
|
888 |
'Makefile' asFilename isWritable |
|
889 |
" |
|
890 |
! |
|
891 |
||
892 |
isExecutable |
|
893 |
"return true, if such a file exists and is executable (by Unix's definition). |
|
894 |
For directories, true is returned if the directory can be entered. |
|
895 |
See isExecutableProgram for a related check." |
|
896 |
||
897 |
^ OperatingSystem isExecutable:nameString |
|
898 |
||
899 |
" |
|
900 |
'/foo/bar' asFilename isExecutable |
|
901 |
'/tmp' asFilename isExecutable |
|
902 |
'Makefile' asFilename isExecutable |
|
903 |
'/bin/ls' asFilename isExecutable |
|
904 |
" |
|
905 |
! |
|
906 |
||
907 |
isExecutableProgram |
|
908 |
"return true, if such a file exists and is an executable program. |
|
909 |
(i.e. for directories, false is returned.)" |
|
910 |
||
911 |
^ (OperatingSystem isExecutable:nameString) |
|
912 |
and:[(OperatingSystem isDirectory:nameString) not] |
|
913 |
||
914 |
" |
|
915 |
'/tmp' asFilename isExecutable |
|
916 |
'/bin/ls' asFilename isExecutable |
|
917 |
'/tmp' asFilename isExecutableProgram |
|
918 |
'/bin/ls' asFilename isExecutableProgram |
|
919 |
" |
|
920 |
! |
|
921 |
||
356 | 922 |
isSymbolicLink |
923 |
"return true, if the file represented by the receiver is a symbolic |
|
924 |
link. Notice that not all OS's support symbolic links; those that do |
|
925 |
not will always return false." |
|
926 |
||
927 |
^ OperatingSystem isSymbolicLink:nameString |
|
928 |
||
929 |
" |
|
930 |
'Make.proto' asFilename isSymbolicLink |
|
931 |
'Makefile' asFilename isSymbolicLink |
|
932 |
" |
|
933 |
! |
|
934 |
||
422 | 935 |
prefixAndSuffix |
936 |
"return an array consisting of my prefix and suffix. |
|
937 |
The suffix is the namepart after the final period character, |
|
938 |
the prefix everything before, except for the period. |
|
939 |
(on some systems, the suffix-character may be different from a period). |
|
940 |
For example, foo.bar.baz has a prefix of 'foo.bar' and a suffix of '.baz'. |
|
941 |
||
942 |
Notice: there is currently no known system which uses other than |
|
943 |
the period character as suffixCharacter." |
|
944 |
||
945 |
|nm idx| |
|
946 |
||
947 |
nm := self baseName. |
|
948 |
idx := nm lastIndexOf:(self class suffixSeparator). |
|
949 |
idx == 0 ifTrue:[ |
|
950 |
^ Array with:nm with:'' |
|
951 |
]. |
|
952 |
^ Array |
|
953 |
with:(nm copyTo:idx-1) |
|
954 |
with:(nm copyFrom:idx+1) |
|
955 |
||
956 |
" |
|
957 |
'abc.st' asFilename prefixAndSuffix |
|
958 |
'abc' asFilename prefixAndSuffix |
|
959 |
'a.b.c' asFilename prefixAndSuffix |
|
960 |
||
961 |
|parts| |
|
962 |
parts := 'Object.st' asFilename prefixAndSuffix. |
|
963 |
((parts at:1) , '.o') asFilename |
|
964 |
" |
|
965 |
||
966 |
"Modified: 7.9.1995 / 11:15:42 / claus" |
|
967 |
! |
|
968 |
||
969 |
suffix |
|
970 |
"return my suffix. |
|
971 |
The suffix is the namepart after the final period character, |
|
972 |
or the empty string, if the name does not contain a period." |
|
973 |
||
974 |
^ self prefixAndSuffix at:2 |
|
975 |
||
976 |
" |
|
977 |
'abc.st' asFilename suffix |
|
978 |
'abc' asFilename suffix |
|
979 |
'a.b.c' asFilename suffix |
|
980 |
" |
|
981 |
||
982 |
"Modified: 7.9.1995 / 11:09:03 / claus" |
|
983 |
! |
|
984 |
||
328 | 985 |
filesMatching:aPattern |
986 |
^ self directoryContents select:[:name | aPattern match:name] |
|
987 |
||
988 |
" |
|
989 |
Filename currentDirectory filesMatching:'M*' |
|
990 |
" |
|
991 |
! |
|
992 |
||
92 | 993 |
filenameCompletion |
440 | 994 |
"try to complete the recevier filename. |
995 |
This method has both a return value and a side effect on the receiver: |
|
996 |
it returns a collection of matching filename objects, |
|
997 |
and leaves changes the receivers filename-string to the longest common |
|
998 |
match. |
|
999 |
If none matches, the returned collection is empty and the recevier is unchanged. |
|
1000 |
If there is only one match, the size of the returned collection is exactly 1, |
|
1001 |
containing the fully expanded filename and the receivers name is changed to it." |
|
92 | 1002 |
|
440 | 1003 |
^ self filenameCompletionIn:nil |
216 | 1004 |
|
440 | 1005 |
" |
1006 |
'mak' asFilename filenameCompletion |
|
1007 |
'Make' asFilename filenameCompletion |
|
1008 |
'Makef' asFilename filenameCompletion;yourself |
|
1009 |
'/u' asFilename filenameCompletion |
|
1010 |
'../../libpr' asFilename inspect filenameCompletion |
|
1011 |
" |
|
1012 |
! |
|
92 | 1013 |
|
440 | 1014 |
filenameCompletionIn:aDirectory |
1015 |
"try to complete the recevier filename. |
|
1016 |
This method has both a return value and a side effect on the receiver: |
|
1017 |
it returns a collection of matching filename objects, |
|
1018 |
and leaves changes the receivers filename-string to the longest common |
|
1019 |
match. |
|
1020 |
If none matches, the returned collection is empty and the recevier is unchanged. |
|
1021 |
If there is only one match, the size of the returned collection is exactly 1, |
|
1022 |
containing the fully expanded filename and the receivers name is changed to it." |
|
1023 |
||
1024 |
|dir baseName matching matchLen try allMatching |
|
1025 |
sepString parentString prefix nMatch| |
|
1026 |
||
1027 |
sepString := self class separator asString. |
|
1028 |
(nameString endsWith:sepString) ifTrue:[ |
|
1029 |
^ #() |
|
328 | 1030 |
]. |
1031 |
||
440 | 1032 |
parentString := self class parentDirectoryName. |
1033 |
baseName := self baseName. |
|
1034 |
baseName ~= nameString ifTrue:[ |
|
1035 |
prefix := self directoryName. |
|
1036 |
]. |
|
1037 |
||
441 | 1038 |
self isAbsolute ifTrue:[ |
440 | 1039 |
dir := self directory |
1040 |
] ifFalse:[ |
|
441 | 1041 |
aDirectory isNil ifTrue:[ |
1042 |
dir := self directory |
|
1043 |
] ifFalse:[ |
|
1044 |
dir := (aDirectory construct:nameString) directory |
|
1045 |
] |
|
328 | 1046 |
]. |
216 | 1047 |
|
92 | 1048 |
matching := OrderedCollection new. |
440 | 1049 |
dir directoryContents do:[:fileName | |
1050 |
((fileName ~= '.') and:[fileName ~= parentString]) ifTrue:[ |
|
1051 |
(fileName startsWith:baseName) ifTrue:[ |
|
216 | 1052 |
matching add:fileName |
1053 |
] |
|
159 | 1054 |
] |
92 | 1055 |
]. |
440 | 1056 |
(nMatch := matching size) > 1 ifTrue:[ |
1057 |
" |
|
1058 |
find the longest common prefix |
|
1059 |
" |
|
1060 |
matchLen := baseName size. |
|
1061 |
matchLen > matching first size ifTrue:[ |
|
1062 |
try := baseName. |
|
1063 |
allMatching := false |
|
1064 |
] ifFalse:[ |
|
1065 |
try := matching first copyTo:matchLen. |
|
1066 |
allMatching := true. |
|
1067 |
]. |
|
1068 |
||
1069 |
[allMatching] whileTrue:[ |
|
1070 |
matching do:[:aName | |
|
1071 |
(aName startsWith:try) ifFalse:[ |
|
1072 |
allMatching := false |
|
1073 |
] |
|
1074 |
]. |
|
1075 |
allMatching ifTrue:[ |
|
1076 |
matchLen < matching first size ifTrue:[ |
|
1077 |
matchLen := matchLen + 1. |
|
1078 |
try := matching first copyTo:matchLen. |
|
1079 |
] ifFalse:[ |
|
1080 |
allMatching := false |
|
1081 |
] |
|
1082 |
] ifFalse:[ |
|
1083 |
try := matching first copyTo:matchLen - 1. |
|
1084 |
] |
|
1085 |
]. |
|
1086 |
" |
|
1087 |
and set my name to the last full match |
|
1088 |
" |
|
1089 |
nameString := try |
|
1090 |
]. |
|
1091 |
||
92 | 1092 |
" |
440 | 1093 |
if I had a directory-prefix, change names in collection ... |
1094 |
" |
|
1095 |
prefix notNil ifTrue:[ |
|
1096 |
prefix = '/' ifTrue:[ |
|
1097 |
"/ avoid introducing double slashes |
|
1098 |
prefix := '' |
|
159 | 1099 |
]. |
440 | 1100 |
matching := matching collect:[:n | prefix , '/' , n]. |
1101 |
nMatch == 1 ifTrue:[ |
|
1102 |
nameString := matching first |
|
1103 |
] ifFalse:[ |
|
1104 |
nMatch > 1 ifTrue:[ |
|
1105 |
nameString := prefix , '/' , nameString |
|
159 | 1106 |
] |
1107 |
] |
|
92 | 1108 |
] ifFalse:[ |
440 | 1109 |
nMatch == 1 ifTrue:[ |
1110 |
nameString := matching first |
|
159 | 1111 |
] |
92 | 1112 |
]. |
440 | 1113 |
|
92 | 1114 |
" |
1115 |
return the match-set, so caller can decide what to do |
|
1116 |
(i.e. show the matches, output a warning etc ...) |
|
1117 |
" |
|
1118 |
^ matching |
|
1119 |
||
440 | 1120 |
" trivial cases: |
1121 |
||
1122 |
'../' asFilename filenameCompletion |
|
1123 |
'/' asFilename filenameCompletion |
|
1124 |
'/usr/' asFilename filenameCompletion |
|
1125 |
||
1126 |
'mak' asFilename filenameCompletion |
|
1127 |
'Make' asFilename filenameCompletion |
|
1128 |
'Makef' asFilename filenameCompletion |
|
1129 |
'/u' asFilename filenameCompletion |
|
1130 |
'../../libpr' asFilename filenameCompletion |
|
85 | 1131 |
" |
2 | 1132 |
! ! |
1133 |
||
174 | 1134 |
!Filename methodsFor:'file queries'! |
1135 |
||
1136 |
info |
|
1137 |
"return the files info; that is a collection of file attributes, |
|
1138 |
(actually a dictionary) where the keys are #type, #uid, #gid, #size etc. |
|
356 | 1139 |
The actual amount and detail returned may depend on the OS used. |
1140 |
On unix, if you ask for the info of a symbolic link, the target |
|
359 | 1141 |
files info is returned. (see also: #linkInfo) |
356 | 1142 |
|
1143 |
On unix, the contents is: |
|
1144 |
id -> the inode number (integer) |
|
1145 |
uid -> the numeric user id of the files owner |
|
1146 |
gid -> the numeric group id of the files owner |
|
1147 |
statusChanged -> the absoluteTime when the files status changed last |
|
1148 |
(i.e. protection change, owner change etc.) |
|
1149 |
accessed -> the absoluteTime when the file was last accessed |
|
1150 |
modified -> the absoluteTime when the file was last modified |
|
1151 |
size -> the size (in bytes) of the file |
|
1152 |
type -> the files type (#regular, #directory, #characterSpecial) |
|
1153 |
mode -> the files access protection bits (rwxrwxrwx mask). |
|
1154 |
||
1155 |
The minimum returned info (i.e. on all OS's) will consist of at least: |
|
1156 |
modified |
|
1157 |
size |
|
1158 |
type |
|
1159 |
||
1160 |
Some OS's (VMS) may return more info. |
|
1161 |
||
1162 |
Dont expect things like uid/gid/mode to be there; write your application |
|
1163 |
to either handle the cases where info-entries are not present, |
|
359 | 1164 |
or (better) use one of isXXXX query methods. (Be prepared for DOS ...) |
356 | 1165 |
" |
174 | 1166 |
|
1167 |
^ OperatingSystem infoOf:nameString |
|
328 | 1168 |
|
1169 |
" |
|
1170 |
Filename currentDirectory info |
|
356 | 1171 |
'/dev/null' asFilename info |
328 | 1172 |
'Make.proto' asFilename info |
356 | 1173 |
'source/Point.st' asFilename info |
359 | 1174 |
'source/Point.st' asFilename linkInfo |
356 | 1175 |
'../../libbasic/Point.st' asFilename info |
1176 |
" |
|
1177 |
! |
|
1178 |
||
1179 |
linkInfo |
|
1180 |
"return the files info if its a symbolic link; nil otherwise. |
|
1181 |
On OS's which do not support symbolic links, nil is always returned. |
|
359 | 1182 |
The information is the same as returned by #info, except that if the |
1183 |
receiver represents a symbolic link, the links information |
|
1184 |
is returned |
|
1185 |
(while in this case, #info returns the info of the target file, |
|
1186 |
which is accessed via the symbolic link). |
|
356 | 1187 |
|
1188 |
In addition to the normal entries, Unix returns an additional entry: |
|
1189 |
path -> the target files pathname |
|
1190 |
||
1191 |
See the comment in #info for more details." |
|
1192 |
||
1193 |
^ OperatingSystem linkInfoOf:nameString |
|
1194 |
||
1195 |
" |
|
1196 |
Filename currentDirectory linkInfo |
|
1197 |
'/dev/null' asFilename linkInfo |
|
1198 |
'Make.proto' asFilename linkInfo |
|
1199 |
'Make.proto' asFilename linkInfo at:#path |
|
1200 |
'source/Point.st' asFilename linkInfo |
|
1201 |
'../../libbasic/Point.st' asFilename linkInfo |
|
328 | 1202 |
" |
1203 |
! |
|
1204 |
||
1205 |
dates |
|
1206 |
"return the files modification and access times as an object (currently a dictionary) |
|
356 | 1207 |
that responds to the at: message with arguments |
1208 |
#modified, #accessed or #statusChanged." |
|
328 | 1209 |
|
1210 |
|info dates| |
|
1211 |
||
1212 |
info := OperatingSystem infoOf:nameString. |
|
1213 |
info isNil ifTrue:[ |
|
1214 |
info := OperatingSystem linkInfoOf:nameString. |
|
1215 |
info isNil ifTrue:[ |
|
1216 |
^ nil |
|
1217 |
] |
|
1218 |
]. |
|
1219 |
dates := IdentityDictionary new. |
|
1220 |
dates at:#modified put:(info at:#modified). |
|
1221 |
dates at:#accessed put:(info at:#accessed). |
|
1222 |
dates at:#statusChanged put:(info at:#statusChanged). |
|
1223 |
^ dates |
|
1224 |
||
1225 |
" |
|
1226 |
Filename currentDirectory dates |
|
1227 |
'../regression' asFilename dates |
|
1228 |
" |
|
174 | 1229 |
! |
1230 |
||
1231 |
fileSize |
|
1232 |
"return the size of the file in bytes" |
|
1233 |
||
1234 |
|info| |
|
1235 |
||
1236 |
info := self info. |
|
1237 |
info isNil ifTrue:[^ nil]. |
|
1238 |
^ info at:#size |
|
1239 |
! |
|
1240 |
||
1241 |
fileType |
|
1242 |
"this returns a string describing the type of contents of |
|
1243 |
the file. This is done using the unix 'file' command, |
|
1244 |
(which usually is configurable by /etc/magic). |
|
1245 |
On non-unix systems, this may return an empty string, not knowning |
|
1246 |
about the contents." |
|
1247 |
||
1248 |
|stream typeString| |
|
1249 |
||
1250 |
typeString := ''. |
|
1251 |
stream := PipeStream readingFrom:('file ' , self pathName). |
|
1252 |
stream notNil ifTrue:[ |
|
1253 |
typeString := stream contents asString. |
|
1254 |
stream close. |
|
1255 |
typeString := typeString copyFrom:(typeString indexOf:$:) + 1. |
|
1256 |
typeString := typeString withoutSeparators |
|
1257 |
] ifFalse:[ |
|
1258 |
" |
|
1259 |
could add some fallback code here, for systems, where no |
|
1260 |
file command is avaliable ... |
|
1261 |
... or at least analyze directory info. |
|
1262 |
" |
|
1263 |
]. |
|
1264 |
^ typeString |
|
1265 |
||
1266 |
" |
|
1267 |
'Makefile' asFilename fileType |
|
325 | 1268 |
'.' asFilename fileType |
174 | 1269 |
'/dev/null' asFilename fileType |
1270 |
'smalltalk.rc' asFilename fileType |
|
325 | 1271 |
'bitmaps/SBrowser.xbm' asFilename fileType |
174 | 1272 |
" |
1273 |
! ! |
|
1274 |
||
1275 |
!Filename methodsFor:'file operations'! |
|
1 | 1276 |
|
159 | 1277 |
delete |
1278 |
"remove the file - same as remove, for ST-80 compatibility" |
|
1279 |
||
542 | 1280 |
self remove |
159 | 1281 |
! |
1282 |
||
92 | 1283 |
remove |
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1284 |
"remove the file/directory - the argument must be convertable to a String. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1285 |
Return true if sucessfull, false if not. |
542 | 1286 |
Use recursiveRemove in order to (recursively) remove non empty directories." |
1287 |
||
1288 |
|ok| |
|
92 | 1289 |
|
545
32adce698074
dont try remove it it does not exist
Claus Gittinger <cg@exept.de>
parents:
542
diff
changeset
|
1290 |
self exists ifFalse:[ ^ self]. |
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1291 |
self isDirectory ifTrue:[ |
542 | 1292 |
ok := OperatingSystem removeDirectory:nameString |
1293 |
] ifFalse:[ |
|
1294 |
ok := OperatingSystem removeFile:nameString |
|
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1295 |
]. |
542 | 1296 |
ok ifFalse:[ |
1297 |
self removeError:self |
|
1298 |
]. |
|
92 | 1299 |
|
1300 |
" |
|
1301 |
(FileStream newFileNamed:'foo') close. |
|
1302 |
'foo' asFilename remove |
|
1303 |
" |
|
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1304 |
|
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1305 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1306 |
'foo' asFilename makeDirectory. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1307 |
'foo/bar' asFilename writeStream close. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1308 |
('foo' asFilename remove) ifFalse:[ |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1309 |
Transcript showCr:'could not remove foo' |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1310 |
] |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1311 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1312 |
|
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1313 |
"Modified: 14.11.1995 / 11:18:26 / cg" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1314 |
! |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1315 |
|
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1316 |
recursiveRemove |
542 | 1317 |
"remove the directory and all of its subfiles/subdirectories." |
1318 |
||
1319 |
|ok| |
|
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1320 |
|
542 | 1321 |
ok := OperatingSystem recursiveRemoveDirectory:nameString. |
1322 |
ok ifFalse:[ |
|
1323 |
self removeError:self |
|
1324 |
]. |
|
538
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1325 |
|
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1326 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1327 |
'foo' asFilename makeDirectory. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1328 |
'foo/bar' asFilename writeStream close. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1329 |
('foo' asFilename remove) ifFalse:[ |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1330 |
Transcript showCr:'could not remove foo' |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1331 |
] |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1332 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1333 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1334 |
'foo' asFilename makeDirectory. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1335 |
'foo/bar' asFilename writeStream close. |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1336 |
('foo' asFilename recursiveRemove) ifFalse:[ |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1337 |
Transcript showCr:'could not remove foo' |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1338 |
] |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1339 |
" |
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1340 |
|
b9337c6c55bb
added recursiveRemove; remove now also removes directories
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1341 |
"Created: 14.11.1995 / 11:19:29 / cg" |
92 | 1342 |
! |
1343 |
||
2 | 1344 |
renameTo:newName |
542 | 1345 |
"rename the file - the argument must be convertable to a String." |
1346 |
||
1347 |
|ok| |
|
2 | 1348 |
|
542 | 1349 |
ok := OperatingSystem renameFile:nameString to:(newName asString). |
1350 |
ok ifFalse:[ |
|
1351 |
self exists ifFalse:[ |
|
1352 |
^ self fileNotFoundError:self |
|
1353 |
]. |
|
1354 |
^ self accessDeniedError:newName asFilename. |
|
1355 |
]. |
|
2 | 1356 |
|
92 | 1357 |
" |
1358 |
'/tmp/foo' asFilename renameTo:'/tmp/bar' |
|
1359 |
" |
|
2 | 1360 |
! |
1361 |
||
68 | 1362 |
copyTo:newName |
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1363 |
"copy the file - the argument must be convertable to a filename. |
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1364 |
Return true if successfull, false if not." |
159 | 1365 |
|
542 | 1366 |
|inStream outStream buffer bufferSize count newFile| |
1 | 1367 |
|
159 | 1368 |
bufferSize := 8 * 1024. |
1369 |
buffer := ByteArray new:bufferSize. |
|
1370 |
inStream := self readStream. |
|
542 | 1371 |
inStream isNil ifTrue:[ |
1372 |
^ self fileNotFoundError:self |
|
1373 |
]. |
|
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1374 |
|
542 | 1375 |
outStream := (newFile := newName asFilename) writeStream. |
1376 |
outStream isNil ifTrue:[ |
|
1377 |
^ self fileCreationError:newFile |
|
159 | 1378 |
]. |
1379 |
||
1380 |
[inStream atEnd] whileFalse:[ |
|
1381 |
count := inStream nextBytes:bufferSize into:buffer. |
|
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1382 |
(outStream nextPutBytes:count from:buffer) ~= count ifTrue:[ |
542 | 1383 |
^ self fileCreationError:newFile |
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1384 |
] |
159 | 1385 |
]. |
1386 |
outStream close. |
|
195 | 1387 |
inStream close. |
159 | 1388 |
|
1389 |
" |
|
1390 |
'Makefile' asFilename copyTo:'Makefile.foo' |
|
1391 |
'smalltalk' asFilename copyTo:'/dev/null' |
|
1392 |
" |
|
1 | 1393 |
! |
1394 |
||
174 | 1395 |
moveTo:newName |
1396 |
"copy the file represented by the receiver, then delete it. |
|
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1397 |
This is different to renaming in case of cross device moves. |
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1398 |
Return true if successfull, false if not." |
174 | 1399 |
|
542 | 1400 |
self copyTo:newName. |
1401 |
self remove |
|
174 | 1402 |
! |
1403 |
||
1404 |
makeDirectory |
|
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1405 |
"create a directory with the receivers name. |
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1406 |
Return true if successfull, false if not." |
174 | 1407 |
|
542 | 1408 |
(OperatingSystem createDirectory:nameString) ifFalse:[ |
1409 |
^ self fileCreationError:self |
|
1410 |
] |
|
174 | 1411 |
! |
1412 |
||
1413 |
addAccessRights:aCollection |
|
1414 |
"add the access rights as specified in aCollection for the file represented |
|
1415 |
by the receiver. The argument must be a collection of symbols, |
|
1416 |
such as #readUser, #writeGroup etc." |
|
1417 |
||
1418 |
|access| |
|
1419 |
||
1420 |
access := OperatingSystem accessModeOf:nameString. |
|
1421 |
aCollection do:[:accessSymbol | |
|
1422 |
access := access bitOr:(OperatingSystem accessMaskFor:accessSymbol). |
|
1423 |
]. |
|
542 | 1424 |
(OperatingSystem changeAccessModeOf:nameString to:access) ifFalse:[ |
1425 |
^ self accessDeniedError:self |
|
1426 |
] |
|
174 | 1427 |
|
1428 |
" |
|
1429 |
'foo' asFilename writeStream close. |
|
1430 |
'foo' asFilename addAccessRights:#(readUser readGroup readOthers). |
|
1431 |
'foo' asFilename addAccessRights:#(writeUser writeGroup writeOthers). |
|
1432 |
'foo' asFilename addAccessRights:#(executeUser executeGroup executeOthers). |
|
1433 |
" |
|
1434 |
! |
|
1435 |
||
1436 |
removeAccessRights:aCollection |
|
1437 |
"remove the access rights as specified in aCollection for the file represented |
|
1438 |
by the receiver. The argument must be a collection of symbols, |
|
1439 |
such as #readUser, #writeGroup etc." |
|
1440 |
||
1441 |
|access| |
|
1442 |
||
1443 |
access := OperatingSystem accessModeOf:nameString. |
|
1444 |
aCollection do:[:accessSymbol | |
|
1445 |
access := access bitAnd:(OperatingSystem accessMaskFor:accessSymbol) bitInvert. |
|
1446 |
]. |
|
542 | 1447 |
(OperatingSystem changeAccessModeOf:nameString to:access) ifFalse:[ |
1448 |
^ self accessDeniedError:self |
|
1449 |
]. |
|
174 | 1450 |
|
1451 |
" |
|
1452 |
'foo' asFilename writeStream close. |
|
1453 |
'foo' asFilename removeAccessRights:#(readUser readGroup readOthers). |
|
1454 |
'foo' asFilename removeAccessRights:#(writeUser writeGroup writeOthers). |
|
1455 |
'foo' asFilename removeAccessRights:#(executeUser executeGroup executeOthers). |
|
1456 |
" |
|
1457 |
! |
|
1458 |
||
1459 |
makeReadableForAll |
|
1460 |
"make the file readable for all - you must have permission to do so." |
|
1461 |
||
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1462 |
^ self addAccessRights:#(readUser readGroup readOthers) |
174 | 1463 |
! |
1464 |
||
1465 |
makeReadable |
|
1466 |
"make the file readable for the owner - you must have permission to do so." |
|
1467 |
||
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1468 |
^ self addAccessRights:#(readUser) |
174 | 1469 |
! |
1470 |
||
1471 |
makeWritable |
|
1472 |
"make the file writableable for all - you must have permission to do so." |
|
1473 |
||
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1474 |
^ self addAccessRights:#(writeUser) |
174 | 1475 |
! |
1476 |
||
1477 |
makeWritableForAll |
|
1478 |
"make the file writable for all - you must have permission to do so." |
|
1479 |
||
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1480 |
^ self addAccessRights:#(writeUser writeGroup writeOthers) |
174 | 1481 |
! |
1482 |
||
1483 |
makeUnwritable |
|
1484 |
"make the file unwritable for all - you must have permission to do so." |
|
1485 |
||
539
11aca4b770ac
more fileOPs return true/false
Claus Gittinger <cg@exept.de>
parents:
538
diff
changeset
|
1486 |
^ self removeAccessRights:#(writeUser writeGroup writeOthers) |
174 | 1487 |
! ! |
1488 |
||
1489 |
!Filename methodsFor:'file utilities'! |
|
1490 |
||
1 | 1491 |
fileIn |
2 | 1492 |
"load smalltalk code from the file" |
1493 |
||
1494 |
^ self readStream fileIn |
|
1 | 1495 |
! |
1496 |
||
174 | 1497 |
edit |
1498 |
"start an editView on the file represented by the receiver" |
|
1499 |
||
1500 |
EditTextView openOn:self asString |
|
276 | 1501 |
|
1502 |
" |
|
1503 |
'smalltalk.rc' asFilename edit |
|
1504 |
" |
|
174 | 1505 |
! |
1506 |
||
1507 |
contentsOfEntireFile |
|
1508 |
"return the contents of the file as a string" |
|
1509 |
||
1510 |
|s contents| |
|
1511 |
||
1512 |
s := self readStream. |
|
1513 |
[ |
|
1514 |
contents := s contents |
|
1515 |
] valueNowOrOnUnwindDo:[s close]. |
|
1516 |
^ contents |
|
1517 |
! ! |
|
1518 |
||
1519 |
!Filename methodsFor:'file access'! |
|
1520 |
||
77 | 1521 |
newReadWriteStream |
325 | 1522 |
"return a stream for read/write the file represented by the receiver. |
1523 |
If the file does not already exist, it is created." |
|
77 | 1524 |
|
1525 |
^ FileStream newFileNamed:nameString |
|
1526 |
! |
|
1527 |
||
1528 |
readWriteStream |
|
325 | 1529 |
"return a stream for read/write the file represented by the receiver. |
1530 |
If the file does not already exist, nil is returned." |
|
77 | 1531 |
|
1532 |
^ FileStream oldFileNamed:nameString |
|
1533 |
! |
|
1534 |
||
1 | 1535 |
readStream |
325 | 1536 |
"return a stream for reading from the file represented by the receiver. |
1537 |
If the file does not already exist, nil is returned." |
|
2 | 1538 |
|
68 | 1539 |
^ FileStream readonlyFileNamed:nameString |
325 | 1540 |
|
1541 |
" |
|
1542 |
'/tmp/foo' asFilename readStream |
|
1543 |
" |
|
1 | 1544 |
! |
1545 |
||
1546 |
writeStream |
|
325 | 1547 |
"return a stream for writing to the file represented by the receiver. |
1548 |
If the file does not already exist, it is created." |
|
2 | 1549 |
|
92 | 1550 |
^ FileStream newFileForWritingNamed:nameString |
325 | 1551 |
|
1552 |
" |
|
1553 |
'/tmp/foo' asFilename writeStream |
|
1554 |
" |
|
92 | 1555 |
! |
1556 |
||
1557 |
appendingWriteStream |
|
325 | 1558 |
"return a stream for appending to the file represented by the receiver. |
1559 |
If the file does not already exist, it is created." |
|
92 | 1560 |
|
1561 |
^ FileStream appendingOldFileNamed:nameString |
|
1 | 1562 |
! ! |
92 | 1563 |
|
1564 |
!Filename methodsFor:'printing & storing'! |
|
1565 |
||
1566 |
storeOn:aStream |
|
174 | 1567 |
"append a printed representation of the receiver to aStream, |
1568 |
which allows reconstructing it via readFrom:" |
|
1569 |
||
92 | 1570 |
aStream nextPut:$(. |
1571 |
nameString storeOn:aStream. |
|
1572 |
aStream nextPutAll:' asFilename)' |
|
1573 |
! |
|
1574 |
||
1575 |
printOn:aStream |
|
174 | 1576 |
"append a printed representation of the receiver to aStream." |
1577 |
||
92 | 1578 |
aStream nextPutAll:'FileName('''. |
1579 |
nameString printOn:aStream. |
|
1580 |
aStream nextPutAll:''')' |
|
1581 |
! ! |