4260 "Copy the file's contents into another file. |
4261 "Copy the file's contents into another file. |
4261 Do it safe in an atomic operation which makes sure that no partially written file appears. |
4262 Do it safe in an atomic operation which makes sure that no partially written file appears. |
4262 The argument must be convertable to a filename. |
4263 The argument must be convertable to a filename. |
4263 Raises an exception, if an error occurs." |
4264 Raises an exception, if an error occurs." |
4264 |
4265 |
4265 |newName inStream accessRights tempStream| |
4266 |newName inStream tempStream tempFilename| |
4266 |
4267 |
4267 newName := newNameArg asFilename. |
4268 newName := newNameArg asFilename. |
4268 |
4269 |
4269 "Contents is not copied if newName represent same file as me." |
4270 "Contents is not copied if newName represent same file as me." |
4270 newName asAbsoluteFilename = self asAbsoluteFilename ifTrue: [ ^ self ]. |
4271 newName asAbsoluteFilename = self asAbsoluteFilename ifTrue: [ ^ self ]. |
4271 |
|
4272 inStream := self readStream. |
4272 inStream := self readStream. |
4273 newName exists ifTrue:[ |
4273 inStream isNil ifTrue:[ |
4274 accessRights := newName accessRights. |
4274 "open failed, but someone did a proceed for the OpenError. |
4275 ] ifFalse:[ |
4275 Ignore this file but continue in order to copy the rest when |
4276 accessRights := self accessRights. |
4276 doing a recursive copy" |
4277 ]. |
4277 ^ self. |
4278 |
4278 ]. |
|
4279 |
4279 [ |
4280 [ |
4280 "let the temp filename start with a ~ to make it invisible" |
4281 "let the temp filename start with a ~ to make it invisible" |
4281 tempStream := FileStream newTemporaryIn:newName directory osNameForDirectory nameTemplate:'~%1_%2'. |
4282 tempStream := FileStream newTemporaryIn:newName directory osNameForDirectory nameTemplate:'~%1_%2'. |
|
4283 tempFilename := tempStream fileName. |
|
4284 |
4282 "ignore the error - may occur when copying to a network drive" |
4285 "ignore the error - may occur when copying to a network drive" |
4283 OsError catch:[ |
4286 OsError catch:[ |
4284 "would be nice to keep the access rights of the original file" |
4287 "would be nice to keep the access rights of the original file" |
|
4288 |accessRights| |
|
4289 |
|
4290 newName exists ifTrue:[ |
|
4291 accessRights := newName accessRights. |
|
4292 ] ifFalse:[ |
|
4293 accessRights := self accessRights. |
|
4294 ]. |
4285 tempStream fileName accessRights:accessRights. |
4295 tempStream fileName accessRights:accessRights. |
4286 ]. |
4296 ]. |
4287 |
4297 |
4288 inStream binary; buffered:false. |
4298 inStream binary; buffered:false. |
4289 tempStream binary; buffered:false. |
4299 tempStream binary; buffered:false. |
4290 [ |
4300 [ |
4291 inStream copyToEndInto:tempStream. |
4301 inStream copyToEndInto:tempStream. |
4292 ] ifCurtailed:[ |
4302 ] ifCurtailed:[ |
4293 tempStream close. |
4303 tempStream close. |
4294 tempStream fileName remove. |
4304 tempFilename remove. |
4295 tempStream := nil. |
4305 tempStream := nil. |
4296 ]. |
4306 ]. |
4297 tempStream syncData. |
4307 tempStream syncData. |
4298 ] ensure:[ |
4308 ] ensure:[ |
4299 inStream close. |
4309 inStream close. |
4300 tempStream notNil ifTrue:[tempStream close]. |
4310 tempStream notNil ifTrue:[tempStream close]. |
4301 ]. |
4311 ]. |
4302 tempStream fileName renameTo:newName. |
4312 tempFilename renameTo:newName. |
4303 |
4313 |
4304 " |
4314 " |
4305 'Make.proto' asFilename safeCopyTo:'/tmp/Makefile.foo' |
4315 'Make.proto' asFilename safeCopyTo:'/tmp/Makefile.foo' |
4306 'Make.proto' asFilename safeCopyTo:'/' |
4316 'Make.proto' asFilename safeCopyTo:'/' |
4307 'smalltalk' asFilename safeCopyTo:'/xxxxxxxxxxxxxxxx/bla' |
4317 'smalltalk' asFilename safeCopyTo:'/xxxxxxxxxxxxxxxx/bla' |
4308 " |
4318 " |
4309 |
4319 |
4310 "Modified: / 10-09-2004 / 09:49:28 / janfrog" |
4320 "Modified: / 10-09-2004 / 09:49:28 / janfrog" |
4311 "Modified: / 29-09-2006 / 16:26:32 / cg" |
4321 "Modified: / 29-09-2006 / 16:26:32 / cg" |
4312 "Modified (comment): / 13-02-2018 / 11:59:48 / mawalch" |
4322 "Modified (comment): / 13-02-2018 / 11:59:48 / mawalch" |
4313 "Modified: / 21-01-2019 / 16:01:21 / Stefan Vogel" |
4323 "Modified: / 15-04-2020 / 20:19:39 / Stefan Vogel" |
4314 ! |
4324 ! |
4315 |
4325 |
4316 truncateTo:newSize |
4326 truncateTo:newSize |
4317 "change the file's size. |
4327 "change the file's size. |
4318 This may not be supported on all operating systems |
4328 This may not be supported on all operating systems |