4 # Utility code for dealing with POSIX extended attributes
6 # Copyright (C) Matthieu Patou <mat@matws.net> 2009 - 2010
7 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2012
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from samba.dcerpc import xattr
24 import samba.xattr_native
28 def copytree_with_xattrs(source, target):
29 """Copy a tree but preserve extended attributes.
31 :param source: Source tree path
32 :param target: Target path
34 shutil.copytree(source, target)
35 copyxattrs(target, source)
38 def copyxattrs(dir, refdir):
39 """Copy extended attributes from a reference dir to a destination dir
41 Both dir are supposed to hold the same files
42 :param dir: Destination dir
43 :param refdir: Reference directory"""
45 for root, dirs, files in os.walk(dir, topdown=True):
47 subdir = root[len(dir):]
48 ref = os.path.join(refdir, subdir, name)
49 statsinfo = os.stat(ref)
50 tgt = os.path.join(root, name)
52 os.chown(tgt, statsinfo.st_uid, statsinfo.st_gid)
53 # Get the xattr attributes if any
55 attribute = samba.xattr_native.wrap_getxattr(ref,
56 xattr.XATTR_NTACL_NAME)
57 samba.xattr_native.wrap_setxattr(tgt,
58 xattr.XATTR_NTACL_NAME,
62 # FIXME:Catch a specific exception
63 attribute = samba.xattr_native.wrap_getxattr(ref,
64 "system.posix_acl_access")
65 samba.xattr_native.wrap_setxattr(tgt,
66 "system.posix_acl_access",
69 # FIXME: Catch a specific exception
72 subdir = root[len(dir):]
73 ref = os.path.join(refdir, subdir, name)
74 statsinfo = os.stat(ref)
75 tgt = os.path.join(root, name)
77 os.chown(os.path.join(root, name), statsinfo.st_uid,
80 attribute = samba.xattr_native.wrap_getxattr(ref,
81 xattr.XATTR_NTACL_NAME)
82 samba.xattr_native.wrap_setxattr(tgt,
83 xattr.XATTR_NTACL_NAME,
86 pass # FIXME: Catch a specific exception
87 attribute = samba.xattr_native.wrap_getxattr(ref,
88 "system.posix_acl_access")
89 samba.xattr_native.wrap_setxattr(tgt,
90 "system.posix_acl_access",