def open(self, sftp_path, flags, attr):
     local_path = self._local_path(sftp_path)
     if (flags & os.O_WRONLY) or (flags & os.O_RDWR):
         return paramiko.SFTP_PERMISSION_DENIED
     h = paramiko.SFTPHandle()
     h.readfile = open(local_path, "rb")
     return h
Beispiel #2
0
	def open(self, path, flags, attr):
		try:
			if (flags & os.O_CREAT) and (attr is not None):
				attr._flags &= ~attr.FLAG_PERMISSIONS
				paramiko.SFTPServer.set_file_attr(self._parsePath(path), attr)

			if flags & os.O_WRONLY:
				if flags & os.O_APPEND:
					fstr = 'ab'
				else:
					fstr = 'wb'
			elif flags & os.O_RDWR:
				if flags & os.O_APPEND:
					fstr = 'a+b'
				else:
					fstr = 'r+b'
			else:
				# O_RDONLY (== 0)
				fstr = 'rb'

			f = self.client.open(self._parsePath(path), fstr)

			fobj = paramiko.SFTPHandle(flags)
			fobj.filename = self._parsePath(path)
			fobj.readfile = f
			fobj.writefile = f
			fobj.client = self.client
			return fobj

			# TODO: verify (socket.error when stopping file upload/download)
		except IOError as e:
			return paramiko.SFTPServer.convert_errno(e.errno)
Beispiel #3
0
    def open(self, sftp_path: str, flags: int, attr: paramiko.SFTPAttributes) -> paramiko.SFTPHandle:
        """
        Opens a file with given remote
        :param sftp_path: Remote path
        :param flags: os open mode file flags
        :param attr: SFTP file attributes
        :return: file handle object
        """
        local_path = self._local_path(sftp_path)
        handle = paramiko.SFTPHandle()
        readfile = None
        writefile = None
        # convert os flags to file handles with python str flags
        if flags & os.O_WRONLY:
            writefile = open(local_path, "wb")

        elif flags & os.O_RDWR:
            readfile = open(local_path, "rb")
            writefile = open(local_path, "wb")
        else:
            readfile = open(local_path, "rb")

        handle.readfile = readfile
        handle.writefile = writefile
        logging.info("File was opened in: {path}".format(path=local_path))
        return handle
Beispiel #4
0
    def open(self, path, flags, attr):
        self.last_operation_time = time.time()
        if flags == 769:
            self.cmd += '上传文件 {0}\n'.format(path)
        elif flags == 0:
            self.cmd += '下载文件 {0}\n'.format(path)
        else:
            self.cmd += '文件操作 {1} {0}\n'.format(path, flags)

        try:
            if (flags & os.O_CREAT) and (attr is not None):
                attr._flags &= ~attr.FLAG_PERMISSIONS
                paramiko.SFTPServer.set_file_attr(self._parsePath(path), attr)

            if flags & os.O_WRONLY:
                fstr = 'ab' if flags & os.O_APPEND else 'wb'
            elif flags & os.O_RDWR:
                fstr = 'a+b' if flags & os.O_APPEND else 'r+b'
            else:
                fstr = 'rb'

            f = self.client.open(self._parsePath(path), fstr)

            fobj = paramiko.SFTPHandle(flags)
            fobj.filename = self._parsePath(path)
            fobj.readfile = f
            fobj.writefile = f
            fobj.client = self.client
            return fobj

            # TODO: verify (socket.error when stopping file upload/download)
        except IOError as e:
            return paramiko.SFTPServer.convert_errno(e.errno)
Beispiel #5
0
 def open(self, path, flags, attr):
     # We ignore `attr` -- we choose the permissions
     read_only = flags == os.O_RDONLY
     if read_only:
         realpath = self.realpath_for_read(path)
     else:
         realpath = self.realpath_for_write(path)
     fd = os.open(realpath, flags, self.FILE_MODE)
     fileobj = os.fdopen(fd, self.flags_to_string(flags), self.FILE_MODE)
     handle = SFTPFileHandle(paramiko.SFTPHandle(flags))
     handle.readfile = fileobj
     if not read_only:
         handle.writefile = fileobj
     return handle
