raise SFTPError(FX_BAD_MESSAGE,
"invalid file open flags: at least one of FXF_READ and FXF_WRITE must be set")
- if not (flags & FXF_CREAT):
- if flags & FXF_EXCL:
- raise SFTPError(FX_BAD_MESSAGE,
- "invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT")
+ if (flags & FXF_EXCL) and not (flags & FXF_CREAT):
+ raise SFTPError(FX_BAD_MESSAGE,
+ "invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT")
path = self._path_from_string(pathstring)
if not path:
metadata['readonly'] = _is_readonly(parent_readonly, filenode)
return _make_sftp_file(self.check_abort, flags, self._convergence, parent=parent,
- childname=childname, filenode=filenode, metadata=metadata)
+ childname=childname, filenode=filenode, metadata=metadata)
def _no_child(f):
if noisy: self.log("_no_child(%r)" % (f,), level=NOISY)
f.trap(NoSuchChildError)
"cannot create a file when the parent directory is read-only")
return _make_sftp_file(self.check_abort, flags, self._convergence, parent=parent,
- childname=childname)
+ childname=childname)
d3.addCallbacks(_got_child, _no_child)
return d3
d.addCallback(lambda node: download_to_data(node))
d.addCallback(lambda data: self.failUnlessReallyEqual(data, "012345678901234"))
- # test WRITE | CREAT without TRUNC
+ # test WRITE | CREAT | APPEND when the file does not already exist
+ d.addCallback(lambda ign:
+ self.handler.openFile("creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT |
+ sftp.FXF_APPEND, {}))
+ def _write_creat_append_new(wf):
+ d2 = wf.writeChunk(10, "0123456789")
+ d2.addCallback(lambda ign: wf.writeChunk(5, "01234"))
+ d2.addCallback(lambda ign: wf.close())
+ return d2
+ d.addCallback(_write_creat_append_new)
+ d.addCallback(lambda ign: self.root.get(u"creatappend"))
+ d.addCallback(lambda node: download_to_data(node))
+ d.addCallback(lambda data: self.failUnlessReallyEqual(data, "012345678901234"))
+
+ # ... and when it does exist
+ d.addCallback(lambda ign:
+ self.handler.openFile("creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT |
+ sftp.FXF_APPEND, {}))
+ def _write_creat_append_existing(wf):
+ d2 = wf.writeChunk(5, "01234")
+ d2.addCallback(lambda ign: wf.close())
+ return d2
+ d.addCallback(_write_creat_append_existing)
+ d.addCallback(lambda ign: self.root.get(u"creatappend"))
+ d.addCallback(lambda node: download_to_data(node))
+ d.addCallback(lambda data: self.failUnlessReallyEqual(data, "01234567890123401234"))
+
+ # test WRITE | CREAT without TRUNC, when the file does not already exist
d.addCallback(lambda ign:
self.handler.openFile("newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {}))
- def _write_notrunc(wf):
+ def _write_creat_new(wf):
d2 = wf.writeChunk(0, "0123456789")
d2.addCallback(lambda ign: wf.close())
return d2
- d.addCallback(_write_notrunc)
+ d.addCallback(_write_creat_new)
d.addCallback(lambda ign: self.root.get(u"newfile2"))
d.addCallback(lambda node: download_to_data(node))
d.addCallback(lambda data: self.failUnlessReallyEqual(data, "0123456789"))
+ # ... and when it does exist
+ d.addCallback(lambda ign:
+ self.handler.openFile("newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {}))
+ def _write_creat_existing(wf):
+ d2 = wf.writeChunk(0, "abcde")
+ d2.addCallback(lambda ign: wf.close())
+ return d2
+ d.addCallback(_write_creat_existing)
+ d.addCallback(lambda ign: self.root.get(u"newfile2"))
+ d.addCallback(lambda node: download_to_data(node))
+ d.addCallback(lambda data: self.failUnlessReallyEqual(data, "abcde56789"))
+
# test writing to a mutable file
d.addCallback(lambda ign:
self.handler.openFile("mutable", sftp.FXF_WRITE, {}))