Documentation ¶
Overview ¶
Package util contains utility functions that are specific to the resource engine.
Index ¶
- Constants
- func AutoEdgeCombiner(ae ...engine.AutoEdge) (engine.AutoEdge, error)
- func B64ToRes(str string) (engine.Res, error)
- func CleanError(err error) string
- func GetGID(group string) (int, error)
- func GetUID(username string) (int, error)
- func LangFieldNameToStructFieldName(kind string) (map[string]string, error)
- func LangFieldNameToStructType(kind string) (map[string]*types.Type, error)
- func LowerStructFieldNameToFieldName(res engine.Res) (map[string]string, error)
- func ParseSymbolicModes(modes []string, from os.FileMode, allowAssign bool) (os.FileMode, error)
- func ResPathUID(res engine.Res) string
- func ResToB64(res engine.Res) (string, error)
- func ResToParamValues(res engine.Res) (map[string]types.Value, error)
- func RestartUnit(ctx context.Context, conn *dbus.Conn, unit string) error
- func StopUnit(ctx context.Context, conn *dbus.Conn, unit string) error
- func StrListCmp(x, y []string) error
- func StrPtrCmp(x, y *string) error
- func StructFieldCompat(st1 interface{}, key1 string, st2 interface{}, key2 string) error
- func StructTagToFieldName(stptr interface{}) (map[string]string, error)
Constants ¶
const ( ModeUser uint32 = 64 ModeGroup uint32 = 8 ModeOther uint32 = 1 ModeRead uint32 = 4 ModeWrite uint32 = 2 ModeExec uint32 = 1 ModeSetU uint32 = 4 ModeSetG uint32 = 2 ModeSticky uint32 = 1 )
Constant bytes for the who (u, g, or o) and the what (r, w, x, s, or t).
const ( // DBusInterface is the dbus interface that contains genereal methods. DBusInterface = "org.freedesktop.DBus" // DBusAddMatch is the dbus method to receive a subset of dbus broadcast // signals. DBusAddMatch = DBusInterface + ".AddMatch" // DBusRemoveMatch is the dbus method to remove a previously defined // AddMatch rule. DBusRemoveMatch = DBusInterface + ".RemoveMatch" // DBusSystemd1Path is the base systemd1 path. DBusSystemd1Path = "/org/freedesktop/systemd1" // DBusSystemd1Iface is the base systemd1 interface. DBusSystemd1Iface = "org.freedesktop.systemd1" // DBusSystemd1ManagerIface is the systemd manager interface used for // interfacing with systemd units. DBusSystemd1ManagerIface = DBusSystemd1Iface + ".Manager" // DBusRestartUnit is the dbus method for restarting systemd units. DBusRestartUnit = DBusSystemd1ManagerIface + ".RestartUnit" // DBusStopUnit is the dbus method for stopping systemd units. DBusStopUnit = DBusSystemd1ManagerIface + ".StopUnit" // DBusSignalJobRemoved is the name of the dbus signal that produces a // message when a dbus job is done (or has errored.) DBusSignalJobRemoved = "JobRemoved" )
Variables ¶
This section is empty.
Functions ¶
func AutoEdgeCombiner ¶
AutoEdgeCombiner takes any number of AutoEdge structs, and combines them into a single one, so that the logic from each one can be built separately, and then combined using this utility. This makes implementing different AutoEdge generators much easier. This respects the Next() and Test() API, and ratchets through each AutoEdge entry until they have all run their course.
func CleanError ¶
CleanError takes the engine errors and prints them on a single line. TODO: maybe we can improve this here, it's a bit ugly.
func GetGID ¶
GetGID returns the GID of a group. It supports a GID or a group name. Caller should first check group is not empty. It will return an error if it can't lookup the GID or group name.
func GetUID ¶
GetUID returns the UID of an user. It supports an UID or an username. Caller should first check user is not empty. It will return an error if it can't lookup the UID or username.
func LangFieldNameToStructFieldName ¶
LangFieldNameToStructFieldName returns the mapping from lang (AST) field names to field name as used in the struct. The logic here is a bit strange; if the resource has struct tags, then it uses those, otherwise it falls back to using the lower case versions of things. It might be clever to combine the two so that tagged fields are used as such, and others are used in lowercase, but this is currently not implemented. TODO: should this behaviour be changed?
func LangFieldNameToStructType ¶
LangFieldNameToStructType returns the mapping from lang (AST) field names, and the expected type in our type system for each. XXX: Should this return unification variables instead of variant types?
func LowerStructFieldNameToFieldName ¶
LowerStructFieldNameToFieldName returns a mapping from the lower case version of each field name to the actual field name. It only returns public fields. It returns an error if it finds a collision.
func ParseSymbolicModes ¶
ParseSymbolicModes parses a slice of symbolic mode strings. By default it will only accept the assignment input (=), but if allowAssign is true, then all symbolic mode strings (=, +, -) can be used as well.
Symbolic mode is expected to be a string of who (user, group, other) then the operation (=, +, -) then the change (read, write, execute, setuid, setgid, sticky).
Eg: ug=rw
If you repeat yourself in the slice (eg. u=rw,u=w) ParseSymbolicModes will fail with an error.
func ResPathUID ¶
ResPathUID returns a unique resource UID based on its name and kind. It's safe to use as a token in a path, and as a result has no slashes in it.
func ResToParamValues ¶
ResToParamValues returns a list of field names and their corresponding values if they are non-zero. This is meant for testing, and should be improved for robustness or with tests if it's ever used for value extraction. This also contains a hack to specifically print from resources that contain interface fields too. Consider moving that into types.ValueOf after testing if that doesn't break other code paths.
func RestartUnit ¶
RestartUnit resarts the given dbus unit and waits for it to finish starting.
func StrListCmp ¶
StrListCmp compares two lists of strings. If they are not the same length or do not contain identical strings in the same order, then this errors.
func StrPtrCmp ¶
StrPtrCmp compares two pointers to strings. If they aren't both nil or aren't both of the same value, then this errors.
func StructFieldCompat ¶
StructFieldCompat returns whether a send struct and key is compatible with a recv struct and key. This inputs must both be a ptr to a string, and a valid key that can be found in the struct tag. The (1) first values are for send, and the (2) second values are for recv. TODO: add a bool to decide if *string to string or string to *string is okay.
func StructTagToFieldName ¶
StructTagToFieldName returns a mapping from recommended alias to actual field name. It returns an error if it finds a collision. It uses the `lang` tags. It must be passed a ptr to a struct or it will error.
Types ¶
This section is empty.