perform.c revision 3364
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.8 1994/05/25 17:59:54 asami Exp $";
3#endif
4
5/*
6 * FreeBSD install - a package for the installation and maintainance
7 * of non-core utilities.
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 *
18 * Jordan K. Hubbard
19 * 18 July 1993
20 *
21 * This is the main body of the add module.
22 *
23 */
24
25#include "lib.h"
26#include "add.h"
27
28#include <signal.h>
29
30static int pkg_do(char *);
31static int sanity_check(char *);
32static char LogDir[FILENAME_MAX];
33
34
35int
36pkg_perform(char **pkgs)
37{
38    int i, err_cnt = 0;
39
40    signal(SIGINT, cleanup);
41    signal(SIGHUP, cleanup);
42
43    if (AddMode == SLAVE)
44	err_cnt = pkg_do(NULL);
45    else {
46	for (i = 0; pkgs[i]; i++)
47	    err_cnt += pkg_do(pkgs[i]);
48    }
49    return err_cnt;
50}
51
52static Package Plist;
53
54/* This is seriously ugly code following.  Written very fast! */
55static int
56pkg_do(char *pkg)
57{
58    char pkg_fullname[FILENAME_MAX];
59    FILE *cfile;
60    char *home;
61    int code = 0;
62    PackingList p;
63    struct stat sb;
64
65    /* Reset some state */
66    if (Plist.head)
67	free_plist(&Plist);
68    LogDir[0] = '\0';
69    if (AddMode == SLAVE) {
70	char tmp_dir[FILENAME_MAX];
71
72	fgets(tmp_dir, FILENAME_MAX, stdin);
73	tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */
74	if (chdir(tmp_dir) == FAIL) {
75	    whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir);
76	    return 1;
77	}
78	read_plist(&Plist, stdin);
79    }
80    else {
81	if (pkg[0] == '/')	/* full pathname? */
82	    strcpy(pkg_fullname, pkg);
83	else
84	    sprintf(pkg_fullname, "%s/%s", home, pkg);
85	if (!fexists(pkg_fullname)) {
86	    whinge("Can't find package '%s'.", pkg_fullname);
87	    return 1;
88	}
89	/*
90	 * Apply a crude heuristic to see how much space the package will
91	 * take up once it's unpacked.  I've noticed that most packages
92	 * compress an average of 65%.
93	 */
94	if (stat(pkg_fullname, &sb) == FAIL) {
95	    whinge("Can't stat package file '%s'.", pkg_fullname);
96	    return 1;
97	}
98	sb.st_size *= 1.65;
99	home = make_playpen(PlayPen, sb.st_size);
100	if (unpack(pkg_fullname, NULL))
101	    return 1;
102
103	if (sanity_check(pkg_fullname))
104	    return 1;
105
106	cfile = fopen(CONTENTS_FNAME, "r");
107	if (!cfile) {
108	    whinge("Unable to open %s file.", CONTENTS_FNAME);
109	    goto fail;
110	}
111	read_plist(&Plist, cfile);
112	fclose(cfile);
113	if (Prefix) {
114	    /*
115	     * If we have a prefix, delete the first one we see and add this
116	     * one in place of it.
117	     */
118	    delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
119	    add_plist_top(&Plist, PLIST_CWD, Prefix);
120	}
121	/* If we're running in MASTER mode, just output the plist and return */
122	if (AddMode == MASTER) {
123	    printf("%s\n", where_playpen());
124	    write_plist(&Plist, stdout);
125	    return 0;
126	}
127    }
128    setenv(PKG_PREFIX_VNAME,
129	   (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
130    PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
131    if (fexists(REQUIRE_FNAME)) {
132	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
133	if (Verbose)
134	    printf("Running requirements file first for %s..\n", PkgName);
135	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
136	    whinge("Package %s fails requirements - not installed.",
137		   pkg_fullname);
138	    return 1;
139	}
140    }
141    if (!NoInstall && fexists(INSTALL_FNAME)) {
142	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
143	if (Verbose)
144	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
145	if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
146	    whinge("Install script returned error status.");
147	    goto fail;
148	}
149    }
150    extract_plist(home, &Plist);
151    if (!NoInstall && fexists(INSTALL_FNAME)) {
152	if (Verbose)
153	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
154	if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
155	    whinge("Install script returned error status.");
156	    goto fail;
157	}
158    }
159    if (!NoRecord && !Fake) {
160	char contents[FILENAME_MAX];
161	FILE *cfile;
162
163	if (getuid() != 0)
164	    whinge("Not running as root - trying to record install anyway.");
165	if (!PkgName) {
166	    whinge("No package name!  Can't record package, sorry.");
167	    code = 1;
168	    goto success;	/* well, partial anyway */
169	}
170	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
171	if (Verbose)
172	    printf("Attempting to record package into %s..\n", LogDir);
173	if (make_hierarchy(LogDir)) {
174	    whinge("Can't record package into '%s', you're on your own!",
175		   LogDir);
176	    bzero(LogDir, FILENAME_MAX);
177	    code = 1;
178	    goto success;	/* close enough for government work */
179	}
180	/* Make sure pkg_info can read the entry */
181	vsystem("chmod a+rx %s", LogDir);
182	if (fexists(DEINSTALL_FNAME))
183	    copy_file(".", DEINSTALL_FNAME, LogDir);
184	if (fexists(REQUIRE_FNAME))
185	    copy_file(".", REQUIRE_FNAME, LogDir);
186	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
187	cfile = fopen(contents, "w");
188	if (!cfile) {
189	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
190		   contents);
191	    goto success; /* can't log, but still keep pkg */
192	}
193	write_plist(&Plist, cfile);
194	fclose(cfile);
195	copy_file(".", DESC_FNAME, LogDir);
196	copy_file(".", COMMENT_FNAME, LogDir);
197	if (Verbose)
198	    printf("Package %s registered in %s\n", PkgName, LogDir);
199    }
200    goto success;
201
202 fail:
203    /* Nuke the whole (installed) show */
204    if (!Fake)
205	delete_package(FALSE, &Plist);
206
207 success:
208    /* delete the packing list contents */
209    leave_playpen();
210
211    return code;
212}
213
214static int
215sanity_check(char *pkg)
216{
217    if (!fexists(CONTENTS_FNAME)) {
218	whinge("Package %s has no CONTENTS file!", pkg);
219	return 1;
220    }
221    if (!fexists(COMMENT_FNAME)) {
222	whinge("Package %s has no COMMENT file!", pkg);
223	return 1;
224    }
225    if (!fexists(DESC_FNAME)) {
226	whinge("Package %s has no DESC file!", pkg);
227	return 1;
228    }
229    return 0;
230}
231
232void
233cleanup(int signo)
234{
235    if (signo)
236	printf("Signal %d received, cleaning up..\n", signo);
237    if (Plist.head) {
238	if (!Fake)
239	    delete_package(FALSE, &Plist);
240	free_plist(&Plist);
241    }
242    if (!Fake && LogDir[0])
243	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
244    leave_playpen();
245}
246