classification
Title: shutil.move clobbers read-only files.
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: shutil.copyfile fails when dst exists read-only
View: 810879
Superseder:
Assigned To: tarek Nosy List: BreamoreBoy, bbrazil, brian.curtin, eric.araujo, jemfinch, jlgijsbers, loewis, pitrou, tarek, tim.golden
Priority: normal Keywords: easy

Created on 2004-12-01 05:40 by jemfinch, last changed 2010-09-13 14:02 by pitrou.

Messages (9)
msg23428 - (view) Author: Jeremy Fincher (jemfinch) Date: 2004-12-01 05:40
The summary states it fine.  shutil.move happily overwrites read-
only files.

It looks like it indiscriminately catches OSError, and never bothers 
to check whether it's a permission error.

It'd be nice if permission errors raised an exception that was a 
subclass of OSError, then it'd be cake to fix this (at least for 
*nices; I'm not sure about the Windows implications).

According to tracker #810879, clobbering read-only files isn't the 
desired behavior for shutil.copyfile, so I doubt it's desired for 
shutil.move.
msg23429 - (view) Author: Johannes Gijsbers (jlgijsbers) * Date: 2004-12-06 22:21
Logged In: YES 
user_id=469548

Your analysis is not correct. On Unix, you need write
permission to the *directory* to rename. So the os.rename()
call simply succeeds on a read-only file if you have write
access to its parent directory. I think we could shield from
this by always using the fallback implementation
(copy2+unlink(src)), but I'm not sure what the implications
of that would be (qua performance and cross-platform issues).
msg23430 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2006-04-12 08:06
Logged In: YES 
user_id=21627

I don't think I will do anything about this anytime soon, so
unassigning myself.
msg107638 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-12 09:30
Is this bug still relevant?
msg113271 - (view) Author: Brian Brazil (bbrazil) * Date: 2010-08-08 14:53
Here's a quick test:

Python 3.2a1+ (py3k:83811, Aug  8 2010, 09:00:22) 
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, shutil
>>> open('a', 'w').write('a')
1
>>> open('b', 'w').write('b')
1
>>> os.chmod('b', 000)
>>> shutil.move('a', 'b')
>>> open('b').read()
'a'
>>> 

This is the correct behaviour on Unix, so I'd say this can be closed off.
msg114985 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-08-26 15:19
Brian, Tim, any comments on this wrt Windows or do you think this can be closed?  Could there be an impact on any other OS?  I'll close if there's no response, unless someone else feels fit to close this anyway.
msg116165 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-09-12 10:31
No reply to msg114985.
msg116237 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-09-12 22:00
I haven't had time to investigate but it shouldn't be closed just yet. Someone will get to it.
msg116305 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-13 14:01
I agree that this follows Unix behaviour: a read-only file is a file whose contents cannot be modified, but you can replace it with another totally different file. You can also delete it, by the way (*).

Also, even if this weren't the desired behaviour, changing it would break compatibility for existing scripts.

(*)

>>> open('b', 'w').write('b')
1
>>> os.chmod('b', 000)
>>> os.remove('b')
>>> open('b', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'b'
History
Date User Action Args
2010-09-13 14:02:20pitrousetstage: test needed ->
type: behavior -> enhancement
versions: - Python 3.1, Python 2.7
2010-09-13 14:01:38pitrousetnosy: + pitrou
messages: + msg116305
2010-09-12 22:00:28brian.curtinsetstatus: closed -> open
resolution: invalid ->
messages: + msg116237
2010-09-12 10:31:34BreamoreBoysetstatus: pending -> closed
resolution: invalid
messages: + msg116165
2010-08-26 15:19:41BreamoreBoysetstatus: open -> pending
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
nosy: + tim.golden, brian.curtin, BreamoreBoy

messages: + msg114985
2010-08-08 14:53:10bbrazilsetnosy: + bbrazil
messages: + msg113271
2010-06-12 09:30:58eric.araujosetassignee: tarek

messages: + msg107638
nosy: + tarek, eric.araujo
2009-04-22 05:08:41ajaksu2setkeywords: + easy
2009-03-30 22:36:36ajaksu2setdependencies: + shutil.copyfile fails when dst exists read-only
type: behavior
stage: test needed
versions: + Python 2.6
2004-12-01 05:40:47jemfinchcreate