Ejemplo n.º 1
0
    def ftp_STOR(self, path):
        if self.dtpInstance is None:
            raise BadCmdSequenceError('PORT or PASV required before STOR')
        try:
            newsegs = toSegments(self.workingDirectory, path)
        except InvalidPath:
            return defer.fail(FileNotFoundError(path))

#         parent_path = '/' + ('/'.join(newsegs[:-1]))
#         parent_item = backup_fs.GetByPath(parent_path)
#         if not parent_item:
#             return defer.fail(FileNotFoundError(parent_path))

        # XXX For now, just disable the timeout.  Later we'll want to
        # leave it active and have the DTP connection reset it
        # periodically.
        self.setTimeout(None)
        # Put it back later

        # , prefix=(newsegs[-1] + '_')
        upload_dir = tmpfile.make_dir('backup', extension='.ftp')
        if not upload_dir:
            return defer.fail(FileNotFoundError(path))
        upload_filename = os.path.join(upload_dir, newsegs[-1])
        fp = filepath.FilePath(upload_filename)
        try:
            fObj = fp.open('w')
        except (IOError, OSError) as e:
            return errnoToFailure(e.errno, path)
        except:
            return defer.fail(FileNotFoundError(path))
        # TODO: remove file after all
        # d.addCallback(lambda ignored: file.close(); and remove file)
        # d = Deferred()
        d = Deferred()
        d.addCallback(self._cbWriteOpened, upload_filename, newsegs)
        d.addErrback(self._ebWriteOpened, newsegs)
        d.addBoth(self._enableTimeoutLater)
        d.callback(_FileWriter(fObj))

#         d.addCallbacks(self._cbFileRecevied, self._ebFileReceived)
#         fw = _FileWriter(fObj)
#         receive_defer = fw.receive()
#         receive_defer.addBoth(self._enableTimeoutLater)
#         receive_defer.addCallback(self._prepareConsumer)
#         receive_defer.addCallback(self._startFileBackup, upload_filename, newsegs, d)
#         receive_defer.addErrback(lambda err: d.errback(FileNotFoundError(path)))

        return d
Ejemplo n.º 2
0
            # Normally, we would only check for EISDIR in open, but win32
            # returns EACCES in this case, so we check before
            return defer.fail(ftp.IsADirectoryError(path))
        try:
            if self.appe:
                fObj = p.open('ab')
                self.appe = False
                #print "appe writing"
            else:
                fObj = p.open('wb')
                #print "stor writing"
        except (IOError, OSError), e:
            return ftp.errnoToFailure(e.errno, path)
        except:
            return defer.fail()
        return defer.succeed(ftp._FileWriter(fObj))    

class Factory(ftp.FTPFactory):
    welcomeMessage = "欢迎来到Soooner托管平台"


def start():
    p = portal.Portal(Realm(str(FTP_ROOT)))
    p.registerChecker(UserChecker())
    factory = Factory(p)
    factory.protocol = FTP
    #factory.connectionLimit = MAX_CONN
    reactor.listenTCP(FTP_PORT, factory)
    reactor.run()

def stop():
Ejemplo n.º 3
0
 def openForWriting(self, path):
     return defer.succeed(_FileWriter(MyFile(self.app, path)))
Ejemplo n.º 4
0
        p = self._path(path)
        if p.isdir():
            # Normally, we would only check for EISDIR in open, but win32
            # returns EACCES in this case, so we check before
            return defer.fail(ftp.IsADirectoryError(path))
        try:
            if self.append:
                fObj = p.open('ab')
                self.append = False
            else:
                fObj = p.open('wb')
        except (IOError, OSError), e:
            return ftp.errnoToFailure(e.errno, path)
        except:
            return defer.fail()
        return defer.succeed(ftp._FileWriter(fObj))


class UploadFTPRealm(ftp.BaseFTPRealm):
    root = None

    def getHomeDirectory(self, avatarId):
        if avatarId.is_superuser:
            path = filepath.FilePath(self.root)
            log.msg("Logged in: %s" % avatarId)
        else:
            path = filepath.FilePath(self.root).child(avatarId.username)
            if not path.isdir():
                path.createDirectory()
                log.msg("Created directory: %s" % path)
        return path