Skip to content

Commit

Permalink
spoof mmap result with a spooky mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
huettenhain committed Sep 27, 2024
1 parent 7ae65d0 commit 74faa22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion refinery/lib/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __exit__(self, *args):
self.stream.seek(self.cursor, io.SEEK_SET)


class MemoryFile(Generic[T], io.IOBase):
class MemoryFile(Generic[T]):
"""
A thin wrapper around (potentially mutable) byte sequences which gives it the
features of a file-like object.
Expand Down
12 changes: 10 additions & 2 deletions refinery/lib/vfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ def mmap(self, length: int = 0, offset: int = 0) -> MemoryFile:
Emulate the result of an `mmap` call to the virtual file.
"""
view = memoryview(self.data)
node = self.node
if length:
view = view[offset:offset + length]
fd = MemoryFile(view, read_as_bytes=True, fileno=self.node)
return fd

class _MappedView(bytearray, MemoryFile):
def __init__(self):
MemoryFile.__init__(self, self, True, node)

mapped = _MappedView()
mapped[:] = view

return mapped

def open(self, mode: str) -> MemoryFile:
"""
Expand Down

0 comments on commit 74faa22

Please sign in to comment.