Skip to content

Commit 322adec

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

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

qiniu/http/regions_provider.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import abc
22
import datetime
3+
import errno
34
import itertools
45
from collections import namedtuple
56
import logging
@@ -300,7 +301,8 @@ def _lock_file_path(self):
300301
memo_cache={},
301302
persist_path=os.path.join(
302303
tempfile.gettempdir(),
303-
'qn-py-sdk-regions-cache.jsonl'
304+
'qn-py-sdk',
305+
'regions-cache.jsonl'
304306
),
305307
last_shrink_at=datetime.datetime.fromtimestamp(0),
306308
shrink_interval=datetime.timedelta(days=1),
@@ -520,8 +522,24 @@ def __init__(
520522
persist_path = kwargs.get('persist_path', None)
521523
last_shrink_at = datetime.datetime.fromtimestamp(0)
522524
if persist_path is None:
523-
persist_path = _global_cache_scope.persist_path
524-
last_shrink_at = _global_cache_scope.last_shrink_at
525+
cache_dir = os.path.dirname(_global_cache_scope.persist_path)
526+
try:
527+
# make sure the cache dir is available for all users.
528+
# we can not use the '/tmp' dir directly on linux,
529+
# because the permission is 0o1777
530+
if not os.path.exists(cache_dir):
531+
# os.makedirs have no exists_ok parameter in python 2.7
532+
os.makedirs(cache_dir)
533+
os.chmod(cache_dir, 0o777)
534+
persist_path = _global_cache_scope.persist_path
535+
last_shrink_at = _global_cache_scope.last_shrink_at
536+
except Exception as err:
537+
if isinstance(err, OSError) and err.errno == errno.EEXIST:
538+
persist_path = _global_cache_scope.persist_path
539+
last_shrink_at = _global_cache_scope.last_shrink_at
540+
else:
541+
logging.warning(
542+
'failed to create cache dir %s. error: %s', cache_dir, err)
525543

526544
shrink_interval = kwargs.get('shrink_interval', None)
527545
if shrink_interval is None:

0 commit comments

Comments
 (0)