Commit 2f34efa7 by rfkelly0

make convert_os_errors() decorate rewrite the resource path if self as an attribute "root_path"

parent 72b8884b
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
import sys import sys
import errno import errno
from fs.path import *
try: try:
from functools import wraps from functools import wraps
except ImportError: except ImportError:
...@@ -153,26 +155,31 @@ def convert_os_errors(func): ...@@ -153,26 +155,31 @@ def convert_os_errors(func):
"""Function wrapper to convert OSError/IOError instances into FSErrors.""" """Function wrapper to convert OSError/IOError instances into FSErrors."""
opname = func.__name__ opname = func.__name__
@wraps(func) @wraps(func)
def wrapper(*args,**kwds): def wrapper(self,*args,**kwds):
try: try:
return func(*args,**kwds) return func(self,*args,**kwds)
except (OSError,IOError), e: except (OSError,IOError), e:
path = getattr(e,"filename",None)
if path and path[0] == "/" and hasattr(self,"root_path"):
path = normpath(path)
if isprefix(self.root_path,path):
path = path[len(self.root_path):]
if not hasattr(e,"errno") or not e.errno: if not hasattr(e,"errno") or not e.errno:
raise OperationFailedError(opname,details=e) raise OperationFailedError(opname,details=e)
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise ResourceNotFoundError(e.filename,opname=opname,details=e) raise ResourceNotFoundError(path,opname=opname,details=e)
if e.errno == errno.ENOTEMPTY: if e.errno == errno.ENOTEMPTY:
raise DirectoryNotEmptyError(e.filename,opname=opname,details=e) raise DirectoryNotEmptyError(path,opname=opname,details=e)
if e.errno == errno.EEXIST: if e.errno == errno.EEXIST:
raise DestinationExistsError(e.filename,opname=opname,details=e) raise DestinationExistsError(path,opname=opname,details=e)
if e.errno == 183: # some sort of win32 equivalent to EEXIST if e.errno == 183: # some sort of win32 equivalent to EEXIST
raise DestinationExistsError(e.filename,opname=opname,details=e) raise DestinationExistsError(path,opname=opname,details=e)
if e.errno == errno.ENOTDIR: if e.errno == errno.ENOTDIR:
raise ResourceInvalidError(e.filename,opname=opname,details=e) raise ResourceInvalidError(path,opname=opname,details=e)
if e.errno == errno.EISDIR: if e.errno == errno.EISDIR:
raise ResourceInvalidError(e.filename,opname=opname,details=e) raise ResourceInvalidError(path,opname=opname,details=e)
if e.errno == errno.EINVAL: if e.errno == errno.EINVAL:
raise ResourceInvalidError(e.filename,opname=opname,details=e) raise ResourceInvalidError(path,opname=opname,details=e)
raise OperationFailedError(opname,details=e) raise OperationFailedError(opname,details=e)
return wrapper return wrapper
......
...@@ -40,7 +40,7 @@ class SFTPFS(FS): ...@@ -40,7 +40,7 @@ class SFTPFS(FS):
class in the paramiko module. class in the paramiko module.
""" """
def __init__(self,connection,root="/",**credentials): def __init__(self,connection,root_path="/",**credentials):
"""SFTPFS constructor. """SFTPFS constructor.
The only required argument is 'connection', which must be something The only required argument is 'connection', which must be something
...@@ -52,7 +52,7 @@ class SFTPFS(FS): ...@@ -52,7 +52,7 @@ class SFTPFS(FS):
* a paramiko.Transport instance * a paramiko.Transport instance
* a paramiko.Channel instance in "sftp" mode * a paramiko.Channel instance in "sftp" mode
The kwd argument 'root' specifies the root directory on the remote The kwd argument 'root_path' specifies the root directory on the remote
machine - access to files outsite this root wil be prevented. Any machine - access to files outsite this root wil be prevented. Any
other keyword arguments are assumed to be credentials to be used when other keyword arguments are assumed to be credentials to be used when
connecting the transport. connecting the transport.
...@@ -71,7 +71,7 @@ class SFTPFS(FS): ...@@ -71,7 +71,7 @@ class SFTPFS(FS):
if not connection.is_authenticated(): if not connection.is_authenticated():
connection.connect(**credentials) connection.connect(**credentials)
self._transport = connection self._transport = connection
self.root = abspath(normpath(root)) self.root_path = abspath(normpath(root_path))
def __del__(self): def __del__(self):
self.close() self.close()
...@@ -111,8 +111,8 @@ class SFTPFS(FS): ...@@ -111,8 +111,8 @@ class SFTPFS(FS):
self._transport.close() self._transport.close()
def _normpath(self,path): def _normpath(self,path):
npath = pathjoin(self.root,relpath(normpath(path))) npath = pathjoin(self.root_path,relpath(normpath(path)))
if not isprefix(self.root,npath): if not isprefix(self.root_path,npath):
raise PathError(path,msg="Path is outside root: %(path)s") raise PathError(path,msg="Path is outside root: %(path)s")
return npath return npath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment