Skip to content

Commit

Permalink
[refactor] Get rid of rarfile CRC error workaround because a fix has …
Browse files Browse the repository at this point in the history
…been merged upstream
  • Loading branch information
mxmlnkn committed Sep 10, 2024
1 parent 6917e92 commit 310e1e5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 77 deletions.
5 changes: 3 additions & 2 deletions core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ full = [
"libarchive-c ~= 5.1, < 6.0",
"python-xz ~= 0.4.0",
"rapidgzip >= 0.13.1",
"rarfile ~= 4.0",
"rarfile ~= 4.1",
"PySquashfsImage == 0.9.0",
"lz4 ~= 4.0.0",
"python-lzo ~= 1.0",
Expand All @@ -64,7 +64,8 @@ full = [
]
bzip2 = ["rapidgzip >= 0.13.1"]
gzip = ["indexed_gzip >= 1.6.3, < 2.0"]
rar = ["rarfile ~= 4.0"]
# Need >= 4.1 because of https://github.com/markokr/rarfile/issues/73
rar = ["rarfile ~= 4.1"]
squashfs = [
"PySquashfsImage == 0.9.0",
"lz4 ~= 4.0.0",
Expand Down
75 changes: 1 addition & 74 deletions core/ratarmountcore/RarMountSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,6 @@
pass


class RawFileInsideRar(io.RawIOBase):
"""
This class works around the CRC error issue by reopening the file when seeking back.
This will be slower for uncompressed files but not for compressed files because
the seek implementation of rarfile also reopens the file on seeking back.
https://github.com/markokr/rarfile/issues/73
https://rarfile.readthedocs.io/api.html#rarfile.RarExtFile.seek
> On uncompressed files, the seeking works by actual seeks so it’s fast.
> On compressed files it's slow - forward seeking happens by reading ahead,
> backwards by re-opening and decompressing from the start.
"""

def __init__(self, reopen, file_size):
io.RawIOBase.__init__(self)
self.reopen = reopen
self.fileobj = reopen()
self.file_size = file_size

def __enter__(self):
return self

def __exit__(self, exception_type, exception_value, exception_traceback):
self.close()

@overrides(io.RawIOBase)
def close(self) -> None:
self.fileobj.close()

@overrides(io.RawIOBase)
def fileno(self) -> int:
# This is a virtual Python level file object and therefore does not have a valid OS file descriptor!
raise io.UnsupportedOperation()

@overrides(io.RawIOBase)
def seekable(self) -> bool:
return self.fileobj.seekable()

@overrides(io.RawIOBase)
def readable(self) -> bool:
return self.fileobj.readable()

@overrides(io.RawIOBase)
def writable(self) -> bool:
return False

@overrides(io.RawIOBase)
def readall(self) -> bytes:
return self.fileobj.readall()

@overrides(io.RawIOBase)
def readinto(self, buffer) -> int:
return self.fileobj.readinto(buffer)

@overrides(io.RawIOBase)
def read(self, size: int = -1) -> bytes:
return self.fileobj.read(size)

@overrides(io.RawIOBase)
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
if whence == io.SEEK_CUR:
offset += self.tell()
elif whence == io.SEEK_END:
offset += self.file_size

if offset < self.tell():
self.fileobj = self.reopen()
return self.fileobj.seek(offset, io.SEEK_SET)

@overrides(io.RawIOBase)
def tell(self) -> int:
return self.fileobj.tell()


class RarMountSource(MountSource):
# Basically copy paste of ZipMountSource because the interfaces are very similar
# I'm honestly not sure how it works that well as it does. It does have some problems
Expand Down Expand Up @@ -269,7 +196,7 @@ def fileVersions(self, path: str) -> int:
def open(self, fileInfo: FileInfo) -> IO[bytes]:
info = fileInfo.userdata[-1][1]
assert isinstance(info, rarfile.RarInfo)
return cast(IO[bytes], RawFileInsideRar(lambda: self.fileObject.open(info, 'r'), info.file_size))
return self.fileObject.open(info, 'r')

@overrides(MountSource)
def __exit__(self, exception_type, exception_value, exception_traceback):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ dependencies = [
'dataclasses; python_version < "3.7.0"',
"python-xz ~= 0.4.0",
"rapidgzip >= 0.13.1",
"rarfile ~= 4.0",
# Need >= 4.1 because of https://github.com/markokr/rarfile/issues/73
"rarfile ~= 4.1",
"libarchive-c ~= 5.1, < 6.0",
]

Expand Down

0 comments on commit 310e1e5

Please sign in to comment.