[ python-Bugs-1666318 ] shutil.copytree doesn't preserve directory permissions

SourceForge.net noreply at sourceforge.net
Sat Mar 24 19:07:06 CET 2007


Bugs item #1666318, was opened at 2007-02-22 17:26
Message generated for change (Comment added) made by thomaswaldmann
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1666318&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Jeff McNeil (j_mcneil)
Assigned to: Nobody/Anonymous (nobody)
Summary: shutil.copytree doesn't preserve directory permissions

Initial Comment:
I am using shutil.copytree to setup new user home directories within an automated system.  The copy2 function is called in order to copy individual files and preserve stat data. 

However, copytree simply calls os.mkdir and leaves directory creation at the mercy of my current umask (in my case, that's daemon context - 0).

I've got to then iterate through the newly copied tree and set permissions on each individual subdirectory. 

Adding a simple copystat(src, dst) on line 112 of shutil.py fixes the problem. 

The result should be uniform; either preserve permissions across the board, or leave it to the mercy of the caller.  I know there's an enhancement request already open to supply a 'func=' kw argument to copytree.



----------------------------------------------------------------------

Comment By: Thomas Waldmann (thomaswaldmann)
Date: 2007-03-24 19:07

Message:
Logged In: YES 
user_id=100649
Originator: NO

I can confirm this bug.

For MoinMoin wiki, we solved that by duplicating this function to our own
tree and we did exactly the proposed change:
 
Adding a simple copystat(src, dst) on line 112 of shutil.py (right below
os.mkdir) fixes this problem.

I also suggest to remove that XXX comment in that function. With the
proposed fix, it gets usable and thus is not only example code any more.

BUT: for avoiding problems on win32, a second fix needs to be done. For
MoinMoin, we call this wrapper around shutil.copystat from our copytree
function:

def copystat(src, dst):
    """Copy stat bits from src to dst

    This should be used when shutil.copystat would be used on directories
    on win32 because win32 does not support utime() for directories.

    According to the official docs written by Microsoft, it returns
ENOACCES if the
    supplied filename is a directory. Looks like a trainee implemented the
function.
    """
    if sys.platform == 'win32' and S_ISDIR(os.stat(dst)[ST_MODE]):
        if os.name == 'nt':
            st = os.stat(src)
            mode = S_IMODE(st[ST_MODE])
            if hasattr(os, 'chmod'):
                os.chmod(dst, mode) # KEEP THIS ONE!
        #else: pass # we are on Win9x,ME - no chmod here
    else:
        shutil.copystat(src, dst)

As you see, some special treatment is needed for win32/nt and directories
- and directories are exactly the use case used by the fixed copytree, so a
fixed copystat is needed or copytree will crash on win32/nt.


----------------------------------------------------------------------

Comment By: Jeff McNeil (j_mcneil)
Date: 2007-02-22 17:28

Message:
Logged In: YES 
user_id=1726175
Originator: YES

python -V
Python 2.4.3

on 

Linux marvin 2.6.18-1.2257.fc5smp #1 SMP Fri Dec 15 16:33:51 EST 2006 i686
i686 i386 GNU/Linux


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1666318&group_id=5470


More information about the Python-bugs-list mailing list