sharing/csv: improve locking

This commit is contained in:
Peter Bieringer
2026-03-07 08:06:19 +01:00
parent b22bc3f327
commit 29ddf28d0b

View File

@@ -30,7 +30,7 @@ class Sharing(sharing.BaseSharing):
_sharing_cache: list[dict] = []
_sharing_db_file: str
# Overloaded functions
# *** Overloaded functions ***
def database_init(self) -> bool:
logger.debug("sharing database initialization for type 'csv'")
sharing_db_file = self.configuration.get("sharing", "database_path")
@@ -158,6 +158,7 @@ class Sharing(sharing.BaseSharing):
index = 0
result = []
with self._storage.acquire_lock("r", path=self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/list/called: ShareType=%r OwnerOrUser=%r User=%r PathOrToken=%r PathMapped=%r EnabledByOwner=%s EnabledByUser=%s HiddenByOwner=%s HiddenByUser=%s", ShareType, OwnerOrUser, User, PathOrToken, PathMapped, EnabledByOwner, EnabledByUser, HiddenByOwner, HiddenByUser)
@@ -213,6 +214,7 @@ class Sharing(sharing.BaseSharing):
""" create sharing """
row: dict
with self._storage.acquire_lock("w", path=self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing: ShareType=%r", ShareType)
if ShareType == "token":
@@ -256,11 +258,11 @@ class Sharing(sharing.BaseSharing):
logger.debug("TRACE/sharing/*/create: add row: %r", row)
self._sharing_cache.append(row)
with self._storage.acquire_lock("w", Owner, path=self._sharing_db_file):
if self._write_csv(self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/create: write CSV done", ShareType)
return {"status": "success"}
logger.error("sharing/%s/create: cannot update CSV database", ShareType)
return {"status": "error"}
@@ -281,6 +283,7 @@ class Sharing(sharing.BaseSharing):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/update: PathOrToken=%r OwnerOrUser=%r PathMapped=%r Properties=%r EnabledByOwner=%s EnabledByUser=%s HiddenByOwner=%s HiddenByUser=%s", ShareType, PathOrToken, OwnerOrUser, PathMapped, Properties, EnabledByOwner, EnabledByUser, HiddenByOwner, HiddenByUser)
with self._storage.acquire_lock("w", path=self._sharing_db_file):
# lookup token
found = False
index = 0
@@ -299,39 +302,36 @@ class Sharing(sharing.BaseSharing):
if found:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/update: found index=%d", ShareType, index)
logger.debug("TRACE/sharing/%s/update: orig row[%d]=%r", ShareType, index, row)
# CSV: remove+adjust+readd
if PathMapped is not None:
row["PathMapped"] = PathMapped
self._sharing_cache[index]["PathMapped"] = PathMapped
if Permissions is not None:
row["Permissions"] = Permissions
self._sharing_cache[index]["Permissions"] = Permissions
if User is not None:
row["User"] = User
self._sharing_cache[index]["User"] = User
if EnabledByOwner is not None:
row["EnabledByOwner"] = EnabledByOwner
self._sharing_cache[index]["EnabledByOwner"] = EnabledByOwner
if EnabledByUser is not None:
row["EnabledByUser"] = EnabledByUser
self._sharing_cache[index]["EnabledByUser"] = EnabledByUser
if HiddenByOwner is not None:
row["HiddenByOwner"] = HiddenByOwner
self._sharing_cache[index]["HiddenByOwner"] = HiddenByOwner
if HiddenByUser is not None:
row["HiddenByUser"] = HiddenByUser
self._sharing_cache[index]["HiddenByUser"] = HiddenByUser
if Properties is not None:
row["Properties"] = Properties
self._sharing_cache[index]["Properties"] = Properties
# update timestamp
row["TimestampUpdated"] = Timestamp
self._sharing_cache[index]["TimestampUpdated"] = Timestamp
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/update: adj row[%d]=%r", ShareType, index, row)
logger.debug("TRACE/sharing/%s/update: adj row[%d]=%r", ShareType, index, self._sharing_cache[index])
# replace row
self._sharing_cache[index] = row
with self._storage.acquire_lock("w", OwnerOrUser, path=self._sharing_db_file):
if self._write_csv(self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/update: write CSV done", ShareType)
return {"status": "success"}
logger.error("sharing/%s/update: cannot update CSV database", ShareType)
return {"status": "error"}
else:
@@ -344,6 +344,7 @@ class Sharing(sharing.BaseSharing):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/delete: PathOrToken=%r", ShareType, PathOrToken)
with self._storage.acquire_lock("w", path=self._sharing_db_file):
# lookup token
found = False
index = 0
@@ -369,17 +370,17 @@ class Sharing(sharing.BaseSharing):
logger.debug("TRACE/sharing/%s/delete: PathOrToken=%r Owner=%r index=%d", ShareType, PathOrToken, row['Owner'], index)
self._sharing_cache.pop(index)
with self._storage.acquire_lock("w", row['Owner'], path=self._sharing_db_file):
if self._write_csv(self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing_by_token: write CSV done")
return {"status": "success"}
logger.error("sharing/%s/delete: cannot update CSV database", ShareType)
return {"status": "error"}
else:
return {"status": "not-found"}
# local functions
# *** local functions ***
def _create_empty_csv(self, file: str) -> bool:
with self._storage.acquire_lock("w", None, path=file):
with open(file, 'w', newline='') as csvfile: