Skip to content

Commit

Permalink
Start adding setMetadata to twisted.vfs, including the SFTP adapter a…
Browse files Browse the repository at this point in the history
…nd osfs backend.

This work is contributed by Canonical Ltd -- I've updated the 
list of copyright holders in LICENSE to reflect this.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@15836 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
spiv committed Feb 6, 2006
1 parent f4cbaa4 commit 941b4c3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Andrew Bennetts
Apple Computer, Inc.
Benjamin Bruheim
Bob Ippolito
Canonical Limited
Christopher Armstrong
Donovan Preston
Eric Mangold
Expand Down
19 changes: 16 additions & 3 deletions twisted/vfs/adapters/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,24 @@ def _attrify(self, node):
}

def getAttrs(self, path, followLinks):
return self._attrify(self.filesystem.fetch(path))

try:
node = self.filesystem.fetch(path)
except ivfs.NotFoundError, e:
raise SFTPError(FX_NO_SUCH_FILE, e.args[0])
return self._attrify(node)

def setAttrs(self, path, attrs):
raise NotImplementedError("NO SETATTR")
try:
node = self.filesystem.fetch(path)
except ivfs.NotFoundError, e:
raise SFTPError(FX_NO_SUCH_FILE, e.args[0])
try:
# XXX: setMetadata isn't yet part of the IFileSystemNode interface
# (but it should be). So we catch AttributeError, and translate it
# to NotImplementedError because it's slightly nicer for clients.
node.setMetadata(attrs)
except AttributeError:
raise NotImplementedError("NO SETATTR")

def readLink(self, path):
raise NotImplementedError("NO LINK")
Expand Down
14 changes: 14 additions & 0 deletions twisted/vfs/backends/osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ def getMetadata(self):
"nlink" : s.st_nlink
}

def setMetadata(self, attrs):
if 'uid' in attrs and 'gid' in attrs:
os.chown(self.realPath, attrs["uid"], attrs["gid"])
if 'permissions' in attrs:
os.chmod(self.realPath, attrs["permissions"])
if 'atime' in attrs or 'mtime' in attrs:
if None in (attrs.get("atime"), attrs.get("mtime")):
st = os.stat(self.realPath)
atime = attrs.get("atime", st.st_atime)
mtime = attrs.get("mtime", st.st_mtime)
else:
atime = attrs['atime']
mtime = attrs['mtime']
os.utime(self.realPath, (atime, mtime))

def rename(self, newName):
from twisted.vfs import pathutils
Expand Down
16 changes: 16 additions & 0 deletions twisted/vfs/ivfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ def getMetadata():
particular value isn't available as gracefully as possible.
"""

# XXX: There should be a setMetadata, probably taking a map of the same form
# returned by getMetadata (although obviously keys like 'nlink' aren't
# settable. Something like:
# def setMetadata(metadata):
# """Sets metadata for a node.
#
# Unrecognised keys will be ignored (but invalid values for a recognised
# key may cause an error to be raised).
#
# Typical keys are 'permissions', 'uid', 'gid', 'atime' and 'mtime'.
#
# @param metadata: a dict, like the one getMetadata returns.
# """
# osfs.OSNode implements this; other backends should be similarly updated.
# -- spiv, 2006-06-02

def remove():
"""
Removes this node.
Expand Down
13 changes: 13 additions & 0 deletions twisted/vfs/test/test_sftp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time

from twisted.conch.ssh.filetransfer import FXF_READ, FXF_WRITE, FXF_CREAT
from twisted.conch.ssh.filetransfer import FXF_APPEND, FXF_EXCL
Expand Down Expand Up @@ -129,6 +130,18 @@ def test_getAttrs(self):
attrs.sort()
self.failUnless(sftpAttrs, attrs)

def test_setAttrs(self):
for mtime in [1, 2, int(time.time())]:
try:
self.sftp.setAttrs('/ned', {'mtime': mtime})
except NotImplementedError:
raise unittest.SkipTest(
"The VFS backend %r doesn't support setAttrs"
% (self.root,))
else:
self.assertEqual(mtime,
self.sftp.getAttrs('/ned', False)['mtime'])

def test_dirlistWithoutAttrs(self):
self.ned.getMetadata = self.f.getMetadata = lambda: {}
for name, lsline, attrs in self.sftp.openDirectory('/'):
Expand Down

0 comments on commit 941b4c3

Please sign in to comment.