Skip to content

Commit

Permalink
Fix filelock: use current umask for filelock >= 3.10 (#6631)
Browse files Browse the repository at this point in the history
fix filelock umask
  • Loading branch information
lhoestq authored Jan 30, 2024
1 parent 3b21d74 commit 237a2a6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/datasets/utils/_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@

from filelock import FileLock as FileLock_
from filelock import UnixFileLock
from filelock import __version__ as _filelock_version
from packaging import version


class FileLock(FileLock_):
"""
A `filelock.FileLock` initializer that handles long paths.
It also uses the current umask for lock files.
"""

MAX_FILENAME_LENGTH = 255

def __init__(self, lock_file, *args, **kwargs):
# The "mode" argument is required if we want to use the current umask in filelock >= 3.10
# In previous previous it was already using the current umask.
if "mode" not in kwargs and version.parse(_filelock_version) >= version.parse("3.10.0"):
umask = os.umask(0o666)
os.umask(umask)
kwargs["mode"] = 0o666 & ~umask
lock_file = self.hash_filename_if_too_long(lock_file)
super().__init__(lock_file, *args, **kwargs)

@classmethod
def hash_filename_if_too_long(cls, path: str) -> str:
path = os.path.abspath(os.path.expanduser(path))
filename = os.path.basename(path)
max_filename_length = cls.MAX_FILENAME_LENGTH
if issubclass(cls, UnixFileLock):
Expand Down

0 comments on commit 237a2a6

Please sign in to comment.