def stat(self, path): """ Return an L{SFTPAttributes} object for a path on the server, or an error code. If your server supports symbolic links (also known as "aliases"), you should follow them. (L{lstat} is the corresponding call that doesn't follow symlinks/aliases.) @param path: the requested path (relative or absolute) to fetch \ file statistics for. @type path: str @return: an attributes object for the given file, or an SFTP error \ code (like L{SFTP_PERMISSION_DENIED}). @rtype: L{SFTPAttributes} I{or error code} """ leaf = self.tree.get_leaf(path, update=False) if leaf is None: leaf = self.tree.get_leaf(path, update=True) if leaf is None: return SFTP_NO_SUCH_FILE sftp_stat = SFTPAttributes() sftp_stat.filename = leaf.name sftp_stat.st_size = int(getattr(leaf.obj, 'size', 1)) if not isinstance(leaf.obj, DataFile): sftp_stat.st_mode = 0o777 | stat.S_IFDIR else: sftp_stat.st_mode = 0o777 | stat.S_IFREG sftp_stat.st_uid = self.user.id sftp_stat.st_gid = 20 sftp_stat.st_atime = time.time() sftp_stat.st_mtime = time.time() return sftp_stat
def _directory(self, name): """Return SFTPAttributes for a directory with given name""" result = SFTPAttributes() result.filename = name result.st_uid = 0 result.st_group = 0 result.st_size = 0 result.st_mode = stat.S_IFDIR | stat.S_IRUSR | stat.S_IXUSR return result
def _get_stat_on_remote_file(self, remote_path): m_time, size = self.os.get_info_from_list_file( self.check_output(self.os.cmd.list_file(remote_path)), remote_path) attr = SFTPAttributes() attr.st_size = int(size) attr.st_mtime = m_time attr.filename = remote_path return attr
def _dir_attr(self, mtime=0): attr = SFTPAttributes() attr.st_size = 1 attr.st_uid = os.getuid() attr.st_gid = os.getgid() attr.st_mtime = mtime attr.st_atime = mtime attr.st_ctime = mtime attr.st_mode = stat.S_IRUSR + stat.S_IXUSR + stat.S_IFDIR return attr
def _file(self, attachment): """Return SFTPAttributes for a given attachment""" if not hasattr(attachment, '_ids'): attachment = self.env['ir.attachment'].browse(attachment) result = SFTPAttributes() result.filename = attachment.datas_fname or attachment.name result.st_uid = 0 result.st_group = 0 result.st_size = attachment.file_size result.st_mode = stat.S_IFREG | stat.S_IRUSR return result
def create_fake_file(filename, size=44): ''' creates sftp compatible file-attrib objects ''' attr = SFTPAttributes() attr.st_size = size attr.st_uid = 0 attr.st_gid = 9 attr.st_mode = 0100666 attr.st_atime = time.time() - 10 attr.st_mtime = time.time() - 5 attr.filename = filename # traversal # remove traversal, store real filename in an extra attrib 'basename' attr.basename = filename.replace("\\", "/").rsplit( '/', 1)[1] if "/" in filename or "\\" in filename else filename LOGGER.info("** %s" % attr.filename) return attr
def from_object(cls, obj): """ Create an `.SFTPAttributes` object from an S3 object in boto format. :param object obj: a dictionary returned by `boto3`. :return: new `.SFTPAttributes` object with the same attribute fields. """ me = SFTPAttributes() if obj.get('Key'): me.st_size = obj.get('Size', 0) me.st_mtime = int(obj.get('LastModified').strftime('%s')) me.filename = os.path.basename(obj.get('Key')) me._flags = SFTPAttributes.FLAG_SIZE + SFTPAttributes.FLAG_PERMISSIONS me.st_mode = stat.S_IFREG + 0o777 # Show always as 777 perms if obj.get('Prefix'): me._flags = SFTPAttributes.FLAG_PERMISSIONS me.st_mode = stat.S_IFDIR + 0o777 # Show always as 777 perms me.filename = obj.get('Prefix').rstrip('/').split('/')[-1] me.st_mtime = int(time.time()) return me
def blob_to_stat(blob): attr = SFTPAttributes() attr.st_mode = 0o0100666 if (blob == None or isinstance(blob, DirectoryBlob)): attr.st_mode &= ~stat.S_IFREG attr.st_mode |= stat.S_IFDIR attr.st_mode |= 0o777 if blob == None: #Assume non-existing files are directories return attr attr.filename = blob.name.strip("/").rsplit("/", 1)[-1] attr.st_size = blob.size attr.st_uid = 1000 attr.st_gid = 1000 ts = blob.time_created.timestamp() attr.st_ctime = ts attr.st_mtime = blob.updated.timestamp() if blob.updated else ts attr.st_atime = blob.updated.timestamp() if blob.updated else ts return attr
def list_folder(self, path): """list files in current remote directory""" try: resp = self.api_client.metadata(self.current_path) log.info('[+] Got response %s..', str(resp)[:40]) if 'contents' in resp: out = [] for f in resp['contents']: attr = SFTPAttributes() attr.filename = os.path.basename(f['path']) if f['is_dir']: attr.st_mode = (stat.S_IFDIR | stat.S_IRWXU) else: attr.st_mode = (stat.S_IFREG | stat.S_IRWXU) attr.st_size = f['bytes'] out.append(attr) except OSError as e: return SFTPServer.convert_errno(e.errno) return out
def _buildSftpFolder(self): attr = SFTPAttributes() attr.st_size = 0 attr.st_mode = S_IFDIR return attr