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 _file_attr(self, mtime=0): attr = SFTPAttributes() attr.st_uid = os.getuid() attr.st_gid = os.getgid() attr.st_mode = stat.S_IRUSR + stat.S_IFREG attr.st_mtime = mtime attr.st_atime = mtime attr.st_ctime = mtime 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 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