* Clean up target.c to use a single consistent path-prefixing subroutine.

* Correct arg typo in mv_within_target_or_die().
* Add new path-prefixing entrypoints to fopen(), collect(), and do mounts.
* Use the above.  MI code is now clean of explicit references to /mnt.
* Lint: add `const' to collect()'s pathname arg.
  include <stdio.h> in factor.c, now that defs.h uses FILE*.
This commit is contained in:
jonathan 1997-11-03 02:38:41 +00:00
parent e7044069f6
commit 3e74d705e2
5 changed files with 131 additions and 45 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: defs.h,v 1.14 1997/11/02 23:43:11 jonathan Exp $ */
/* $NetBSD: defs.h,v 1.15 1997/11/03 02:38:41 jonathan Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -220,7 +220,7 @@ int config_network __P((void));
void mnt_net_config __P((void));
/* From run.c */
int collect __P((int kind, char **buffer, char *name, ...));
int collect __P((int kind, char **buffer, const char *name, ...));
int run_prog __P((char *, ...));
void run_prog_or_die __P((char *, ...));
int run_prog_or_continue __P((char *, ...));
@ -253,6 +253,9 @@ void trunc_target_file __P((const char *path));
int target_chdir __P(( const char *path));
void target_chdir_or_die __P((const char *dir));
int target_already_root __P((void));
FILE* target_fopen __P((const char *filename, const char *type));
int target_collect_file __P((int kind, char **buffer, char *name));
int is_active_rootpart __P((const char *partname));
void dup_file_into_target __P((const char *filename));
void mv_within_target_or_die __P((const char *from, const char *to));
int target_mount __P((const char *fstype, const char *from, const char* on));

View File

@ -1,4 +1,4 @@
/* $NetBSD: disks.c,v 1.9 1997/11/03 00:04:53 jonathan Exp $ */
/* $NetBSD: disks.c,v 1.10 1997/11/03 02:38:45 jonathan Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -71,6 +71,7 @@
/* Local prototypes */
static void get_disks (void);
static void foundffs (struct data *list, int num);
static int fsck_root __P((void));
static void
do_ffs_newfs(const char *partname, int part, const char *mountpoint);
@ -298,15 +299,15 @@ void make_filesystems (void)
static void
do_ffs_newfs(const char *partname, int partno, const char *mountpoint)
{
char devname[STRSIZE];
run_prog_or_continue ("/sbin/newfs /dev/r%s", partname);
if (*mountpoint) {
snprintf(devname, STRSIZE, "/dev/%s", partname);
if (partno > 0) {
make_target_dir(mountpoint);
run_prog_or_continue ("/sbin/mount -v /dev/%s /mnt%s",
partname, mountpoint);
target_mount("-v", devname, mountpoint);
} else
run_prog_or_continue ("/sbin/mount -v /dev/%s%c"
" /mnt", partname);
target_mount("-v", devname, mountpoint);
}
}
@ -317,7 +318,7 @@ void make_fstab (void)
/* Create the fstab. */
make_target_dir("/etc");
f = fopen ("/mnt/etc/fstab", "w");
f = target_fopen ("/etc/fstab", "w");
if (f == NULL) {
#ifndef DEBUG
(void)fprintf (stderr, msg_string (MSG_createfstab));
@ -424,29 +425,44 @@ do_fsck(char *disk, char *part)
return 1;
}
int
fsck_root()
{
int res;
char devname[STRSIZE];
if ((res = do_fsck (diskdev, "a")) <= 0) {
msg_display (MSG_badfs, diskdev, "a", res);
process_menu (MENU_ok);
return res;
}
/* Mount /dev/<diskdev>a on target's "". Prefixing will DTRT. */
snprintf(devname, STRSIZE, "/dev/%sa", diskdev);
if ((res = target_mount("", devname, "")) <= 0) {
msg_display (MSG_badmount, diskdev, "a");
process_menu (MENU_ok);
return res;
}
return 0;
}
int
fsck_disks (void)
{ char *fstab;
int fstabsize;
int i;
int res;
int err;
char devname[STRSIZE];
/* First the root device. */
if ((res = do_fsck (diskdev, "a")) <= 0) {
msg_display (MSG_badfs, diskdev, "a", res);
process_menu (MENU_ok);
return 0;
if (!target_already_root()) {
if (fsck_root() <= 0)
return 0;
}
if (run_prog ("/sbin/mount /dev/%sa /mnt", diskdev)) {
msg_display (MSG_badmount, diskdev, "a");
process_menu (MENU_ok);
return 0;
}
/* Get the fstab. */
/* Get fstab entries from the target-root /etc/fstab. */
fs_num = 0;
fstabsize = collect (T_FILE, &fstab, "/mnt/etc/fstab");
fstabsize = target_collect_file (T_FILE, &fstab, "/etc/fstab");
if (fstabsize < 0) {
/* error ! */
return 0;
@ -460,7 +476,8 @@ fsck_disks (void)
process_menu (MENU_ok);
return 0;
}
if (run_prog ("/sbin/mount /dev/%s /mnt/%s", dev[i], mnt[i])) {
snprintf(devname, STRSIZE, "/dev/%s", dev[i]);
if ((err = target_mount("", devname, mnt[i])) <= 0) {
msg_display (MSG_badmount, dev[i], "");
process_menu (MENU_ok);
return 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: factor.c,v 1.2 1997/10/01 05:04:26 phil Exp $ */
/* $NetBSD: factor.c,v 1.3 1997/11/03 02:38:47 jonathan Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -36,6 +36,7 @@
*
*/
#include <stdio.h> /* defs.h uses FILE* */
#include "defs.h"
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: run.c,v 1.3 1997/11/02 03:45:30 jonathan Exp $ */
/* $NetBSD: run.c,v 1.4 1997/11/03 02:38:50 jonathan Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -63,7 +63,7 @@ char* va_prog_cmdstr(char *cmd, va_list ap);
#define BUFSIZE 4096
int
collect (int kind, char **buffer, char *name, ...)
collect (int kind, char **buffer, const char *name, ...)
{
size_t nbytes; /* Number of bytes in buffer. */
size_t fbytes; /* Number of bytes in file. */

View File

@ -1,4 +1,4 @@
/* $NetBSD: target.c,v 1.3 1997/11/02 23:43:13 jonathan Exp $ */
/* $NetBSD: target.c,v 1.4 1997/11/03 02:38:52 jonathan Exp $ */
/*
* Copyright 1997 Jonathan Stone
@ -55,7 +55,8 @@
static void make_prefixed_dir __P((const char *prefix, const char *path));
const char* target_prefix __P((void));
static int do_target_chdir __P((const char *dir, int flag));
static const char* concat_paths __P((const char *prefix, const char *suffix));
static const char * target_expand __P((const char *pathname));
/*
* Pathname prefixing glue to support installation either
@ -75,6 +76,49 @@ const char* target_prefix(void)
return("/mnt");
}
/*
* 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.
*/
static const char*
concat_paths(const char* prefix, const char *suffix)
{
static char realpath[STRSIZE];
/* 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)
snprintf(realpath, STRSIZE, "%s%s", prefix, suffix);
else
snprintf(realpath, STRSIZE, "%s/%s", prefix, suffix);
return realpath;
}
/*
* 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.
*/
static const char*
target_expand(const char *tgtpath)
{
return concat_paths(target_prefix(), tgtpath);
}
/* Is the root we're running from is the root we're trying to upgrade? */
int target_already_root()
@ -95,10 +139,7 @@ int is_active_rootpart(const char *partname)
static void
make_prefixed_dir(const char *prefix, const char *path)
{
if (path[0] == '/' || prefix[0] == 0 || path[0] == 0)
run_prog_or_continue("/bin/mkdir -p %s%s", prefix, path);
else
run_prog_or_continue("/bin/mkdir -p %s/%s", prefix, path);
run_prog_or_continue("/bin/mkdir -p %s", concat_paths(prefix, path));
}
@ -130,7 +171,7 @@ make_ramdisk_dir(const char *path)
void
append_to_target_file(const char *path, const char *string)
{
run_prog_or_die("echo %s >> %s/%s", string, target_prefix(), path);
run_prog_or_die("echo %s >> %s", target_expand(path));
}
/*
@ -162,19 +203,15 @@ sprintf_to_target_file(const char *path, const char *format, ...)
void
trunc_target_file(const char *path)
{
run_prog_or_die("cat < /dev/null > %s/%s", target_prefix(), path);
run_prog_or_die("cat < /dev/null > %s", target_expand(path));
}
static int do_target_chdir(const char *dir, int must_succeed)
{
char tgt_dir[STRSIZE];
const char *tgt_dir;
int error = 0;
if (dir[0] == '/')
snprintf(tgt_dir, STRSIZE, "%s%s", target_prefix(), dir);
else
snprintf(tgt_dir, STRSIZE, "%s/%s", target_prefix(), dir);
tgt_dir = target_expand(dir);
#ifndef DEBUG
error = chdir(tgt_dir);
@ -209,16 +246,44 @@ int target_chdir(const char *dir)
void dup_file_into_target(const char *filename)
{
if (!target_already_root()) {
run_prog ("/bin/cp %s %s/%s",
filename, target_prefix(), filename);
const char *realpath = target_expand(filename);
run_prog ("/bin/cp %s %s", filename, realpath);
}
}
/* Do a mv where both pathnames are within the target filesystem. */
void mv_within_target_or_die(const char *from, const char *to)
void mv_within_target_or_die(const char *frompath, const char *topath)
{
run_prog_or_die("mv %s/%s",
target_prefix(), from,
target_prefix(), to);
char realfrom[STRSIZE];
char realto[STRSIZE];
strncpy(realfrom, target_expand(frompath), STRSIZE);
strncpy(realto, target_expand(topath), STRSIZE);
run_prog_or_die("mv %s %s", realfrom, realto);
}
/* fopen a pathname in the target. */
FILE* target_fopen (const char *filename, const char *type)
{
return fopen(target_expand(filename), type);
}
/*
* Do a mount onto a moutpoint in the install target.
* NB: does not prefix mount-from, which probably breaks nullfs mounts.
*/
int target_mount(const char *fstype, const char *from, const char *on)
{
int error;
const char *realmount = target_expand(on);
error = run_prog("/sbin/mount %s %s %s", fstype, from, realmount);
return (error);
}
int target_collect_file(int kind, char **buffer, char *name)
{
return collect(kind, buffer, target_expand(name));
}