s4:kdc: adjust formatting of samba_kdc_update_pac() documentation
[Samba.git] / python / samba / xattr.py
blob2f4813cba4790744fc432e21bcd9f7cdeea23a1e
1 # Utility code for dealing with POSIX extended attributes
3 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2012
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from samba.dcerpc import xattr
19 import os
20 import samba.xattr_native
21 import shutil
24 def copyattrs(frompath, topath):
25 """Copy ACL related attributes from a path to another path."""
26 for attr_name in (xattr.XATTR_NTACL_NAME, "system.posix_acl_access"):
27 # Get the xattr attributes if any
28 try:
29 attribute = samba.xattr_native.wrap_getxattr(frompath,
30 attr_name)
31 samba.xattr_native.wrap_setxattr(topath,
32 attr_name,
33 attribute)
34 except Exception:
35 pass
36 # FIXME:Catch a specific exception
39 def copytree_with_xattrs(src, dst):
40 """Recursively copy a directory tree using shutil.copy2(), preserving xattrs.
42 The destination directory must not already exist.
43 If exception(s) occur, an Error is raised with a list of reasons.
44 """
45 names = os.listdir(src)
47 os.makedirs(dst)
48 for name in names:
49 srcname = os.path.join(src, name)
50 dstname = os.path.join(dst, name)
51 if os.path.islink(srcname):
52 linkto = os.readlink(srcname)
53 os.symlink(linkto, dstname)
54 elif os.path.isdir(srcname):
55 copytree_with_xattrs(srcname, dstname)
56 else:
57 # Will raise a SpecialFileError for unsupported file types
58 shutil.copy2(srcname, dstname)
59 shutil.copystat(src, dst)
60 copyattrs(src, dst)