Skip to content

Commit db80988

Browse files
author
atollk
committed
Added support for MDTM command in FTPFS.getmodified.
1 parent 9654592 commit db80988

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2525
test suites.
2626
- `FSTestCases` now builds the large data required for `upload` and `download` tests only
2727
once in order to reduce the total testing time.
28+
- FTP servers that do not support the MLST command now try to use the MDTM command to
29+
retrieve the last modification timestamp of a resource.
30+
Closes [#456](https://github.com/PyFilesystem/pyfilesystem2/pull/456).
2831

2932
### Fixed
3033

fs/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import six
2323

2424
from . import copy, errors, fsencode, iotools, move, tools, walk, wildcard
25+
from .errors import Unsupported
2526
from .glob import BoundGlobber
2627
from .mode import validate_open_mode
2728
from .path import abspath, join, normpath
@@ -675,8 +676,16 @@ def getmodified(self, path):
675676
The *modified timestamp* of a file is the point in time
676677
that the file was last changed. Depending on the file system,
677678
it might only have limited accuracy.
679+
678680
"""
679-
return self.getinfo(path, ("detail", "modified")).modified
681+
timestamp = self.getinfo(path, ("detail", "modified")).modified
682+
if timestamp is None:
683+
raise Unsupported(
684+
"Last modified time is not supported by the filesystem for the requested resource: "
685+
+ path
686+
)
687+
else:
688+
return timestamp
680689

681690
def getmeta(self, namespace="standard"):
682691
# type: (Text) -> Mapping[Text, object]

fs/ftpfs.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from datetime import datetime
1717
from ftplib import FTP
1818

19+
from .time import epoch_to_datetime
20+
1921
try:
2022
from ftplib import FTP_TLS
2123
except ImportError as err:
@@ -579,11 +581,14 @@ def create(self, path, wipe=False):
579581
def getmodified(self, path):
580582
# type: (Text) -> datetime
581583
if "MDTM" in self.features:
582-
# TODO
583-
pass
584-
585-
return self.getinfo(path, ["details", "modified"]).modified
586-
584+
with self._lock:
585+
with ftp_errors(self, path=path):
586+
cmd = "MDTM " + _encode(self.validatepath(path), self.ftp.encoding)
587+
response = self.ftp.sendcmd(cmd)
588+
time_num = self._parse_ftp_time(response.split()[1])
589+
if time_num is not None:
590+
return epoch_to_datetime(time_num)
591+
return super(FTPFS, self).getmodified(path)
587592

588593
@classmethod
589594
def _parse_ftp_time(cls, time_text):

0 commit comments

Comments
 (0)