mirror of
https://github.com/SELinuxProject/setools
synced 2025-04-11 03:51:26 +00:00
Type/TypeAttribute: Refactor to load attributes on construction.
This commit is contained in:
parent
9f8bb014b8
commit
5de464a226
@ -74,9 +74,18 @@ cdef class Type(BaseType):
|
|||||||
|
|
||||||
"""A type."""
|
"""A type."""
|
||||||
|
|
||||||
|
cdef:
|
||||||
|
readonly object ispermissive
|
||||||
|
list _aliases
|
||||||
|
list _attrs
|
||||||
|
# type_datum_t.s.value is needed by the
|
||||||
|
# alias iterator
|
||||||
|
uint32_t value
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
cdef factory(SELinuxPolicy policy, sepol.type_datum_t *symbol):
|
cdef inline Type factory(SELinuxPolicy policy, sepol.type_datum_t *symbol):
|
||||||
"""Factory function for creating Type objects."""
|
"""Factory function for creating Type objects."""
|
||||||
|
cdef Type t
|
||||||
if symbol.flavor != sepol.TYPE_TYPE:
|
if symbol.flavor != sepol.TYPE_TYPE:
|
||||||
raise ValueError("{0} is not a type".format(
|
raise ValueError("{0} is not a type".format(
|
||||||
policy.type_value_to_name(symbol.s.value - 1)))
|
policy.type_value_to_name(symbol.s.value - 1)))
|
||||||
@ -84,35 +93,24 @@ cdef class Type(BaseType):
|
|||||||
try:
|
try:
|
||||||
return _type_cache[<uintptr_t>symbol]
|
return _type_cache[<uintptr_t>symbol]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
t = Type()
|
t = Type.__new__(Type)
|
||||||
t.policy = policy
|
t.policy = policy
|
||||||
t.handle = symbol
|
t.handle = symbol
|
||||||
_type_cache[<uintptr_t>symbol] = t
|
_type_cache[<uintptr_t>symbol] = t
|
||||||
|
t.value = symbol.s.value
|
||||||
|
t.name = policy.type_value_to_name(symbol.s.value - 1)
|
||||||
|
t.ispermissive = <bint>symbol.flags & sepol.TYPE_FLAGS_PERMISSIVE
|
||||||
return t
|
return t
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
cdef inline void _load_aliases(self):
|
||||||
# shallow copy as all of the members are immutable
|
"""Helper method to load aliases."""
|
||||||
newobj = Type.factory(self.policy, self.handle)
|
if self._aliases is None:
|
||||||
memo[id(self)] = newobj
|
self._aliases = list(self.policy.type_aliases(self))
|
||||||
return newobj
|
|
||||||
|
|
||||||
def __getstate__(self):
|
cdef inline void _load_attributes(self):
|
||||||
return (self.policy, self._pickle())
|
"""Helper method to load attributes."""
|
||||||
|
if self._attrs is None:
|
||||||
def __setstate__(self, state):
|
self._attrs = list(TypeAttributeEbitmapIterator.factory(self.policy, &self.handle.types))
|
||||||
self.policy = state[0]
|
|
||||||
self._unpickle(state[1])
|
|
||||||
|
|
||||||
cdef bytes _pickle(self):
|
|
||||||
return <bytes>(<char *>self.handle)
|
|
||||||
|
|
||||||
cdef _unpickle(self, bytes handle):
|
|
||||||
memcpy(&self.handle, <char *>handle, sizeof(sepol.type_datum_t*))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def ispermissive(self):
|
|
||||||
"""(T/F) the type is permissive."""
|
|
||||||
return <bint>self.handle.flags & sepol.TYPE_FLAGS_PERMISSIVE
|
|
||||||
|
|
||||||
def expand(self):
|
def expand(self):
|
||||||
"""Generator that expands this into its member types."""
|
"""Generator that expands this into its member types."""
|
||||||
@ -120,22 +118,29 @@ cdef class Type(BaseType):
|
|||||||
|
|
||||||
def attributes(self):
|
def attributes(self):
|
||||||
"""Generator that yields all attributes for this type."""
|
"""Generator that yields all attributes for this type."""
|
||||||
return TypeAttributeEbitmapIterator.factory(self.policy, &self.handle.types)
|
self._load_attributes()
|
||||||
|
return iter(self._attrs)
|
||||||
|
|
||||||
def aliases(self):
|
def aliases(self):
|
||||||
"""Generator that yields all aliases for this type."""
|
"""Generator that yields all aliases for this type."""
|
||||||
return self.policy.type_aliases(self)
|
self._load_aliases()
|
||||||
|
return iter(self._aliases)
|
||||||
|
|
||||||
def statement(self):
|
def statement(self):
|
||||||
attrs = list(self.attributes())
|
cdef:
|
||||||
aliases = list(self.aliases())
|
size_t count
|
||||||
stmt = "type {0}".format(self)
|
str stmt
|
||||||
if aliases:
|
|
||||||
if len(aliases) > 1:
|
self._load_attributes()
|
||||||
stmt += " alias {{ {0} }}".format(' '.join(aliases))
|
self._load_aliases()
|
||||||
else:
|
count = len(self._aliases)
|
||||||
stmt += " alias {0}".format(aliases[0])
|
|
||||||
for attr in attrs:
|
stmt = "type {0}".format(self.name)
|
||||||
|
if count > 1:
|
||||||
|
stmt += " alias {{ {0} }}".format(' '.join(self._aliases))
|
||||||
|
elif count == 1:
|
||||||
|
stmt += " alias {0}".format(self._aliases[0])
|
||||||
|
for attr in self._attrs:
|
||||||
stmt += ", {0}".format(attr)
|
stmt += ", {0}".format(attr)
|
||||||
stmt += ";"
|
stmt += ";"
|
||||||
return stmt
|
return stmt
|
||||||
@ -145,9 +150,12 @@ cdef class TypeAttribute(BaseType):
|
|||||||
|
|
||||||
"""A type attribute."""
|
"""A type attribute."""
|
||||||
|
|
||||||
|
cdef list _types
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
cdef factory(SELinuxPolicy policy, sepol.type_datum_t *symbol):
|
cdef inline TypeAttribute factory(SELinuxPolicy policy, sepol.type_datum_t *symbol):
|
||||||
"""Factory function for creating TypeAttribute objects."""
|
"""Factory function for creating TypeAttribute objects."""
|
||||||
|
cdef TypeAttribute a
|
||||||
if symbol.flavor != sepol.TYPE_ATTRIB:
|
if symbol.flavor != sepol.TYPE_ATTRIB:
|
||||||
raise ValueError("{0} is not an attribute".format(
|
raise ValueError("{0} is not an attribute".format(
|
||||||
policy.type_value_to_name(symbol.s.value - 1)))
|
policy.type_value_to_name(symbol.s.value - 1)))
|
||||||
@ -155,11 +163,12 @@ cdef class TypeAttribute(BaseType):
|
|||||||
try:
|
try:
|
||||||
return _typeattr_cache[<uintptr_t>symbol]
|
return _typeattr_cache[<uintptr_t>symbol]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
t = TypeAttribute()
|
a = TypeAttribute.__new__(TypeAttribute)
|
||||||
t.policy = policy
|
_typeattr_cache[<uintptr_t>symbol] = a
|
||||||
t.handle = symbol
|
a.policy = policy
|
||||||
_typeattr_cache[<uintptr_t>symbol] = t
|
a.handle = symbol
|
||||||
return t
|
a.name = policy.type_value_to_name(symbol.s.value - 1)
|
||||||
|
return a
|
||||||
|
|
||||||
def __contains__(self, other):
|
def __contains__(self, other):
|
||||||
for type_ in self.expand():
|
for type_ in self.expand():
|
||||||
@ -170,23 +179,27 @@ cdef class TypeAttribute(BaseType):
|
|||||||
|
|
||||||
def expand(self):
|
def expand(self):
|
||||||
"""Generator that expands this attribute into its member types."""
|
"""Generator that expands this attribute into its member types."""
|
||||||
return TypeEbitmapIterator.factory(self.policy, &self.handle.types)
|
if self._types is None:
|
||||||
|
self._types = list(TypeEbitmapIterator.factory(self.policy, &self.handle.types))
|
||||||
|
|
||||||
|
return iter(self._types)
|
||||||
|
|
||||||
def attributes(self):
|
def attributes(self):
|
||||||
"""Generator that yields all attributes for this type."""
|
"""Generator that yields all attributes for this type."""
|
||||||
raise SymbolUseError("{0} is an attribute, thus does not have attributes.".format(self))
|
raise SymbolUseError("{0} is an attribute, thus does not have attributes.".format(
|
||||||
|
self.name))
|
||||||
|
|
||||||
def aliases(self):
|
def aliases(self):
|
||||||
"""Generator that yields all aliases for this type."""
|
"""Generator that yields all aliases for this type."""
|
||||||
raise SymbolUseError("{0} is an attribute, thus does not have aliases.".format(self))
|
raise SymbolUseError("{0} is an attribute, thus does not have aliases.".format(self.name))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ispermissive(self):
|
def ispermissive(self):
|
||||||
"""(T/F) the type is permissive."""
|
"""(T/F) the type is permissive."""
|
||||||
raise SymbolUseError("{0} is an attribute, thus cannot be permissive.".format(self))
|
raise SymbolUseError("{0} is an attribute, thus cannot be permissive.".format(self.name))
|
||||||
|
|
||||||
def statement(self):
|
def statement(self):
|
||||||
return "attribute {0};".format(self)
|
return "attribute {0};".format(self.name)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -308,7 +321,7 @@ cdef class TypeAliasHashtabIterator(HashtabIterator):
|
|||||||
i = TypeAliasHashtabIterator()
|
i = TypeAliasHashtabIterator()
|
||||||
i.policy = policy
|
i.policy = policy
|
||||||
i.table = table
|
i.table = table
|
||||||
i.primary = primary.handle.s.value
|
i.primary = primary.value
|
||||||
i.reset()
|
i.reset()
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user