-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle_read_install_pkg.c
343 lines (266 loc) · 11.9 KB
/
bundle_read_install_pkg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-
* Copyright (c) 2007-2009 Chris Reinhardt
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $MidnightBSD: src/lib/libmport/install_primative.c,v 1.2 2008/04/26 17:59:26 ctriv Exp $
*/
#include "mport.h"
#include "mport_private.h"
#include <sys/stat.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <archive_entry.h>
static int do_pre_install(mportInstance *, mportBundleRead *, mportPackageMeta *);
static int do_actual_install(mportInstance *, mportBundleRead *, mportPackageMeta *);
static int do_post_install(mportInstance *, mportBundleRead *, mportPackageMeta *);
static int run_pkg_install(mportInstance *, mportBundleRead *, mportPackageMeta *, const char *);
static int run_mtree(mportInstance *, mportBundleRead *, mportPackageMeta *);
static int display_pkg_msg(mportInstance *, mportBundleRead *, mportPackageMeta *);
int mport_bundle_read_install_pkg(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
if (do_pre_install(mport, bundle, pkg) != MPORT_OK)
RETURN_CURRENT_ERROR;
if (do_actual_install(mport, bundle, pkg) != MPORT_OK)
RETURN_CURRENT_ERROR;
if (do_post_install(mport, bundle, pkg) != MPORT_OK)
RETURN_CURRENT_ERROR;
return MPORT_OK;
}
/* This does everything that has to happen before we start installing files.
* We run mtree, pkg-install PRE-INSTALL, etc...
*/
static int do_pre_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
/* run mtree */
if (run_mtree(mport, bundle, pkg) != MPORT_OK)
RETURN_CURRENT_ERROR;
/* run pkg-install PRE-INSTALL */
if (run_pkg_install(mport, bundle, pkg, "PRE-INSTALL") != MPORT_OK)
RETURN_CURRENT_ERROR;
return MPORT_OK;
}
static int do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
int file_total, ret;
int file_count = 0;
mportAssetListEntryType type;
struct archive_entry *entry;
char *data, *checksum, *orig_cwd;
char file[FILENAME_MAX], cwd[FILENAME_MAX], dir[FILENAME_MAX];
sqlite3_stmt *assets, *count, *insert;
sqlite3 *db;
db = mport->db;
/* sadly, we can't just use abs pathnames, because it will break hardlinks */
orig_cwd = getcwd(NULL, 0);
/* get the file count for the progress meter */
if (mport_db_prepare(db, &count, "SELECT COUNT(*) FROM stub.assets WHERE type=%i AND pkg=%Q", ASSET_FILE, pkg->name) != MPORT_OK)
RETURN_CURRENT_ERROR;
switch (sqlite3_step(count)) {
case SQLITE_ROW:
file_total = sqlite3_column_int(count, 0);
sqlite3_finalize(count);
break;
default:
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
sqlite3_finalize(count);
RETURN_CURRENT_ERROR;
}
mport_call_progress_init_cb(mport, "Installing %s-%s", pkg->name, pkg->version);
/* Insert the package meta row into the packages table (We use pack here because things might have been twiddled) */
/* Note that this will be marked as dirty by default */
if (mport_db_do(db, "INSERT INTO packages (pkg, version, origin, prefix, lang, options, comment) VALUES (%Q,%Q,%Q,%Q,%Q,%Q,%Q)", pkg->name, pkg->version, pkg->origin, pkg->prefix, pkg->lang, pkg->options, pkg->comment) != MPORT_OK)
goto ERROR;
/* Insert the assets into the master table (We do this one by one because we want to insert file
* assets as absolute paths. */
if (mport_db_prepare(db, &insert, "INSERT INTO assets (pkg, type, data, checksum) values (%Q,?,?,?)", pkg->name) != MPORT_OK)
goto ERROR;
/* Insert the depends into the master table */
if (mport_db_do(db, "INSERT INTO depends (pkg, depend_pkgname, depend_pkgversion, depend_port) SELECT pkg,depend_pkgname,depend_pkgversion,depend_port FROM stub.depends WHERE pkg=%Q", pkg->name) != MPORT_OK)
goto ERROR;
/* Insert the categories into the master table */
if (mport_db_do(db, "INSERT INTO categories (pkg, category) SELECT pkg, category FROM stub.categories WHERE pkg=%Q", pkg->name) != MPORT_OK)
goto ERROR;
if (mport_db_prepare(db, &assets, "SELECT type,data,checksum FROM stub.assets WHERE pkg=%Q", pkg->name) != MPORT_OK)
goto ERROR;
(void)strlcpy(cwd, pkg->prefix, sizeof(cwd));
if (mport_chdir(mport, cwd) != MPORT_OK)
goto ERROR;
while (1) {
ret = sqlite3_step(assets);
if (ret == SQLITE_DONE)
break;
if (ret != SQLITE_ROW) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
type = (mportAssetListEntryType)sqlite3_column_int(assets, 0);
data = (char *)sqlite3_column_text(assets, 1);
checksum = (char *)sqlite3_column_text(assets, 2);
switch (type) {
case ASSET_CWD:
(void)strlcpy(cwd, data == NULL ? pkg->prefix : data, sizeof(cwd));
if (mport_chdir(mport, cwd) != MPORT_OK)
goto ERROR;
break;
case ASSET_EXEC:
if (mport_run_asset_exec(mport, data, cwd, file) != MPORT_OK)
goto ERROR;
break;
case ASSET_FILE:
if (mport_bundle_read_next_entry(bundle, &entry) != MPORT_OK)
goto ERROR;
(void)snprintf(file, FILENAME_MAX, "%s%s/%s", mport->root, cwd, data);
archive_entry_set_pathname(entry, file);
if (mport_bundle_read_extract_next_file(bundle, entry) != MPORT_OK)
goto ERROR;
(mport->progress_step_cb)(++file_count, file_total, file);
break;
default:
/* do nothing */
break;
}
/* insert this assest into the master database */
if (sqlite3_bind_int(insert, 1, (int)type) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
if (type == ASSET_FILE) {
/* don't put the root in the database! */
if (sqlite3_bind_text(insert, 2, file + strlen(mport->root), -1, SQLITE_STATIC) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
if (sqlite3_bind_text(insert, 3, checksum, -1, SQLITE_STATIC) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
} else if (type == ASSET_DIRRM || type == ASSET_DIRRMTRY) {
(void)snprintf(dir, FILENAME_MAX, "%s/%s", cwd, data);
if (sqlite3_bind_text(insert, 2, dir, -1, SQLITE_STATIC) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
if (sqlite3_bind_null(insert, 3) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
} else {
if (sqlite3_bind_text(insert, 2, data, -1, SQLITE_STATIC) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
if (sqlite3_bind_null(insert, 3) != SQLITE_OK) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
}
if (sqlite3_step(insert) != SQLITE_DONE) {
SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
goto ERROR;
}
sqlite3_reset(insert);
}
sqlite3_finalize(assets);
sqlite3_finalize(insert);
if (mport_db_do(db, "UPDATE packages SET status='clean' WHERE pkg=%Q", pkg->name) != MPORT_OK)
goto ERROR;
mport_pkgmeta_logevent(mport, pkg, "Installed");
(mport->progress_free_cb)();
(void)mport_chdir(NULL, orig_cwd);
free(orig_cwd);
return MPORT_OK;
ERROR:
(mport->progress_free_cb)();
free(orig_cwd);
//rollback();
RETURN_CURRENT_ERROR;
}
#define COPY_METAFILE(TYPE) (void)snprintf(from, FILENAME_MAX, "%s/%s/%s-%s/%s", bundle->tmpdir, MPORT_STUB_INFRA_DIR, pkg->name, pkg->version, TYPE); \
if (mport_file_exists(from)) { \
(void)snprintf(to, FILENAME_MAX, "%s%s/%s-%s/%s", mport->root, MPORT_INST_INFRA_DIR, pkg->name, pkg->version, TYPE); \
if (mport_mkdir(dirname(to)) != MPORT_OK) \
RETURN_CURRENT_ERROR; \
if (mport_copy_file(from, to) != MPORT_OK) \
RETURN_CURRENT_ERROR; \
}
static int do_post_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
char to[FILENAME_MAX], from[FILENAME_MAX];
COPY_METAFILE(MPORT_MTREE_FILE);
COPY_METAFILE(MPORT_INSTALL_FILE);
COPY_METAFILE(MPORT_DEINSTALL_FILE);
COPY_METAFILE(MPORT_MESSAGE_FILE);
if (display_pkg_msg(mport, bundle, pkg) != MPORT_OK)
RETURN_CURRENT_ERROR;
return run_pkg_install(mport, bundle, pkg, "POST-INSTALL");
}
static int run_mtree(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
char file[FILENAME_MAX];
int ret;
(void)snprintf(file, FILENAME_MAX, "%s/%s/%s-%s/%s", bundle->tmpdir, MPORT_STUB_INFRA_DIR, pkg->name, pkg->version, MPORT_MTREE_FILE);
if (mport_file_exists(file)) {
if ((ret = mport_xsystem(mport, "%s -U -f %s -d -e -p %s >/dev/null", MPORT_MTREE_BIN, file, pkg->prefix)) != 0)
RETURN_ERRORX(MPORT_ERR_FATAL, "%s returned non-zero: %i", MPORT_MTREE_BIN, ret);
}
return MPORT_OK;
}
static int run_pkg_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg, const char *mode)
{
char file[FILENAME_MAX];
int ret;
(void)snprintf(file, FILENAME_MAX, "%s/%s/%s-%s/%s", bundle->tmpdir, MPORT_STUB_INFRA_DIR, pkg->name, pkg->version, MPORT_INSTALL_FILE);
if (mport_file_exists(file)) {
if (chmod(file, 755) != 0)
RETURN_ERRORX(MPORT_ERR_FATAL, "chmod(%s, 755): %s", file, strerror(errno));
if ((ret = mport_xsystem(mport, "PKG_PREFIX=%s %s %s %s", pkg->prefix, file, pkg->name, mode)) != 0)
RETURN_ERRORX(MPORT_ERR_FATAL, "%s %s returned non-zero: %i", MPORT_INSTALL_FILE, mode, ret);
}
return MPORT_OK;
}
static int display_pkg_msg(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
{
char filename[FILENAME_MAX];
char *buf;
struct stat st;
FILE *file;
(void)snprintf(filename, FILENAME_MAX, "%s/%s/%s-%s/%s", bundle->tmpdir, MPORT_STUB_INFRA_DIR, pkg->name, pkg->version, MPORT_MESSAGE_FILE);
if (stat(filename, &st) == -1)
/* if we couldn't stat the file, we assume there isn't a pkg-msg */
return MPORT_OK;
if ((file = fopen(filename, "r")) == NULL)
RETURN_ERRORX(MPORT_ERR_FATAL, "Couldn't open %s: %s", filename, strerror(errno));
if ((buf = (char *)malloc((st.st_size + 1) * sizeof(char))) == NULL)
RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
if (fread(buf, 1, st.st_size, file) != st.st_size) {
free(buf);
RETURN_ERRORX(MPORT_ERR_FATAL, "Read error: %s", strerror(errno));
}
buf[st.st_size] = 0;
mport_call_msg_cb(mport, buf);
free(buf);
return MPORT_OK;
}