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

Error when closing any 7z archive #14

Open
krateng opened this issue Oct 23, 2022 · 1 comment
Open

Error when closing any 7z archive #14

krateng opened this issue Oct 23, 2022 · 1 comment

Comments

@krateng
Copy link

krateng commented Oct 23, 2022

This happens with almost every 7z archive that contains anything.

With archive.7z being an archive in the current directory:

myfs = fs.archive.open_archive(".","archive.7z")
myfs.close()

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 278, in close
    self._saver.save(self)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 58, in save
    self.to_stream(fs)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 86, in to_stream
    self._to(temp, fs)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/sevenzipfs/__init__.py", line 293, in _to
    _7z.worker.archive(_7z.fp, _7z.files, folder, deref=_7z.dereference)
  File "/home/krateng/.local/lib/python3.10/site-packages/py7zr/py7zr.py", line 1494, in archive
    foutsize, crc = self.write(fp, f, (f.is_symlink and not deref), folder)
  File "/home/krateng/.local/lib/python3.10/site-packages/py7zr/py7zr.py", line 1460, in write
    with f.origin.open(mode="rb") as fd:
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/sevenzipfs/__init__.py", line 37, in open
    return self.fs.openbin(self.path, mode)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/wrapfs.py", line 194, in openbin
    bin_file = _fs.openbin(_path, mode=mode, buffering=-1, **options)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/wrap.py", line 149, in openbin
    return self._rfs.openbin(path, mode, buffering, **options)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/sevenzipfs/__init__.py", line 194, in openbin
    return iocursor.Cursor(decompressed[relpath(_path)].getbuffer())
KeyError: 'randomfileinthearchive.ext'

It also happens when I provide a pre-initialized Filesystem object as the parent of the archive fs and when I use context manager instead of manually closing.

If I create a new archive with only one empty file in it, it works. If that file contains anything, it doesn't work anymore.

The same error happens when trying to read a file.

@krateng
Copy link
Author

krateng commented Oct 24, 2022

After some initial experimenting, I think this

try:
_7z = py7zr.SevenZipFile(self._handle, 'r', password=self._password)
decompressed = _7z.read([_path])
except py7zr.exceptions.PasswordRequired as exc:
raise errors.PermissionDenied(msg="7z archive is password protected", exc=exc)
except lzma.LZMAError as exc:
raise errors.OperationFailed(exc=exc)
finally:
if _7z is not None:
_7z.close()
return iocursor.Cursor(decompressed[relpath(_path)].getbuffer())

needs to pass the relpath to _7z.read. This fixes my issues with reading files, but I haven't looked too much into the overall architecture of the whole package, so I don't know if this clashes with something else.

This now also leads to the archive not closing anymore (even nothing has been written to it) - it seems to be stuck endlessly repeating the function

File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 278, in close
    self._saver.save(self)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 58, in save
    self.to_stream(fs)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/base.py", line 86, in to_stream
    self._to(temp, fs)
  File "/home/krateng/.local/lib/python3.10/site-packages/fs/archive/sevenzipfs/__init__.py", line 294, in _to
    _7z.worker.archive(_7z.fp, _7z.files, folder, deref=_7z.dereference)
  File "/home/krateng/.local/lib/python3.10/site-packages/py7zr/py7zr.py", line 1494, in archive
    foutsize, crc = self.write(fp, f, (f.is_symlink and not deref), folder)
  File "/home/krateng/.local/lib/python3.10/site-packages/py7zr/py7zr.py", line 1461, in write
    insize, foutsize, crc = compressor.compress(fd, fp)
  File "/home/krateng/.local/lib/python3.10/site-packages/py7zr/compressor.py", line 884, in compress
    data = compressor.compress(data)

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