Ejemplo n.º 1
0
    def close(self):
        """
        Close the file.

        This method returns nothing if the close succeeds immediately, or a
        Deferred that is called back when the close succeeds.
        """
        self.env.log.debug("InMemoryFile.close() - %s" % (self.filename))
        # create file
        self.data.seek(0)
        # write file after close
        if not self.data:
            return
        if not (self.flags & FXF_WRITE):
            return
        if not self.filename:
            return
        # check for resource
        proc = SFTPProcessor(self.env)
        # set user id
        proc.user = self.attrs['uid']
        try:
            # create new resource
            proc.run(PUT, self.filename, self.data)
        except SeisHubError, e:
            raise SFTPError(FX_FAILURE, e.message)
Ejemplo n.º 2
0
    def openDirectory(self, path):
        directory = self.filesystem.fetch(path)
        if not ivfs.IFileSystemContainer.providedBy(directory):
            raise SFTPError(FX_NOT_A_DIRECTORY, path)

        class DirList:
            def __init__(self, iter):
                self.iter = iter

            def __iter__(self):
                return self

            def next(self):

                (name, attrs) = self.iter.next()

                class st:
                    pass

                s = st()
                s.st_mode = attrs["permissions"]
                s.st_uid = attrs["uid"]
                s.st_gid = attrs["gid"]
                s.st_size = attrs["size"]
                s.st_mtime = attrs["mtime"]
                s.st_nlink = attrs["nlink"]
                return (name, lsLine(name, s), attrs)

            def close(self):
                return

        return DirList(
            iter([(name, _attrify(file))
                  for (name, file) in self.filesystem.fetch(path).children()]))
Ejemplo n.º 3
0
    def getAttrs(self, path, followLinks):
        """
        Return the attributes for the given path.

        This method returns a dictionary in the same format as the attrs
        argument to openFile or a Deferred that is called back with same.

        @param path: the path to return attributes for as a string.
        @param followLinks: a boolean.  If it is True, follow symbolic links
        and return attributes for the real path at the base.  If it is False,
        return attributes for the specified path.
        """
        self.env.log.debug("openDirectory(%s, %s)" % (repr(path),
                                                       repr(followLinks)))
        # lockup filename in utf-8
        path = self.webSafe(path)
        # query the directory via SFTP processor
        proc = SFTPProcessor(self.env)
        try:
            result = proc.run(GET, path)
        except NotFoundError:
            raise SFTPError(FX_NO_SUCH_FILE, '')
        except:
            raise
        else:
            if isinstance(result, dict):
                return {'permissions': 040755}
            else:
                return result.getMetadata()
Ejemplo n.º 4
0
 def _readFile(self, filename, flags, attrs):  # @UnusedVariable
     # read file via SFTP processor
     proc = SFTPProcessor(self.env)
     try:
         result = proc.run(GET, filename)
     except SeisHubError, e:
         raise SFTPError(FX_FAILURE, e.message)
Ejemplo n.º 5
0
 def f(*args, **kwargs):
     try:
         result = function(*args, **kwargs)
         if isinstance(result, defer.Deferred):
             result.addErrback(_ebtranslateErrors)
         return result
     except ivfs.PermissionError, e:
         raise SFTPError(FX_PERMISSION_DENIED, str(e))
Ejemplo n.º 6
0
 def errback(failure):
     failure.trap(NotFound)
     if self.flags & FXF_CREAT == FXF_CREAT:
         return self.swiftfilesystem.touchFile(self.fullpath)
     if self.flags & FXF_TRUNC == FXF_TRUNC:
         return self.swiftfilesystem.touchFile(self.fullpath)
     else:
         raise SFTPError(FX_NO_SUCH_FILE, 'File Not Found')
Ejemplo n.º 7
0
 def _writeFile(self, filename, flags, attrs):
     # check if file exists
     proc = SFTPProcessor(self.env)
     try:
         proc.run(HEAD, filename)
     except NotFoundError:
         # set username
         attrs['uid'] = self.avatar.username
         return InMemoryFile(self.env, filename, flags, attrs)
     except SeisHubError, e:
         raise SFTPError(FX_FAILURE, e.message)