Beispiel #6
0
 def open(self, sftp_path, flags, attr):
     print('open')
     try:
         tmp_dir = '/tmp'
         tmp_file = '%s%s' % (tmp_dir, sftp_path)
         if not os.path.isdir(tmp_dir):
             os.makedirs(tmp_dir)
         self.s3.download_file(self.bucket,
                               self.removeLeadingSlash(sftp_path), tmp_file)
         data = open(tmp_file, 'rb')
         h = paramiko.SFTPHandle()
         h.readfile = data
         return h
     except Exception as e:
         print(e)
Beispiel #7
0
    def open(self, path, flags, attr=None):
        binary_flag = getattr(os, 'O_BINARY', 0)
        flags |= binary_flag

        if flags & os.O_WRONLY:
            if flags & os.O_APPEND:
                mode = 'ab'
            else:
                mode = 'wb'
        elif flags & os.O_RDWR:
            if flags & os.O_APPEND:
                mode = 'a+b'
            else:
                mode = 'r+b'
        else:
            mode = 'rb'

        if 'r' in mode:
            operate = "Download"
            action = PERMS_ACTION_NAME_DOWNLOAD_FILE
        elif 'a' in mode:
            operate = "Append"
            action = PERMS_ACTION_NAME_UPLOAD_FILE
        else:
            operate = "Upload"
            action = PERMS_ACTION_NAME_UPLOAD_FILE

        success = False
        try:
            self.check_action(path, action)
            client, rpath = self.get_sftp_client_rpath(path)
            f = client.open(rpath, mode, bufsize=4096)
            f.prefetch()
            obj = paramiko.SFTPHandle(flags)
            obj.filename = rpath
            obj.readfile = f
            obj.writefile = f
            result = obj
            success = True
            return result
        finally:
            self.create_ftp_log(path, operate, success)
Beispiel #8
0
    def open(self, path, flags, attr):
        binary_flag = getattr(os, 'O_BINARY', 0)
        flags |= binary_flag
        success = False

        if flags & os.O_WRONLY:
            if flags & os.O_APPEND:
                mode = 'ab'
            else:
                mode = 'wb'
        elif flags & os.O_RDWR:
            if flags & os.O_APPEND:
                mode = 'a+b'
            else:
                mode = 'r+b'
        else:
            mode = 'rb'

        sftp, rpath = self.get_sftp_rpath(path)
        if 'r' in mode:
            operate = "Download"
        else:
            operate = "Upload"

        result = None
        if sftp is not None:
            try:
                f = sftp.open(rpath, mode, bufsize=4096)
                obj = paramiko.SFTPHandle(flags)
                obj.filename = rpath
                obj.readfile = f
                obj.writefile = f
                result = obj
                success = True
            except OSError:
                pass
        self.create_ftp_log(path, operate, success)
        return result
Beispiel #9
0
    def open(self, path, flags, attr=None):
        binary_flag = getattr(os, 'O_BINARY', 0)
        flags |= binary_flag
        success = False

        if flags & os.O_WRONLY:
            if flags & os.O_APPEND:
                mode = 'ab'
            else:
                mode = 'wb'
        elif flags & os.O_RDWR:
            if flags & os.O_APPEND:
                mode = 'a+b'
            else:
                mode = 'r+b'
        else:
            mode = 'rb'

        if 'r' in mode:
            operate = "Download"
        elif 'a' in mode:
            operate = "Append"
        else:
            operate = "Upload"

        try:
            client, rpath = self.get_sftp_client_rpath(path)
            f = client.open(rpath, mode, bufsize=4096)
            obj = paramiko.SFTPHandle(flags)
            obj.filename = rpath
            obj.readfile = f
            obj.writefile = f
            result = obj
            success = True
            return result
        finally:
            self.create_ftp_log(path, operate, success)