From 9f4b67cec4d3a075c8fa94e507ccad6d80366667 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 29 Sep 2023 00:10:44 +0300 Subject: [PATCH] site_cache_dir: use .dvc/tmp/btime and fallback to st_birthtime This will ensure that we won't be using index cache if you delete your repo and start/clone fresh. --- dvc/repo/__init__.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 1ae65b006..fca9ecc5b 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -577,6 +577,26 @@ class Repo: return DVCFileSystem(repo=self, subrepos=self.subrepos, **self._fs_conf) + @cached_property + def _btime(self): + if not self.tmp_dir: + return None + + # Not all python versions/filesystems/platforms provide creation + # time (st_birthtime, stx_btime, etc), so we use our own dummy + # file and its mtime instead. + path = os.path.join(self.tmp_dir, "btime") + + try: + with open(path, "x"): # pylint: disable=unspecified-encoding + pass + except FileNotFoundError: + return None + except FileExistsError: + pass + + return os.path.getmtime(path) + @cached_property def site_cache_dir(self) -> str: import getpass @@ -611,10 +631,15 @@ class Repo: # of the token components are changed. salt = 0 + # NOTE: This helps us avoid accidentally reusing cache for repositories + # that just happened to be at the same path as old deleted ones. + btime = self._btime or getattr(os.stat(root_dir), "st_birthtime", None) + md5 = hashlib.md5( # noqa: S324 # nosec B324, B303 str( ( root_dir, + btime, getpass.getuser(), version_tuple[0], salt,