#150 Invalidate cache in rename method 6 months ago

Comment by ~dolfinus on ~sschwarzer/ftputil

~sschwarzer I've checked changes you made, it's working now. Thanks!

#150 Invalidate cache in rename method 7 months ago

Ticket created by ~dolfinus on ~sschwarzer/ftputil

Python 3.7.2 ftputil 5.0.2

I'm renaming an existing file, and then calling .path.exists(path) do check if old file does not exist. But exists and other methods, like stat, returns cached data about already deleted file:

ftpHost.path.exists("/ftp/file.tmp")  # False
ftpHost.upload("/my/local/file", "/ftp/file.tmp")
ftpHost.path.exists("/ftp/file.tmp")  # True
ftpHost.rename("/ftp/file.tmp", "/ftp/file")
ftpHostt.path.exists("/ftp/file")  # True

This is because rename method does not invalidate the cache:

--- a/ftputil/host.py
+++ b/ftputil/host.py
@@ -892,6 +892,8 @@ class FTPHost:
         source_head, source_tail = self.path.split(source)
+        source_absolute_path = self.path.abspath(source)
         target_head, target_tail = self.path.split(target)
+        target_absolute_path = self.path.abspath(target)
         paths_contain_whitespace = (" " in source_head) or (" " in target_head)
         if paths_contain_whitespace and source_head == target_head:
             # Both items are in the same directory.
@@ -906,6 +908,8 @@ class FTPHost:
             # Use straightforward command.
             with ftputil.error.ftplib_error_to_ftp_os_error:
                 self._session.rename(source, target)
+        self.stat_cache.invalidate(source_absolute_path)
+        self.stat_cache.invalidate(target_absolute_path)
     # XXX: One could argue to put this method into the `_Stat` class, but I
     # refrained from that because then `_Stat` would have to know about