summaryrefslogtreecommitdiff
path: root/source4/scripting/python/samba/xattr.py
blob: 55ae0416759ef54151639c41fd39e2271f39c4e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# vim: expandtab
#
# Utility code for dealing with POSIX extended attributes
#
# Copyright (C) Matthieu Patou <mat@matws.net> 2009 - 2010
# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2012
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from samba.dcerpc import xattr
import os
import samba.xattr_native
import shutil


def copytree_with_xattrs(source, target):
    """Copy a tree but preserve extended attributes.

    :param source: Source tree path
    :param target: Target path
    """
    shutil.copytree(source, target)
    copyxattrs(target, source)


def copyxattrs(dir, refdir):
    """Copy extended attributes from a reference dir to a destination dir

    Both dir are supposed to hold the same files
    :param dir: Destination dir
    :param refdir: Reference directory"""

    for root, dirs, files in os.walk(dir, topdown=True):
        for name in files:
            subdir = root[len(dir):]
            ref = os.path.join(refdir, subdir, name)
            statsinfo = os.stat(ref)
            tgt = os.path.join(root, name)
            try:
                os.chown(tgt, statsinfo.st_uid, statsinfo.st_gid)
                # Get the xattr attributes if any
                try:
                    attribute = samba.xattr_native.wrap_getxattr(ref,
                                                 xattr.XATTR_NTACL_NAME)
                    samba.xattr_native.wrap_setxattr(tgt,
                                                 xattr.XATTR_NTACL_NAME,
                                                 attribute)
                except Exception:
                    pass
                    # FIXME:Catch a specific exception
                attribute = samba.xattr_native.wrap_getxattr(ref,
                                                 "system.posix_acl_access")
                samba.xattr_native.wrap_setxattr(tgt,
                                                 "system.posix_acl_access",
                                                  attribute)
            except Exception:
                # FIXME: Catch a specific exception
                continue
        for name in dirs:
            subdir = root[len(dir):]
            ref = os.path.join(refdir, subdir, name)
            statsinfo = os.stat(ref)
            tgt = os.path.join(root, name)
            try:
                os.chown(os.path.join(root, name), statsinfo.st_uid,
                          statsinfo.st_gid)
                try:
                    attribute = samba.xattr_native.wrap_getxattr(ref,
                                                 xattr.XATTR_NTACL_NAME)
                    samba.xattr_native.wrap_setxattr(tgt,
                                                 xattr.XATTR_NTACL_NAME,
                                                 attribute)
                except Exception:
                    pass # FIXME: Catch a specific exception
                attribute = samba.xattr_native.wrap_getxattr(ref,
                                                 "system.posix_acl_access")
                samba.xattr_native.wrap_setxattr(tgt,
                                                 "system.posix_acl_access",
                                                  attribute)

            except Exception:
                continue