[ python-Bugs-1017553 ] filemode() in tarfile.py makes wrong file mode strings

SourceForge.net noreply at sourceforge.net
Fri Aug 27 14:40:27 CEST 2004


Bugs item #1017553, was opened at 2004-08-27 14:29
Message generated for change (Comment added) made by pingupeter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1017553&group_id=5470

Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Peter Loje Hansen (pingupeter)
Assigned to: Nobody/Anonymous (nobody)
Summary: filemode() in tarfile.py makes wrong file mode strings

Initial Comment:
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



----------------------------------------------------------------------

>Comment By: Peter Loje Hansen (pingupeter)
Date: 2004-08-27 14:40

Message:
Logged In: YES 
user_id=1112055

Sorry for the broken formatting. I have uploaded the test
script in stead.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1017553&group_id=5470


More information about the Python-bugs-list mailing list