utilities.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "restore.h"
#include <ctype.h>
#include <errno.h>
#include <syslog.h>
#include <limits.h>
/* LINTED: this file really is necessary */
#include <euc.h>
#include <widec.h>
/*
* Insure that all the components of a pathname exist. Note that
* lookupname() and addentry() both expect complex names as
* input arguments, so a double NULL needs to be added to each name.
*/
void
char *name;
{
char *start;
if (start == 0)
return;
if (*cp != '/')
continue;
*cp = '\0';
}
/* LINTED: result fits in a short */
*cp = '/';
}
}
/*
* Change a name to a unique temporary name.
*/
void
{
char *newname;
/* LINTED: result fits in a short */
/* LINTED: savename guarantees strlen will fit */
}
/*
* Generate a temporary name for an entry.
*/
char *
{
static char name[MAXPATHLEN];
long i = 0;
i++;
return (name);
}
/*
* Rename a file or directory.
*/
void
char *fp;
char *tp;
{
char tobuf[MAXPATHLEN];
char *pathend;
/*
* The to pointer argument is assumed to be either a fully
* specified path (starting with "./") or a simple temporary
* file name (starting with TMPHDR). If passed a simple temp
* file name, we need to set up the descriptors explicitly.
*/
} else {
}
} else
gettext("Warning: cannot rename %s to %s: %s\n"),
} else {
}
}
/*
* Create a new node (directory). Note that, because we have no
* mkdirat() function, fchdir() must be used set up the appropriate
* name space context prior to the call to mkdir() if we are
* operating in attribute space.
*/
void
{
char *cp;
int dfd;
gettext("Warning: cannot create %s: %s"),
return;
}
}
/* LINTED: result fits in a short */
} else {
}
}
}
/*
* Remove an old node (directory). See comment above on newnode()
* for explanation of fchdir() use below.
*/
void
{
char *cp;
int dfd;
/* LINTED: result fits in a short */
/* LINTED: result fits in a short */
gettext("Warning: cannot remove %s: %s"),
return;
}
}
} else {
}
}
}
/*
* Remove a leaf.
*/
void
{
char *cp;
int dfd;
/* LINTED: result fits in a short */
/* LINTED: result fits in a short */
} else {
}
}
/*
* Create a link.
* This function assumes that the context has already been set
* for the link file to be created (i.e., we have "fchdir-ed"
* into attribute space already if this is an attribute link).
*/
int type;
{
char linkbuf[MAXPATHLEN];
char *name;
if (dfd == -1) {
"Warning: cannot restore %s link %s->%s\n"),
goto out;
}
/* No trailing \0 from readlink(2) */
> 0) &&
gettext("Symbolic link %s->%s ok\n"),
goto out;
} else {
"Warning: cannot create symbolic link %s->%s: %s"),
goto out;
}
}
gettext("Hard link %s->%s ok\n"),
goto out;
} else {
"Warning: cannot create hard link %s->%s: %s\n"),
goto out;
}
}
} else {
goto out;
}
else
out:
}
return (result);
}
/*
* Find lowest-numbered inode (above "start") that needs to be extracted.
* Caller knows that a return value of maxino means there's nothing left.
*/
{
continue;
return (start);
}
return (start);
}
/*
* Find highest-numbered inode (below "start") that needs to be extracted.
*/
{
continue;
return (start);
}
return (start);
}
/*
* report on a badly formed entry
*/
void
char *msg;
{
/* Our callers are expected to handle our returning. */
}
/*
* Construct a string indicating the active flag bits of an entry.
*/
char *
{
flagbuf[0] = '\0';
sizeof (flagbuf));
return (&flagbuf[1]);
}
/*
* Check to see if a name is on a dump tape.
*/
char *name;
{
return (ino);
}
/*
* Elicit a reply.
*/
char *question;
{
int c;
do {
"Error reading response\n"));
return (FAIL);
}
return (FAIL);
}
if (isupper(c))
c = tolower(c);
if (c == yesorno[0])
return (GOOD);
return (FAIL);
}
/*
* handle unexpected inconsistencies
*/
/*
* Note that a panic w/ EOF on the tty means all panics will return...
*/
#ifdef __STDC__
#include <stdarg.h>
/* VARARGS1 */
void
{
abort();
done(1);
}
}
#else
#include <varargs.h>
/* VARARGS1 */
void
{
char *msg;
abort();
done(1);
}
#endif
/*
* Locale-specific version of ctime
*/
char *
{
static char buf[256];
return (buf);
}
static int
{
int result = 1;
result = 0;
}
return (result);
}
/*
* Safely open a file.
*/
int
{
static int init_syslog = 1;
int fd;
int working_mode;
int saverr;
char *errtext;
if (init_syslog) {
init_syslog = 0;
}
/*
* Don't want to be spoofed into trashing something we
* shouldn't, thus the following rigamarole. If it doesn't
* exist, we create it and proceed. Otherwise, require that
* what's there be a real file with no extraneous links and
* owned by whoever ran us.
*
* The silliness with using both lstat() and fstat() is to avoid
* race-condition games with someone replacing the file with a
* symlink after we've opened it. If there was an flstat(),
* we wouldn't need the fstat().
*
* The initial open with the hard-coded flags is ok even if we
* are intending to open only for reading. If it succeeds,
* then the file did not exist, and we'll synthesize an appropriate
* complaint below. Otherwise, it does exist, so we won't be
* truncating it with the open.
*/
AT_SYMLINK_NOFOLLOW) < 0) {
return (-1);
}
return (-1);
}
"Unexpected condition detected: %s used to exist, but doesn't any longer\n");
filename);
}
return (-1);
}
AT_SYMLINK_NOFOLLOW) < 0) {
return (-1);
}
return (-1);
}
"Unexpected condition detected: %s's lstat(2) information changed\n");
return (-1);
}
"Unexpected condition detected: %s's stat(2) information changed\n");
return (-1);
}
/*
* If inode, device, or type are wrong, bail out.
*/
"Unexpected condition detected: %s is not a regular file\n");
return (-1);
}
/*
* Bad link count implies someone's linked our
* target to something else, which we probably
* shouldn't step on.
*/
"Unexpected condition detected: %s must have exactly one link\n");
return (-1);
}
/*
* Root might make a file, but non-root might
* need to open it. If the permissions let us
* get this far, then let it through.
*/
post_lstat.st_uid != 0) {
"Unsupported condition detected: %s must be owned by uid %ld or 0\n");
(long)getuid());
(long)getuid());
return (-1);
}
"ftruncate(%s): %s\n",
return (-1);
}
}
} else {
/*
* Didn't exist, but couldn't open it.
*/
return (-1);
}
} else {
/*
* If truncating open succeeded for a read-only open,
* bail out, as we really shouldn't have succeeded.
*/
/* Undo the O_CREAT */
return (-1);
}
}
return (fd);
}
/*
* STDIO version of safe_open. Equivalent to fopen64(...).
*/
FILE *
{
int fd;
int bmode;
/*
* accepts only modes "r", "r+", and "w"
*/
if (smode[0] == 'r') {
}
} else {
gettext("internal error: safe_fopen: invalid mode `%s'\n"),
smode);
return (NULL);
}
/*
* caller is expected to report error.
*/
if (fd >= 0)
}
/*
* Read the contents of a directory.
*/
int
char *name;
{
return (FAIL);
}
}
fp++;
return (FAIL);
}
}
return (GOOD);
}
#ifdef __STDC__
#else
static int gmatch();
static int addg();
#endif
/*
* XXX This value is ASCII (but not language) dependent. In
* ASCII, it is the DEL character (unlikely to appear in paths).
* If you are compiling on an EBCDIC-based machine, re-define
* this (0x7f is '"') to be something like 0x7 (DEL). It's
* either this hack or re-write the expand() algorithm...
*/
#define DELIMCHAR ((char)0x7f)
/*
* Expand a file name.
* "as" is the pattern to expand.
* "rflg" non-zero indicates that we're recursing.
* "ap" is where to put the results of the expansion.
*
* Our caller guarantees that "as" is at least the string ".".
*/
int
char *as;
int rflg;
{
char dir = 0;
char *rescan = 0;
char *s, *cs;
char slash;
char *rs;
char c;
/*
* check for meta chars
*/
slash = 0;
if (*cs++ == 0) {
break;
else
return (0);
} else if (*cs == '/') {
slash++;
}
}
for (;;) {
if (cs == s) {
s = "";
break;
} else if (*--cs == '/') {
*cs = 0;
if (s == cs)
s = "/";
break;
}
}
dir++;
count = 0;
if (*cs == 0)
if (dir) {
/*
* check for rescan
*/
do {
if (*rs == '/') {
*rs = 0;
}
} while (*rs++);
/* LINTED: result fits into an int */
continue;
continue;
return (-1);
}
count++;
}
}
if (rescan) {
/* LINTED: result fits into an int */
if (count) {
count = 0;
1, ap);
if (size < 0) {
return (size);
}
rindexa++;
}
}
*rescan = '/';
}
}
s = as;
while ((c = *s) != '\0')
*s++ = (c != DELIMCHAR ? c : '/');
return (count);
}
/*
* Check for a name match
*/
static int
gmatch(s, p)
wchar_t *s; /* string to test */
wchar_t *p; /* pattern to match against */
{
long scc; /* source character to text */
wchar_t c; /* pattern character to match */
char ok; /* [x-y] range match status */
long lc; /* left character of [x-y] range */
scc = *s++;
switch (c = *p++) {
case '[':
ok = 0;
lc = -1;
while (c = *p++) {
if (c == ']') {
} else if (c == '-') {
/*
* Check both ends must belong to
* the same codeset.
*/
/*
* If not, ignore the '-'
* operator and [x-y] is
* treated as if it were
* [xy].
*/
ok++;
ok++;
ok++;
} else {
lc = c;
ok++;
}
}
/* No closing bracket => failure */
return (0);
default:
if (c != scc)
return (0);
/*FALLTHROUGH*/
case '?':
case '*':
if (*p == 0)
return (1);
s--;
while (*s) {
if (gmatch(s++, p))
return (1);
}
return (0);
case 0:
return (scc == 0);
}
}
/*
* Construct a matched name.
*/
static int
char *as1; /* The current directory */
char *as3; /* The file name in dp */
{
int c;
if (c == DELIMCHAR) {
*s2++ = '/';
break;
}
/* LINTED narrowing cast */
*s2++ = (char)c;
}
s2++;
*s2++ = '/';
continue;
/*LINTED [empty loop body]*/
}
}
*s2 = '\0';
return (-1);
return (0);
}
/*
* Resolve a "complex" pathname (as generated by myname()) into
* a file descriptor and a relative path. The file descriptor
* will reference the hidden directory containing the attribute
* named by the relative path. If the provided path is not
* complex, the returned file descriptor will be AT_FDCWD and rpath
* will equal path.
*
* This function is intended to be used to transform a complex
* pathname into a pair of handles that can be used to actually
* manipulate the named file. Since extended attributes have
* an independant name space, a file descriptor for a directory
* in the attribute name space is necessary to actually manipulate
* the attribute file (via the path-relative xxxat() system calls
* or a call to fchdir()).
*
* In the event of an error, the returned file descriptor will be
* -1. It is expected that callers will either check for this
* condition directly, or attempt to use the descriptor, fail, and
* generate an appropriate context-specific error message.
*
* This function is pretty much a no-op for "simple" (non-attribute)
* paths.
*/
void
char *path;
int *fd;
char **rpath;
{
int tfd;
while (*path != '\0' &&
}
return;
gettext("Warning: cannot fully resolve %s: %s"),
}
}
/*
* Copy a complex pathname to another string. Note that the
* length returned by this function is the number of characters
* up to (but not including) the final NULL.
*/
int
char *s1;
char *s2;
int max;
{
int nullseen = 0;
int len = 0;
if (*s2++ == '\0') {
if (nullseen)
return (len-1);
else
nullseen = 1;
} else {
nullseen = 0;
}
}
*s1 = '\0';
if (nullseen == 0)
*--s1 = '\0';
gettext("Warning: unterminated source string in complexcpy\n"));
return (max-1);
}