Ejemplo n.º 8
0
    def stopProducing(self):
        if self._task:
            try:
                self._task.stop()
            except task.TaskStopped:
                pass

        for buf in self._writeBuffer:
            d, _ = buf
            d.errback(SFTPError(FX_CONNECTION_LOST, 'Connection Lost'))
            self._writeBuffer.remove(buf)
        self._writeBuffer = []
Ejemplo n.º 9
0
    def openFile(self, filename, flags, attrs):
        createPlease = False
        exclusive = False
        openFlags = 0
        if flags & FXF_READ == FXF_READ and flags & FXF_WRITE == 0:
            openFlags = os.O_RDONLY
        if flags & FXF_WRITE == FXF_WRITE and flags & FXF_READ == 0:
            createPlease = True
            openFlags = os.O_WRONLY
        if flags & FXF_WRITE == FXF_WRITE and flags & FXF_READ == FXF_READ:
            createPlease = True
            openFlags = os.O_RDWR
        if flags & FXF_APPEND == FXF_APPEND:
            createPlease = True
            openFlags |= os.O_APPEND
        if flags & FXF_CREAT == FXF_CREAT:
            createPlease = True
            openFlags |= os.O_CREAT
        if flags & FXF_TRUNC == FXF_TRUNC:
            openFlags |= os.O_TRUNC
        if flags & FXF_EXCL == FXF_EXCL:
            exclusive = True

        # XXX Once we change readChunk/writeChunk we'll have to wrap
        # child in something that implements those.

        pathSegments = self.filesystem.splitPath(filename)
        dirname, basename = pathSegments[:-1], pathSegments[-1]
        parentNode = self.filesystem.fetch('/'.join(dirname))
        if createPlease:
            child = parentNode.createFile(basename, exclusive)
        elif parentNode.exists(basename):
            child = parentNode.child(basename)
            if not ivfs.IFileSystemLeaf.providedBy(child):
                raise SFTPError(FX_FILE_IS_A_DIRECTORY, filename)
        else:
            raise SFTPError(FX_NO_SUCH_FILE, filename)
        child.open(openFlags)
        return AdaptFileSystemLeafToISFTPFile(child)
Ejemplo n.º 10
0
    def makeDirectory(self, path, attrs):  # @UnusedVariable
        """
        Make a directory.

        This method returns when the directory is created, or a Deferred that
        is called back when it is created.

        @param path: the name of the directory to create as a string.
        @param attrs: a dictionary of attributes to create the directory with.
        Its meaning is the same as the attrs in the L{openFile} method.
        """
        msg = "Directories can't be added via SFTP."
        raise SFTPError(FX_OP_UNSUPPORTED, msg)
Ejemplo n.º 11
0
def translateErrors(function):
    """Decorator that catches VFSErrors and re-raises them as the corresponding
    SFTPErrors."""
    def f(*args, **kwargs):
        try:
            result = function(*args, **kwargs)
            if isinstance(result, defer.Deferred):
                result.addErrback(_ebtranslateErrors)
            return result
        except ivfs.PermissionError, e:
            raise SFTPError(FX_PERMISSION_DENIED, str(e))
        except ivfs.NotFoundError, e:
            raise SFTPError(FX_NO_SUCH_FILE, e.args[0])
Ejemplo n.º 12
0
    def removeDirectory(self, path):  # @UnusedVariable
        """
        Remove a directory (non-recursively)

        It is an error to remove a directory that has files or directories in
        it.

        This method returns when the directory is removed, or a Deferred that
        is called back when it is removed.

        @param path: the directory to remove.
        """
        msg = "Directories can't be deleted via SFTP."
        raise SFTPError(FX_OP_UNSUPPORTED, msg)
Ejemplo n.º 13
0
    def openFile(self, filename, flags, attrs):
        """
        Called when the clients asks to open a file.

        @param filename: a string representing the file to open.

        @param flags: an integer of the flags to open the file with, ORed
        together. The flags and their values are listed at the bottom of this
        file.

        @param attrs: a list of attributes to open the file with.  It is a
        dictionary, consisting of 0 or more keys.  The possible keys are::

          size: the size of the file in bytes
          uid: the user ID of the file as an integer
          gid: the group ID of the file as an integer
          permissions: the permissions of the file with as an integer.
          the bit representation of this field is defined by POSIX.
          atime: the access time of the file as seconds since the epoch.
          mtime: the modification time of the file as seconds since the epoch.
          ext_*: extended attributes.  The server is not required to
          understand this, but it may.

        NOTE: there is no way to indicate text or binary files.  it is up
        to the SFTP client to deal with this.

        This method returns an object that meets the ISFTPFile interface.
        Alternatively, it can return a L{Deferred} that will be called back
        with the object.
        """
        self.env.log.debug("openFile(%s, %s, %s)" % (repr(filename),
                                                      repr(flags),
                                                      repr(attrs)))
        # lockup filename in utf-8
        filename = self.webSafe(filename)
        if flags & FXF_READ == FXF_READ:
            return InMemoryFile(self.env, filename, flags, attrs)
        elif flags & FXF_WRITE == FXF_WRITE:
            return self._writeFile(filename, flags, attrs)
        else:
            msg = "Don't know how to handle this request"
            raise SFTPError(FX_FAILURE, msg)
