55ae0416759ef54151639c41fd39e2271f39c4e5
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / xattr.py
1 #!/usr/bin/env python
2 # vim: expandtab
3 #
4 # Utility code for dealing with POSIX extended attributes
5 #
6 # Copyright (C) Matthieu Patou <mat@matws.net> 2009 - 2010
7 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2012
8 #
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.
13 #
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.
18 #
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/>.
21
22 from samba.dcerpc import xattr
23 import os
24 import samba.xattr_native
25 import shutil
26
27
28 def copytree_with_xattrs(source, target):
29     """Copy a tree but preserve extended attributes.
30
31     :param source: Source tree path
32     :param target: Target path
33     """
34     shutil.copytree(source, target)
35     copyxattrs(target, source)
36
37
38 def copyxattrs(dir, refdir):
39     """Copy extended attributes from a reference dir to a destination dir
40
41     Both dir are supposed to hold the same files
42     :param dir: Destination dir
43     :param refdir: Reference directory"""
44
45     for root, dirs, files in os.walk(dir, topdown=True):
46         for name in files:
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)
51             try:
52                 os.chown(tgt, statsinfo.st_uid, statsinfo.st_gid)
53                 # Get the xattr attributes if any
54                 try:
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,
59                                                  attribute)
60                 except Exception:
61                     pass
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",
67                                                   attribute)
68             except Exception:
69                 # FIXME: Catch a specific exception
70                 continue
71         for name in dirs:
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)
76             try:
77                 os.chown(os.path.join(root, name), statsinfo.st_uid,
78                           statsinfo.st_gid)
79                 try:
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,
84                                                  attribute)
85                 except Exception:
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",
91                                                   attribute)
92
93             except Exception:
94                 continue