Esempio n. 1
0
def fgetattr(filename, compact=False):
    """Get the list of set system attributes for file specified by 'path'.
    Returns a list of verbose attributes by default. If 'compact' is True,
    return a string consisting of compact option identifiers."""

    from pkg.misc import force_text
    if not isinstance(filename, six.string_types):
        raise TypeError("filename must be string type")

    cattrs = ffi.new("char[F_ATTR_ALL]")
    response = ffi.new("nvlist_t **")
    # ffi.gc return a new cdata object that points to the same data. Later,
    # when this new cdata object is garbage-collected, the destructor
    # (in this case 'lib.nvlist_free' will be called.
    response[0] = ffi.gc(response[0], lib.nvlist_free)
    bval = ffi.new("boolean_t *")
    pair = ffi.NULL
    next_pair = ffi.new("nvpair_t *")
    attr_list = []

    fd = os.open(filename, os.O_RDONLY)
    if fd == -1:
        raise OSError(ffi.errno, os.strerror(ffi.errno), filename)

    if lib.fgetattr(fd, lib.XATTR_VIEW_READWRITE, response):
        os.close(fd)
        raise OSError(ffi.errno, os.strerror(ffi.errno), filename)
    os.close(fd)

    count = 0
    pair = lib.nvlist_next_nvpair(response[0], pair)
    while pair != ffi.NULL:
        name = lib.nvpair_name(pair)
        next_pair = lib.nvlist_next_nvpair(response[0], pair)
        # we ignore all non-boolean attrs
        if lib.nvpair_type(pair) != lib.DATA_TYPE_BOOLEAN_VALUE:
            pair = next_pair
            continue

        if lib.nvpair_value_boolean_value(pair, bval) != 0:
            raise OSError("could not read attr value")

        if bval[0]:
            if compact:
                if count >= F_ATTR_ALL:
                    raise OSError("Too many system attributes found")
                cattrs[count] = lib.attr_to_option(lib.name_to_attr(name))[0]
                count += 1
            else:
                # ffi.string returns a bytes
                string = force_text(ffi.string(name))
                if string:
                    attr_list.append(string)
        pair = next_pair

    if compact:
        cattrs = force_text(ffi.string(cattrs))
        return cattrs
    return attr_list
Esempio n. 2
0
def get_attr_dict():
    """Get a dictionary containing all supported system attributes in the form:

        { <verbose_name>: <compact_option>,
          ...
        }
    """

    from pkg.misc import force_text
    sys_attrs = {}
    for i in range(F_ATTR_ALL):
        if not is_supported(i):
            continue
        key = force_text(ffi.string(lib.attr_to_name(i)))
        value = force_text(ffi.string(lib.attr_to_option(i)))
        sys_attrs.setdefault(key, value)
    return sys_attrs