Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit c754070

Browse files
committed
Lock also for reading.
Moved the construction of fname. Allow for reference to issuer name in fdir specification.
1 parent fc22fc8 commit c754070

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/oidcmsg/storage/abfile.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import time
4+
from urllib.parse import quote_plus
45

56
from filelock import FileLock
67

@@ -23,7 +24,7 @@ class AbstractFileSystem(Storage):
2324
Not directories in directories.
2425
"""
2526

26-
def __init__(self, conf_dict):
27+
def __init__(self, conf_dict, ):
2728
"""
2829
items = FileSystem(
2930
{
@@ -44,15 +45,12 @@ def __init__(self, conf_dict):
4445

4546
super().__init__(conf_dict)
4647
self.config = conf_dict
47-
_fdir = conf_dict.get('fdir', '')
48-
if '{issuer}' in _fdir:
49-
issuer = conf_dict.get('issuer')
50-
if not issuer:
51-
raise ValueError('Missing issuer value')
52-
self.fdir = _fdir.format(issuer=issuer)
53-
else:
54-
self.fdir = _fdir
5548

49+
_fdir = conf_dict.get('fdir', '.')
50+
if "issuer" in conf_dict:
51+
_fdir = os.path.join(_fdir, quote_plus(conf_dict["issuer"]))
52+
53+
self.fdir = _fdir
5654
self.fmtime = {}
5755
self.storage = {}
5856

@@ -87,11 +85,13 @@ def __getitem__(self, item):
8785
:return:
8886
"""
8987
item = self.key_conv.serialize(item)
90-
if self._is_file(item):
91-
if self.is_changed(item):
92-
logger.info("File content change in {}".format(item))
93-
fname = os.path.join(self.fdir, item)
94-
self.storage[item] = self._read_info(fname)
88+
fname = os.path.join(self.fdir, item)
89+
if self._is_file(fname):
90+
lock = FileLock('{}.lock'.format(fname))
91+
with lock:
92+
if self.is_changed(item, fname):
93+
logger.info("File content change in {}".format(item))
94+
self.storage[item] = self._read_info(fname)
9595

9696
logger.debug('Read from "%s"', item)
9797
return self.storage[item]
@@ -165,19 +165,17 @@ def get_mtime(fname):
165165

166166
return mtime
167167

168-
def _is_file(self, item):
169-
fname = os.path.join(self.fdir, item)
168+
def _is_file(self, fname):
170169
return os.path.isfile(fname)
171170

172-
def is_changed(self, item):
171+
def is_changed(self, item, fname):
173172
"""
174173
Find out if this item has been modified since last.
175174
When I get here I know that item points to an existing file.
176175
177176
:param item: A key
178177
:return: True/False
179178
"""
180-
fname = os.path.join(self.fdir, item)
181179
mtime = self.get_mtime(fname)
182180

183181
try:
@@ -223,7 +221,7 @@ def synch(self):
223221
continue
224222

225223
if f in self.fmtime:
226-
if self.is_changed(f):
224+
if self.is_changed(f, fname):
227225
self.storage[f] = self._read_info(fname)
228226
else:
229227
mtime = self.get_mtime(fname)

0 commit comments

Comments
 (0)