This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: filemode() in tarfile.py makes wrong file mode strings
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: akuchling Nosy List: akuchling, pingupeter
Priority: normal Keywords:

Created on 2004-08-27 12:29 by pingupeter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
filemodes.py pingupeter, 2004-08-27 12:34 Test script
Messages (4)
msg22242 - (view) Author: Peter Loje Hansen (pingupeter) Date: 2004-08-27 12:29
filemode(07111) produces the string "---x--x--x" in
stead of the correct output "---s--s--t"

This small test script provides a correct filemode
function:

========================================
#!/usr/bin/env python

#---------------------------------------------------------
# Bits used in the mode field, values in octal.
#---------------------------------------------------------
S_IFLNK = 0120000        # symbolic link
S_IFREG = 0100000        # regular file
S_IFBLK = 0060000        # block device
S_IFDIR = 0040000        # directory
S_IFCHR = 0020000        # character device
S_IFIFO = 0010000        # fifo

TSUID   = 04000          # set UID on execution
TSGID   = 02000          # set GID on execution
TSVTX   = 01000          # reserved

TUREAD  = 0400           # read by owner
TUWRITE = 0200           # write by owner
TUEXEC  = 0100           # execute/search by owner
TGREAD  = 0040           # read by group
TGWRITE = 0020           # write by group
TGEXEC  = 0010           # execute/search by group
TOREAD  = 0004           # read by other
TOWRITE = 0002           # write by other
TOEXEC  = 0001           # execute/search by other

filemode_table = (
    (S_IFLNK, "l",
     S_IFREG, "-",
     S_IFBLK, "b",
     S_IFDIR, "d",
     S_IFCHR, "c",
     S_IFIFO, "p"),
    (TUREAD,  "r"),
    (TUWRITE, "w"),
    (TUEXEC,  "x", TSUID, "S", TUEXEC|TSUID, "s"),
    (TGREAD,  "r"),
    (TGWRITE, "w"),
    (TGEXEC,  "x", TSGID, "S", TGEXEC|TSGID, "s"),
    (TOREAD,  "r"),
    (TOWRITE, "w"),
    (TOEXEC,  "x", TSVTX, "T", TOEXEC|TSVTX, "t"))


def filemode(mode):
    """Convert a file's mode to a string of the form
       -rwxrwxrwx.
       Used by TarFile.list()
    """
    s = ""
    for t in filemode_table:
        p = "-"
        for i in range(0, len(t), 2):
            if mode & t[i] == t[i]:
                p = t[i+1]
        s += p
    return s


def filemode_old(mode):
    """Convert a file's mode to a string of the form
       -rwxrwxrwx.
       Used by TarFile.list()
    """
    s = ""
    for t in filemode_table:
        while True:
            if mode & t[0] == t[0]:
                s += t[1]
            elif len(t) > 2:
                t = t[2:]
                continue
            else:
                s += "-"
            break
    return s


def main():
    print "filemode_old:\tfilemode:"
    for p in (
        00777 | 00000,
        00777 | 01000,
        00777 | 04000,
        00777 | 07000,
        00000 | 07000,
        00111 | 07000
        ):
        print "%s\t%s" % (filemode_old(p), filemode(p))


if __name__ == "__main__":
    main()
========================================

Regards
Peter Loje Hansen

msg22243 - (view) Author: Peter Loje Hansen (pingupeter) Date: 2004-08-27 12:40
Logged In: YES 
user_id=1112055

Sorry for the broken formatting. I have uploaded the test
script in stead.
msg22244 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2004-10-20 11:21
Logged In: YES 
user_id=11375

Patch #1043972 is a fix for this bug.
msg22245 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2004-10-20 11:57
Logged In: YES 
user_id=11375

Fixed by committing patch #1043972. Thanks for your bug report!
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40835
2004-08-27 12:29:13pingupetercreate