Exemple #1
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
Exemple #2
0
def fsetattr(filename, attr):
    """Set system attributes for a file. The system attributes can either be
    passed as a list of verbose attribute names or a string that consists of
    a sequence of compact attribute options.

    Raises ValueError for invalid system attributes or OSError (with errno set)
    if any of the library calls fail.

    Input examples:
      verbose attributes example: ['hidden', 'archive', 'sensitive', ... ]

    compact attributes example: 'HAT'
    """

    from pkg.misc import force_bytes
    if not isinstance(filename, six.string_types):
        raise TypeError("filename must be string type")
    if not attr:
        raise TypeError("{0} is not a valid system attribute".format(attr))

    compact = False
    sys_attr = -1
    request = ffi.new("nvlist_t **")
    request[0] = ffi.gc(request[0], lib.nvlist_free)

    if lib.nvlist_alloc(request, lib.NV_UNIQUE_NAME, 0) != 0:
        raise OSError(ffi.errno, os.strerror(ffi.errno))

    # A single string indicates system attributes are passed in compact
    # form (e.g. AHi), verbose attributes are read as a list of strings.
    if isinstance(attr, six.string_types):
        compact = True

    for c in attr:
        c = force_bytes(c)
        if compact:
            sys_attr = lib.option_to_attr(c)
        else:
            sys_attr = lib.name_to_attr(c)

        if sys_attr == lib.F_ATTR_INVAL:
            if compact:
                raise ValueError("{0} is not a valid compact system "
                                 "attribute".format(attr))
            else:
                raise ValueError("{0} is not a valid verbose system "
                                 "attribute".format(attr))
        if not is_supported(sys_attr):
            if compact:
                raise ValueError("{0} is not a supported compact system "
                                 "attribute".format(attr))
            else:
                raise ValueError("{0} is not a supported verbose system "
                                 "attribute".format(attr))
        if lib.nvlist_add_boolean_value(request[0], lib.attr_to_name(sys_attr),
                                        1) != 0:
            raise OSError(ffi.errno, os.strerror(ffi.errno))

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

    if lib.fsetattr(fd, lib.XATTR_VIEW_READWRITE, request[0]):
        os.close(fd)
        raise OSError(ffi.errno, os.strerror(ffi.errno), filename)
    os.close(fd)