fancycode / pylzma

Python bindings for the LZMA library
http://www.joachim-bauch.de/projects/pylzma/
GNU Lesser General Public License v2.1
234 stars 72 forks source link

Feature: py7zlib: archive file mode and format API #62

Open miurahr opened 5 years ago

miurahr commented 5 years ago

It is better that py7zlib have an API to retrieve file modes. I've been working for it and PR soon.

And I'd like to propose supporting Unix file mode (rwx) p7zip on UNIX has an attribute trick to store a file mode, when FILE_ATTRIBUTE_UNIX_EXTENSION bit is set, then store a mode in high 16bit.

https://github.com/warexify/p7zip/blob/master/CPP/7zip/Archive/7z/7zUpdate.cpp#L882

 // p7zip uses the trick to store posix attributes in high 16 bits
      if (ui.Attrib & 0x8000)
      {
        unsigned st_mode = ui.Attrib >> 16;
// st_mode = 00111;

Here is a proposal of API design.

f = fp.getmember(name)
if f.is_executable():
    print("%s is an executable" % name)
if f.is_readonly():
    print("%s is a read only file" % name)

There are some design issue that how it should be in Windows platform. A question is whether it checks unix attribute extension only on Unix/Linux/Mac, or always checks it on any platform.

miurahr commented 5 years ago

Here is a use case; https://github.com/miurahr/aqtinstall/blob/topic-pylzma04/aqt/installer.py

FILE_ATTRIBUTE_READONLY = 0x01
FILE_ATTRIBUTE_HIDDEN = 0x02
FILE_ATTRIBUTE_SYSTEM = 0x04
FILE_ATTRIBUTE_DIRECTORY = 0x10
FILE_ATTRIBUTE_ARCHIVE = 0x20
FILE_ATTRIBUTE_NORMAL = 0x80
FILE_ATTRIBUTE_TEMPORARY = 0x0100

def _set_file_property(target_f, outfilename):
    if os.name == 'posix':
        st_mode = target_f.get_mode()
        if st_mode is not None:
            os.chmod(outfilename, st_mode)
    elif os.name == 'nt':
        if target_f.is_hidden():
            win32api.SetFileAttributes(outfilename, FILE_ATTRIBUTE_HIDDEN)
        if target_f.is_readonly():
            win32api.SetFileAttributes(outfilename, FILE_ATTRIBUTE_READONLY)
        if target_f.is_system_file():
            win32api.SetFileAttributes(outfilename, FILE_ATTRIBUTE_SYSTEM)

It is redundant in Windows code, so it is better to introduce get_file_attribute() method to grab all bits like st_mode.

miurahr commented 5 years ago

It also should support symbolic link. There is a stat module in python which handle file modes and formats, we can help user to use stat.ISLINK(mode) to check archive file formats.