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

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

Open
miurahr opened this issue Mar 27, 2019 · 2 comments
Open

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

miurahr opened this issue Mar 27, 2019 · 2 comments

Comments

@miurahr
Copy link

miurahr commented Mar 27, 2019

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
Copy link
Author

miurahr commented Mar 27, 2019

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
Copy link
Author

miurahr commented Mar 28, 2019

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.

@miurahr miurahr changed the title Feature: py7zlib: file mode detection API Feature: py7zlib: archive file mode and format API Mar 28, 2019
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