storage.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. #
  5. # This library 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.
  9. #
  10. # This library 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.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Storage backends.
  19. This module loads the storage backend, according to the storage configuration.
  20. Default storage uses one folder per collection and one file per collection
  21. entry.
  22. """
  23. import binascii
  24. import contextlib
  25. import json
  26. import os
  27. import pickle
  28. import posixpath
  29. import shlex
  30. import subprocess
  31. import sys
  32. import threading
  33. import time
  34. from contextlib import contextmanager
  35. from hashlib import md5
  36. from importlib import import_module
  37. from itertools import chain, groupby
  38. from random import getrandbits
  39. from tempfile import NamedTemporaryFile, TemporaryDirectory
  40. import vobject
  41. if sys.version_info >= (3, 5):
  42. # HACK: Avoid import cycle for Python < 3.5
  43. from . import xmlutils
  44. if os.name == "nt":
  45. import ctypes
  46. import ctypes.wintypes
  47. import msvcrt
  48. LOCKFILE_EXCLUSIVE_LOCK = 2
  49. if ctypes.sizeof(ctypes.c_void_p) == 4:
  50. ULONG_PTR = ctypes.c_uint32
  51. else:
  52. ULONG_PTR = ctypes.c_uint64
  53. class Overlapped(ctypes.Structure):
  54. _fields_ = [
  55. ("internal", ULONG_PTR),
  56. ("internal_high", ULONG_PTR),
  57. ("offset", ctypes.wintypes.DWORD),
  58. ("offset_high", ctypes.wintypes.DWORD),
  59. ("h_event", ctypes.wintypes.HANDLE)]
  60. lock_file_ex = ctypes.windll.kernel32.LockFileEx
  61. lock_file_ex.argtypes = [
  62. ctypes.wintypes.HANDLE,
  63. ctypes.wintypes.DWORD,
  64. ctypes.wintypes.DWORD,
  65. ctypes.wintypes.DWORD,
  66. ctypes.wintypes.DWORD,
  67. ctypes.POINTER(Overlapped)]
  68. lock_file_ex.restype = ctypes.wintypes.BOOL
  69. unlock_file_ex = ctypes.windll.kernel32.UnlockFileEx
  70. unlock_file_ex.argtypes = [
  71. ctypes.wintypes.HANDLE,
  72. ctypes.wintypes.DWORD,
  73. ctypes.wintypes.DWORD,
  74. ctypes.wintypes.DWORD,
  75. ctypes.POINTER(Overlapped)]
  76. unlock_file_ex.restype = ctypes.wintypes.BOOL
  77. elif os.name == "posix":
  78. import fcntl
  79. INTERNAL_TYPES = ("multifilesystem",)
  80. def load(configuration, logger):
  81. """Load the storage manager chosen in configuration."""
  82. if sys.version_info < (3, 5):
  83. # HACK: Avoid import cycle for Python < 3.5
  84. global xmlutils
  85. from . import xmlutils
  86. storage_type = configuration.get("storage", "type")
  87. if storage_type == "multifilesystem":
  88. collection_class = Collection
  89. else:
  90. try:
  91. collection_class = import_module(storage_type).Collection
  92. except Exception as e:
  93. raise RuntimeError("Failed to load storage module %r: %s" %
  94. (storage_type, e)) from e
  95. logger.info("Storage type is %r", storage_type)
  96. class CollectionCopy(collection_class):
  97. """Collection copy, avoids overriding the original class attributes."""
  98. CollectionCopy.configuration = configuration
  99. CollectionCopy.logger = logger
  100. return CollectionCopy
  101. def check_and_sanitize_item(vobject_item, is_collection=False, uid=None,
  102. tag=None):
  103. """Check vobject items for common errors and add missing UIDs.
  104. ``multiple`` indicates that the vobject_item contains unrelated components.
  105. If ``uid`` is not set, the UID is generated randomly.
  106. """
  107. if tag and tag not in ("VCALENDAR", "VADDRESSBOOK"):
  108. raise ValueError("Unsupported collection tag: %r" % tag)
  109. if vobject_item.name == "VCALENDAR" and tag == "VCALENDAR":
  110. component_name = None
  111. object_uid = None
  112. object_uid_set = False
  113. for component in vobject_item.components():
  114. # https://tools.ietf.org/html/rfc4791#section-4.1
  115. if component.name == "VTIMEZONE":
  116. continue
  117. if component_name is None or is_collection:
  118. component_name = component.name
  119. elif component_name != component.name:
  120. raise ValueError("Muliple component types in object: %r, %r" %
  121. (component_name, component.name))
  122. if component_name not in ("VTODO", "VEVENT", "VJOURNAL"):
  123. continue
  124. component_uid = get_uid(component)
  125. if not object_uid_set or is_collection:
  126. object_uid_set = True
  127. object_uid = component_uid
  128. if component_uid is None:
  129. component.add("UID").value = uid or random_uuid4()
  130. elif not component_uid:
  131. component.uid.value = uid or random_uuid4()
  132. elif not object_uid or not component_uid:
  133. raise ValueError("Multiple %s components without UID in "
  134. "object" % component_name)
  135. elif object_uid != component_uid:
  136. raise ValueError(
  137. "Muliple %s components with different UIDs in object: "
  138. "%r, %r" % (component_name, object_uid, component_uid))
  139. # vobject interprets recurrence rules on demand
  140. try:
  141. component.rruleset
  142. except Exception as e:
  143. raise ValueError("invalid recurrence rules in %s" %
  144. component.name) from e
  145. elif vobject_item.name == "VCARD" and tag == "VADDRESSBOOK":
  146. # https://tools.ietf.org/html/rfc6352#section-5.1
  147. object_uid = get_uid(vobject_item)
  148. if object_uid is None:
  149. vobject_item.add("UID").value = uid or random_uuid4()
  150. elif not object_uid:
  151. vobject_item.uid.value = uid or random_uuid4()
  152. elif vobject_item.name == "VLIST" and tag == "VADDRESSBOOK":
  153. # Custom format used by SOGo Connector to store lists of contacts
  154. pass
  155. else:
  156. raise ValueError("Item type %r not supported in %s collection" %
  157. (vobject_item.name, repr(tag) if tag else "generic"))
  158. def check_and_sanitize_props(props):
  159. """Check collection properties for common errors."""
  160. tag = props.get("tag")
  161. if tag and tag not in ("VCALENDAR", "VADDRESSBOOK"):
  162. raise ValueError("Unsupported collection tag: %r" % tag)
  163. def random_uuid4():
  164. """Generate a pseudo-random UUID"""
  165. r = "%016x" % getrandbits(128)
  166. return "%s-%s-%s-%s-%s" % (r[:8], r[8:12], r[12:16], r[16:20], r[20:])
  167. def scandir(path, only_dirs=False, only_files=False):
  168. """Iterator for directory elements. (For compatibility with Python < 3.5)
  169. ``only_dirs`` only return directories
  170. ``only_files`` only return files
  171. """
  172. if sys.version_info >= (3, 5):
  173. for entry in os.scandir(path):
  174. if ((not only_files or entry.is_file()) and
  175. (not only_dirs or entry.is_dir())):
  176. yield entry.name
  177. else:
  178. for name in os.listdir(path):
  179. p = os.path.join(path, name)
  180. if ((not only_files or os.path.isfile(p)) and
  181. (not only_dirs or os.path.isdir(p))):
  182. yield name
  183. def get_etag(text):
  184. """Etag from collection or item.
  185. Encoded as quoted-string (see RFC 2616).
  186. """
  187. etag = md5()
  188. etag.update(text.encode("utf-8"))
  189. return '"%s"' % etag.hexdigest()
  190. def get_uid(vobject_component):
  191. """UID value of an item if defined."""
  192. return ((hasattr(vobject_component, "uid") or None) and
  193. vobject_component.uid.value)
  194. def get_uid_from_object(vobject_item):
  195. """UID value of an calendar/addressbook object."""
  196. if vobject_item.name == "VCALENDAR":
  197. if hasattr(vobject_item, "vevent"):
  198. return get_uid(vobject_item.vevent)
  199. if hasattr(vobject_item, "vjournal"):
  200. return get_uid(vobject_item.vjournal)
  201. if hasattr(vobject_item, "vtodo"):
  202. return get_uid(vobject_item.vtodo)
  203. elif vobject_item.name == "VCARD":
  204. return get_uid(vobject_item)
  205. return None
  206. def sanitize_path(path):
  207. """Make path absolute with leading slash to prevent access to other data.
  208. Preserve a potential trailing slash.
  209. """
  210. trailing_slash = "/" if path.endswith("/") else ""
  211. path = posixpath.normpath(path)
  212. new_path = "/"
  213. for part in path.split("/"):
  214. if not is_safe_path_component(part):
  215. continue
  216. new_path = posixpath.join(new_path, part)
  217. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  218. return new_path + trailing_slash
  219. def is_safe_path_component(path):
  220. """Check if path is a single component of a path.
  221. Check that the path is safe to join too.
  222. """
  223. return path and "/" not in path and path not in (".", "..")
  224. def is_safe_filesystem_path_component(path):
  225. """Check if path is a single component of a local and posix filesystem
  226. path.
  227. Check that the path is safe to join too.
  228. """
  229. return (
  230. path and not os.path.splitdrive(path)[0] and
  231. not os.path.split(path)[0] and path not in (os.curdir, os.pardir) and
  232. not path.startswith(".") and not path.endswith("~") and
  233. is_safe_path_component(path))
  234. def path_to_filesystem(root, *paths):
  235. """Convert path to a local filesystem path relative to base_folder.
  236. `root` must be a secure filesystem path, it will be prepend to the path.
  237. Conversion of `paths` is done in a secure manner, or raises ``ValueError``.
  238. """
  239. paths = [sanitize_path(path).strip("/") for path in paths]
  240. safe_path = root
  241. for path in paths:
  242. if not path:
  243. continue
  244. for part in path.split("/"):
  245. if not is_safe_filesystem_path_component(part):
  246. raise UnsafePathError(part)
  247. safe_path_parent = safe_path
  248. safe_path = os.path.join(safe_path, part)
  249. # Check for conflicting files (e.g. case-insensitive file systems
  250. # or short names on Windows file systems)
  251. if (os.path.lexists(safe_path) and
  252. part not in scandir(safe_path_parent)):
  253. raise CollidingPathError(part)
  254. return safe_path
  255. class UnsafePathError(ValueError):
  256. def __init__(self, path):
  257. message = "Can't translate name safely to filesystem: %r" % path
  258. super().__init__(message)
  259. class CollidingPathError(ValueError):
  260. def __init__(self, path):
  261. message = "File name collision: %r" % path
  262. super().__init__(message)
  263. class ComponentExistsError(ValueError):
  264. def __init__(self, path):
  265. message = "Component already exists: %r" % path
  266. super().__init__(message)
  267. class ComponentNotFoundError(ValueError):
  268. def __init__(self, path):
  269. message = "Component doesn't exist: %r" % path
  270. super().__init__(message)
  271. class Item:
  272. def __init__(self, collection, item=None, href=None, last_modified=None,
  273. text=None, etag=None, uid=None, name=None,
  274. component_name=None):
  275. """Initialize an item.
  276. ``collection`` the parent collection.
  277. ``href`` the href of the item.
  278. ``last_modified`` the HTTP-datetime of when the item was modified.
  279. ``text`` the text representation of the item (optional if ``item`` is
  280. set).
  281. ``item`` the vobject item (optional if ``text`` is set).
  282. ``etag`` the etag of the item (optional). See ``get_etag``.
  283. ``uid`` the UID of the object (optional). See ``get_uid_from_object``.
  284. """
  285. if text is None and item is None:
  286. raise ValueError("at least one of 'text' or 'item' must be set")
  287. self.collection = collection
  288. self.href = href
  289. self.last_modified = last_modified
  290. self._text = text
  291. self._item = item
  292. self._etag = etag
  293. self._uid = uid
  294. self._name = name
  295. self._component_name = component_name
  296. def __getattr__(self, attr):
  297. return getattr(self.item, attr)
  298. def serialize(self):
  299. if self._text is None:
  300. try:
  301. self._text = self.item.serialize()
  302. except Exception as e:
  303. raise RuntimeError("Failed to serialize item %r from %r: %s" %
  304. (self.href, self.collection.path, e)) from e
  305. return self._text
  306. @property
  307. def item(self):
  308. if self._item is None:
  309. try:
  310. self._item = vobject.readOne(self._text)
  311. except Exception as e:
  312. raise RuntimeError("Failed to parse item %r from %r: %s" %
  313. (self.href, self.collection.path, e)) from e
  314. return self._item
  315. @property
  316. def etag(self):
  317. """Encoded as quoted-string (see RFC 2616)."""
  318. if self._etag is None:
  319. self._etag = get_etag(self.serialize())
  320. return self._etag
  321. @property
  322. def uid(self):
  323. if self._uid is None:
  324. self._uid = get_uid_from_object(self.item)
  325. return self._uid
  326. @property
  327. def name(self):
  328. if self._name is not None:
  329. return self._name
  330. return self.item.name
  331. @property
  332. def component_name(self):
  333. if self._component_name is not None:
  334. return self._component_name
  335. return xmlutils.find_tag(self.item)
  336. class BaseCollection:
  337. # Overriden on copy by the "load" function
  338. configuration = None
  339. logger = None
  340. # Properties of instance
  341. """The sanitized path of the collection without leading or trailing ``/``.
  342. """
  343. path = ""
  344. @property
  345. def owner(self):
  346. """The owner of the collection."""
  347. return self.path.split("/", maxsplit=1)[0]
  348. @property
  349. def is_principal(self):
  350. """Collection is a principal."""
  351. return bool(self.path) and "/" not in self.path
  352. @owner.setter
  353. def owner(self, value):
  354. # DEPRECATED: Included for compatibility reasons
  355. pass
  356. @is_principal.setter
  357. def is_principal(self, value):
  358. # DEPRECATED: Included for compatibility reasons
  359. pass
  360. @classmethod
  361. def discover(cls, path, depth="0"):
  362. """Discover a list of collections under the given ``path``.
  363. ``path`` is sanitized.
  364. If ``depth`` is "0", only the actual object under ``path`` is
  365. returned.
  366. If ``depth`` is anything but "0", it is considered as "1" and direct
  367. children are included in the result.
  368. The root collection "/" must always exist.
  369. """
  370. raise NotImplementedError
  371. @classmethod
  372. def move(cls, item, to_collection, to_href):
  373. """Move an object.
  374. ``item`` is the item to move.
  375. ``to_collection`` is the target collection.
  376. ``to_href`` is the target name in ``to_collection``. An item with the
  377. same name might already exist.
  378. """
  379. if item.collection.path == to_collection.path and item.href == to_href:
  380. return
  381. to_collection.upload(to_href, item.item)
  382. item.collection.delete(item.href)
  383. @property
  384. def etag(self):
  385. """Encoded as quoted-string (see RFC 2616)."""
  386. etag = md5()
  387. for item in self.get_all():
  388. etag.update((item.href + "/" + item.etag).encode("utf-8"))
  389. etag.update(json.dumps(self.get_meta(), sort_keys=True).encode())
  390. return '"%s"' % etag.hexdigest()
  391. @classmethod
  392. def create_collection(cls, href, collection=None, props=None):
  393. """Create a collection.
  394. ``href`` is the sanitized path.
  395. If the collection already exists and neither ``collection`` nor
  396. ``props`` are set, this method shouldn't do anything. Otherwise the
  397. existing collection must be replaced.
  398. ``collection`` is a list of vobject components.
  399. ``props`` are metadata values for the collection.
  400. ``props["tag"]`` is the type of collection (VCALENDAR or
  401. VADDRESSBOOK). If the key ``tag`` is missing, it is guessed from the
  402. collection.
  403. """
  404. raise NotImplementedError
  405. def sync(self, old_token=None):
  406. """Get the current sync token and changed items for synchronization.
  407. ``old_token`` an old sync token which is used as the base of the
  408. delta update. If sync token is missing, all items are returned.
  409. ValueError is raised for invalid or old tokens.
  410. WARNING: This simple default implementation treats all sync-token as
  411. invalid. It adheres to the specification but some clients
  412. (e.g. InfCloud) don't like it. Subclasses should provide a
  413. more sophisticated implementation.
  414. """
  415. token = "http://radicale.org/ns/sync/%s" % self.etag.strip("\"")
  416. if old_token:
  417. raise ValueError("Sync token are not supported")
  418. return token, self.list()
  419. def list(self):
  420. """List collection items."""
  421. raise NotImplementedError
  422. def get(self, href):
  423. """Fetch a single item."""
  424. raise NotImplementedError
  425. def get_multi(self, hrefs):
  426. """Fetch multiple items. Duplicate hrefs must be ignored.
  427. DEPRECATED: use ``get_multi2`` instead
  428. """
  429. return (self.get(href) for href in set(hrefs))
  430. def get_multi2(self, hrefs):
  431. """Fetch multiple items.
  432. Functionally similar to ``get``, but might bring performance benefits
  433. on some storages when used cleverly. It's not required to return the
  434. requested items in the correct order. Duplicated hrefs can be ignored.
  435. Returns tuples with the href and the item or None if the item doesn't
  436. exist.
  437. """
  438. return ((href, self.get(href)) for href in hrefs)
  439. def get_all(self):
  440. """Fetch all items.
  441. Functionally similar to ``get``, but might bring performance benefits
  442. on some storages when used cleverly.
  443. """
  444. return map(self.get, self.list())
  445. def get_all_filtered(self, filters):
  446. """Fetch all items with optional filtering.
  447. This can largely improve performance of reports depending on
  448. the filters and this implementation.
  449. Returns tuples in the form ``(item, filters_matched)``.
  450. ``filters_matched`` is a bool that indicates if ``filters`` are fully
  451. matched.
  452. This returns all events by default
  453. """
  454. return ((item, False) for item in self.get_all())
  455. def pre_filtered_list(self, filters):
  456. """List collection items with optional pre filtering.
  457. DEPRECATED: use ``get_all_filtered`` instead
  458. """
  459. return self.get_all()
  460. def has(self, href):
  461. """Check if an item exists by its href.
  462. Functionally similar to ``get``, but might bring performance benefits
  463. on some storages when used cleverly.
  464. """
  465. return self.get(href) is not None
  466. def upload(self, href, vobject_item):
  467. """Upload a new or replace an existing item."""
  468. raise NotImplementedError
  469. def delete(self, href=None):
  470. """Delete an item.
  471. When ``href`` is ``None``, delete the collection.
  472. """
  473. raise NotImplementedError
  474. def get_meta(self, key=None):
  475. """Get metadata value for collection.
  476. Return the value of the property ``key``. If ``key`` is ``None`` return
  477. a dict with all properties
  478. """
  479. raise NotImplementedError
  480. def set_meta(self, props):
  481. """Set metadata values for collection.
  482. ``props`` a dict with updates for properties. If a value is empty, the
  483. property must be deleted.
  484. DEPRECATED: use ``set_meta_all`` instead
  485. """
  486. raise NotImplementedError
  487. def set_meta_all(self, props):
  488. """Set metadata values for collection.
  489. ``props`` a dict with values for properties.
  490. """
  491. delta_props = self.get_meta()
  492. for key in delta_props.keys():
  493. if key not in props:
  494. delta_props[key] = None
  495. delta_props.update(props)
  496. self.set_meta(self, delta_props)
  497. @property
  498. def last_modified(self):
  499. """Get the HTTP-datetime of when the collection was modified."""
  500. raise NotImplementedError
  501. def serialize(self):
  502. """Get the unicode string representing the whole collection."""
  503. if self.get_meta("tag") == "VCALENDAR":
  504. in_vcalendar = False
  505. vtimezones = ""
  506. included_tzids = set()
  507. vtimezone = []
  508. tzid = None
  509. components = ""
  510. # Concatenate all child elements of VCALENDAR from all items
  511. # together, while preventing duplicated VTIMEZONE entries.
  512. # VTIMEZONEs are only distinguished by their TZID, if different
  513. # timezones share the same TZID this produces errornous ouput.
  514. # VObject fails at this too.
  515. for item in self.get_all():
  516. depth = 0
  517. for line in item.serialize().split("\r\n"):
  518. if line.startswith("BEGIN:"):
  519. depth += 1
  520. if depth == 1 and line == "BEGIN:VCALENDAR":
  521. in_vcalendar = True
  522. elif in_vcalendar:
  523. if depth == 1 and line.startswith("END:"):
  524. in_vcalendar = False
  525. if depth == 2 and line == "BEGIN:VTIMEZONE":
  526. vtimezone.append(line)
  527. elif vtimezone:
  528. vtimezone.append(line)
  529. if depth == 2 and line.startswith("TZID:"):
  530. tzid = line[len("TZID:"):]
  531. elif depth == 2 and line.startswith("END:"):
  532. if tzid is None or tzid not in included_tzids:
  533. if vtimezones:
  534. vtimezones += "\r\n"
  535. vtimezones += "\r\n".join(vtimezone)
  536. included_tzids.add(tzid)
  537. vtimezone.clear()
  538. tzid = None
  539. elif depth >= 2:
  540. if components:
  541. components += "\r\n"
  542. components += line
  543. if line.startswith("END:"):
  544. depth -= 1
  545. return "\r\n".join(filter(bool, (
  546. "BEGIN:VCALENDAR",
  547. "VERSION:2.0",
  548. "PRODID:-//PYVOBJECT//NONSGML Version 1//EN",
  549. vtimezones,
  550. components,
  551. "END:VCALENDAR")))
  552. elif self.get_meta("tag") == "VADDRESSBOOK":
  553. return "".join((item.serialize() for item in self.get_all()))
  554. return ""
  555. @classmethod
  556. @contextmanager
  557. def acquire_lock(cls, mode, user=None):
  558. """Set a context manager to lock the whole storage.
  559. ``mode`` must either be "r" for shared access or "w" for exclusive
  560. access.
  561. ``user`` is the name of the logged in user or empty.
  562. """
  563. raise NotImplementedError
  564. @classmethod
  565. def verify(cls):
  566. """Check the storage for errors."""
  567. return True
  568. class Collection(BaseCollection):
  569. """Collection stored in several files per calendar."""
  570. def __init__(self, path, principal=None, folder=None,
  571. filesystem_path=None):
  572. # DEPRECATED: Remove principal and folder attributes
  573. if folder is None:
  574. folder = self._get_collection_root_folder()
  575. # Path should already be sanitized
  576. self.path = sanitize_path(path).strip("/")
  577. self._encoding = self.configuration.get("encoding", "stock")
  578. # DEPRECATED: Use ``self._encoding`` instead
  579. self.encoding = self._encoding
  580. if filesystem_path is None:
  581. filesystem_path = path_to_filesystem(folder, self.path)
  582. self._filesystem_path = filesystem_path
  583. self._props_path = os.path.join(
  584. self._filesystem_path, ".Radicale.props")
  585. self._meta_cache = None
  586. self._etag_cache = None
  587. self._item_cache_cleaned = False
  588. @classmethod
  589. def _get_collection_root_folder(cls):
  590. filesystem_folder = os.path.expanduser(
  591. cls.configuration.get("storage", "filesystem_folder"))
  592. return os.path.join(filesystem_folder, "collection-root")
  593. @contextmanager
  594. def _atomic_write(self, path, mode="w", newline=None, sync_directory=True):
  595. directory = os.path.dirname(path)
  596. tmp = NamedTemporaryFile(
  597. mode=mode, dir=directory, delete=False, prefix=".Radicale.tmp-",
  598. newline=newline, encoding=None if "b" in mode else self._encoding)
  599. try:
  600. yield tmp
  601. try:
  602. self._fsync(tmp.fileno())
  603. except OSError as e:
  604. raise RuntimeError("Fsync'ing file %r failed: %s" %
  605. (path, e)) from e
  606. tmp.close()
  607. os.replace(tmp.name, path)
  608. except:
  609. tmp.close()
  610. os.remove(tmp.name)
  611. raise
  612. if sync_directory:
  613. self._sync_directory(directory)
  614. @staticmethod
  615. def _find_available_file_name(exists_fn, suffix=""):
  616. # Prevent infinite loop
  617. for _ in range(1000):
  618. file_name = random_uuid4() + suffix
  619. if not exists_fn(file_name):
  620. return file_name
  621. # something is wrong with the PRNG
  622. raise RuntimeError("No unique random sequence found")
  623. @classmethod
  624. def _fsync(cls, fd):
  625. if cls.configuration.getboolean("storage", "filesystem_fsync"):
  626. if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
  627. fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
  628. else:
  629. os.fsync(fd)
  630. @classmethod
  631. def _sync_directory(cls, path):
  632. """Sync directory to disk.
  633. This only works on POSIX and does nothing on other systems.
  634. """
  635. if not cls.configuration.getboolean("storage", "filesystem_fsync"):
  636. return
  637. if os.name == "posix":
  638. try:
  639. fd = os.open(path, 0)
  640. try:
  641. cls._fsync(fd)
  642. finally:
  643. os.close(fd)
  644. except OSError as e:
  645. raise RuntimeError("Fsync'ing directory %r failed: %s" %
  646. (path, e)) from e
  647. @classmethod
  648. def _makedirs_synced(cls, filesystem_path):
  649. """Recursively create a directory and its parents in a sync'ed way.
  650. This method acts silently when the folder already exists.
  651. """
  652. if os.path.isdir(filesystem_path):
  653. return
  654. parent_filesystem_path = os.path.dirname(filesystem_path)
  655. # Prevent infinite loop
  656. if filesystem_path != parent_filesystem_path:
  657. # Create parent dirs recursively
  658. cls._makedirs_synced(parent_filesystem_path)
  659. # Possible race!
  660. os.makedirs(filesystem_path, exist_ok=True)
  661. cls._sync_directory(parent_filesystem_path)
  662. @classmethod
  663. def discover(cls, path, depth="0", child_context_manager=(
  664. lambda path, href=None: contextlib.ExitStack())):
  665. # Path should already be sanitized
  666. sane_path = sanitize_path(path).strip("/")
  667. attributes = sane_path.split("/") if sane_path else []
  668. folder = cls._get_collection_root_folder()
  669. # Create the root collection
  670. cls._makedirs_synced(folder)
  671. try:
  672. filesystem_path = path_to_filesystem(folder, sane_path)
  673. except ValueError as e:
  674. # Path is unsafe
  675. cls.logger.debug("Unsafe path %r requested from storage: %s",
  676. sane_path, e, exc_info=True)
  677. return
  678. # Check if the path exists and if it leads to a collection or an item
  679. if not os.path.isdir(filesystem_path):
  680. if attributes and os.path.isfile(filesystem_path):
  681. href = attributes.pop()
  682. else:
  683. return
  684. else:
  685. href = None
  686. sane_path = "/".join(attributes)
  687. collection = cls(sane_path)
  688. if href:
  689. yield collection.get(href)
  690. return
  691. yield collection
  692. if depth == "0":
  693. return
  694. for href in collection.list():
  695. with child_context_manager(sane_path, href):
  696. yield collection.get(href)
  697. for href in scandir(filesystem_path, only_dirs=True):
  698. if not is_safe_filesystem_path_component(href):
  699. if not href.startswith(".Radicale"):
  700. cls.logger.debug("Skipping collection %r in %r", href,
  701. sane_path)
  702. continue
  703. child_path = posixpath.join(sane_path, href)
  704. with child_context_manager(child_path):
  705. yield cls(child_path)
  706. @classmethod
  707. def verify(cls):
  708. item_errors = collection_errors = 0
  709. @contextlib.contextmanager
  710. def exception_cm(path, href=None):
  711. nonlocal item_errors, collection_errors
  712. try:
  713. yield
  714. except Exception as e:
  715. if href:
  716. item_errors += 1
  717. name = "item %r in %r" % (href, path.strip("/"))
  718. else:
  719. collection_errors += 1
  720. name = "collection %r" % path.strip("/")
  721. cls.logger.error("Invalid %s: %s", name, e, exc_info=True)
  722. remaining_paths = [""]
  723. while remaining_paths:
  724. path = remaining_paths.pop(0)
  725. cls.logger.debug("Verifying collection %r", path)
  726. with exception_cm(path):
  727. saved_item_errors = item_errors
  728. collection = None
  729. for item in cls.discover(path, "1", exception_cm):
  730. if not collection:
  731. collection = item
  732. collection.get_meta()
  733. continue
  734. if isinstance(item, BaseCollection):
  735. remaining_paths.append(item.path)
  736. else:
  737. cls.logger.debug("Verified item %r in %r",
  738. item.href, path)
  739. if item_errors == saved_item_errors:
  740. collection.sync()
  741. return item_errors == 0 and collection_errors == 0
  742. @classmethod
  743. def create_collection(cls, href, collection=None, props=None):
  744. folder = cls._get_collection_root_folder()
  745. # Path should already be sanitized
  746. sane_path = sanitize_path(href).strip("/")
  747. filesystem_path = path_to_filesystem(folder, sane_path)
  748. if not props:
  749. cls._makedirs_synced(filesystem_path)
  750. return cls(sane_path)
  751. parent_dir = os.path.dirname(filesystem_path)
  752. cls._makedirs_synced(parent_dir)
  753. # Create a temporary directory with an unsafe name
  754. with TemporaryDirectory(
  755. prefix=".Radicale.tmp-", dir=parent_dir) as tmp_dir:
  756. # The temporary directory itself can't be renamed
  757. tmp_filesystem_path = os.path.join(tmp_dir, "collection")
  758. os.makedirs(tmp_filesystem_path)
  759. self = cls(sane_path, filesystem_path=tmp_filesystem_path)
  760. self.set_meta_all(props)
  761. if collection:
  762. if props.get("tag") == "VCALENDAR":
  763. collection, = collection
  764. items = []
  765. for content in ("vevent", "vtodo", "vjournal"):
  766. items.extend(
  767. getattr(collection, "%s_list" % content, []))
  768. items_by_uid = groupby(sorted(items, key=get_uid), get_uid)
  769. vobject_items = {}
  770. for uid, items in items_by_uid:
  771. new_collection = vobject.iCalendar()
  772. for item in items:
  773. new_collection.add(item)
  774. # href must comply to is_safe_filesystem_path_component
  775. # and no file name collisions must exist between hrefs
  776. href = self._find_available_file_name(
  777. vobject_items.get, suffix=".ics")
  778. vobject_items[href] = new_collection
  779. self._upload_all_nonatomic(vobject_items)
  780. elif props.get("tag") == "VADDRESSBOOK":
  781. vobject_items = {}
  782. for card in collection:
  783. # href must comply to is_safe_filesystem_path_component
  784. # and no file name collisions must exist between hrefs
  785. href = self._find_available_file_name(
  786. vobject_items.get, suffix=".vcf")
  787. vobject_items[href] = card
  788. self._upload_all_nonatomic(vobject_items)
  789. # This operation is not atomic on the filesystem level but it's
  790. # very unlikely that one rename operations succeeds while the
  791. # other fails or that only one gets written to disk.
  792. if os.path.exists(filesystem_path):
  793. os.rename(filesystem_path, os.path.join(tmp_dir, "delete"))
  794. os.rename(tmp_filesystem_path, filesystem_path)
  795. cls._sync_directory(parent_dir)
  796. return cls(sane_path)
  797. def upload_all_nonatomic(self, vobject_items):
  798. """DEPRECATED: Use ``_upload_all_nonatomic``"""
  799. return self._upload_all_nonatomic(vobject_items)
  800. def _upload_all_nonatomic(self, vobject_items):
  801. """Upload a new set of items.
  802. This takes a mapping of href and vobject items and
  803. uploads them nonatomic and without existence checks.
  804. """
  805. cache_folder = os.path.join(self._filesystem_path,
  806. ".Radicale.cache", "item")
  807. self._makedirs_synced(cache_folder)
  808. for href, vobject_item in vobject_items.items():
  809. if not is_safe_filesystem_path_component(href):
  810. raise UnsafePathError(href)
  811. try:
  812. cache_content = self._item_cache_content(href, vobject_item)
  813. _, _, _, text, _, _, _, _ = cache_content
  814. except Exception as e:
  815. raise ValueError(
  816. "Failed to store item %r in temporary collection %r: %s" %
  817. (href, self.path, e)) from e
  818. with self._atomic_write(os.path.join(cache_folder, href), "wb",
  819. sync_directory=False) as f:
  820. pickle.dump(cache_content, f)
  821. path = path_to_filesystem(self._filesystem_path, href)
  822. with self._atomic_write(
  823. path, newline="", sync_directory=False) as f:
  824. f.write(text)
  825. self._sync_directory(cache_folder)
  826. self._sync_directory(self._filesystem_path)
  827. @classmethod
  828. def move(cls, item, to_collection, to_href):
  829. if not is_safe_filesystem_path_component(to_href):
  830. raise UnsafePathError(to_href)
  831. os.replace(
  832. path_to_filesystem(item.collection._filesystem_path, item.href),
  833. path_to_filesystem(to_collection._filesystem_path, to_href))
  834. cls._sync_directory(to_collection._filesystem_path)
  835. if item.collection._filesystem_path != to_collection._filesystem_path:
  836. cls._sync_directory(item.collection._filesystem_path)
  837. # Move the item cache entry
  838. cache_folder = os.path.join(item.collection._filesystem_path,
  839. ".Radicale.cache", "item")
  840. to_cache_folder = os.path.join(to_collection._filesystem_path,
  841. ".Radicale.cache", "item")
  842. cls._makedirs_synced(to_cache_folder)
  843. try:
  844. os.replace(os.path.join(cache_folder, item.href),
  845. os.path.join(to_cache_folder, to_href))
  846. except FileNotFoundError:
  847. pass
  848. else:
  849. cls._makedirs_synced(to_cache_folder)
  850. if cache_folder != to_cache_folder:
  851. cls._makedirs_synced(cache_folder)
  852. # Track the change
  853. to_collection._update_history_etag(to_href, item)
  854. item.collection._update_history_etag(item.href, None)
  855. to_collection._clean_history_cache()
  856. if item.collection._filesystem_path != to_collection._filesystem_path:
  857. item.collection._clean_history_cache()
  858. @classmethod
  859. def _clean_cache(cls, folder, names, max_age=None):
  860. """Delete all ``names`` in ``folder`` that are older than ``max_age``.
  861. """
  862. age_limit = time.time() - max_age if max_age is not None else None
  863. modified = False
  864. for name in names:
  865. if not is_safe_filesystem_path_component(name):
  866. continue
  867. if age_limit is not None:
  868. try:
  869. # Race: Another process might have deleted the file.
  870. mtime = os.path.getmtime(os.path.join(folder, name))
  871. except FileNotFoundError:
  872. continue
  873. if mtime > age_limit:
  874. continue
  875. cls.logger.debug("Found expired item in cache: %r", name)
  876. # Race: Another process might have deleted or locked the
  877. # file.
  878. try:
  879. os.remove(os.path.join(folder, name))
  880. except (FileNotFoundError, PermissionError):
  881. continue
  882. modified = True
  883. if modified:
  884. cls._sync_directory(folder)
  885. def _update_history_etag(self, href, item):
  886. """Updates and retrieves the history etag from the history cache.
  887. The history cache contains a file for each current and deleted item
  888. of the collection. These files contain the etag of the item (empty
  889. string for deleted items) and a history etag, which is a hash over
  890. the previous history etag and the etag separated by "/".
  891. """
  892. history_folder = os.path.join(self._filesystem_path,
  893. ".Radicale.cache", "history")
  894. try:
  895. with open(os.path.join(history_folder, href), "rb") as f:
  896. cache_etag, history_etag = pickle.load(f)
  897. except (FileNotFoundError, pickle.UnpicklingError, ValueError) as e:
  898. if isinstance(e, (pickle.UnpicklingError, ValueError)):
  899. self.logger.warning(
  900. "Failed to load history cache entry %r in %r: %s",
  901. href, self.path, e, exc_info=True)
  902. cache_etag = ""
  903. # Initialize with random data to prevent collisions with cleaned
  904. # expired items.
  905. history_etag = binascii.hexlify(os.urandom(16)).decode("ascii")
  906. etag = item.etag if item else ""
  907. if etag != cache_etag:
  908. self._makedirs_synced(history_folder)
  909. history_etag = get_etag(history_etag + "/" + etag).strip("\"")
  910. try:
  911. # Race: Other processes might have created and locked the file.
  912. with self._atomic_write(os.path.join(history_folder, href),
  913. "wb") as f:
  914. pickle.dump([etag, history_etag], f)
  915. except PermissionError:
  916. pass
  917. return history_etag
  918. def _get_deleted_history_hrefs(self):
  919. """Returns the hrefs of all deleted items that are still in the
  920. history cache."""
  921. history_folder = os.path.join(self._filesystem_path,
  922. ".Radicale.cache", "history")
  923. try:
  924. for href in scandir(history_folder):
  925. if not is_safe_filesystem_path_component(href):
  926. continue
  927. if os.path.isfile(os.path.join(self._filesystem_path, href)):
  928. continue
  929. yield href
  930. except FileNotFoundError:
  931. pass
  932. def _clean_history_cache(self):
  933. # Delete all expired cache entries of deleted items.
  934. history_folder = os.path.join(self._filesystem_path,
  935. ".Radicale.cache", "history")
  936. self._clean_cache(history_folder, self._get_deleted_history_hrefs(),
  937. max_age=self.configuration.getint(
  938. "storage", "max_sync_token_age"))
  939. def sync(self, old_token=None):
  940. # The sync token has the form http://radicale.org/ns/sync/TOKEN_NAME
  941. # where TOKEN_NAME is the md5 hash of all history etags of present and
  942. # past items of the collection.
  943. def check_token_name(token_name):
  944. if len(token_name) != 32:
  945. return False
  946. for c in token_name:
  947. if c not in "0123456789abcdef":
  948. return False
  949. return True
  950. old_token_name = None
  951. if old_token:
  952. # Extract the token name from the sync token
  953. if not old_token.startswith("http://radicale.org/ns/sync/"):
  954. raise ValueError("Malformed token: %r" % old_token)
  955. old_token_name = old_token[len("http://radicale.org/ns/sync/"):]
  956. if not check_token_name(old_token_name):
  957. raise ValueError("Malformed token: %r" % old_token)
  958. # Get the current state and sync-token of the collection.
  959. state = {}
  960. token_name_hash = md5()
  961. # Find the history of all existing and deleted items
  962. for href, item in chain(
  963. ((item.href, item) for item in self.get_all()),
  964. ((href, None) for href in self._get_deleted_history_hrefs())):
  965. history_etag = self._update_history_etag(href, item)
  966. state[href] = history_etag
  967. token_name_hash.update((href + "/" + history_etag).encode("utf-8"))
  968. token_name = token_name_hash.hexdigest()
  969. token = "http://radicale.org/ns/sync/%s" % token_name
  970. if token_name == old_token_name:
  971. # Nothing changed
  972. return token, ()
  973. token_folder = os.path.join(self._filesystem_path,
  974. ".Radicale.cache", "sync-token")
  975. token_path = os.path.join(token_folder, token_name)
  976. old_state = {}
  977. if old_token_name:
  978. # load the old token state
  979. old_token_path = os.path.join(token_folder, old_token_name)
  980. try:
  981. # Race: Another process might have deleted the file.
  982. with open(old_token_path, "rb") as f:
  983. old_state = pickle.load(f)
  984. except (FileNotFoundError, pickle.UnpicklingError,
  985. ValueError) as e:
  986. if isinstance(e, (pickle.UnpicklingError, ValueError)):
  987. self.logger.warning(
  988. "Failed to load stored sync token %r in %r: %s",
  989. old_token_name, self.path, e, exc_info=True)
  990. # Delete the damaged file
  991. try:
  992. os.remove(old_token_path)
  993. except (FileNotFoundError, PermissionError):
  994. pass
  995. raise ValueError("Token not found: %r" % old_token)
  996. # write the new token state or update the modification time of
  997. # existing token state
  998. if not os.path.exists(token_path):
  999. self._makedirs_synced(token_folder)
  1000. try:
  1001. # Race: Other processes might have created and locked the file.
  1002. with self._atomic_write(token_path, "wb") as f:
  1003. pickle.dump(state, f)
  1004. except PermissionError:
  1005. pass
  1006. else:
  1007. # clean up old sync tokens and item cache
  1008. self._clean_cache(token_folder, os.listdir(token_folder),
  1009. max_age=self.configuration.getint(
  1010. "storage", "max_sync_token_age"))
  1011. self._clean_history_cache()
  1012. else:
  1013. # Try to update the modification time
  1014. try:
  1015. # Race: Another process might have deleted the file.
  1016. os.utime(token_path)
  1017. except FileNotFoundError:
  1018. pass
  1019. changes = []
  1020. # Find all new, changed and deleted (that are still in the item cache)
  1021. # items
  1022. for href, history_etag in state.items():
  1023. if history_etag != old_state.get(href):
  1024. changes.append(href)
  1025. # Find all deleted items that are no longer in the item cache
  1026. for href, history_etag in old_state.items():
  1027. if href not in state:
  1028. changes.append(href)
  1029. return token, changes
  1030. def list(self):
  1031. for href in scandir(self._filesystem_path, only_files=True):
  1032. if not is_safe_filesystem_path_component(href):
  1033. if not href.startswith(".Radicale"):
  1034. self.logger.debug(
  1035. "Skipping item %r in %r", href, self.path)
  1036. continue
  1037. yield href
  1038. def get(self, href, verify_href=True):
  1039. item, metadata = self._get_with_metadata(href, verify_href=verify_href)
  1040. return item
  1041. def _item_cache_hash(self, raw_text):
  1042. _hash = md5()
  1043. _hash.update(raw_text)
  1044. return _hash.hexdigest()
  1045. def _item_cache_content(self, href, vobject_item, cache_hash=None):
  1046. text = vobject_item.serialize()
  1047. if cache_hash is None:
  1048. cache_hash = self._item_cache_hash(text.encode(self._encoding))
  1049. etag = get_etag(text)
  1050. uid = get_uid_from_object(vobject_item)
  1051. name = vobject_item.name
  1052. tag, start, end = xmlutils.find_tag_and_time_range(vobject_item)
  1053. return cache_hash, uid, etag, text, name, tag, start, end
  1054. def _store_item_cache(self, href, vobject_item, cache_hash=None):
  1055. cache_folder = os.path.join(self._filesystem_path, ".Radicale.cache",
  1056. "item")
  1057. content = self._item_cache_content(href, vobject_item, cache_hash)
  1058. self._makedirs_synced(cache_folder)
  1059. try:
  1060. # Race: Other processes might have created and locked the
  1061. # file.
  1062. with self._atomic_write(os.path.join(cache_folder, href),
  1063. "wb") as f:
  1064. pickle.dump(content, f)
  1065. except PermissionError:
  1066. pass
  1067. return content
  1068. _cache_locks = {}
  1069. _cache_locks_lock = threading.Lock()
  1070. @contextmanager
  1071. def _acquire_cache_lock(self, ns=""):
  1072. with contextlib.ExitStack() as lock_stack:
  1073. with contextlib.ExitStack() as locks_lock_stack:
  1074. locks_lock_stack.enter_context(self._cache_locks_lock)
  1075. lock_id = ns + "/" + self.path
  1076. lock = self._cache_locks.get(lock_id)
  1077. if not lock:
  1078. cache_folder = os.path.join(self._filesystem_path,
  1079. ".Radicale.cache")
  1080. self._makedirs_synced(cache_folder)
  1081. lock_path = None
  1082. if self.configuration.getboolean(
  1083. "storage", "filesystem_locking"):
  1084. lock_path = os.path.join(
  1085. cache_folder,
  1086. ".Radicale.lock" + (".%s" % ns if ns else ""))
  1087. lock = FileBackedRwLock(lock_path)
  1088. self._cache_locks[lock_id] = lock
  1089. lock_stack.enter_context(lock.acquire_lock(
  1090. "w", lambda: locks_lock_stack.pop_all().close()))
  1091. try:
  1092. yield
  1093. finally:
  1094. with self._cache_locks_lock:
  1095. lock_stack.pop_all().close()
  1096. if not lock.in_use():
  1097. del self._cache_locks[lock_id]
  1098. def _load_item_cache(self, href):
  1099. cache_folder = os.path.join(self._filesystem_path, ".Radicale.cache",
  1100. "item")
  1101. cache_hash = uid = etag = text = name = tag = start = end = None
  1102. try:
  1103. with open(os.path.join(cache_folder, href), "rb") as f:
  1104. cache_hash, uid, etag, text, name, tag, start, end = \
  1105. pickle.load(f)
  1106. except FileNotFoundError as e:
  1107. pass
  1108. except (pickle.UnpicklingError, ValueError) as e:
  1109. self.logger.warning(
  1110. "Failed to load item cache entry %r in %r: %s",
  1111. href, self.path, e, exc_info=True)
  1112. return cache_hash, uid, etag, text, name, tag, start, end
  1113. def _clean_item_cache(self):
  1114. cache_folder = os.path.join(self._filesystem_path, ".Radicale.cache",
  1115. "item")
  1116. self._clean_cache(cache_folder, (
  1117. href for href in scandir(cache_folder) if not
  1118. os.path.isfile(os.path.join(self._filesystem_path, href))))
  1119. def _get_with_metadata(self, href, verify_href=True):
  1120. """Like ``get`` but additonally returns the following metadata:
  1121. tag, start, end: see ``xmlutils.find_tag_and_time_range``. If
  1122. extraction of the metadata failed, the values are all ``None``."""
  1123. if verify_href:
  1124. try:
  1125. if not is_safe_filesystem_path_component(href):
  1126. raise UnsafePathError(href)
  1127. path = path_to_filesystem(self._filesystem_path, href)
  1128. except ValueError as e:
  1129. self.logger.debug(
  1130. "Can't translate name %r safely to filesystem in %r: %s",
  1131. href, self.path, e, exc_info=True)
  1132. return None, None
  1133. else:
  1134. path = os.path.join(self._filesystem_path, href)
  1135. try:
  1136. with open(path, "rb") as f:
  1137. raw_text = f.read()
  1138. except (FileNotFoundError, IsADirectoryError):
  1139. return None, None
  1140. # The hash of the component in the file system. This is used to check,
  1141. # if the entry in the cache is still valid.
  1142. input_hash = self._item_cache_hash(raw_text)
  1143. cache_hash, uid, etag, text, name, tag, start, end = \
  1144. self._load_item_cache(href)
  1145. vobject_item = None
  1146. if input_hash != cache_hash:
  1147. with contextlib.ExitStack() as lock_stack:
  1148. # Lock the item cache to prevent multpile processes from
  1149. # generating the same data in parallel.
  1150. # This improves the performance for multiple requests.
  1151. if self._lock.locked() == "r":
  1152. lock_stack.enter_context(self._acquire_cache_lock("item"))
  1153. # Check if another process created the file in the meantime
  1154. cache_hash, uid, etag, text, name, tag, start, end = \
  1155. self._load_item_cache(href)
  1156. if input_hash != cache_hash:
  1157. try:
  1158. vobject_items = tuple(vobject.readComponents(
  1159. raw_text.decode(self._encoding)))
  1160. if len(vobject_items) != 1:
  1161. raise RuntimeError("Content contains %d components"
  1162. % len(vobject_items))
  1163. vobject_item = vobject_items[0]
  1164. check_and_sanitize_item(vobject_item, uid=uid,
  1165. tag=self.get_meta("tag"))
  1166. cache_hash, uid, etag, text, name, tag, start, end = \
  1167. self._store_item_cache(
  1168. href, vobject_item, input_hash)
  1169. except Exception as e:
  1170. raise RuntimeError("Failed to load item %r in %r: %s" %
  1171. (href, self.path, e)) from e
  1172. # Clean cache entries once after the data in the file
  1173. # system was edited externally.
  1174. if not self._item_cache_cleaned:
  1175. self._item_cache_cleaned = True
  1176. self._clean_item_cache()
  1177. last_modified = time.strftime(
  1178. "%a, %d %b %Y %H:%M:%S GMT",
  1179. time.gmtime(os.path.getmtime(path)))
  1180. return Item(
  1181. self, href=href, last_modified=last_modified, etag=etag,
  1182. text=text, item=vobject_item, uid=uid, name=name,
  1183. component_name=tag), (tag, start, end)
  1184. def get_multi2(self, hrefs):
  1185. # It's faster to check for file name collissions here, because
  1186. # we only need to call os.listdir once.
  1187. files = None
  1188. for href in hrefs:
  1189. if files is None:
  1190. # List dir after hrefs returned one item, the iterator may be
  1191. # empty and the for-loop is never executed.
  1192. files = os.listdir(self._filesystem_path)
  1193. path = os.path.join(self._filesystem_path, href)
  1194. if (not is_safe_filesystem_path_component(href) or
  1195. href not in files and os.path.lexists(path)):
  1196. self.logger.debug(
  1197. "Can't translate name safely to filesystem: %r", href)
  1198. yield (href, None)
  1199. else:
  1200. yield (href, self.get(href, verify_href=False))
  1201. def get_all(self):
  1202. # We don't need to check for collissions, because the the file names
  1203. # are from os.listdir.
  1204. return (self.get(href, verify_href=False) for href in self.list())
  1205. def get_all_filtered(self, filters):
  1206. tag, start, end, simple = xmlutils.simplify_prefilters(filters)
  1207. if not tag:
  1208. # no filter
  1209. yield from ((item, simple) for item in self.get_all())
  1210. return
  1211. for item, (itag, istart, iend) in (
  1212. self._get_with_metadata(href, verify_href=False)
  1213. for href in self.list()):
  1214. if tag == itag and istart < end and iend > start:
  1215. yield item, simple and (start <= istart or iend <= end)
  1216. def upload(self, href, vobject_item):
  1217. if not is_safe_filesystem_path_component(href):
  1218. raise UnsafePathError(href)
  1219. try:
  1220. cache_hash, uid, etag, text, name, tag, _, _ = \
  1221. self._store_item_cache(href, vobject_item)
  1222. except Exception as e:
  1223. raise ValueError("Failed to store item %r in collection %r: %s" %
  1224. (href, self.path, e)) from e
  1225. path = path_to_filesystem(self._filesystem_path, href)
  1226. with self._atomic_write(path, newline="") as fd:
  1227. fd.write(text)
  1228. # Clean the cache after the actual item is stored, or the cache entry
  1229. # will be removed again.
  1230. self._clean_item_cache()
  1231. item = Item(self, href=href, etag=etag, text=text, item=vobject_item,
  1232. uid=uid, name=name, component_name=tag)
  1233. # Track the change
  1234. self._update_history_etag(href, item)
  1235. self._clean_history_cache()
  1236. return item
  1237. def delete(self, href=None):
  1238. if href is None:
  1239. # Delete the collection
  1240. parent_dir = os.path.dirname(self._filesystem_path)
  1241. try:
  1242. os.rmdir(self._filesystem_path)
  1243. except OSError:
  1244. with TemporaryDirectory(
  1245. prefix=".Radicale.tmp-", dir=parent_dir) as tmp:
  1246. os.rename(self._filesystem_path, os.path.join(
  1247. tmp, os.path.basename(self._filesystem_path)))
  1248. self._sync_directory(parent_dir)
  1249. else:
  1250. self._sync_directory(parent_dir)
  1251. else:
  1252. # Delete an item
  1253. if not is_safe_filesystem_path_component(href):
  1254. raise UnsafePathError(href)
  1255. path = path_to_filesystem(self._filesystem_path, href)
  1256. if not os.path.isfile(path):
  1257. raise ComponentNotFoundError(href)
  1258. os.remove(path)
  1259. self._sync_directory(os.path.dirname(path))
  1260. # Track the change
  1261. self._update_history_etag(href, None)
  1262. self._clean_history_cache()
  1263. def get_meta(self, key=None):
  1264. # reuse cached value if the storage is read-only
  1265. if self._lock.locked() == "w" or self._meta_cache is None:
  1266. try:
  1267. try:
  1268. with open(self._props_path, encoding=self._encoding) as f:
  1269. self._meta_cache = json.load(f)
  1270. except FileNotFoundError:
  1271. self._meta_cache = {}
  1272. check_and_sanitize_props(self._meta_cache)
  1273. except ValueError as e:
  1274. raise RuntimeError("Failed to load properties of collection "
  1275. "%r: %s" % (self.path, e)) from e
  1276. return self._meta_cache.get(key) if key else self._meta_cache
  1277. def set_meta_all(self, props):
  1278. with self._atomic_write(self._props_path, "w") as f:
  1279. json.dump(props, f, sort_keys=True)
  1280. @property
  1281. def last_modified(self):
  1282. relevant_files = chain(
  1283. (self._filesystem_path,),
  1284. (self._props_path,) if os.path.exists(self._props_path) else (),
  1285. (os.path.join(self._filesystem_path, h) for h in self.list()))
  1286. last = max(map(os.path.getmtime, relevant_files))
  1287. return time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(last))
  1288. @property
  1289. def etag(self):
  1290. # reuse cached value if the storage is read-only
  1291. if self._lock.locked() == "w" or self._etag_cache is None:
  1292. self._etag_cache = super().etag
  1293. return self._etag_cache
  1294. _lock = None
  1295. @classmethod
  1296. @contextmanager
  1297. def acquire_lock(cls, mode, user=None):
  1298. folder = os.path.expanduser(cls.configuration.get(
  1299. "storage", "filesystem_folder"))
  1300. if not cls._lock:
  1301. cls._makedirs_synced(folder)
  1302. lock_path = None
  1303. if cls.configuration.getboolean("storage", "filesystem_locking"):
  1304. lock_path = os.path.join(folder, ".Radicale.lock")
  1305. close_lock_file = cls.configuration.getboolean(
  1306. "storage", "filesystem_close_lock_file")
  1307. cls._lock = FileBackedRwLock(lock_path, close_lock_file)
  1308. with cls._lock.acquire_lock(mode):
  1309. yield
  1310. # execute hook
  1311. hook = cls.configuration.get("storage", "hook")
  1312. if mode == "w" and hook:
  1313. cls.logger.debug("Running hook")
  1314. subprocess.check_call(
  1315. hook % {"user": shlex.quote(user or "Anonymous")},
  1316. shell=True, cwd=folder)
  1317. class FileBackedRwLock:
  1318. """A readers-Writer lock that can additionally lock a file.
  1319. All requests are processed in FIFO order.
  1320. """
  1321. def __init__(self, path=None, close_lock_file=True):
  1322. """Initilize a lock.
  1323. ``path`` the file that is used for locking (optional)
  1324. ``close_lock_file`` close the lock file, when unlocked and no requests
  1325. are pending
  1326. """
  1327. self._path = path
  1328. self._close_lock_file = close_lock_file
  1329. self._lock = threading.Lock()
  1330. self._waiters = []
  1331. self._lock_file = None
  1332. self._lock_file_locked = False
  1333. self._readers = 0
  1334. self._writer = False
  1335. def locked(self):
  1336. if self._writer:
  1337. return "w"
  1338. if self._readers:
  1339. return "r"
  1340. return ""
  1341. def in_use(self):
  1342. with self._lock:
  1343. return self._waiters or self._readers or self._writer
  1344. @contextmanager
  1345. def acquire_lock(self, mode, sync_callback=None):
  1346. def condition():
  1347. if mode == "r":
  1348. return not self._writer
  1349. else:
  1350. return not self._writer and self._readers == 0
  1351. # Use a primitive lock which only works within one process as a
  1352. # precondition for inter-process file-based locking
  1353. with self._lock:
  1354. if sync_callback:
  1355. sync_callback()
  1356. if self._waiters or not condition():
  1357. # Use FIFO for access requests
  1358. waiter = threading.Condition(lock=self._lock)
  1359. self._waiters.append(waiter)
  1360. while True:
  1361. waiter.wait()
  1362. if condition():
  1363. break
  1364. self._waiters.pop(0)
  1365. if mode == "r":
  1366. self._readers += 1
  1367. # Notify additional potential readers
  1368. if self._waiters:
  1369. self._waiters[0].notify()
  1370. else:
  1371. self._writer = True
  1372. if self._path and not self._lock_file_locked:
  1373. if not self._lock_file:
  1374. self._lock_file = open(self._path, "w+")
  1375. if os.name == "nt":
  1376. handle = msvcrt.get_osfhandle(self._lock_file.fileno())
  1377. flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
  1378. overlapped = Overlapped()
  1379. if not lock_file_ex(handle, flags, 0, 1, 0, overlapped):
  1380. raise RuntimeError("Locking the storage failed "
  1381. "(can be disabled in the config): "
  1382. "%s" % ctypes.FormatError())
  1383. elif os.name == "posix":
  1384. _cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH
  1385. try:
  1386. fcntl.flock(self._lock_file.fileno(), _cmd)
  1387. except OSError as e:
  1388. raise RuntimeError("Locking the storage failed "
  1389. "(can be disabled in the config): "
  1390. "%s" % e) from e
  1391. else:
  1392. raise RuntimeError("Locking the storage failed "
  1393. "(can be disabled in the config): "
  1394. "Unsupported operating system")
  1395. self._lock_file_locked = True
  1396. try:
  1397. yield
  1398. finally:
  1399. with self._lock:
  1400. if mode == "r":
  1401. self._readers -= 1
  1402. else:
  1403. self._writer = False
  1404. if self._lock_file_locked and self._readers == 0:
  1405. if os.name == "nt":
  1406. handle = msvcrt.get_osfhandle(self._lock_file.fileno())
  1407. overlapped = Overlapped()
  1408. if not unlock_file_ex(handle, 0, 1, 0, overlapped):
  1409. raise RuntimeError("Unlocking the storage failed: "
  1410. "%s" % ctypes.FormatError())
  1411. elif os.name == "posix":
  1412. try:
  1413. fcntl.flock(self._lock_file.fileno(),
  1414. fcntl.LOCK_UN)
  1415. except OSError as e:
  1416. raise RuntimeError("Unlocking the storage failed: "
  1417. "%s" % e) from e
  1418. else:
  1419. raise RuntimeError("Unlocking the storage failed: "
  1420. "Unsupported operating system")
  1421. if self._close_lock_file and not self._waiters:
  1422. self._lock_file.close()
  1423. self._lock_file = None
  1424. self._lock_file_locked = False
  1425. if self._waiters:
  1426. self._waiters[0].notify()