def list_folder(self, path): out = [] prefix = self._path2prefix(path) result = self.s3bucket.meta.client.list_objects( Bucket=self.s3bucket.name, Delimiter='/', Prefix=prefix) directories = result.get('CommonPrefixes') if directories: for directory in directories: attr = paramiko.SFTPAttributes() attr.filename = directory.get('Prefix') attr.filename = attr.filename[len( prefix):] # remove the prepended prefix path attr.filename = attr.filename[: -1] # prefixes always come with trailing slashes attr.st_mode = (stat.S_IFDIR | stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC) attr.st_atime = 946684800 attr.st_mtime = attr.st_atime out.append(attr) for f in self.s3bucket.objects.filter(Delimiter='/', Prefix=prefix): attr = paramiko.SFTPAttributes() attr.filename = f.key attr.filename = attr.filename[len( prefix):] # remove the prepended prefix path attr.st_mode = (stat.S_IFREG | stat.S_IREAD | stat.S_IWRITE) attr.st_atime = int( ((f.last_modified.replace(tzinfo=None) - f.last_modified.utcoffset()) - datetime.datetime(1970, 1, 1)).total_seconds()) attr.st_mtime = attr.st_atime attr.st_size = f.size out.append(attr) return out
def stat(self, path): if not isinstance(path, unicode): path = path.decode(self.encoding) info = self.fs.getinfo(path) get = info.get stat = paramiko.SFTPAttributes() stat.filename = basename(path).encode(self.encoding) stat.st_size = info.get("size") if 'st_atime' in info: stat.st_atime = get('st_atime') elif 'accessed_time' in info: stat.st_atime = time.mktime(get("accessed_time").timetuple()) if 'st_mtime' in info: stat.st_mtime = get('st_mtime') else: if 'modified_time' in info: stat.st_mtime = time.mktime(get("modified_time").timetuple()) if isdir(self.fs, path, info): stat.st_mode = 0777 | statinfo.S_IFDIR else: stat.st_mode = 0777 | statinfo.S_IFREG return stat
def _file_attr(storage, path): attr = paramiko.SFTPAttributes() attr.filename = os.path.basename(path) try: attr.st_size = storage.size(path) except: attr.st_size = 0 attr.st_uid = 0 attr.st_gid = 0 directories, files = storage.listdir(os.path.dirname(path)) logger.debug('check {} : {}, {}'.format(path, directories, files)) attr.st_mode = ((_stat.S_IFREG if (path and (os.path.basename(path) in files)) else _stat.S_IFDIR) | _stat.S_IRUSR | _stat.S_IWUSR | _stat.S_IXUSR | _stat.S_IRGRP | _stat.S_IWGRP | _stat.S_IXGRP) try: attr.st_atime = _timestamp(storage.accessed_time(path)) except: attr.st_atime = 0 try: # if hasattr(storage, 'modified_time'): attr.st_mtime = _timestamp(storage.modified_time(path)) except: attr.st_mtime = 0 logger.debug('{} : {}'.format(path, attr)) return attr
def _stat(self, path, option='--dereference'): path = self._adjust_cwd(path.strip()) command = 'stat --format "%s %u %g %f %X %Y" ' + option + ' ' + paramiko.py3compat.u( path) retval, stdout, stderr = self.exec_command_wait(command) if retval == 0: if stderr.strip(): self.logger.warning( 'There was nonempty stderr in the stat command: {}'.format( stderr)) stat = paramiko.SFTPAttributes() statlist = stdout.split() stat.st_size = int(statlist[0]) stat.st_uid = int(statlist[1]) stat.st_guid = int(statlist[2]) stat.st_mode = int(statlist[3], 16) stat.st_atime = int(statlist[4]) stat.st_mtime = int(statlist[5]) return stat raise IOError( 2, 'Error while executing stat. Exit code: {}'.format(retval))
def stat(self, path): path = path.rstrip('/') if path == '': info = paramiko.SFTPAttributes() info.st_size = 0 info.st_mode = 0o777 | stat.S_IFDIR info.filename = '/' return info elif path in ('/user', '/collection'): info = paramiko.SFTPAttributes() info.st_size = 0 info.st_mode = 0o777 | stat.S_IFDIR info.filename = path[1:] return info obj = lookUpPath(path, filter=False, user=self.server.girderUser) return _stat(obj['document'], obj['model'])
def _directory_attr(self, filename): attr = paramiko.SFTPAttributes() attr.filename = filename attr.st_size = 0 attr.st_uid = 0 attr.st_gid = 0 attr.st_mode = _stat.S_IFDIR | _stat.S_IRUSR | _stat.S_IXUSR | _stat.S_IRGRP | _stat.S_IXGRP attr.st_atime = 0 attr.st_mtime = 0 return attr
def stat(self, path): prefix = self._path2prefix(path)[:-1] try: obj = self.s3bucket.meta.client.get_object( Bucket=self.s3bucket.name, Key=prefix) # it's a file attr = paramiko.SFTPAttributes() attr.filename = path attr.st_mode = (stat.S_IFREG | stat.S_IREAD | stat.S_IWRITE) attr.st_mtime = obj['LastModified'] return attr except botocore.exceptions.ClientError: # it doesn't exist, so it's likely a prefix attr = paramiko.SFTPAttributes() attr.filename = path attr.st_mode = (stat.S_IFDIR | stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC) attr.st_atime = 946684800 attr.st_mtime = attr.st_atime return attr
def stat(self,path): info = self.fs.getinfo(path) stat = paramiko.SFTPAttributes() stat.filename = basename(path) stat.st_size = info.get("size") stat.st_atime = time.mktime(info.get("accessed_time").timetuple()) stat.st_mtime = time.mktime(info.get("modified_time").timetuple()) if self.fs.isdir(path): stat.st_mode = 0777 | statinfo.S_IFDIR else: stat.st_mode = 0777 | statinfo.S_IFREG return stat
def __init__(self, value=None, path=None): init = lambda x: StringIO.__init__(self, x) if value is None: init("") ftype = 'dir' size = 4096 else: init(value) ftype = 'file' size = len(value) attr = ssh.SFTPAttributes() attr.st_mode = {'file': stat.S_IFREG, 'dir': stat.S_IFDIR}[ftype] attr.st_size = size attr.filename = os.path.basename(path) self.attributes = attr
def list_folder(self, path): path = path.rstrip('/') entries = [] if path == '': for model in ('collection', 'user'): info = paramiko.SFTPAttributes() info.st_size = 0 info.st_mode = 0o777 | stat.S_IFDIR info.filename = model.encode('utf8') entries.append(info) elif path in ('/user', '/collection'): model = path[1:] for doc in ModelImporter.model(model).list(user=self.server.girderUser): entries.append(_stat(doc, model)) else: obj = lookUpPath(path, filter=False, user=self.server.girderUser) return self._list(obj['model'], obj['document']) return entries
def _stat(doc, model): info = paramiko.SFTPAttributes() if model == 'user': info.filename = doc['login'].encode('utf8') else: info.filename = doc['name'].encode('utf8') if 'updated' in doc: info.st_mtime = time.mktime(doc['updated'].timetuple()) elif 'created' in doc: info.st_mtime = time.mktime(doc['created'].timetuple()) if model == 'file': info.st_mode = 0o777 | stat.S_IFREG info.st_size = _getFileSize(doc) else: info.st_mode = 0o777 | stat.S_IFDIR info.st_size = 0 return info
def stat(self, path): rv = paramiko.SFTPAttributes() rv.st_size = len(self.server.filename2stringio[path].getvalue()) return rv
def stat(self): attr = paramiko.SFTPAttributes() attr.st_mode = (stat.S_IFREG | stat.S_IREAD | stat.S_IWRITE) return attr
def __init__(self, name: str, mode: str = 'r', bufsize: int = -1, hostname: str = None, port: int = 22, username: str = None, password: str = None, keyfile: str = None, root: str = None, mkdir: bool = True, *args, **kwargs): """Open/create a file over a SSH connection. Args: name: Name of file. mode: Open mode. bufsize: Size of buffer size for SFTP connection. hostname: Name of host to connect to. port: Port on host to connect to. username: Username to log in on host. password: Password for username. keyfile: Path to SSH key on local machine. root: Root directory on host. mkdir: Whether or not to automatically create directories. """ # no root given? if root is None: raise ValueError('No root directory given.') # filename is not allowed to start with a / or contain .. if name.startswith('/') or '..' in name: raise ValueError('Only files within root directory are allowed.') # build filename self.filename = name full_path = os.path.join(root, name) # connect self._ssh = paramiko.SSHClient() self._ssh.load_system_host_keys() self._ssh.connect(hostname, port=port, username=username, password=password, key_filename=keyfile) self._sftp = self._ssh.open_sftp() # need to create directory? path = os.path.dirname(full_path) try: self._sftp.chdir(path) except IOError: if mkdir: self._sftp.mkdir(path) else: raise ValueError( 'Cannot write into sub-directory with disabled mkdir option.' ) # just the code from paramiko.SFTPClient.open imode = 0 if ('r' in mode) or ('+' in mode): imode |= paramiko.sftp.SFTP_FLAG_READ if ('w' in mode) or ('+' in mode) or ('a' in mode): imode |= paramiko.sftp.SFTP_FLAG_WRITE if 'w' in mode: imode |= paramiko.sftp.SFTP_FLAG_CREATE | paramiko.sftp.SFTP_FLAG_TRUNC if 'a' in mode: imode |= paramiko.sftp.SFTP_FLAG_CREATE | paramiko.sftp.SFTP_FLAG_APPEND if 'x' in mode: imode |= paramiko.sftp.SFTP_FLAG_CREATE | paramiko.sftp.SFTP_FLAG_EXCL attrblock = paramiko.SFTPAttributes() t, msg = self._sftp._request(paramiko.sftp.CMD_OPEN, full_path, imode, attrblock) if t != paramiko.sftp.CMD_HANDLE: raise paramiko.SFTPError('Expected handle') handle = msg.get_binary() # init FileIO paramiko.SFTPFile.__init__(self, self._sftp, handle, mode, bufsize)
def start_subsystem(self, name, transport, channel): self.sock = channel username = self.get_server().transport.get_username() home = 'root' if username == 'root' else 'home/%s' % username self._send_server_version() while True: try: t, data = self._read_packet() msg = paramiko.Message(data) req_num = msg.get_int() if t == sftp.CMD_REALPATH: path = msg.get_text() self.log_event(sftp_cmd='realpath', path=path) self.send_response( req_num, sftp.CMD_NAME, 1, '/' + clean_path(home + '/' + path.replace('\\', '/')), '', paramiko.SFTPAttributes(), ) elif t == sftp.CMD_OPEN: self.log_event(sftp_cmd='open', path=msg.get_text()) self.send_response(req_num, sftp.CMD_HANDLE, secrets.token_hex(4)) elif t == sftp.CMD_CLOSE: self.log_event(sftp_cmd='close') self.send_status(req_num, paramiko.SFTP_OK) elif t == sftp.CMD_READ: self.log_event(sftp_cmd='read') self.send_status(req_num, paramiko.SFTP_BAD_MESSAGE) elif t == sftp.CMD_WRITE: self.log_event(sftp_cmd='write') self.send_status(req_num, paramiko.SFTP_OK) elif t == sftp.CMD_REMOVE: self.log_event(sftp_cmd='remove', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_RENAME: self.log_event( sftp_cmd='rename', source=msg.get_text(), destination=msg.get_text(), ) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_MKDIR: self.log_event(sftp_cmd='mkdir', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_RMDIR: self.log_event(sftp_cmd='rmdir', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_OPENDIR: self.log_event(sftp_cmd='opendir', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_READDIR: self.log_event(sftp_cmd='readdir') self.send_status(req_num, paramiko.SFTP_BAD_MESSAGE) elif t == sftp.CMD_STAT: self.log_event(sftp_cmd='stat', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_LSTAT: self.log_event(sftp_cmd='lstat', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_FSTAT: self.log_event(sftp_cmd='fstat') self.send_status(req_num, paramiko.SFTP_BAD_MESSAGE) elif t == sftp.CMD_SETSTAT: self.log_event(sftp_cmd='setstat', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_FSETSTAT: self.log_event(sftp_cmd='fsetstat') self.send_status(req_num, paramiko.SFTP_BAD_MESSAGE) elif t == sftp.CMD_READLINK: self.log_event(sftp_cmd='readlink', path=msg.get_text()) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_SYMLINK: self.log_event( sftp_cmd='symlink', target=msg.get_text(), link_name=msg.get_text(), ) self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) elif t == sftp.CMD_EXTENDED: tag = msg.get_text() if tag == 'check-file': self.send_status(req_num, paramiko.SFTP_BAD_HANDLE) elif tag.endswith('@openssh.com'): self.send_status(req_num, paramiko.SFTP_PERMISSION_DENIED) self.log_event(sftp_cmd='extended', tag=tag) else: self.log_event(sftp_cmd_raw=t) self.send_status(req_num, paramiko.SFTP_OP_UNSUPPORTED) except EOFError: return except Exception as e: traceback.print_exc() return