Ejemplo n.º 14
0
    def removeFile(self, filename):
        """
        Remove the given file.

        This method returns when the remove succeeds, or a Deferred that is
        called back when it succeeds.

        @param filename: the name of the file as a string.
        """
        self.env.log.debug("removeFile(%s)" % (filename))
        # lockup filename in utf-8
        filename = self.webSafe(filename)
        # query the directory via SFTP processor
        proc = SFTPProcessor(self.env)
        # set current user id
        proc.user = self.avatar.username
        try:
            proc.run(DELETE, filename)
        except ForbiddenError, e:
            raise SFTPError(FX_OP_UNSUPPORTED, e.message)
Ejemplo n.º 15
0
    def renameFile(self, oldpath, newpath):
        """
        Rename the given file.

        This method returns when the rename succeeds, or a L{Deferred} that is
        called back when it succeeds. If the rename fails, C{renameFile} will
        raise an implementation-dependent exception.

        @param oldpath: the current location of the file.
        @param newpath: the new file name.
        """
        self.env.log.debug("renameFile(%s, %s)" % (oldpath, newpath))
        # lockup filename in utf-8
        oldpath = self.webSafe(oldpath)
        # query the directory via SFTP processor
        proc = SFTPProcessor(self.env)
        destination = self.env.getRestUrl() + newpath
        try:
            proc.run(MOVE, oldpath,
                     received_headers={'Destination': destination})
        except ForbiddenError, e:
            raise SFTPError(FX_OP_UNSUPPORTED, e.message)
Ejemplo n.º 16
0
    def openDirectory(self, path):
        """
        Open a directory for scanning.

        This method returns an iterable object that has a close() method,
        or a Deferred that is called back with same.

        The close() method is called when the client is finished reading
        from the directory.  At this point, the iterable will no longer
        be used.

        The iterable should return triples of the form (filename,
        longname, attrs) or Deferreds that return the same.  The
        sequence must support __getitem__, but otherwise may be any
        'sequence-like' object.

        filename is the name of the file relative to the directory.
        logname is an expanded format of the filename.  The recommended format
        is:
        -rwxr-xr-x   1 mjos     staff      348911 Mar 25 14:29 t-filexfer
        1234567890 123 12345678 12345678 12345678 123456789012

        The first line is sample output, the second is the length of the field.
        The fields are: permissions, link count, user owner, group owner,
        size in bytes, modification time.

        attrs is a dictionary in the format of the attrs argument to openFile.

        @param path: the directory to open.
        """
        self.env.log.debug("openDirectory(%s)" % (repr(path)))
        # lockup filename in utf-8
        path = self.webSafe(path)
        # query the directory via SFTP processor
        proc = SFTPProcessor(self.env)
        try:
            result = proc.run(HEAD, path)
        except SeisHubError, e:
            raise SFTPError(FX_FAILURE, e.message)
Ejemplo n.º 17
0
    def connectionLost(self, reason):
        """
            For some reason, the HTTP connection has been lost. We can either
            be done reading from Swift or something back could have happened.
        """
        from twisted.web._newclient import ResponseDone
        from twisted.web.http import PotentialDataLoss

        self.done = True

        if reason.check(ResponseDone) or reason.check(PotentialDataLoss):
            self._readloop()
            for callback in self._recv_listeners:
                d, _, _ = callback
                d.errback(reason)
            self._recv_listeners = []
            self.finished.callback(None)
        else:
            for callback in self._recv_listeners:
                d, _, _ = callback
                d.errback(SFTPError(FX_CONNECTION_LOST, 'Connection Lost'))
            self._recv_listeners = []
            self.finished.errback(reason)
