#
# 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
#
#
#
"""
Generic interfaces for manipulating files in an alternate root. There
routines guarantee that if you perform operations on a specified path, and
that path is contained within "root", then those operations will not affect
any files living outside of "root". These routines mainly protect us when
accessing paths which could contain symbolic links which might otherwise
redirect us to an unexpected file system object.
"""
# standard python classes
import errno
import os
import stat
# pkg classes
#
# pkg.syscallat is only needed until we have a newer version of python which
# has native support for all the *at(2) system calls we use below.
# ---------------------------------------------------------------------------
# Misc Functions
#
"""Strip the leading '/' from a path using os.path.split()."""
path_new = None
while True:
if not tail:
break
if path_new:
else:
return path_new
"""Given a file descriptor return the path to that file descriptor."""
# ---------------------------------------------------------------------------
# Functions for accessing files in an alternate image
#
"""A function similar to os.open() that ensures that the path
we're accessing resides within a specified directory subtree.
'root' is a directory that path must reside in.
'path' is a path that is interpreted relative to 'root'. i.e., 'root'
is prepended to path. 'path' can not contain any symbolic links that
would cause an access to be redirected outside of 'root'. If this
happens we'll raise an OSError exception with errno set to EREMOTE
'mode' optional permissions mask used if we create 'path'
'create' optional flag indicating if we should create 'path'
'truncate' optional flag indicating if we should truncate 'path' after
opening it."""
# all paths must be absolute
# we can't truncate a file unless we open it for writing
# if create is true the user must supply a mode mask
# we're going to update root and path so prepare an error
# message with the existing values now.
eremote = _("Path outside alternate root: root={root}, "
# make target into a relative path
# now open the alternate root and get its path
try:
except OSError as e:
raise e
# now open the target file, get its path, and make sure it
# lives in the alternate root
path_fd = None
try:
except OSError as e:
raise e
if not path_fd:
# the file doesn't exist so we should try to create it.
# we'll do this by first opening the directory which
# will contain the file and then using openat within
# that directory.
try:
path_dir_fd = \
except OSError as e:
raise e
# we opened the directory, now create the file
try:
except OSError as e:
raise e
# we created the file
assert path_fd
# verify that the file we opened lives in the alternate root
try:
except OSError as e:
raise e
if truncate:
# the user wanted us to truncate the file
try:
except OSError as e:
raise e
return path_fd
"""A function similar to os.unlink() that ensures that the path
we're accessing resides within a specified directory subtree.
'noent_ok' optional flag indicating if it's ok for 'path' to be
missing.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
# all paths must be absolute
# make target into a relative path
try:
except OSError as e:
return
raise e
try:
except OSError as e:
return
raise e
return
"""A function similar to os.rename() that ensures that the path
we're accessing resides within a specified directory subtree.
'src' and 'dst' are paths that are interpreted relative to 'root'.
i.e., 'root' is prepended to both. 'src' and 'dst' can not contain
any symbolic links that would cause an access to be redirected outside
of 'root'. If this happens we'll raise an OSError exception with
errno set to EREMOTE
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
# all paths must be absolute
# make target into a relative path
try:
except OSError as e:
raise e
try:
except OSError as e:
raise e
return
"""A function similar to os.mkdir() that ensures that the path we're
opening resides within a specified directory subtree.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
# all paths must be absolute
# make target into a relative path
try:
except OSError as e:
raise e
return
"""A function similar to os.stat() that ensures that the path
we're accessing resides within a specified directory subtree.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
try:
except OSError as e:
raise e
return si
"""A function similar to os.path.isdir() that ensures that the path
we're accessing resides within a specified directory subtree.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
try:
except OSError as e:
return False
raise e
return True
return False
"""A function similar to os.path.exists() that ensures that the path
we're accessing resides within a specified directory subtree.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
try:
except OSError as e:
return False
raise e
return True
"""A function similar to filecmp.cmp() that ensures that the path
we're accessing resides within a specified directory subtree.
For all other parameters, refer to the 'ar_open' function
for an explanation of their usage and effects."""
try:
while True:
# we're done
break
break
except OSError as e:
if fd1:
if fd2:
raise e
return diff
"""A function that attempts to determine if a user or root pkg(7)
managed image can be found at 'root'. If 'root' does point to a
pkg(7) image, then we return the relative path to the image metadata
directory."""
#
# why would an image have two pkg metadata directories.
# is this image corrupt?
#
return None
if user_img:
return image.img_user_prefix
if root_img:
return image.img_root_prefix
return None