Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_rarfile does not set the pos to the file header #106

Open
0xMattina opened this issue Jul 29, 2024 · 0 comments
Open

is_rarfile does not set the pos to the file header #106

0xMattina opened this issue Jul 29, 2024 · 0 comments

Comments

@0xMattina
Copy link

rarfile/rarfile.py

Lines 348 to 367 in db1df33

def get_rar_version(xfile):
"""Check quickly whether file is rar archive.
"""
with XFile(xfile) as fd:
buf = fd.read(len(RAR5_ID))
if buf.startswith(RAR_ID):
return RAR_V3
elif buf.startswith(RAR5_ID):
return RAR_V5
return 0
def is_rarfile(xfile):
"""Check quickly whether file is rar archive.
"""
try:
return get_rar_version(xfile) > 0
except OSError:
# File not found or not accessible, ignore
return False

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Test code:

import tarfile
from io import BytesIO

import rarfile

if __name__ == '__main__':
    with open(r"d:\test.zip", "rb") as fp_file:
        fp_io = BytesIO(fp_file.read())

    print("before <is_tarfile> pos:", fp_io.tell())
    print("<is_tarfile> result:", tarfile.is_tarfile(fp_io))
    print("after <is_tarfile> pos:", fp_io.tell())
    print()
    print("before <is_rarfile> pos:", fp_io.tell())
    print("<is_rarfile> result:", rarfile.is_rarfile(fp_io))
    print("after <is_rarfile> pos:", fp_io.tell())

Output:

before <is_tarfile> pos: 0
<is_tarfile> result: False
after <is_tarfile> pos: 0

before <is_rarfile> pos: 0
<is_rarfile> result: False
after <is_rarfile> pos: 8

When the args xfile is BytesIO, is_rarfile don't set pos to the value before call.
I tried some other libraries, and tarfile is a std library, it will set pos to the value before call.
I think if is_rarfile does the same, it will improve the logic and consistency of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant