Skip to content

Commit 1a67289

Browse files
committed
fix: permissions of the region provider cache file
1 parent 132c9d2 commit 1a67289

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

qiniu/http/regions_provider.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ def _lock_file_path(self):
300300
memo_cache={},
301301
persist_path=os.path.join(
302302
tempfile.gettempdir(),
303-
'qn-py-sdk-regions-cache.jsonl'
303+
'qn-py-sdk',
304+
'regions-cache.jsonl'
304305
),
305306
last_shrink_at=datetime.datetime.fromtimestamp(0),
306307
shrink_interval=datetime.timedelta(days=1),
@@ -520,8 +521,23 @@ def __init__(
520521
persist_path = kwargs.get('persist_path', None)
521522
last_shrink_at = datetime.datetime.fromtimestamp(0)
522523
if persist_path is None:
523-
persist_path = _global_cache_scope.persist_path
524-
last_shrink_at = _global_cache_scope.last_shrink_at
524+
cache_dir = os.path.dirname(_global_cache_scope.persist_path)
525+
try:
526+
# make sure the cache dir is available for all users.
527+
# we can not use the '/tmp' dir directly on linux,
528+
# because the permission is 0o1777
529+
if os.path.exists(cache_dir):
530+
# os.makedirs have no exists_ok parameter in python 2.7
531+
os.makedirs(cache_dir, mode=0o777)
532+
persist_path = _global_cache_scope.persist_path
533+
last_shrink_at = _global_cache_scope.last_shrink_at
534+
except Exception as err:
535+
if "File exists" in str(err):
536+
persist_path = _global_cache_scope.persist_path
537+
last_shrink_at = _global_cache_scope.last_shrink_at
538+
else:
539+
logging.warning(
540+
'failed to create cache dir %s. error: %s', cache_dir, err)
525541

526542
shrink_interval = kwargs.get('shrink_interval', None)
527543
if shrink_interval is None:

0 commit comments

Comments
 (0)