def get_mode_string(mode):
    """
    Convert a file's mode to a string of the form '-rwxrwxrwx'.
    Taken (simplified) from cpython 3.3 stat module: https://hg.python.org/cpython/file/3.3/Lib/stat.py
    """
    # pylint: disable=invalid-name,unused-variable,too-many-locals

    # Constants used as S_IFMT() for various file types
    # (not all are implemented on all systems)

    S_IFDIR = 0o040000  # directory
    S_IFCHR = 0o020000  # character device
    S_IFBLK = 0o060000  # block device
    S_IFREG = 0o100000  # regular file
    S_IFIFO = 0o010000  # fifo (named pipe)
    S_IFLNK = 0o120000  # symbolic link
    S_IFSOCK = 0o140000  # socket file

    # Names for permission bits

    S_ISUID = 0o4000  # set UID bit
    S_ISGID = 0o2000  # set GID bit
    S_ENFMT = S_ISGID  # file locking enforcement
    S_ISVTX = 0o1000  # sticky bit
    S_IREAD = 0o0400  # Unix V7 synonym for S_IRUSR
    S_IWRITE = 0o0200  # Unix V7 synonym for S_IWUSR
    S_IEXEC = 0o0100  # Unix V7 synonym for S_IXUSR
    S_IRWXU = 0o0700  # mask for owner permissions
    S_IRUSR = 0o0400  # read by owner
    S_IWUSR = 0o0200  # write by owner
    S_IXUSR = 0o0100  # execute by owner
    S_IRWXG = 0o0070  # mask for group permissions
    S_IRGRP = 0o0040  # read by group
    S_IWGRP = 0o0020  # write by group
    S_IXGRP = 0o0010  # execute by group
    S_IRWXO = 0o0007  # mask for others (not in group) permissions
    S_IROTH = 0o0004  # read by others
    S_IWOTH = 0o0002  # write by others
    S_IXOTH = 0o0001  # execute by others

    # yapf:disable
    _filemode_table = (
        ((S_IFLNK, 'l'),
         (S_IFREG, '-'),
         (S_IFBLK, 'b'),
         (S_IFDIR, 'd'),
         (S_IFCHR, 'c'),
         (S_IFIFO, 'p')),

        ((S_IRUSR, 'r'),),
        ((S_IWUSR, 'w'),),
        ((S_IXUSR | S_ISUID, 's'),
         (S_ISUID, 'S'),
         (S_IXUSR, 'x')),

        ((S_IRGRP, 'r'),),
        ((S_IWGRP, 'w'),),
        ((S_IXGRP | S_ISGID, 's'),
         (S_ISGID, 'S'),
         (S_IXGRP, 'x')),

        ((S_IROTH, 'r'),),
        ((S_IWOTH, 'w'),),
        ((S_IXOTH | S_ISVTX, 't'),
         (S_ISVTX, 'T'),
         (S_IXOTH, 'x'))
    )
    # yapf:enable

    perm = []
    for table in _filemode_table:
        for bit, char in table:
            if mode & bit == bit:
                perm.append(char)
                break
        else:
            perm.append('-')
    return ''.join(perm)