/*
* 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
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
*
* This module contains code required to remove special contents from
* the contents file when a pkgrm is done on a system upgraded to use
* the new database.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <fnmatch.h>
#include <pkgstrct.h>
#include "pkglib.h"
#include <libintl.h>
/* This specifies the maximum length of a contents file line read in. */
"insufficient memory."
"an access failure."
"root path too long"
/*
* strcompare
*
* This function is used by qsort to sort an array of special contents
* rule strings. This array must be sorted to facilitate efficient
* rule processing. See qsort(3c) regarding qsort compare functions.
*/
static int
{
if (i < 0)
return (-1);
if (i > 0)
return (1);
return (0);
}
/*
* match
*
* This function determines whether a file name (pc) matches a rule
* from the special contents file (pcrule). We assume that neither
* string is ever NULL.
*
* Return: 1 on match, 0 on no match.
* Side effects: none.
*/
static int
{
int wild = 0;
wild = 1;
}
if (!wild) {
return (1);
} else {
int j;
if (j == 0)
return (1);
}
return (0);
}
/*
* search_special_contents
*
* This function assumes that a series of calls will be made requesting
* whether a given path matches the special contents rules or not. We
* assume that
*
* a) the special_contents array is sorted
* b) the calls will be made with paths in a sorted order
*
* Given that, we can keep track of where the last search ended and
* begin the new search at that point. This reduces the cost of a
* special contents matching search to O(n) from O(n^2).
*
* ppcSC A pointer to an array of special contents obtained via
* get_special_contents().
* path A path: determine whether it matches the special
* contents rules or not.
* piX The position in the special_contents array we have already
* arrived at through searching. This must be initialized to
* zero before initiating a series of search_special_contents
* operations.
*
* Example:
* {
* int i = 0, j, max;
* char **ppSC = NULL;
* if (get_special_contents(NULL, &ppcSC, &max) != 0) exit(1);
* for (j = 0; paths != NULL && paths[j] != NULL; j++) {
* if (search_special_contents(ppcSC, path[j], &i)) {
* do_something_with_special_path(path[j]);
* }
* }
* }
*
* Return: 1 if there is a match, 0 otherwise.
* Side effects: The value of *piX will be set between calls to this
* function. To make this function thread safe, use search arrays.
* Also: Nonmatching entries are eliminated, set to NULL.
*/
static int
{
int wild;
return (0);
int j, k;
(*piX)++;
continue;
}
/*
* Depending on whether the path string compared with the
* rule, we take different actions. If the path is less
* than the rule, we keep the rule. If the path equals
* the rule, we advance the rule (as long as the rule is
* not a wild card). If the path is greater than the rule,
* we have to advance the rule list until we are less or equal
* again. This way we only have to make one pass through the
* rules, as we make one pass through the path strings. We
* assume that the rules and the path strings are sorted.
*/
if (k < 0) {
if (wild == 0)
return (0);
return (1);
break;
} else if (k == 0) {
return (x);
} else {
/* One last try. */
return (1);
/*
* As pcpath > ppcSC[*piX] we have passed up this
* rule - it cannot apply. Therefore, we do not
* need to retain it. Removing the rule will make
* subsequent searching more efficient.
*/
(*piX)++;
}
}
return (0);
}
/*
* get_special_contents
*
* Retrieves the special contents file entries, if they exist. These
* are sorted. We do not assume the special_contents file is in sorted
* order.
*
* pcroot The root of the install database. If NULL assume '/'.
* pppcSC A pointer to a char **. This pointer will be set to
* point at NULL if there is no special_contents file or
* to a sorted array of strings, NULL terminated, otherwise.
* piMax The # of entries in the special contents result.
*
* Returns: 0 on no error, nonzero on error.
* Side effects: the pppcSC pointer is set to point at a newly
* allocated array of pointers to strings.. The caller must
* free this buffer. The value of *piMax is set to the # of
* entries in ppcSC.
*/
static int
{
int e, i;
char **ppc;
struct stat s;
/* Initialize the return values. */
*piMax = 0;
pcroot = "/";
}
return (1);
}
} else {
>= PATH_MAX) {
return (1);
}
}
errno = 0;
return (0); /* No special contents file. Do nothing. */
/* Could not open special contents which exists */
return (1);
}
return (1);
}
int n;
continue;
}
*piMax = i;
return (0);
}
/*
* free_special_contents
*
* This function frees special_contents which have been allocated using
* get_special_contents.
*
* pppcSC A pointer to a buffer allocated using get_special_contents.
* max The number of entries allocated.
*
* Result: None.
* Side effects: Frees memory allocated using get_special_contents and
* sets the pointer passed in to NULL.
*/
static void
{
int i;
return;
}
/*
* get_path
*
* Return the first field of a string delimited by a space.
*
* pcline A line from the contents file.
*
* Return: NULL if an error. Otherwise a string allocated by this
* function. The caller must free the string.
* Side effects: none.
*/
static char *
{
return (NULL);
return (pc);
}
/*
* generate_special_contents_rules
*
* This procedure will generate an array of integers which will be a mask
* to apply to the ppcfextra array. If set to 1, then the content must be
* added to the contents file. Otherwise it will not be: The old contents
* file will be used for this path value, if one even exists.
*
* ient The number of ppcfextra contents installed.
* ppcfent The contents installed.
* ppcSC The rules (special contents)
* max The number of special contents rules.
* ppiIndex The array of integer values, determining whether
* individual ppcfextra items match special contents rules.
* This array will be created and set in this function and
* returned.
*
* Return: 0 success, nonzero failure
* Side effects: allocates an array of integers that the caller must free.
*/
static int
{
int i, j;
return (1);
}
/*
* For each entry in ppcfextra, check if it matches a rule.
* If it does not, set the entry in the index to -1.
*/
&j, max) == 1) {
pi[i] = 1;
} else {
pi[i] = 0;
}
}
/*
* In case we ran out of rules before contents, we will not use
* those contents. Make sure these contents are set to 0 and
* will not be copied from the ppcfent array into the contents
* file.
*/
for (i = i; i < ient; i++)
pi[i] = 0;
return (0);
}
/*
* pathcmp
*
* Compare a path to a cfent. It will match either if the path is
* equal to the cfent path, or if the cfent is a symbolic or link
* and *that* matches.
*
* path a path
* pent a contents entry
*
* Returns: as per strcmp
* Side effects: none.
*/
static int
{
int i;
char *p, *q;
/* A path without additional chars strcmp's to less */
if (i == 0)
i = -1;
} else {
/* Break the link path into two pieces. */
*p = '\0';
/* Compare the first piece. */
/* If equal we must compare the second piece. */
if (i == 0) {
q = p + 1;
}
/* Restore the link path. */
*p = '=';
}
} else {
}
return (i);
}
/*
* -----------------------------------------------------------------------
* Externally visible function.
*/
/*
* special_contents_remove
*
* Given a set of entries to remove and an alternate root, this function
* will do everything required to ensure that the entries are removed
* from the contents file if they are listed in the special_contents
* file. The contents file will get changed only in the case that the
* entire operation has succeeded.
*
* ient The number of entries.
* ppcfent The entries to remove.
* pcroot The alternate install root. Could be NULL. In this
* case, assume root is '/'
*
* Result: 0 on success, nonzero on failure. If an error occurs, an
* error string will get output to standard error alerting the user.
* Side effects: The contents file may change as a result of this call,
* such that lines in the in the file will be changed or removed.
* If the call fails, a t.contents file may be left behind. This
* temporary file should be removed subsequently.
*/
int
{
int i, j; /* Indexes into contents & special contents */
time_t t; /* Used to create a timestamp comment. */
goto remove_done;
}
result = 1;
goto remove_done;
}
/* Check if there are no special contents actions to take. */
goto remove_done;
}
>= PATH_MAX ||
>= PATH_MAX) {
result = -1;
goto remove_done;
}
} else {
>= PATH_MAX ||
>= PATH_MAX) {
result = -1;
goto remove_done;
}
}
/* Open the temporary contents file to write, contents to read. */
/*
* This is not a problem since no contents means nothing
* to remove due to special contents rules.
*/
result = 0;
goto remove_done;
}
/* can't write contents file, something is wrong. */
result = 1;
goto remove_done;
}
/* Given the access test above, this should not happen. */
result = 1;
goto remove_done;
}
/* open t.contents failed */
result = 1;
goto remove_done;
}
!= 0) {
result = 1;
goto remove_done;
}
/*
* Copy contents to t.contents unless there is an entry in
* the ppcfent array which corresponds to an index set to 1.
*
* These items are the removed package contents which matche an
* entry in ppcSC (the special_contents rules).
*
* Since both the contents and rules are sorted, we can
* make a single efficient pass.
*/
/*
* Note: This could be done better: We should figure out
* which are the last 2 lines and only trim those off.
* This will suffice to do this and will only be done as
* part of special_contents handling.
*/
if (line[0] == '#')
continue; /* Do not copy the final 2 comment lines */
int k;
while (piIndex[i] == 0)
i++;
if (i < ient)
if (k < 0 || i >= ient) {
/* Just copy contents -> t.contents */
/*EMPTY*/
} else if (k == 0) {
/* We have a match. Do not copy the content. */
i++;
continue;
} else while (i < ient) {
/*
* This is a complex case: The content
* entry is further along alphabetically
* than the rule. Skip over all rules which
* apply until we come to a rule which is
* greater than the current entry, or equal
* to it. If equal, do not copy, otherwise
* do copy the entry.
*/
if (piIndex[i] == 0) {
i++;
continue;
>= 0) {
i++;
if (k == 0) {
break;
}
} else {
/* path < rule, end special case */
break;
}
}
/*
* Avoid copying the old content when path == rule
* This occurs when the complex case ends on a match.
*/
if (k == 0)
continue;
}
/* Failing to write output would be catastrophic. */
result = 1;
break;
}
}
if (result == 0) {
result = 1;
}
} else {
/*
* Do not output a diagnostic message. This condition
* occurs only when we are unable to clean up after
* a failure. A temporary file will linger.
*/
result = 1;
}
}
return (result);
}