SFTP: allow FXF_WRITE | FXF_TRUNC (#1050).

This commit is contained in:
david-sarah 2010-05-18 21:32:40 -07:00
parent bde27cab57
commit f0945526ce
2 changed files with 17 additions and 7 deletions

View File

@ -657,7 +657,6 @@ class GeneralSFTPFile(PrefixingLogMixin):
if (flags & FXF_TRUNC) or not filenode: if (flags & FXF_TRUNC) or not filenode:
# We're either truncating or creating the file, so we don't need the old contents. # We're either truncating or creating the file, so we don't need the old contents.
assert flags & FXF_CREAT, flags
self.consumer = OverwriteableFileConsumer(self.check_abort, 0, tempfile_maker) self.consumer = OverwriteableFileConsumer(self.check_abort, 0, tempfile_maker)
self.consumer.finish() self.consumer.finish()
else: else:
@ -878,9 +877,6 @@ class SFTPUserHandler(ConchUser, PrefixingLogMixin):
"invalid file open flags: at least one of FXF_READ and FXF_WRITE must be set") "invalid file open flags: at least one of FXF_READ and FXF_WRITE must be set")
if not (flags & FXF_CREAT): if not (flags & FXF_CREAT):
if flags & FXF_TRUNC:
raise SFTPError(FX_BAD_MESSAGE,
"invalid file open flags: FXF_TRUNC cannot be set without FXF_CREAT")
if flags & FXF_EXCL: if flags & FXF_EXCL:
raise SFTPError(FX_BAD_MESSAGE, raise SFTPError(FX_BAD_MESSAGE,
"invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT") "invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT")

View File

@ -534,9 +534,9 @@ class Handler(GridTestMixin, ShouldFailMixin, unittest.TestCase):
self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile '' WRITE|CREAT|TRUNC", self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile '' WRITE|CREAT|TRUNC",
self.handler.openFile, "", sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {})) self.handler.openFile, "", sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {}))
# TRUNC is not valid without CREAT # TRUNC is not valid without CREAT if the file does not already exist
d.addCallback(lambda ign: d.addCallback(lambda ign:
self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "openFile newfile WRITE|TRUNC", self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile newfile WRITE|TRUNC",
self.handler.openFile, "newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {})) self.handler.openFile, "newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {}))
# EXCL is not valid without CREAT # EXCL is not valid without CREAT
@ -672,6 +672,20 @@ class Handler(GridTestMixin, ShouldFailMixin, unittest.TestCase):
d.addCallback(lambda node: download_to_data(node)) d.addCallback(lambda node: download_to_data(node))
d.addCallback(lambda data: self.failUnlessReallyEqual(data, "01234567890123")) d.addCallback(lambda data: self.failUnlessReallyEqual(data, "01234567890123"))
# test WRITE | TRUNC without CREAT, when the file already exists
# This is invalid according to section 6.3 of the SFTP spec, but required for interoperability,
# since POSIX does allow O_WRONLY | O_TRUNC.
d.addCallback(lambda ign:
self.handler.openFile("newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {}))
def _write_trunc(wf):
d2 = wf.writeChunk(0, "01234")
d2.addCallback(lambda ign: wf.close())
return d2
d.addCallback(_write_trunc)
d.addCallback(lambda ign: self.root.get(u"newfile"))
d.addCallback(lambda node: download_to_data(node))
d.addCallback(lambda data: self.failUnlessReallyEqual(data, "01234"))
# test EXCL flag # test EXCL flag
d.addCallback(lambda ign: d.addCallback(lambda ign:
self.handler.openFile("excl", sftp.FXF_WRITE | sftp.FXF_CREAT | self.handler.openFile("excl", sftp.FXF_WRITE | sftp.FXF_CREAT |
@ -1033,4 +1047,4 @@ class Handler(GridTestMixin, ShouldFailMixin, unittest.TestCase):
self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "extendedRequest foo bar", self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "extendedRequest foo bar",
self.handler.extendedRequest, "foo", "bar")) self.handler.extendedRequest, "foo", "bar"))
return d return d