Ejemplo n.º 18
0
 def errback(failure):
     failure.trap(NotFound, Conflict)
     if failure.check(NotFound):
         return
     if failure.check(Conflict):
         raise SFTPError(FX_FAILURE, 'Directory Not Empty')
Ejemplo n.º 19
0
 def _errClose(self, failure):
     failure.trap(ConnectionLost, NotFound)
     if failure.check(ConnectionLost):
         raise SFTPError(FX_CONNECTION_LOST, "Connection Lost")
     elif failure.check(NotFound):
         raise SFTPError(FX_FAILURE, "Container Doesn't Exist")
Ejemplo n.º 20
0
 def errback(failure):
     raise SFTPError(FX_FAILURE, 'Upload Failure')
Ejemplo n.º 21
0
 def makeLink(self, linkPath, targetPath):  # @UnusedVariable
     msg = "Symbolic can'T be created via SFTP."
     raise SFTPError(FX_OP_UNSUPPORTED, msg)
Ejemplo n.º 22
0
 def errback(failure):
     failure.trap(NotFound, Conflict)
     if failure.check(NotFound):
         raise SFTPError(FX_NO_SUCH_FILE, 'No Such File')
     if failure.check(Conflict):
         raise NotImplementedError
Ejemplo n.º 23
0
 def errback(failure):
     failure.trap(NotFound)
     raise SFTPError(FX_NO_SUCH_FILE, 'Not Found')
Ejemplo n.º 24
0
 def removeFile(self, filename):
     node = self.filesystem.fetch(filename)
     if not ivfs.IFileSystemLeaf.providedBy(node):
         raise SFTPError(FX_FILE_IS_A_DIRECTORY, filename)
     node.remove()
Ejemplo n.º 25
0
def translateErrors(function):
    """Decorator that catches VFSErrors and re-raises them as the corresponding
    SFTPErrors."""
    def f(*args, **kwargs):
        try:
            result = function(*args, **kwargs)
            if isinstance(result, defer.Deferred):
                result.addErrback(_ebtranslateErrors)
            return result
        except ivfs.PermissionError, e:
            raise SFTPError(FX_PERMISSION_DENIED, str(e))
        except ivfs.NotFoundError, e:
            raise SFTPError(FX_NO_SUCH_FILE, e.args[0])
        except ivfs.AlreadyExistsError, e:
            raise SFTPError(FX_FILE_ALREADY_EXISTS, e.args[0])
        except ivfs.VFSError, e:
            raise SFTPError(FX_FAILURE, str(e))
        except NotImplementedError, e:
            raise SFTPError(FX_OP_UNSUPPORTED, str(e))

    util.mergeFunctionMetadata(function, f)
    return f


def _ebtranslateErrors(failure):
    """This just re-raises the failure so that the translateErrors decorator
    around this errback can intercept it if it wants to."""
    failure.raiseException()

Ejemplo n.º 26
0
 def readLink(self, path):  # @UnusedVariable
     msg = "Symbolic links are not supported yet."
     raise SFTPError(FX_OP_UNSUPPORTED, msg)
Ejemplo n.º 27
0
            result = proc.run(GET, filename)
        except SeisHubError, e:
            raise SFTPError(FX_FAILURE, e.message)
        except:
            raise
        # set attributes
        try:
            self.attrs.update(result.getMetadata())
        except:
            pass
        if IRESTResource.providedBy(result):
            # some REST resource
            try:
                data = result.render_GET(proc)
            except SeisHubError, e:
                raise SFTPError(FX_FAILURE, e.message)
            except:
                raise
            self.data = StringIO.StringIO(data)
        elif IFileSystemResource.providedBy(result):
            # some file system resource
            self.data = result.open()
        else:
            msg = "I don't know how to handle this resource type %s"
            raise SFTPError(FX_FAILURE, msg % type(result))

    def readChunk(self, offset, length):
        """
        Read from the file.

        If EOF is reached before any data is read, raise EOFError.
Ejemplo n.º 28
0
 def errback(failure):
     failure.trap(NotFound)
     raise SFTPError(FX_FAILURE, "Container Doesn't Exist")
Ejemplo n.º 29
0
 def errback(failure):
     failure.trap(NotFound)
     raise SFTPError(FX_FAILURE, 'Not Found')