/usr/lib/python2.7/dist-packages/twext/web2/dav/xattrprops.py is in calendarserver 5.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | # Copyright (c) 2009 Twisted Matrix Laboratories.
# See LICENSE for details.
##
# Copyright (c) 2005-2014 Apple Computer, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
##
"""
DAV Property store using file system extended attributes.
This API is considered private to static.py and is therefore subject to
change.
"""
__all__ = ["xattrPropertyStore"]
import urllib
import sys
import zlib
import errno
from operator import setitem
from zlib import compress, decompress
from cPickle import UnpicklingError, loads as unpickle
import xattr
if getattr(xattr, 'xattr', None) is None:
raise ImportError("wrong xattr package imported")
from twisted.python.util import untilConcludes
from twisted.python.failure import Failure
from twisted.python.log import err
from txdav.xml.base import encodeXMLName
from txdav.xml.parser import WebDAVDocument
from twext.web2 import responsecode
from twext.web2.http import HTTPError, StatusResponse
from twext.web2.dav.http import statusForFailure
# RFC 2518 Section 12.13.1 says that removal of non-existing property
# is not an error. python-xattr on Linux fails with ENODATA in this
# case. On Darwin and FreeBSD, the xattr library fails with ENOATTR,
# which CPython does not expose. Its value is 93.
_ATTR_MISSING = (93,)
if hasattr(errno, "ENODATA"):
_ATTR_MISSING += (errno.ENODATA,)
class xattrPropertyStore (object):
"""
This implementation uses Bob Ippolito's xattr package, available from::
http://undefined.org/python/#xattr
Note that the Bob's xattr package is specific to Linux and Darwin, at least
presently.
"""
#
# Dead properties are stored as extended attributes on disk. In order to
# avoid conflicts with other attributes, prefix dead property names.
#
deadPropertyXattrPrefix = "WebDAV:"
# Linux seems to require that attribute names use a "user." prefix.
# FIXME: Is is a system-wide thing, or a per-filesystem thing?
# If the latter, how to we detect the file system?
if sys.platform == "linux2":
deadPropertyXattrPrefix = "user."
def _encode(clazz, name, uid=None):
result = urllib.quote(encodeXMLName(*name), safe='{}:')
if uid:
result = uid + result
r = clazz.deadPropertyXattrPrefix + result
return r
def _decode(clazz, name):
name = urllib.unquote(name[len(clazz.deadPropertyXattrPrefix):])
index1 = name.find("{")
index2 = name.find("}")
if (index1 is -1 or index2 is -1 or not len(name) > index2):
raise ValueError("Invalid encoded name: %r" % (name,))
if index1 == 0:
uid = None
else:
uid = name[:index1]
propnamespace = name[index1+1:index2]
propname = name[index2+1:]
return (propnamespace, propname, uid)
_encode = classmethod(_encode)
_decode = classmethod(_decode)
def __init__(self, resource):
self.resource = resource
self.attrs = xattr.xattr(self.resource.fp.path)
def get(self, qname, uid=None):
"""
Retrieve the value of a property stored as an extended attribute on the
wrapped path.
@param qname: The property to retrieve as a two-tuple of namespace URI
and local name.
@param uid: The per-user identifier for per user properties.
@raise HTTPError: If there is no value associated with the given
property.
@return: A L{WebDAVDocument} representing the value associated with the
given property.
"""
try:
data = self.attrs.get(self._encode(qname, uid))
except KeyError:
raise HTTPError(StatusResponse(
responsecode.NOT_FOUND,
"No such property: %s" % (encodeXMLName(*qname),)
))
except IOError, e:
if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT:
raise HTTPError(StatusResponse(
responsecode.NOT_FOUND,
"No such property: %s" % (encodeXMLName(*qname),)
))
else:
raise HTTPError(StatusResponse(
statusForFailure(Failure()),
"Unable to read property: %s" % (encodeXMLName(*qname),)
))
#
# Unserialize XML data from an xattr. The storage format has changed
# over time:
#
# 1- Started with XML
# 2- Started compressing the XML due to limits on xattr size
# 3- Switched to pickle which is faster, still compressing
# 4- Back to compressed XML for interoperability, size
#
# We only write the current format, but we also read the old
# ones for compatibility.
#
legacy = False
try:
data = decompress(data)
except zlib.error:
legacy = True
try:
doc = WebDAVDocument.fromString(data)
except ValueError:
try:
doc = unpickle(data)
except UnpicklingError:
format = "Invalid property value stored on server: %s %s"
msg = format % (encodeXMLName(*qname), data)
err(None, msg)
raise HTTPError(
StatusResponse(responsecode.INTERNAL_SERVER_ERROR, msg))
else:
legacy = True
if legacy:
self.set(doc.root_element)
return doc.root_element
def set(self, property, uid=None):
"""
Store the given property as an extended attribute on the wrapped path.
@param uid: The per-user identifier for per user properties.
@param property: A L{WebDAVElement} to store.
"""
key = self._encode(property.qname(), uid)
value = compress(property.toxml(pretty=False))
untilConcludes(setitem, self.attrs, key, value)
# Update the resource because we've modified it
self.resource.fp.restat()
def delete(self, qname, uid=None):
"""
Remove the extended attribute from the wrapped path which stores the
property given by C{qname}.
@param uid: The per-user identifier for per user properties.
@param qname: The property to delete as a two-tuple of namespace URI
and local name.
"""
key = self._encode(qname, uid)
try:
try:
self.attrs.remove(key)
except KeyError:
pass
except IOError, e:
if e.errno not in _ATTR_MISSING:
raise
except:
raise HTTPError(StatusResponse(
statusForFailure(Failure()),
"Unable to delete property: %s", (key,)
))
def contains(self, qname, uid=None):
"""
Determine whether the property given by C{qname} is stored in an
extended attribute of the wrapped path.
@param qname: The property to look up as a two-tuple of namespace URI
and local name.
@param uid: The per-user identifier for per user properties.
@return: C{True} if the property exists, C{False} otherwise.
"""
key = self._encode(qname, uid)
try:
self.attrs.get(key)
except KeyError:
return False
except IOError, e:
if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT:
return False
raise HTTPError(StatusResponse(
statusForFailure(Failure()),
"Unable to read property: %s" % (key,)
))
else:
return True
def list(self, uid=None, filterByUID=True):
"""
Enumerate the property names stored in extended attributes of the
wrapped path.
@param uid: The per-user identifier for per user properties.
@return: A C{list} of property names as two-tuples of namespace URI and
local name.
"""
prefix = self.deadPropertyXattrPrefix
try:
attrs = iter(self.attrs)
except IOError, e:
if e.errno == errno.ENOENT:
return []
raise HTTPError(StatusResponse(
statusForFailure(Failure()),
"Unable to list properties: %s", (self.resource.fp.path,)
))
else:
results = [
self._decode(name)
for name in attrs
if name.startswith(prefix)
]
if filterByUID:
return [
(namespace, name)
for namespace, name, propuid in results
if propuid == uid
]
else:
return results
|