#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
"""module describing a generic packaging object
This module contains the Action class, which represents a generic packaging
object."""
import errno
import os
try:
# Some versions of python don't have these constants.
except AttributeError:
import six
import stat
import types
from . import _common
# Directories must precede all filesystem object actions; hardlinks must follow
# all filesystem object actions (except links). Note that user and group
# actions precede file actions (so that the system permits chown'ing them to
# users and groups that may be delivered during the same operation); this
# initial contents of those files.
"set",
"depend",
"group",
"user",
"dir",
"file",
"hardlink",
"link",
"driver",
"unknown",
"license",
"legacy",
"signature"
)))
# EmptyI for argument defaults; no import to avoid pkg.misc dependency.
def quote_attr_value(s):
"""Returns a properly quoted version of the provided string suitable for
use as an attribute value for actions in string form."""
if " " in s or "'" in s or "\"" in s or s == "":
if "\"" not in s:
return '"{0}"'.format(s)
elif "'" not in s:
return "'{0}'".format(s)
return s
"""This metaclass automatically assigns a subclass of Action a
namespace_group member if it hasn't already been specified. This is a
convenience for classes which are the sole members of their group and
don't want to hardcode something arbitrary and unique."""
nsg = None
# We only look at subclasses of Action, and we ignore multiple
# inheritance.
# Iterate through the inheritance chain to see if any
# parent class has a namespace_group member, and grab
# its value.
if c == Action:
break
if nsg is not None:
break
# If the class didn't have a namespace_group member
# already, assign one. If we found one in our traversal
# above, use that, otherwise make one up.
if "namespace_group" not in dict:
if not nsg:
# Prepare for the next class.
"""Returns the serialized state of this object in a format
that that can be easily stored using JSON, pickle, etc."""
"""Allocate a new object using previously serialized state
obtained via getstate()."""
# metaclass-assignment; pylint: disable=W1623
"""Class representing a generic packaging object.
An Action is a very simple wrapper around two dictionaries: a named set
of data streams and a set of attributes. Data streams generally
represent files on disk, and attributes represent metadata about those
files.
"""
# 'name' is the name of the action, as specified in a manifest.
# 'key_attr' is the name of the attribute whose value must be unique in
# the namespace of objects represented by a particular action. For
# instance, a file's key_attr would be its pathname. Or a driver's
# key_attr would be the driver name. When 'key_attr' is None, it means
# that all attributes of the action are distinguishing.
key_attr = None
# 'globally_identical' is True if all actions representing a single
# object on a system must be identical.
# 'refcountable' is True if the action type can safely be delivered
# multiple times.
# 'namespace_group' is a string whose value is shared by actions which
# share a common namespace. As a convenience to the classes which are
# the sole members of their group, this is set to a non-None value for
# subclasses by the NSG metaclass.
namespace_group = None
# 'ordinality' is a numeric value that is used during action comparison
# to determine action sorting.
# 'unique_attrs' is a tuple listing the attributes which must be
# identical in order for an action to be safely delivered multiple times
# (for those that can be).
unique_attrs = ()
# The version of signature used.
# Most types of actions do not have a payload.
# Python 3 will ignore the __metaclass__ field, but it's still useful
# for class attribute access.
# __init__ is provided as a native function (see end of class
# declaration).
"""This function sets the data field of the action.
The "data" parameter is the file to use to set the data field.
It can be a string which is the path to the file, a function
which provides the file when called, or a file handle to the
file."""
if data is None:
return
def file_opener():
try:
except EnvironmentError as e:
raise \
return
# Data is not None, and is callable.
return
return
try:
except AttributeError:
try:
try:
except (AttributeError, TypeError):
try:
try:
except (AttributeError,
except (AttributeError, TypeError):
# Raw data was provided; fake a
# file object.
except EnvironmentError as e:
"""Serialize the action into manifest form.
The form is the name, followed by the SHA1 hash, if it exists,
(this use of a positional SHA1 hash is deprecated, with
pkg.*hash.* attributes being preferred over positional hashes)
followed by attributes in the form 'key=value'. All fields are
space-separated; fields with spaces in the values are quoted.
Note that an object with a datastream may have been created in
such a way that the hash field is not populated, or not
populated with real data. The action classes do not guarantee
that at the time that __str__() is called, the hash is properly
computed. This may need to be done externally.
"""
try:
if h:
if "=" not in h and " " not in h and \
'"' not in h:
out += " " + h
else:
except AttributeError:
# No hash to stash.
pass
# Sort so that we get consistent action attribute ordering.
# We pay a performance penalty to do so, but it seems worth it.
for k in sattrs:
# Octal literal in Python 3 begins with "0o", such as
# "0o755", but we want to keep "0755" in the output.
try:
except KeyError:
# If we can't find the attribute, it must be the
# hash. 'h' will only be in scope if the block
# at the start succeeded.
v = h
for lmt in v
])
elif " " in v or "'" in v or "\"" in v or v == "":
if "\"" not in v:
elif "'" not in v:
else:
else:
return out
"""Create a stable string representation of an action that
is deterministic in its creation. If creating a string from an
action is non-deterministic, then manifest signing cannot work.
The parameter "a" is the signature action that's going to use
the string produced. It's needed for the signature string
action, and is here to keep the method signature the same.
"""
# Any changes to this function or any subclasses sig_str mean
# Action.sig_version must be incremented.
def q(s):
if " " in s or "'" in s or "\"" in s or s == "":
if "\"" not in s:
return '"{0}"'.format(s)
elif "'" not in s:
return "'{0}'".format(s)
else:
return '"{0}"'.format(
else:
return s
# Sort so that we get consistent action attribute ordering.
# We pay a performance penalty to do so, but it seems worth it.
# Octal literal in Python 3 begins with "0o", such as
# "0o755", but we want to keep "0755" in the output.
])
elif " " in v or "'" in v or "\"" in v or v == "":
if "\"" not in v:
elif "'" not in v:
else:
else:
return out
return True
return False
return False
return True
return True
else:
return False
return True
else:
return False
"""Returns True if other represents a non-ignorable change from
self. By default, this means two actions are different if any
of their attributes are different.
When cmp_policy is CMP_UNSIGNED, check the unsigned versions
of hashes instead of signed versions of hashes on both actions.
This prevents comparing all hash attributes as simple value
comparisons, and instead compares only non-hash attributes,
then tests the most preferred hash for equivalence. When
cmp_policy is CMP_ALL, compare using all attributes.
"""
# Comparing different action types.
return True
# Are all attributes identical? Most actions don't change, so
# a simple equality comparison should be sufficient.
if self.has_payload:
# If payload present, must also compare some
# object attributes.
return False
else:
return False
# If action has payload, perform hash comparison first. For
# actions with a payload, hash attributes usually change, but
# other attributes do not.
if self.has_payload:
# If both actions are for elf files, determine
# if we should compare based on elf content
# hash.
# If caller requested unsigned
# comparison, and no policy is
# available, compare based on elf
# content hash.
elif pkgplan:
# Avoid circular import.
import CONTENT_UPDATE_POLICY
CONTENT_UPDATE_POLICY) == \
"when-required":
# If policy is available and
# allows it, then compare based
# on elf content hash.
# digest.get_common_preferred_hash() tries to return the
# most preferred hash attribute and falls back to
# returning the action.hash values if there are no other
# common hash attributes, and will throw an
# AttributeError if one or the other actions don't have
# an action.hash attribute.
try:
return True
# If there's no common preferred hash, we have
# to treat these actions as different.
return True
except AttributeError:
# If action.hash is set on exactly one of self
# and other, then we're trying to compare
# actions of disparate subclasses.
"hash"):
raise AssertionError(
"attempt to compare a "
"{0} action to a {1} action".format(
else:
# If hashes were equal or not applicable, then compare remaining
# attributes.
return True
for a in sset:
x = sattrs[a]
y = oattrs[a]
if x != y:
return True
else:
return True
return False
"""Returns the attributes that have different values between
other and self."""
l.add(k)
l.add(k)
return (l)
"""Removes duplicate values from values which are lists."""
"""Generate the information needed to index this action.
This method, and the overriding methods in subclasses, produce
a list of four-tuples. The tuples are of the form
(action_name, key, token, full value). action_name is the
string representation of the kind of action generating the
tuple. 'file' and 'depend' are two examples. It is required to
not be None. Key is the string representation of the name of
the attribute being indexed. Examples include 'basename' and
'path'. Token is the token to be searched against. Full value
is the value to display to the user in the event this token
matches their query. This is useful for things like categories
where what matched the query may be a substring of what the
desired user output is.
"""
# Indexing based on the SHA-1 hash is enough for the generic
# case.
return [
]
return []
"""Given an image root, return the installed path of the action
if it has a installable payload (i.e. 'path' attribute)."""
try:
except KeyError:
return
""" Return the distinguishing name for this action,
preceded by the type of the distinguishing name. For
example, for a file action, 'path' might be the
key_attr. So, the distinguished name might be
"""
return "{0}: {1}".format(
"""Make directory specified by 'path' with given permissions, as
well as all missing parent directories. Permissions are
specified by the keyword arguments 'mode', 'uid', and 'gid'.
The difference between this and os.makedirs() is that the
permissions specify only those of the leaf directory. Missing
parent directories inherit the permissions of the deepest
existing directory. The leaf directory will also inherit any
permissions not explicitly set."""
# generate the components of the path. The first
# element will be empty since all absolute paths
# always start with a root specifier.
# Fill in the first path with the root of the filesystem
# (this ends up being something like C:\ on windows systems,
# and "/" on unix.
for i, e in g:
# os.path.isdir() follows links, which isn't
# desirable here.
try:
except OSError as e:
break
raise
if p == path:
# Allow caller to handle target by
# letting the operation continue,
# and whatever error is encountered
# being raised to the caller.
break
err_txt = _("Unable to create {path}; a "
"parent directory {p} has been replaced "
"with a file or link. Please restore the "
"parent directory and try again.").format(
**locals())
else:
# XXX Because the filelist codepath may create
# directories with incorrect permissions (see
# pkgtarfile.py), we need to correct those permissions
# here. Note that this solution relies on all
# intermediate directories being explicitly created by
# the packaging system; otherwise intermediate
# directories will not get their permissions corrected.
try:
except OSError as e:
raise
return
for i, e in g:
try:
except OSError as e:
raise
err_txt = _("Unable to create {path}; a "
"parent directory {p} has been replaced "
"with a file or link. Please restore the "
"parent directory and try again.").format(
**locals())
try:
except OSError as e:
raise
# Create the leaf with any requested permissions, substituting
# missing perms with the parent's perms.
try:
except OSError as e:
raise
"""Return the names of any facet or variant tags in this
action."""
# Hot path; grab reference to attrs and use list comprehensions
# to construct the results. This is faster than iterating over
# attrs once and appending to two lists separately.
"""Return the VariantCombinationTemplate that the variant tags
of this action define."""
)))
"""Strip actions of attributes which are unnecessary once
those actions have been installed in an image. Stripped
actions are saved in an images stripped action cache and used
for conflicting actions checks during image planning
operations."""
# strip out variant and facet information
continue
# keep unique attributes
continue
# keep file action overlay attributes
continue
# keep specified keys
continue
continue
"""Remove all variant tags from the attrs dictionary."""
if k.startswith("variant."):
"""Returns a tuple of lists of the form (errors, warnings,
info). The error list will be empty if the action has been
correctly installed in the given image."""
return [], [], []
"""Private, common validation logic for filesystem objects that
returns a list of tuples of the form (attr_name, error_message).
"""
errors = []
else:
# Common case for our packages is 4 so place that first.
# The group, mode, and owner attributes are intentionally only
# required during publication as it is anticipated that the
# there will eventually be defaults for these (possibly parent
# directory, etc.). By only requiring these attributes here,
# it prevents publication of packages for which no default
# currently exists, while permitting future changes to remove
# that limitaiton and use sane defaults.
if not bad_mode:
try:
except (TypeError, ValueError):
else:
if bad_mode:
if not raw_mode:
"value must be of the form '644', "
"'0644', or '04755'.")))
"specified once")))
else:
"mode; value must be of the form '644', "
try:
except AttributeError:
"once")))
try:
except AttributeError:
"once")))
return errors
"""Returns a tuple of the form (owner, group) containing the uid
and gid of the filesystem object. If the attributes are missing
or invalid, an InvalidActionAttributesError exception will be
raised."""
# The attribute may be missing.
# Now attempt to determine the uid and raise an appropriate
# exception if it can't be.
try:
except KeyError:
if not owner:
# Owner was missing; let validate raise a more
# informative error.
# Otherwise, the user is unknown; attempt to report why.
# What package owned the user that was removed?
"installed; the owner '{owner}' was "
"removed by '{src_fmri}'.").format(
# This indicates an error on the part of the
# caller; the user should have been added
# before attempting to install the file.
raise
# If this spot was reached, the user wasn't part of
# the operation plan and is completely unknown or
# invalid.
"installed; '{owner}' is an unknown "
# The attribute may be missing.
# Now attempt to determine the gid and raise an appropriate
# exception if it can't be.
try:
except KeyError:
if not group:
# Group was missing; let validate raise a more
# informative error.
# Otherwise, the group is unknown; attempt to report
# why.
# What package owned the group that was removed?
"installed; the group '{group}' was "
"removed by '{src_fmri}'.").format(
# This indicates an error on the part of the
# caller; the group should have been added
# before attempting to install the file.
raise
# If this spot was reached, the group wasn't part of
# the operation plan and is completely unknown or
# invalid.
"installed; '{group}' is an unknown "
"""Common verify logic for filesystem objects."""
errors = []
warnings = []
info = []
assert ftype is not None
tmap = {
}
else:
try:
except KeyError:
owner))
owner = None
try:
except KeyError:
group))
group = None
lstat = None
try:
except OSError as e:
# It's acceptable for files with
# preserve=legacy to be missing;
# nothing more to validate.
_("Missing: {0} does not exist").format(
else:
_("Unexpected Error: {0}").format(e))
if abort:
"'{expected}'").format(
"({found_id:d})' should be '{expected_name} "
"({expected_id:d})'").format(
"({found_id})' should be '{expected_name} "
"({expected_id})'").format(
"{expected}").format(
"""Returns True if the action transition requires a
datastream."""
return False
"""return list containing value of named attribute."""
try:
except KeyError:
return []
return [value]
return value
"""Returns references to paths in action."""
return []
"""Client-side method that performs pre-install actions."""
pass
"""Client-side method that installs the object."""
pass
"""Client-side method that performs post-install actions."""
pass
"""Client-side method that performs pre-remove actions."""
pass
"""Client-side method that removes the object."""
pass
"""Shared logic for removing file and link objects."""
# Necessary since removal logic is reused by install.
if not fmri:
try:
except EnvironmentError as e:
# Already gone; don't care.
return
# User has replaced item with mountpoint, or a
# package has been poorly implemented.
err_txt = _("Unable to remove {0}; it is in use "
"as a mountpoint. To continue, please "
"unmount the filesystem at the target "
# os.path.ismount() is broken for lofs
# filesystems, so give a more generic
# error.
err_txt = _("Unable to remove {0}; it is in "
"use by the system, another process, or "
# Was expecting a directory in this failure
# case, it is not, so raise the error.
raise
# Raise these permissions exceptions as-is.
raise
# An unexpected error.
# Attempting to remove a directory as performed above
# gives EPERM. First, try to remove the directory,
# if it isn't empty, salvage it.
try:
except OSError as e:
# Raise permissions exceptions as-is.
raise
# An unexpected error.
"""Client-side method that performs post-remove actions."""
pass
"""Callables in excludes list returns True
if action is to be included, False if
not"""
for c in excludes:
return False
return True
"""Performs additional validation of action attributes that
for performance or other reasons cannot or should not be done
during Action object creation. An ActionError exception (or
subclass of) will be raised if any attributes are not valid.
This is primarily intended for use during publication or during
error handling to provide additional diagonostics.
'fmri' is an optional package FMRI (object or string) indicating
what package contained this action.
"""
"""Common validation logic for all action types.
'fmri' is an optional package FMRI (object or string) indicating
what package contained this action.
'numeric_attrs' is a list of attributes that must have an
integer value.
'raise_errors' is a boolean indicating whether errors should be
raised as an exception or returned as a list of tuples of the
form (attr_name, error_message).
'single_attrs' is a list of attributes that should only be
specified once.
"""
errors = []
elif attr in numeric_attrs:
try:
except (TypeError, ValueError):
for attr in required_attrs:
if not val or \
if raise_errors and errors:
return errors
"""Verifies that the specified path doesn't contain one or more
symlinks relative to the image root. Raises an
ActionExecutionError exception if path check fails."""
if parent_path in valid_dirs:
return
if parent_path == real_parent_path:
return
# Now test each component of the parent path until one is found
# to be a link. When found, that's the parent that has been
# redirected to some other location.
while 1:
# No parent directories up to the root were
# found to be links, so assume this is ok.
return
# We've found the parent that changed locations.
break
# Drop the final component.
err_txt = _("Cannot install '{final_path}'; parent directory "
"{parent_dir} is a link to {parent_target}. To "
"continue, move the directory to its original location and "
# create a bound method (no unbound method in Python 3)
# create an unbound method