Fixed permissions issue with cache file. #2084

This commit is contained in:
morpheus65535 2023-03-12 19:51:04 -04:00
parent 587af7d138
commit fb6ac47bea
4 changed files with 29 additions and 12 deletions

View file

@ -40,7 +40,7 @@ class FileCache(MutableMapping):
:param str appname: The app/script the cache should be associated with.
:param str flag: How the cache should be opened. See below for details.
:param mode: The Unix mode for the cache files.
:param mode: The Unix mode for the cache files or False to prevent changing permissions.
:param str keyencoding: The encoding the keys use, defaults to 'utf-8'.
This is used if *serialize* is ``False``; the keys are treated as
:class:`bytes` objects.
@ -247,7 +247,8 @@ class FileCache(MutableMapping):
with os.fdopen(fh, self._flag) as f:
f.write(self._dumps(bytesvalue))
rename(tmp, filename)
os.chmod(filename, self._mode)
if self._mode:
os.chmod(filename, self._mode)
def _read_from_file(self, filename):
"""Read data from filename."""
@ -256,7 +257,7 @@ class FileCache(MutableMapping):
return self._loads(f.read())
except (IOError, OSError):
logger.warning('Error opening file: {}'.format(filename))
raise
return None
def __setitem__(self, key, value):
ekey = self._encode_key(key)

View file

@ -46,7 +46,15 @@ if os.name == 'nt': # pragma: no cover
dst = unicode(dst, sys.getfilesystemencoding())
if _rename_atomic(src, dst):
return True
return _MoveFileEx(src, dst, _MOVEFILE_REPLACE_EXISTING | _MOVEFILE_WRITE_THROUGH)
retry = 0
rv = False
while not rv and retry < 100:
rv = _MoveFileEx(src, dst, _MOVEFILE_REPLACE_EXISTING |
_MOVEFILE_WRITE_THROUGH)
if not rv:
time.sleep(0.001)
retry += 1
return rv
# new in Vista and Windows Server 2008
_CreateTransaction = ctypes.windll.ktmw32.CreateTransaction
@ -60,11 +68,18 @@ if os.name == 'nt': # pragma: no cover
if ta == -1:
return False
try:
rv = _MoveFileTransacted(src, dst, None, None,
_MOVEFILE_REPLACE_EXISTING |
_MOVEFILE_WRITE_THROUGH, ta)
if rv:
rv = _CommitTransaction(ta)
retry = 0
rv = False
while not rv and retry < 100:
rv = _MoveFileTransacted(src, dst, None, None,
_MOVEFILE_REPLACE_EXISTING |
_MOVEFILE_WRITE_THROUGH, ta)
if rv:
rv = _CommitTransaction(ta)
break
else:
time.sleep(0.001)
retry += 1
return rv
finally:
_CloseHandle(ta)
@ -77,7 +92,7 @@ if os.name == 'nt': # pragma: no cover
return
# Fall back to "move away and replace"
try:
shutil.move(src, dst)
os.rename(src, dst)
except OSError as e:
if e.errno != errno.EEXIST:
raise

View file

@ -8,7 +8,8 @@ class SZFileBackend(CacheBackend):
def __init__(self, arguments):
self._cache = FileCache(arguments.pop("appname", None), flag=arguments.pop("flag", "c"),
serialize=arguments.pop("serialize", True),
app_cache_dir=arguments.pop("app_cache_dir", None))
app_cache_dir=arguments.pop("app_cache_dir", None),
mode=False)
def get(self, key):
value = self._cache.get(key, NO_VALUE)

View file

@ -128,7 +128,7 @@ webencodings==0.5.1
# Required-by: subzero
backports.functools-lru-cache==1.6.4
fcache==0.4.7
fcache==0.4.7 # https://github.com/tsroten/fcache/pull/34
json_tricks==3.16.1
tld==0.12.6 # https://github.com/barseghyanartur/tld/pull/119