1/*	$NetBSD: upgrade.c,v 1.52 2012/01/07 18:54:31 christos Exp $	*/
2
3/*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 *    or promote products derived from this software without specific prior
19 *    written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35/* upgrade.c -- upgrade an installation. */
36
37#include <sys/param.h>
38#include <stdio.h>
39#include <curses.h>
40#include <errno.h>
41#include "defs.h"
42#include "msg_defs.h"
43#include "menu_defs.h"
44
45/*
46 * local prototypes
47 */
48static int save_X(const char *);
49static int merge_X(const char *);
50
51/*
52 * Do the system upgrade.
53 */
54void
55do_upgrade(void)
56{
57
58	msg_display(MSG_upgradeusure);
59	process_menu(MENU_noyes, NULL);
60	if (!yesno)
61		return;
62
63	get_ramsize();
64
65	if (find_disks(msg_string(MSG_upgrade)) < 0)
66		return;
67
68	if (md_pre_update() < 0)
69		return;
70
71	process_menu(MENU_distset, NULL);
72
73	if (mount_disks() != 0)
74		return;
75
76
77	/*
78	 * Save X symlink, ...
79	 */
80	if (save_X("/usr/X11R6"))
81		return;
82	if (save_X("/usr/X11R7"))
83		return;
84
85#ifdef AOUT2ELF
86	move_aout_libs();
87#endif
88	/* Do any md updating of the file systems ... e.g. bootblocks,
89	   copy file systems ... */
90	if (!md_update())
91		return;
92
93	wrefresh(curscr);
94	wmove(stdscr, 0, 0);
95	wclear(stdscr);
96	wrefresh(stdscr);
97
98	/* Done with disks. Ready to get and unpack tarballs. */
99	if (get_and_unpack_sets(1, MSG_disksetupdoneupdate,
100	    MSG_upgrcomplete, MSG_abortupgr) != 0)
101		return;
102
103	if (!md_post_extract() == 0)
104		return;
105
106	merge_X("/usr/X11R6");
107	merge_X("/usr/X11R7");
108
109	sanity_check();
110}
111
112/*
113 * Save X symlink to X.old so it can be recovered later
114 */
115static int
116save_X(const char *xroot)
117{
118	char newx[MAXPATHLEN], oldx[MAXPATHLEN];
119
120	strlcpy(newx, xroot, sizeof(newx));
121	strlcat(newx, "/bin/X", sizeof(newx));
122	strlcpy(oldx, newx, sizeof(oldx));
123	strlcat(oldx, ".old", sizeof(oldx));
124
125	/* Only care for X if it's a symlink */
126	if (target_symlink_exists_p(newx)) {
127		if (target_symlink_exists_p(oldx)) {
128			msg_display(MSG_X_oldexists, xroot, xroot, xroot,
129			    xroot, xroot, xroot, xroot, xroot, xroot, xroot,
130			    xroot);
131			process_menu(MENU_ok, NULL);
132			return EEXIST;
133		}
134
135#ifdef DEBUG
136		printf("saving %s as %s ...", newx, oldx);
137#endif
138
139		/* Move target .../X to .../X.old.  Abort on error. */
140		mv_within_target_or_die(newx, oldx);
141	}
142
143	return 0;
144}
145
146/*
147 * Merge back saved target X files after unpacking the new
148 * sets has completed.
149 */
150static int
151merge_X(const char *xroot)
152{
153	char newx[MAXPATHLEN], oldx[MAXPATHLEN];
154
155	strlcpy(newx, xroot, sizeof(newx));
156	strlcat(newx, "/bin/X", sizeof(newx));
157	strlcpy(oldx, newx, sizeof(oldx));
158	strlcat(oldx, ".old", sizeof(oldx));
159
160	if (target_symlink_exists_p(oldx)) {
161		/* Only move back X if it's a symlink - we don't want
162		 * to restore old binaries */
163		mv_within_target_or_die(oldx, newx);
164	}
165
166	return 0;
167}
168
169/*
170 * Unpacks sets,  clobbering existing contents.
171 */
172void
173do_reinstall_sets(void)
174{
175
176	unwind_mounts();
177	msg_display(MSG_reinstallusure);
178	process_menu(MENU_noyes, NULL);
179	if (!yesno)
180		return;
181
182	if (find_disks(msg_string(MSG_reinstall)) < 0)
183		return;
184
185	process_menu(MENU_distset, NULL);
186
187	if (mount_disks() != 0)
188		return;
189
190	/* Unpack the distribution. */
191	if (get_and_unpack_sets(0, NULL, MSG_unpackcomplete, MSG_abortunpack) != 0)
192		return;
193
194	sanity_check();
195}
196