aboutsummaryrefslogtreecommitdiff
blob: 9a8704d702561ba217640e7cd99b0b45f2e329fb (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright 2010-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

"""Portability shim for xattr support

Exported API is the xattr object with get/get_all/set/remove/list operations.
We do not include the functions that Python 3.3+ provides in the os module as
the signature there is different compared to xattr.

See the standard xattr module for more documentation:
	https://pypi.python.org/pypi/pyxattr
"""

from __future__ import print_function

import contextlib
import os
import subprocess

from portage.exception import OperationNotSupported


class _XattrGetAll(object):
	"""Implement get_all() using list()/get() if there is no easy bulk method"""

	@classmethod
	def get_all(cls, item, nofollow=False, namespace=None):
		return [(name, cls.get(item, name, nofollow=nofollow, namespace=namespace))
		        for name in cls.list(item, nofollow=nofollow, namespace=namespace)]


class _XattrSystemCommands(_XattrGetAll):
	"""Implement things with getfattr/setfattr"""

	@staticmethod
	def _parse_output(output):
		for line in output.readlines():
			if line.startswith(b'#'):
				continue
			line = line.rstrip()
			if not line:
				continue
			# The lines will have the format:
			#	user.hex=0x12345
			#	user.base64=0sAQAAAgAgAAAAAAAAAAAAAAAAAAA=
			#	user.string="value0"
			# But since we don't do interpretation on the value (we just
			# save & restore it), don't bother with decoding here.
			yield line.split(b'=', 1)

	@staticmethod
	def _call(*args, **kwargs):
		proc = subprocess.Popen(*args, **kwargs)
		if proc.stdin:
			proc.stdin.close()
		proc.wait()
		return proc

	@classmethod
	def get(cls, item, name, nofollow=False, namespace=None):
		if namespace:
			name = '%s.%s' % (namespace, name)
		cmd = ['getfattr', '--absolute-names', '-n', name, item]
		if nofollow:
			cmd += ['-h']
		proc = cls._call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

		value = None
		for _, value in cls._parse_output(proc.stdout):
			break

		proc.stdout.close()
		return value

	@classmethod
	def set(cls, item, name, value, _flags=0, namespace=None):
		if namespace:
			name = '%s.%s' % (namespace, name)
		cmd = ['setfattr', '-n', name, '-v', value, item]
		cls._call(cmd)

	@classmethod
	def remove(cls, item, name, nofollow=False, namespace=None):
		if namespace:
			name = '%s.%s' % (namespace, name)
		cmd = ['setfattr', '-x', name, item]
		if nofollow:
			cmd += ['-h']
		cls._call(cmd)

	@classmethod
	def list(cls, item, nofollow=False, namespace=None, _names_only=True):
		cmd = ['getfattr', '-d', '--absolute-names', item]
		if nofollow:
			cmd += ['-h']
		cmd += ['-m', ('^%s[.]' % namespace) if namespace else '-']
		proc = cls._call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

		ret = []
		if namespace:
			namespace = '%s.' % namespace
		for name, value in cls._parse_output(proc.stdout):
			if namespace:
				if name.startswith(namespace):
					name = name[len(namespace):]
				else:
					continue
			if _names_only:
				ret.append(name)
			else:
				ret.append((name, value))

		proc.stdout.close()
		return ret

	@classmethod
	def get_all(cls, item, nofollow=False, namespace=None):
		return cls.list(item, nofollow=nofollow, namespace=namespace,
		                _names_only=False)


class _XattrStub(_XattrGetAll):
	"""Fake object since system doesn't support xattrs"""

	# pylint: disable=unused-argument

	@staticmethod
	def _raise():
		e = OSError('stub')
		e.errno = OperationNotSupported.errno
		raise e

	@classmethod
	def get(cls, item, name, nofollow=False, namespace=None):
		cls._raise()

	@classmethod
	def set(cls, item, name, value, flags=0, namespace=None):
		cls._raise()

	@classmethod
	def remove(cls, item, name, nofollow=False, namespace=None):
		cls._raise()

	@classmethod
	def list(cls, item, nofollow=False, namespace=None):
		cls._raise()


if hasattr(os, 'getxattr'):
	# Easy as pie -- active python supports it.
	class xattr(_XattrGetAll):
		"""Python >=3.3 and GNU/Linux"""

		# pylint: disable=unused-argument

		@staticmethod
		def get(item, name, nofollow=False, namespace=None):
			return os.getxattr(item, name, follow_symlinks=not nofollow)

		@staticmethod
		def set(item, name, value, flags=0, namespace=None):
			return os.setxattr(item, name, value, flags=flags)

		@staticmethod
		def remove(item, name, nofollow=False, namespace=None):
			return os.removexattr(item, name, follow_symlinks=not nofollow)

		@staticmethod
		def list(item, nofollow=False, namespace=None):
			return os.listxattr(item, follow_symlinks=not nofollow)

else:
	try:
		# Maybe we have the xattr module.
		import xattr

	except ImportError:
		try:
			# Maybe we have the attr package.
			with open(os.devnull, 'wb') as f:
				subprocess.call(['getfattr', '--version'], stdout=f)
				subprocess.call(['setfattr', '--version'], stdout=f)
			xattr = _XattrSystemCommands

		except OSError:
			# Stub it out completely.
			xattr = _XattrStub


# Add a knob so code can take evasive action as needed.
XATTRS_WORKS = xattr != _XattrStub


@contextlib.contextmanager
def preserve_xattrs(path, nofollow=False, namespace=None):
	"""Context manager to save/restore extended attributes on |path|

	If you want to rewrite a file (possibly replacing it with a new one), but
	want to preserve the extended attributes, this will do the trick.

	# First read all the extended attributes.
	with save_xattrs('/some/file'):
		... rewrite the file ...
	# Now the extended attributes are restored as needed.
	"""
	kwargs = {'nofollow': nofollow,}
	if namespace:
		# Compiled xattr python module does not like it when namespace=None.
		kwargs['namespace'] = namespace

	old_attrs = dict(xattr.get_all(path, **kwargs))
	try:
		yield
	finally:
		new_attrs = dict(xattr.get_all(path, **kwargs))
		for name, value in new_attrs.items():
			if name not in old_attrs:
				# Clear out new ones.
				xattr.remove(path, name, **kwargs)
			elif new_attrs[name] != old_attrs[name]:
				# Update changed ones.
				xattr.set(path, name, value, **kwargs)

		for name, value in old_attrs.items():
			if name not in new_attrs:
				# Re-add missing ones.
				xattr.set(path, name, value, **kwargs)