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

stat.filemode returns "-" for sockets and unknown types #62113

Closed
tiran opened this issue May 6, 2013 · 7 comments
Closed

stat.filemode returns "-" for sockets and unknown types #62113

tiran opened this issue May 6, 2013 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented May 6, 2013

BPO 17913
Nosy @vstinner, @tiran, @iritkatriel
Files
  • filemode.patch
  • file_modes_strings.patch: New mode2str function which fixes this bug, and the reverse function str2mode.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-09-13.06:10:42.613>
    created_at = <Date 2013-05-06.09:11:20.771>
    labels = ['type-bug', 'library']
    title = 'stat.filemode returns "-" for sockets and unknown types'
    updated_at = <Date 2021-09-13.06:10:42.612>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2021-09-13.06:10:42.612>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-09-13.06:10:42.613>
    closer = 'christian.heimes'
    components = ['Library (Lib)']
    creation = <Date 2013-05-06.09:11:20.771>
    creator = 'christian.heimes'
    dependencies = []
    files = ['30152', '30662']
    hgrepos = []
    issue_num = 17913
    keywords = ['patch']
    message_count = 7.0
    messages = ['188496', '188607', '191576', '191578', '200761', '401602', '401686']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'christian.heimes', 'rmilne', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue17913'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @tiran
    Copy link
    Member Author

    tiran commented May 6, 2013

    The function stat.filemode() has a fallback to "-" for file type when it is unable to properly detect the type of a file. This gives wrong results for any file type that is not in the lookup table.

    For example it doesn't check for S_ISSOCK:

    >>> s = os.stat("/var/run/sdp") t
    >>> stat.filemode(s.st_mode)
    '-rw-rw-rw-'
    $ ls -la /var/run/sdp
    srw-rw-rw- 1 root root 0 Mai  2 16:08 /var/run/sdp

    Also see bpo-11016 for more file types.

    I'm going to work on the matter soonish.

    @tiran tiran added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels May 6, 2013
    @vstinner
    Copy link
    Member

    vstinner commented May 6, 2013

    Can you maybe add an unit test?

    @rmilne
    Copy link
    Mannequin

    rmilne mannequin commented Jun 21, 2013

    I've added some changes to the module which (I believe) not only fix this bug, but add the reverse function, converting a permission string to a number.

    The diff was made against Lib/stat.py @ 84234:f32dad3a243e, which was the most recent version of the file I could find. My patch is intended for Python 2.7, but should work on 3.3 (though I haven't tested it there).

    My 'filemode' function ('mode2str') is not as short and elegant as the current one, but then it should fix this bug, and it doesn't require duplicating so many of the module's constants in a lookup table. It also includes a check to make sure mode characters (like 'p', 's', '-', etc) are defined for every numeric mode in the module... so long as their names start with the prefix "S_IF" (... which may lead to conflicts with bpo-17924).

    I may also have gone beyond the scope, and added code to recognise the git link file mode (see http://stackoverflow.com/questions/737673/how-to-read-the-mode-field-of-git-ls-trees-output), but this extension should be easy to recognise and remove.

    Lastly, there are some minor whitespace and sorting changes (notably, S_IF* defs sorted by mode num).

    As for tests, I couldn't find any for this module in Lib/test. I have at least included a command line test which exhaustively tests the encoding and decoding of every valid file mode. If you want, I've even a test script which creates an example of every valid file type (apart from socket and git link types), with every valid file mode!

    @rmilne
    Copy link
    Mannequin

    rmilne mannequin commented Jun 21, 2013

    I already see my name change will cause problems. The tarfile.py (Lib/tarfile.py:277) module relies on the function being named 'filemode'.

    As the stat.filemode function is relatively new, I don't know how many other modules rely on it. I changed the name because I thought the new name (mode2str) better described what the function does, and is orthogonal to the reverse function (str2mode).

    But, of course, I've no issue with whatever the functions are finally named... I'll leave the decision to someone with a bit more experience.

    @tiran
    Copy link
    Member Author

    tiran commented Oct 21, 2013

    The C code in Python 3.4's _stat.c module already handle unsupported types correctly. I'm going to apply my patch to the Python implementation in 2.7, 3.3 and 3.4 soonish. Or is somebody against a fix of 2.7 and 3.3?

    @iritkatriel
    Copy link
    Member

    I think this is fixed now

    >>> s = os.stat('/var/run/vpncontrol.sock')
    >>> stat.filemode(s.st_mode)
    'srw-------'

    iritkatriel@Irits-MBP cpython % ls -l /var/run/vpncontrol.sock
    srw------- 1 root daemon 0 19 Aug 11:15 /var/run/vpncontrol.sock

    @tiran
    Copy link
    Member Author

    tiran commented Sep 13, 2021

    Thanks for the ping. The issue has been fixed a long time ago.

    @tiran tiran closed this as completed Sep 13, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants