2000-12-22 13:12:12 +03:00
|
|
|
/* $NetBSD: target.c,v 1.30 2000/12/22 10:12:13 mrg Exp $ */
|
1997-11-02 11:12:06 +03:00
|
|
|
|
|
|
|
/*
|
1997-11-02 11:30:39 +03:00
|
|
|
* Copyright 1997 Jonathan Stone
|
1997-11-02 11:12:06 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1999-06-20 10:08:13 +04:00
|
|
|
* This product includes software developed for the NetBSD Project by
|
1997-11-02 11:30:39 +03:00
|
|
|
* Jonathan Stone.
|
|
|
|
* 4. The name of Jonathan Stone may not be used to endorse
|
1997-11-02 11:12:06 +03:00
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
1997-11-02 11:30:39 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
|
1997-11-02 11:12:06 +03:00
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-10-11 15:10:11 +04:00
|
|
|
/* Copyright below applies to the realpath() code */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1991, 1993, 1995
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Jan-Simon Pendry.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1997-11-03 12:47:00 +03:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2000-12-22 13:12:12 +03:00
|
|
|
__RCSID("$NetBSD: target.c,v 1.30 2000/12/22 10:12:13 mrg Exp $");
|
1997-11-03 12:47:00 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* target.c -- path-prefixing routines to access the target installation
|
2000-12-05 01:47:02 +03:00
|
|
|
* filesystems. Makes the install tools more independent of whether
|
1997-11-03 12:47:00 +03:00
|
|
|
* we're installing into a separate filesystem hierarchy mounted under /mnt,
|
|
|
|
* or into the currently active root mounted on /.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h> /* XXX vm_param.h always defines TRUE*/
|
1997-11-09 15:47:06 +03:00
|
|
|
#include <sys/types.h>
|
1997-11-03 12:47:00 +03:00
|
|
|
#include <sys/sysctl.h>
|
1997-11-09 15:47:06 +03:00
|
|
|
#include <sys/stat.h> /* stat() */
|
|
|
|
#include <sys/mount.h> /* statfs() */
|
1997-11-02 11:12:06 +03:00
|
|
|
|
2000-10-19 18:42:57 +04:00
|
|
|
#include <fcntl.h>
|
1997-11-02 11:12:06 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
1997-11-03 12:47:00 +03:00
|
|
|
#include <curses.h> /* defines TRUE, but checks */
|
1997-11-05 10:28:20 +03:00
|
|
|
#include <errno.h>
|
1997-11-03 12:47:00 +03:00
|
|
|
|
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
#include "defs.h"
|
|
|
|
#include "md.h"
|
|
|
|
#include "msg_defs.h"
|
|
|
|
#include "menu_defs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* local prototypes
|
|
|
|
*/
|
2000-12-22 13:12:12 +03:00
|
|
|
static const char* get_rootdev (void);
|
|
|
|
static const char* mounted_rootpart (void);
|
|
|
|
int target_on_current_disk (void);
|
|
|
|
int must_mount_root (void);
|
1997-11-09 15:47:06 +03:00
|
|
|
|
2000-12-22 13:12:12 +03:00
|
|
|
static void make_prefixed_dir (const char *prefix, const char *path);
|
|
|
|
static int do_target_chdir (const char *dir, int flag);
|
2000-09-26 17:26:02 +04:00
|
|
|
int target_test(unsigned int mode, const char *path);
|
2000-12-22 13:12:12 +03:00
|
|
|
int target_test_dir (const char *path); /* deprecated */
|
|
|
|
int target_test_file (const char *path); /* deprecated */
|
|
|
|
int target_test_symlink (const char *path); /* deprecated */
|
1997-11-02 11:12:06 +03:00
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
void backtowin(void);
|
|
|
|
|
2000-12-22 13:12:12 +03:00
|
|
|
void unwind_mounts (void);
|
|
|
|
int mount_with_unwind (const char *fstype, const char *from, const char *on);
|
1997-12-04 12:05:35 +03:00
|
|
|
|
|
|
|
/* Record a mount for later unwinding of target mounts. */
|
|
|
|
struct unwind_mount {
|
|
|
|
struct unwind_mount *um_prev;
|
|
|
|
char um_mountpoint[STRSIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Unwind-mount stack */
|
1998-06-20 17:05:48 +04:00
|
|
|
struct unwind_mount *unwind_mountlist = NULL;
|
1997-12-04 12:05:35 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Debugging options
|
|
|
|
*/
|
|
|
|
/*#define DEBUG_ROOT*/ /* turn on what-is-root? debugging. */
|
|
|
|
/*#define DEBUG_UNWIND*/ /* turn on unwind-target-mount debugging. */
|
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
/*
|
|
|
|
* debugging helper. curses...
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
|
|
|
backtowin()
|
1997-11-09 15:47:06 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
fflush(stdout); /* curses does not leave stdout linebuffered. */
|
|
|
|
getchar(); /* wait for user to press return */
|
|
|
|
wrefresh(stdscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get name of current root device from kernel via sysctl.
|
|
|
|
* On NetBSD-1.3_ALPHA, this just returns the name of a
|
|
|
|
* device (e.g., "sd0"), not a specific partition -- like "sd0a",
|
|
|
|
* or "sd0b" for root-in-swap.
|
1998-06-20 17:05:48 +04:00
|
|
|
*/
|
|
|
|
static const char *
|
1997-11-09 15:47:06 +03:00
|
|
|
get_rootdev()
|
1997-11-03 12:47:00 +03:00
|
|
|
{
|
|
|
|
int mib[2];
|
1997-11-09 15:47:06 +03:00
|
|
|
static char rootdev[STRSIZE];
|
1997-11-03 12:47:00 +03:00
|
|
|
size_t varlen;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_ROOT_DEVICE;
|
1997-11-09 15:47:06 +03:00
|
|
|
varlen = sizeof(rootdev);
|
|
|
|
if (sysctl(mib, 2, rootdev, &varlen, NULL, 0) < 0)
|
1997-11-03 12:47:00 +03:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
1997-11-09 15:47:06 +03:00
|
|
|
printf("get_rootdev(): sysctl returns %s\n", rootdev);
|
1997-11-03 12:47:00 +03:00
|
|
|
#endif
|
1997-11-09 15:47:06 +03:00
|
|
|
return (rootdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if current root and target are on the same
|
|
|
|
* device (e.g., both on "sd0") or on different devices
|
|
|
|
* e.g., target is "sd0" and root is "le0" (nfs).
|
|
|
|
*/
|
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
target_on_current_disk()
|
1997-11-09 15:47:06 +03:00
|
|
|
{
|
|
|
|
int same;
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-09-20 23:53:35 +04:00
|
|
|
if (strcmp(diskdev, "") == 0) /* nothing selected yet */
|
|
|
|
same = 1; /* bad assumption: this is a live system */
|
|
|
|
else
|
|
|
|
same = (strcmp(diskdev, get_rootdev()) == 0);
|
1997-11-09 15:47:06 +03:00
|
|
|
return (same);
|
1997-11-03 12:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-11-09 15:47:06 +03:00
|
|
|
* must_mount_root -- check to see if the current root
|
|
|
|
* partition and the target root partition are on the same
|
|
|
|
* device. If they are, we need to have the root mounted read/write
|
|
|
|
* in order to find out whether they're the same partition.
|
|
|
|
* (this is arguably a bug in the kernel API.)
|
|
|
|
*
|
|
|
|
* check to see if they're
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
must_mount_root()
|
1997-11-09 15:47:06 +03:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
#if defined(DEBUG) || defined(DEBUG_ROOT)
|
1998-06-20 17:05:48 +04:00
|
|
|
endwin();
|
|
|
|
printf("must_mount_root\n");
|
|
|
|
backtowin();
|
1997-11-09 15:47:06 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if they're on different devices, we're OK. */
|
|
|
|
if (target_on_current_disk() == 0) {
|
|
|
|
#if defined(DEBUG) || defined(DEBUG_ROOT)
|
|
|
|
endwin();
|
|
|
|
printf("must_mount_root: %s and %s, no?\n",
|
1998-06-20 17:05:48 +04:00
|
|
|
diskdev, get_rootdev());
|
1997-11-09 15:47:06 +03:00
|
|
|
fflush(stdout);
|
|
|
|
backtowin();
|
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If they're on the same device, and root not mounted, yell. */
|
|
|
|
result = (strcmp(mounted_rootpart(), "root_device") == 0);
|
|
|
|
|
|
|
|
#if defined(DEBUG) || defined(DEBUG_ROOT)
|
|
|
|
endwin();
|
|
|
|
printf("must_mount_root %s and root_device gives %d\n",
|
1998-06-20 17:05:48 +04:00
|
|
|
mounted_rootpart(), result);
|
1997-11-09 15:47:06 +03:00
|
|
|
fflush(stdout);
|
|
|
|
backtowin();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-12-05 01:47:02 +03:00
|
|
|
* Is the root partition we're running from the same as the root
|
|
|
|
* which the user has selected to install/upgrade?
|
1997-11-09 15:47:06 +03:00
|
|
|
* Uses global variable "diskdev" to find the selected device for
|
|
|
|
* install/upgrade.
|
|
|
|
* FIXME -- disklabel-editing code assumes that partition 'a' is always root.
|
1997-11-03 12:47:00 +03:00
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_already_root()
|
1997-11-03 12:47:00 +03:00
|
|
|
{
|
|
|
|
register int result;
|
1997-11-09 15:47:06 +03:00
|
|
|
char diskdevroot[STRSIZE];
|
|
|
|
|
|
|
|
/* if they're on different devices, we're OK. */
|
|
|
|
if (target_on_current_disk() == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* otherwise, install() or upgrade() should already did
|
|
|
|
* have forced the user to explicitly mount the root,
|
|
|
|
* so we can find out via statfs(). Abort if not.
|
|
|
|
*/
|
1997-11-03 12:47:00 +03:00
|
|
|
|
2000-09-20 23:53:35 +04:00
|
|
|
if (strcmp(diskdev, "") == 0) {
|
|
|
|
/* no root partition was ever selected. Assume that
|
|
|
|
* the currently mounted one should be used */
|
|
|
|
result = 1;
|
|
|
|
} else {
|
|
|
|
/* append 'a' to the partitionless target disk device name. */
|
|
|
|
snprintf(diskdevroot, STRSIZE, "%s%c", diskdev, 'a');
|
|
|
|
result = is_active_rootpart(diskdevroot);
|
|
|
|
}
|
1998-06-20 17:05:48 +04:00
|
|
|
return (result);
|
1997-11-03 12:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ask the kernel what the current root is.
|
2000-12-05 01:47:02 +03:00
|
|
|
* If it's "root_device", we should just give up now.
|
1997-11-09 15:47:06 +03:00
|
|
|
* (Why won't the kernel tell us? If we booted with "n",
|
|
|
|
* or an explicit bootpath, the operator told *it* ....)
|
|
|
|
*
|
2000-12-05 01:47:02 +03:00
|
|
|
* Don't cache the answer in case the user suspends and remounts.
|
1997-11-09 15:47:06 +03:00
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
static const char *
|
|
|
|
mounted_rootpart()
|
1997-11-03 12:47:00 +03:00
|
|
|
{
|
1997-11-09 15:47:06 +03:00
|
|
|
struct statfs statfsbuf;
|
1997-11-03 12:47:00 +03:00
|
|
|
int result;
|
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
static char statrootstr[STRSIZE];
|
1998-08-10 06:23:45 +04:00
|
|
|
memset(&statfsbuf, 0, sizeof(statfsbuf));
|
1997-11-09 15:47:06 +03:00
|
|
|
result = statfs("/", &statfsbuf);
|
|
|
|
if (result < 0) {
|
|
|
|
fprintf(stderr, "Help! statfs() can't find root: %s\n",
|
1998-06-20 17:05:48 +04:00
|
|
|
strerror(errno));
|
1997-11-09 15:47:06 +03:00
|
|
|
fflush(stderr);
|
1999-01-21 11:02:17 +03:00
|
|
|
if (logging)
|
|
|
|
fprintf(log, "Help! statfs() can't find root: %s\n",
|
|
|
|
strerror(errno));
|
1997-11-09 15:47:06 +03:00
|
|
|
exit(errno);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#if defined(DEBUG)
|
|
|
|
endwin();
|
|
|
|
printf("mounted_rootpart: got %s on %s\n",
|
1998-06-20 17:05:48 +04:00
|
|
|
statfsbuf.f_mntonname, statfsbuf.f_mntfromname);
|
1997-11-09 15:47:06 +03:00
|
|
|
fflush(stdout);
|
|
|
|
backtowin();
|
|
|
|
#endif
|
1997-11-03 12:47:00 +03:00
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
/*
|
|
|
|
* Check for unmounted root. We can't tell which partition
|
|
|
|
*/
|
|
|
|
strncpy(statrootstr, statfsbuf.f_mntfromname, STRSIZE);
|
1998-06-20 17:05:48 +04:00
|
|
|
return (statrootstr);
|
1997-11-09 15:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is this device partition (e.g., "sd0a") mounted as root?
|
|
|
|
* Note difference from target_on_current_disk()!
|
|
|
|
*/
|
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
is_active_rootpart(devpart)
|
|
|
|
const char *devpart;
|
1997-11-09 15:47:06 +03:00
|
|
|
{
|
|
|
|
const char *root = 0;
|
|
|
|
int result;
|
1997-12-02 06:02:29 +03:00
|
|
|
static char devdirdevpart[STRSIZE];
|
1997-11-09 15:47:06 +03:00
|
|
|
|
|
|
|
/* check to see if the devices match? */
|
|
|
|
|
|
|
|
/* this changes on mounts, so don't cache it. */
|
|
|
|
root = mounted_rootpart();
|
|
|
|
|
1997-12-02 06:02:29 +03:00
|
|
|
/* prepend /dev. */
|
|
|
|
/* XXX post-1.3, use strstr to strip "/dev" from input. */
|
|
|
|
snprintf(devdirdevpart, STRSIZE, "/dev/%s", devpart);
|
|
|
|
|
|
|
|
result = (strcmp(devdirdevpart, root) == 0);
|
|
|
|
|
1997-11-09 15:47:06 +03:00
|
|
|
#if defined(DEBUG) || defined(DEBUG_ROOT)
|
|
|
|
endwin();
|
1997-12-02 06:02:29 +03:00
|
|
|
printf("is_active_rootpart: activeroot = %s, query=%s, mung=%s, answer=%d\n",
|
1998-06-20 17:05:48 +04:00
|
|
|
root, devpart, devdirdevpart, result);
|
1997-11-09 15:47:06 +03:00
|
|
|
fflush(stdout);
|
|
|
|
backtowin();
|
1997-11-03 12:47:00 +03:00
|
|
|
#endif
|
1997-11-09 15:47:06 +03:00
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
return (result);
|
1997-11-03 12:47:00 +03:00
|
|
|
}
|
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
/*
|
|
|
|
* Pathname prefixing glue to support installation either
|
|
|
|
* from in-ramdisk miniroots or on-disk diskimages.
|
|
|
|
* If our root is on the target disk, the install target is mounted
|
|
|
|
* on /mnt and we need to prefix installed pathnames with /mnt.
|
|
|
|
* otherwise we are installing to the currently-active root and
|
|
|
|
* no prefix is needed.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
const char *
|
|
|
|
target_prefix()
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX fetch sysctl variable for current root, and compare
|
1998-06-20 17:05:48 +04:00
|
|
|
* to the devicename of the install target disk.
|
1997-11-02 11:12:06 +03:00
|
|
|
*/
|
1997-11-03 12:47:00 +03:00
|
|
|
return(target_already_root() ? "" : "/mnt");
|
1997-11-02 11:12:06 +03:00
|
|
|
}
|
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
/*
|
|
|
|
* concatenate two pathnames.
|
|
|
|
* XXX returns either input args or result in a static buffer.
|
|
|
|
* The caller must copy if it wants to use the pathname past the
|
|
|
|
* next call to a target-prefixing function, or to modify the inputs..
|
|
|
|
* Used only internally so this is probably safe.
|
|
|
|
*/
|
2000-10-11 15:10:11 +04:00
|
|
|
const char*
|
1998-06-20 17:05:48 +04:00
|
|
|
concat_paths(prefix, suffix)
|
|
|
|
const char* prefix;
|
|
|
|
const char *suffix;
|
1997-11-03 05:38:41 +03:00
|
|
|
{
|
2000-10-11 15:10:11 +04:00
|
|
|
static char realpath[MAXPATHLEN];
|
1997-11-03 05:38:41 +03:00
|
|
|
|
|
|
|
/* absolute prefix and null suffix? */
|
|
|
|
if (prefix[0] == '/' && suffix[0] == 0)
|
|
|
|
return prefix;
|
|
|
|
|
|
|
|
/* null prefix and absolute suffix? */
|
|
|
|
if (prefix[0] == 0 && suffix[0] == '/')
|
|
|
|
return suffix;
|
|
|
|
|
|
|
|
/* avoid "//" */
|
|
|
|
if (suffix[0] == '/' || suffix[0] == 0)
|
2000-10-11 15:10:11 +04:00
|
|
|
snprintf(realpath, MAXPATHLEN, "%s%s", prefix, suffix);
|
1997-11-03 05:38:41 +03:00
|
|
|
else
|
2000-10-11 15:10:11 +04:00
|
|
|
snprintf(realpath, MAXPATHLEN, "%s/%s", prefix, suffix);
|
1998-06-20 17:05:48 +04:00
|
|
|
return (realpath);
|
1997-11-03 05:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do target prefix expansion on a pathname.
|
|
|
|
* XXX uses concat_paths and so returns result in a static buffer.
|
|
|
|
* The caller must copy if it wants to use the pathname past the
|
|
|
|
* next call to a target-prefixing function, or to modify the inputs..
|
|
|
|
* Used only internally so this is probably safe.
|
1997-11-04 04:39:03 +03:00
|
|
|
*
|
|
|
|
* Not static so other functions can generate target related file names.
|
1997-11-03 05:38:41 +03:00
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
const char *
|
|
|
|
target_expand(tgtpath)
|
|
|
|
const char *tgtpath;
|
1997-11-03 05:38:41 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
return concat_paths(target_prefix(), tgtpath);
|
|
|
|
}
|
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
/* Make a directory, with a prefix like "/mnt" or possibly just "". */
|
|
|
|
static void
|
1998-06-20 17:05:48 +04:00
|
|
|
make_prefixed_dir(prefix, path)
|
|
|
|
const char *prefix;
|
|
|
|
const char *path;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
run_prog(0, NULL, "/bin/mkdir -p %s", concat_paths(prefix, path));
|
1997-11-02 11:12:06 +03:00
|
|
|
}
|
|
|
|
|
2000-12-05 01:47:02 +03:00
|
|
|
/* Make a directory with a pathname relative to the installation target. */
|
1997-11-02 11:12:06 +03:00
|
|
|
void
|
1998-06-20 17:05:48 +04:00
|
|
|
make_target_dir(path)
|
|
|
|
const char *path;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
make_prefixed_dir(target_prefix(), path);
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
/* Make a directory with a pathname in the currently-mounted root. */
|
1997-11-02 11:12:06 +03:00
|
|
|
void
|
1998-06-20 17:05:48 +04:00
|
|
|
make_ramdisk_dir(path)
|
|
|
|
const char *path;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
make_prefixed_dir(path, "");
|
|
|
|
}
|
|
|
|
|
1999-01-26 02:34:20 +03:00
|
|
|
#if 0
|
|
|
|
/* unused, will not work with new run.c */
|
1997-11-02 11:12:06 +03:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Append |string| to the filename |path|, where |path| is
|
|
|
|
* relative to the root of the install target.
|
|
|
|
* for example,
|
|
|
|
* echo_to_target_file( "Newbie.NetBSD.ORG", "/etc/myname");
|
|
|
|
* would set the default hostname at the next reboot of the installed-on disk.
|
|
|
|
*/
|
|
|
|
void
|
1998-06-20 17:05:48 +04:00
|
|
|
append_to_target_file(path, string)
|
|
|
|
const char *path;
|
|
|
|
const char *string;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
run_prog(RUN_FATAL, NULL, "echo %s >> %s", string, target_expand(path));
|
1997-11-02 11:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As append_to_target_file, but with ftrunc semantics.
|
|
|
|
*/
|
|
|
|
void
|
1998-06-20 17:05:48 +04:00
|
|
|
echo_to_target_file(path, string)
|
|
|
|
const char *path;
|
|
|
|
const char *string;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
|
|
|
trunc_target_file(path);
|
|
|
|
append_to_target_file(path, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sprintf_to_target_file(const char *path, const char *format, ...)
|
|
|
|
{
|
|
|
|
char lines[STRSIZE];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
trunc_target_file(path);
|
|
|
|
|
1997-11-13 20:26:25 +03:00
|
|
|
va_start(ap, format);
|
1997-11-02 11:12:06 +03:00
|
|
|
vsnprintf(lines, STRSIZE, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
append_to_target_file(path, lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-06-20 17:05:48 +04:00
|
|
|
trunc_target_file(path)
|
|
|
|
const char *path;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
run_prog(RUN_FATAL, NULL, "cat < /dev/null > %s", target_expand(path));
|
1997-11-02 11:12:06 +03:00
|
|
|
}
|
1999-01-26 02:34:20 +03:00
|
|
|
#endif /* if 0 */
|
1997-11-02 11:12:06 +03:00
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
static int
|
|
|
|
do_target_chdir(dir, must_succeed)
|
|
|
|
const char *dir;
|
|
|
|
int must_succeed;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1997-11-03 05:38:41 +03:00
|
|
|
const char *tgt_dir;
|
1997-11-11 03:43:31 +03:00
|
|
|
int error;
|
1997-11-02 11:12:06 +03:00
|
|
|
|
1997-11-11 03:43:31 +03:00
|
|
|
error = 0;
|
1997-11-03 05:38:41 +03:00
|
|
|
tgt_dir = target_expand(dir);
|
1997-11-02 11:12:06 +03:00
|
|
|
|
|
|
|
#ifndef DEBUG
|
1997-11-05 10:28:20 +03:00
|
|
|
/* chdir returns -1 on error and sets errno. */
|
1998-06-20 17:05:48 +04:00
|
|
|
if (chdir(tgt_dir) < 0)
|
1997-11-05 10:28:20 +03:00
|
|
|
error = errno;
|
1999-01-21 11:02:17 +03:00
|
|
|
if (logging) {
|
|
|
|
fprintf(log, "cd to %s\n", tgt_dir);
|
|
|
|
fflush(log);
|
|
|
|
}
|
|
|
|
if (scripting) {
|
|
|
|
fprintf(script, "cd %s\n", tgt_dir);
|
|
|
|
fflush(script);
|
|
|
|
}
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
if (error && must_succeed) {
|
1997-11-05 10:28:20 +03:00
|
|
|
fprintf(stderr, msg_string(MSG_realdir),
|
|
|
|
target_prefix(), strerror(error));
|
1999-01-21 11:02:17 +03:00
|
|
|
if (logging)
|
|
|
|
fprintf(log, msg_string(MSG_realdir),
|
|
|
|
target_prefix(), strerror(error));
|
1997-11-02 11:12:06 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
#else
|
|
|
|
printf("target_chdir (%s)\n", tgt_dir);
|
|
|
|
return (0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
|
|
|
target_chdir_or_die(dir)
|
|
|
|
const char *dir;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
(void) do_target_chdir(dir, 1);
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_chdir(dir)
|
|
|
|
const char *dir;
|
1997-11-02 11:12:06 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-02 11:12:06 +03:00
|
|
|
return(do_target_chdir(dir, 0));
|
|
|
|
}
|
1997-11-03 02:43:11 +03:00
|
|
|
|
1998-02-20 05:33:51 +03:00
|
|
|
/*
|
|
|
|
* Copy a file from the current root into the target system,
|
|
|
|
* where the destination pathname is relative to the target root.
|
|
|
|
* Does not check for copy-to-self when target is current root.
|
|
|
|
*/
|
1999-04-09 14:24:38 +04:00
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
cp_to_target(srcpath, tgt_path)
|
|
|
|
const char *srcpath;
|
|
|
|
const char *tgt_path;
|
1998-02-20 05:33:51 +03:00
|
|
|
{
|
|
|
|
const char *realpath = target_expand(tgt_path);
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
return run_prog(0, NULL, "/bin/cp %s %s", srcpath, realpath);
|
1998-02-20 05:33:51 +03:00
|
|
|
}
|
|
|
|
|
1997-11-03 02:43:11 +03:00
|
|
|
/*
|
|
|
|
* Duplicate a file from the current root to the same pathname
|
1998-02-20 05:33:51 +03:00
|
|
|
* in the target system. Pathname must be an absolute pathname.
|
1997-11-03 02:43:11 +03:00
|
|
|
* If we're running in the target, do nothing.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
|
|
|
dup_file_into_target(filename)
|
|
|
|
const char *filename;
|
1997-11-03 02:43:11 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
|
|
|
if (!target_already_root())
|
1998-02-20 05:33:51 +03:00
|
|
|
cp_to_target(filename, filename);
|
1997-11-03 02:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
/*
|
|
|
|
* Do a mv where both pathnames are within the target filesystem.
|
|
|
|
*/
|
2000-09-08 06:50:42 +04:00
|
|
|
void
|
|
|
|
mv_within_target_or_die(frompath, topath)
|
1998-06-20 17:05:48 +04:00
|
|
|
const char *frompath;
|
|
|
|
const char *topath;
|
1997-11-03 05:38:41 +03:00
|
|
|
{
|
|
|
|
char realfrom[STRSIZE];
|
|
|
|
char realto[STRSIZE];
|
|
|
|
|
|
|
|
strncpy(realfrom, target_expand(frompath), STRSIZE);
|
|
|
|
strncpy(realto, target_expand(topath), STRSIZE);
|
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
run_prog(RUN_FATAL, NULL, "mv %s %s", realfrom, realto);
|
1997-11-03 05:38:41 +03:00
|
|
|
}
|
|
|
|
|
1997-11-05 10:28:20 +03:00
|
|
|
/* Do a cp where both pathnames are within the target filesystem. */
|
1998-06-20 17:05:48 +04:00
|
|
|
int cp_within_target(frompath, topath)
|
|
|
|
const char *frompath;
|
|
|
|
const char *topath;
|
1997-11-05 10:28:20 +03:00
|
|
|
{
|
|
|
|
char realfrom[STRSIZE];
|
|
|
|
char realto[STRSIZE];
|
|
|
|
|
|
|
|
strncpy(realfrom, target_expand(frompath), STRSIZE);
|
|
|
|
strncpy(realto, target_expand(topath), STRSIZE);
|
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
return (run_prog(0, NULL, "cp -p %s %s", realfrom, realto));
|
1997-11-05 10:28:20 +03:00
|
|
|
}
|
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
/* fopen a pathname in the target. */
|
1998-06-20 17:05:48 +04:00
|
|
|
FILE *
|
|
|
|
target_fopen(filename, type)
|
|
|
|
const char *filename;
|
|
|
|
const char *type;
|
1997-11-03 05:38:41 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
return fopen(target_expand(filename), type);
|
|
|
|
}
|
|
|
|
|
1997-12-04 12:05:35 +03:00
|
|
|
/*
|
|
|
|
* Do a mount and record the mountpoint in a list of mounts to
|
|
|
|
* unwind after completing or aborting a mount.
|
|
|
|
*/
|
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
mount_with_unwind(fstype, from, on)
|
|
|
|
const char *fstype;
|
|
|
|
const char *from;
|
|
|
|
const char *on;
|
1997-12-04 12:05:35 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct unwind_mount * m;
|
|
|
|
|
|
|
|
m = malloc(sizeof(*m));
|
|
|
|
if (m == 0)
|
|
|
|
return (ENOMEM); /* XXX */
|
|
|
|
|
|
|
|
strncpy(m->um_mountpoint, on, STRSIZE);
|
|
|
|
m->um_prev = unwind_mountlist;
|
|
|
|
unwind_mountlist = m;
|
|
|
|
|
|
|
|
#ifdef DEBUG_UNWIND
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "mounting %s with unwind\n", on);
|
|
|
|
backtowin();
|
|
|
|
#endif
|
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
error = run_prog(0, NULL, "/sbin/mount %s %s %s", fstype, from, on);
|
1997-12-04 12:05:35 +03:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-12-05 01:47:02 +03:00
|
|
|
* unwind the mount stack, unmounting mounted filesystems.
|
1997-12-04 12:05:35 +03:00
|
|
|
* For now, ignore any errors in unmount.
|
|
|
|
* (Why would we be unable to unmount? The user has suspended
|
|
|
|
* us and forked shell sitting somewhere in the target root?)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
unwind_mounts()
|
|
|
|
{
|
|
|
|
struct unwind_mount *m, *prev;
|
1997-12-05 17:00:59 +03:00
|
|
|
volatile static int unwind_in_progress = 0;
|
|
|
|
|
|
|
|
/* signal safety */
|
|
|
|
if (unwind_in_progress)
|
|
|
|
return;
|
|
|
|
unwind_in_progress = 1;
|
1997-12-04 12:05:35 +03:00
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
for (m = unwind_mountlist; m; ) {
|
|
|
|
struct unwind_mount *prev;
|
|
|
|
#ifdef DEBUG_UNWIND
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "unmounting %s\n", m->um_mountpoint);
|
|
|
|
backtowin();
|
|
|
|
#endif
|
2000-10-12 03:47:55 +04:00
|
|
|
run_prog(0, NULL, "/sbin/umount %s", m->um_mountpoint);
|
1997-12-04 12:05:35 +03:00
|
|
|
prev = m->um_prev;
|
|
|
|
free(m);
|
|
|
|
m = prev;
|
|
|
|
}
|
|
|
|
unwind_mountlist = NULL;
|
1997-12-05 17:00:59 +03:00
|
|
|
unwind_in_progress = 0;
|
1997-12-04 12:05:35 +03:00
|
|
|
}
|
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
/*
|
2000-12-05 01:47:02 +03:00
|
|
|
* Do a mount onto a mountpoint in the install target.
|
1997-11-03 05:38:41 +03:00
|
|
|
* NB: does not prefix mount-from, which probably breaks nullfs mounts.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_mount(fstype, from, on)
|
|
|
|
const char *fstype;
|
|
|
|
const char *from;
|
|
|
|
const char *on;
|
1997-11-03 05:38:41 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
const char *realmount = target_expand(on);
|
|
|
|
|
2000-12-05 01:47:02 +03:00
|
|
|
/* mount and record for unmounting when done. */
|
1997-12-04 12:05:35 +03:00
|
|
|
error = mount_with_unwind(fstype, from, realmount);
|
|
|
|
|
1997-11-03 05:38:41 +03:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_collect_file(kind, buffer, name)
|
|
|
|
int kind;
|
|
|
|
char **buffer;
|
|
|
|
char *name;
|
1997-11-03 02:43:11 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
const char *realname =target_expand(name);
|
1997-11-03 12:47:00 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("collect real name %s\n", realname);
|
|
|
|
#endif
|
|
|
|
return collect(kind, buffer, realname);
|
1997-11-03 02:43:11 +03:00
|
|
|
}
|
1997-11-05 10:28:20 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify a pathname already exists in the target root filesystem,
|
|
|
|
* by running test "testflag" on the expanded target pathname.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
2000-09-26 17:26:02 +04:00
|
|
|
target_test(mode, path)
|
|
|
|
unsigned int mode;
|
1998-06-20 17:05:48 +04:00
|
|
|
const char *path;
|
1997-11-05 10:28:20 +03:00
|
|
|
{
|
|
|
|
const char *realpath = target_expand(path);
|
|
|
|
register int result;
|
|
|
|
|
2000-09-26 17:26:02 +04:00
|
|
|
result = !file_mode_match(realpath, mode);
|
1999-01-21 11:02:17 +03:00
|
|
|
if (scripting)
|
|
|
|
(void)fprintf(script, "if [ $? != 0 ]; then echo \"%s does not exist!\"; fi\n", realpath);
|
1997-11-05 10:28:20 +03:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
1998-06-20 17:05:48 +04:00
|
|
|
printf("target_test(%s %s) returning %d\n", test, realpath, result);
|
1997-11-05 10:28:20 +03:00
|
|
|
#endif
|
1998-06-20 17:05:48 +04:00
|
|
|
return (result);
|
1997-11-05 10:28:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify a directory already exists in the target root
|
|
|
|
* filesystem. Do not create the directory if it doesn't exist.
|
|
|
|
* Assumes that sysinst has already mounted the target root.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_test_dir(path)
|
|
|
|
const char *path;
|
1997-11-05 10:28:20 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-09-26 17:26:02 +04:00
|
|
|
return target_test(S_IFDIR, path);
|
1997-11-05 10:28:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify an ordinary file already exists in the target root
|
|
|
|
* filesystem. Do not create the directory if it doesn't exist.
|
|
|
|
* Assumes that sysinst has already mounted the target root.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
target_test_file(path)
|
|
|
|
const char *path;
|
1997-11-05 10:28:20 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
2000-09-26 17:26:02 +04:00
|
|
|
return target_test(S_IFREG, path);
|
1997-11-05 10:28:20 +03:00
|
|
|
}
|
1997-12-05 17:00:59 +03:00
|
|
|
|
2000-09-08 06:50:42 +04:00
|
|
|
int
|
|
|
|
target_test_symlink(path)
|
|
|
|
const char *path;
|
|
|
|
{
|
|
|
|
|
2000-09-26 17:26:02 +04:00
|
|
|
return target_test(S_IFLNK, path);
|
2000-09-08 06:50:42 +04:00
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
int target_file_exists_p(path)
|
|
|
|
const char *path;
|
1997-12-05 17:00:59 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-12-05 17:00:59 +03:00
|
|
|
return (target_test_file(path) == 0);
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
int target_dir_exists_p(path)
|
|
|
|
const char *path;
|
1997-12-05 17:00:59 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1997-12-05 17:00:59 +03:00
|
|
|
return (target_test_dir(path) == 0);
|
|
|
|
}
|
2000-09-08 06:50:42 +04:00
|
|
|
|
|
|
|
int target_symlink_exists_p(path)
|
|
|
|
const char *path;
|
|
|
|
{
|
|
|
|
|
|
|
|
return (target_test_symlink(path) == 0);
|
|
|
|
}
|
2000-10-11 15:10:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXX had to include this to deal with symlinks in some places. When the target
|
|
|
|
* disk is mounted under /mnt, absolute symlinks on it don't work right. This
|
|
|
|
* function will resolve them using the mountpoint as prefix. Copied verbatim
|
|
|
|
* from libc, with added prefix handling.
|
|
|
|
*
|
|
|
|
* char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
|
|
|
|
*
|
|
|
|
* Find the real name of path, by removing all ".", ".." and symlink
|
|
|
|
* components. Returns (resolved) on success, or (NULL) on failure,
|
|
|
|
* in which case the path which caused trouble is left in (resolved).
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
target_realpath(const char *path, char *resolved)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
int fd, n, rootd, serrno, nlnk = 0;
|
|
|
|
char *p, *q, wbuf[MAXPATHLEN];
|
|
|
|
|
|
|
|
/* Save the starting point. */
|
|
|
|
if ((fd = open(".", O_RDONLY)) < 0) {
|
|
|
|
(void)strcpy(resolved, ".");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the dirname and basename from the path to be resolved.
|
|
|
|
* Change directory to the dirname component.
|
|
|
|
* lstat the basename part.
|
|
|
|
* if it is a symlink, read in the value and loop.
|
|
|
|
* if it is a directory, then change to that directory.
|
|
|
|
* get the current directory name and append the basename.
|
|
|
|
*/
|
|
|
|
if (target_prefix() != NULL && strcmp(target_prefix(), "") != 0)
|
|
|
|
snprintf(resolved, MAXPATHLEN, "%s/%s", target_prefix(), path);
|
|
|
|
else {
|
|
|
|
(void)strncpy(resolved, path, MAXPATHLEN - 1);
|
|
|
|
resolved[MAXPATHLEN - 1] = '\0';
|
|
|
|
}
|
|
|
|
loop:
|
|
|
|
q = strrchr(resolved, '/');
|
|
|
|
if (q != NULL) {
|
|
|
|
p = q + 1;
|
|
|
|
if (q == resolved)
|
|
|
|
q = "/";
|
|
|
|
else {
|
|
|
|
do {
|
|
|
|
--q;
|
|
|
|
} while (q > resolved && *q == '/');
|
|
|
|
q[1] = '\0';
|
|
|
|
q = resolved;
|
|
|
|
}
|
|
|
|
if (chdir(q) < 0)
|
|
|
|
goto err1;
|
|
|
|
} else
|
|
|
|
p = resolved;
|
|
|
|
|
|
|
|
/* Deal with the last component. */
|
|
|
|
if (lstat(p, &sb) == 0) {
|
|
|
|
if (S_ISLNK(sb.st_mode)) {
|
|
|
|
if (nlnk++ >= MAXSYMLINKS) {
|
|
|
|
errno = ELOOP;
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
n = readlink(p, wbuf, MAXPATHLEN);
|
|
|
|
if (n < 0)
|
|
|
|
goto err1;
|
|
|
|
wbuf[n] = '\0';
|
|
|
|
if (wbuf[0] == '/')
|
|
|
|
snprintf(resolved, MAXPATHLEN, "%s%s", target_prefix(),
|
|
|
|
wbuf);
|
|
|
|
else
|
|
|
|
strcpy(resolved, wbuf);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
|
|
if (chdir(p) < 0)
|
|
|
|
goto err1;
|
|
|
|
p = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the last component name and get the full pathname of
|
|
|
|
* the current directory.
|
|
|
|
*/
|
|
|
|
(void)strncpy(wbuf, p, (sizeof(wbuf) - 1));
|
|
|
|
|
|
|
|
/*
|
2000-12-05 01:47:02 +03:00
|
|
|
* Call the internal internal version of getcwd which
|
2000-10-11 15:10:11 +04:00
|
|
|
* does a physical search rather than using the $PWD short-cut
|
|
|
|
*/
|
|
|
|
if (getcwd(resolved, MAXPATHLEN) == 0)
|
|
|
|
goto err1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Join the two strings together, ensuring that the right thing
|
|
|
|
* happens if the last component is empty, or the dirname is root.
|
|
|
|
*/
|
|
|
|
if (resolved[0] == '/' && resolved[1] == '\0')
|
|
|
|
rootd = 1;
|
|
|
|
else
|
|
|
|
rootd = 0;
|
|
|
|
|
|
|
|
if (*wbuf) {
|
|
|
|
if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
if (rootd == 0)
|
|
|
|
(void)strcat(resolved, "/"); /* XXX: strcat is safe */
|
|
|
|
(void)strcat(resolved, wbuf); /* XXX: strcat is safe */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go back to where we came from. */
|
|
|
|
if (fchdir(fd) < 0) {
|
|
|
|
serrno = errno;
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's okay if the close fails, what's an fd more or less? */
|
|
|
|
(void)close(fd);
|
|
|
|
return (resolved);
|
|
|
|
|
|
|
|
err1: serrno = errno;
|
|
|
|
(void)fchdir(fd);
|
|
|
|
err2: (void)close(fd);
|
|
|
|
errno = serrno;
|
|
|
|
return (NULL);
|
|
|
|
}
|