pyfilesystem2 icon indicating copy to clipboard operation
pyfilesystem2 copied to clipboard

FTPFS, why don't use the rename directive (RNRF RNTO) to implement the move method

Open CDU-Ge opened this issue 1 year ago • 1 comments

I've noticed that in FTPFS, the implementation of move comes from FS, however, when moving in FTPFS, it's too slow, especially when renaming.

CDU-Ge avatar Dec 03 '23 07:12 CDU-Ge

for example:

import fs
from fs.ftpfs import FTPFS as _FTPFS
from fs.ftpfs import ftp_errors


class NotSupportedError(Exception):
    pass


class FTPFS(_FTPFS):
    def move(self, src_path: str, dst_path: str, overwrite=False, preserve_time=False):
        try:
            with self._lock:
                _path = self.validatepath(dst_path)
                if not overwrite and self.exists(_path):
                    raise fs.errors.DestinationExists(_path)
                with ftp_errors(self, _path):
                    try:
                        if overwrite:
                            with ftp_errors(self, _path):
                                if self.exists(_path):
                                    self.ftp.rename(_path, _path + ".pyfilesystem2.tmp")
                                    self.ftp.rename(src_path, dst_path)
                                    self.ftp.delete(_path + ".pyfilesystem2.tmp")
                        else:
                            self.ftp.rename(src_path, dst_path)
                    except ftplib.error_perm as e:
                        if str(e) == "550 RNTO failed.":  # some ftp server does not support rename to other directory
                            raise NotSupportedError()
                        raise e
        except NotSupportedError:
            super().move(src_path, dst_path, overwrite, preserve_time)

CDU-Ge avatar Dec 03 '23 09:12 CDU-Ge