read implementation of Fuse in python not returning as expected, cat: <mountpoint>: bad address

I am trying to fetch data from some repository and am trying to display file contents of any file for instance with cat <filename> inside a repository directory.

def read(self, path, size, offset, fh=None):
	file_content = ''
	path_ele = path.split('/')
	print('***[read]')
	print(path)
	if path.endswith('/') or path[1] == '.':
		print('ok')
		return file_content
	else:
		path = path.split('/')
		repo_name = path[-2]
		file_name = path[-1]
		print(repo_name, file_name)
		for item in self.user.get_user().get_repos():
			if item.name == repo_name:
				files = item.get_dir_contents('/')
				for file_ in files:
					if file_name == file_.name:
						file_content = item.get_file_contents(file_name).decoded_content
						print(len(file_content.decode('utf-8')))
						print(type(file_content.decode('utf-8')))
						return file_content

When I do cat on a file in a repository directory it gives me an error caused by the following line

assert retsize <= size, 'actual amount read %d greater than expected %d' % (retsize, size)

in fusepys read function link.

Answers

You do not respect all parameters of read, i.e. size and offset. Certain programs or commands read only chunks of files at a time. This means that they expect to be able to read x bytes (size) starting at position y bytes (offset). So your primary mistake in your code snipped is that you just return the entire file for every read operation.

A fix could start with something trivial like returning file_content[offset:(offset+size)]. When I say "start", you have to keep in mind that you must also raise proper errors if the offset or size (for the given offset) passed into your read function are out of bounds.

Posted on by s-m-e