From 898d367ddff01282ab0808a66e7d5fe086272b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20K=C3=B6ster?= Date: Thu, 4 Jul 2024 16:41:27 +0200 Subject: [PATCH] fix: fix touch and retrieval of mtime for directories (#20) --- snakemake_storage_plugin_fs/__init__.py | 26 ++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/snakemake_storage_plugin_fs/__init__.py b/snakemake_storage_plugin_fs/__init__.py index cb4d189..45a8a7e 100644 --- a/snakemake_storage_plugin_fs/__init__.py +++ b/snakemake_storage_plugin_fs/__init__.py @@ -160,7 +160,7 @@ async def inventory(self, cache: IOCacheStorageInterface): lstat = self._stat(follow_symlinks=False) else: lstat = stat - cache.mtime[key] = Mtime(storage=lstat.st_mtime) + cache.mtime[key] = Mtime(storage=self._stat_to_mtime(lstat)) cache.size[key] = stat.st_size cache.exists_in_storage[key] = True @@ -191,7 +191,7 @@ def exists(self) -> bool: def mtime(self) -> float: # return the modification time - return self._stat(follow_symlinks=False).st_mtime + return self._stat_to_mtime(self._stat(follow_symlinks=False)) def size(self) -> int: # return the size in bytes @@ -253,11 +253,23 @@ def _stat(self, follow_symlinks: bool = True): def touch(self): if self.query_path.exists(): if self.query_path.is_dir(): - flag = self.query_path / ".snakemake_timestamp" - # Create the flag file if it doesn't exist - if not flag.exists(): - with open(flag, "w"): + timestamp = self._timestamp_path + # Create the timestamp file if it doesn't exist + if not timestamp.exists(): + with open(timestamp, "w"): pass - lutime(flag, None) + lutime(timestamp, None) else: lutime(str(self.query_path), None) + + @property + def _timestamp_path(self): + return self.query_path / ".snakemake_timestamp" + + def _stat_to_mtime(self, stat): + if self.query_path.is_dir(): + # use the timestamp file if possible + timestamp = self._timestamp_path + if timestamp.exists(): + return os.stat(timestamp, follow_symlinks=False).st_mtime + return stat.st_mtime