forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.c
312 lines (284 loc) · 8.06 KB
/
create.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
/*-
* Copyright (c) 2011-2012 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2011 Will Andrews <[email protected]>
* 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
* in this position and unchanged.
* 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(S) ``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(S) 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.
*/
#ifdef HAVE_CONFIG_H
#include "pkg_config.h"
#endif
#include <sys/param.h>
#include <sys/queue.h>
#ifdef PKG_COMPAT
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif
#include <err.h>
#include <getopt.h>
#include <stdio.h>
#include <pkg.h>
#include <string.h>
#include <unistd.h>
#include <sysexits.h>
#include "pkgcli.h"
struct pkg_entry {
struct pkg *pkg;
STAILQ_ENTRY(pkg_entry) next;
};
STAILQ_HEAD(pkg_head, pkg_entry);
void
usage_create(void)
{
fprintf(stderr, "Usage: pkg create [-On] [-f format] [-o outdir] "
"[-p plist] [-r rootdir] -m metadatadir\n");
fprintf(stderr, "Usage: pkg create [-On] [-f format] [-o outdir] "
"[-r rootdir] -M manifest\n");
fprintf(stderr, " pkg create [-Ognx] [-f format] [-o outdir] "
"[-r rootdir] pkg-name ...\n");
fprintf(stderr, " pkg create [-On] [-f format] [-o outdir] "
"[-r rootdir] -a\n\n");
fprintf(stderr, "For more information see 'pkg help create'.\n");
}
static int
pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt,
const char * const outdir, bool overwrite)
{
int i, ret = EPKG_OK, retcode = EPKG_OK;
struct pkg *pkg = NULL;
struct pkgdb *db = NULL;
struct pkgdb_it *it = NULL;
int query_flags = PKG_LOAD_DEPS | PKG_LOAD_FILES |
PKG_LOAD_CATEGORIES | PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS |
PKG_LOAD_OPTIONS | PKG_LOAD_MTREE | PKG_LOAD_LICENSES |
PKG_LOAD_USERS | PKG_LOAD_GROUPS | PKG_LOAD_SHLIBS_REQUIRED |
PKG_LOAD_SHLIBS_PROVIDED | PKG_LOAD_ANNOTATIONS;
struct pkg_head head = STAILQ_HEAD_INITIALIZER(head);
struct pkg_entry *e = NULL;
char pkgpath[MAXPATHLEN];
const char *format = NULL;
bool foundone;
if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
pkgdb_close(db);
return (EX_IOERR);
}
/* XXX: get rid of hardcoded timeouts */
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
pkgdb_close(db);
warnx("Cannot get a read lock on a database, it is locked by another process");
return (EX_TEMPFAIL);
}
switch (fmt) {
case TXZ:
format = "txz";
break;
case TBZ:
format = "tbz";
break;
case TGZ:
format = "tgz";
break;
case TAR:
format = "tar";
break;
}
for (i = 0; i < argc || match == MATCH_ALL; i++) {
if (match == MATCH_ALL) {
printf("Loading the package list...\n");
if ((it = pkgdb_query(db, NULL, match)) == NULL)
goto cleanup;
match = !MATCH_ALL;
} else
if ((it = pkgdb_query(db, argv[i], match)) == NULL)
goto cleanup;
foundone = false;
while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK) {
if ((e = malloc(sizeof(struct pkg_entry))) == NULL)
err(1, "malloc(pkg_entry)");
e->pkg = pkg;
pkg = NULL;
STAILQ_INSERT_TAIL(&head, e, next);
foundone = true;
}
if (!foundone) {
warnx("No installed package matching \"%s\" found\n",
argv[i]);
retcode++;
}
pkgdb_it_free(it);
if (ret != EPKG_END)
retcode++;
}
while (!STAILQ_EMPTY(&head)) {
e = STAILQ_FIRST(&head);
STAILQ_REMOVE_HEAD(&head, next);
if (!overwrite) {
pkg_snprintf(pkgpath, sizeof(pkgpath), "%S/%n-%v.%S",
outdir, e->pkg, e->pkg, format);
if (access(pkgpath, F_OK) == 0) {
pkg_printf("%n-%v already packaged, skipping...\n",
e->pkg, e->pkg);
pkg_free(e->pkg);
free(e);
continue;
}
}
pkg_printf("Creating package for %n-%v\n", e->pkg, e->pkg);
if (pkg_create_installed(outdir, fmt, e->pkg) !=
EPKG_OK)
retcode++;
pkg_free(e->pkg);
free(e);
}
cleanup:
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
pkgdb_close(db);
return (retcode);
}
/*
* options:
* -x: regex
* -g: globbing
* -r: rootdir for the package
* -m: path to dir where to find the metadata
* -M: manifest file
* -f <format>: format could be txz, tgz, tbz or tar
* -o: output directory where to create packages by default ./ is used
*/
int
exec_create(int argc, char **argv)
{
match_t match = MATCH_EXACT;
const char *outdir = NULL;
const char *format = NULL;
const char *rootdir = NULL;
const char *metadatadir = NULL;
const char *manifest = NULL;
char *plist = NULL;
pkg_formats fmt;
int ch;
bool overwrite = true;
bool old = false;
struct option longopts[] = {
{ "all", no_argument, NULL, 'a' },
{ "glob", no_argument, NULL, 'g' },
{ "regex", no_argument, NULL, 'x' },
{ "format", required_argument, NULL, 'f' },
{ "root-dir", required_argument, NULL, 'r' },
{ "metadata", required_argument, NULL, 'm' },
{ "manifest", required_argument, NULL, 'M' },
{ "out-dir", required_argument, NULL, 'o' },
{ "no-clobber", no_argument, NULL, 'n' },
{ "plist", required_argument, NULL, 'p' },
{ "old", no_argument, NULL, 'O' },
{ NULL, 0, NULL, 0 },
};
while ((ch = getopt_long(argc, argv, "agxf:r:m:M:o:np:O", longopts, NULL)) != -1) {
switch (ch) {
case 'a':
match = MATCH_ALL;
break;
case 'g':
match = MATCH_GLOB;
break;
case 'x':
match = MATCH_REGEX;
break;
case 'f':
format = optarg;
break;
case 'o':
outdir = optarg;
break;
case 'r':
rootdir = optarg;
break;
case 'm':
metadatadir = optarg;
break;
case 'M':
manifest = optarg;
break;
case 'n':
overwrite = false;
break;
case 'p':
plist = optarg;
break;
case 'O':
old = true;
break;
default:
usage_create();
return (EX_USAGE);
}
}
argc -= optind;
argv += optind;
if (match != MATCH_ALL && metadatadir == NULL && manifest == NULL &&
argc == 0) {
usage_create();
return (EX_USAGE);
}
if (metadatadir == NULL && manifest == NULL && rootdir != NULL) {
warnx("Do not specify a rootdir when creating a package from an installed package");
usage_create();
return (EX_USAGE);
}
if (outdir == NULL)
outdir = "./";
if (format == NULL) {
fmt = old ? TBZ : TXZ;
} else {
if (format[0] == '.')
++format;
if (strcmp(format, "txz") == 0)
fmt = TXZ;
else if (strcmp(format, "tbz") == 0)
fmt = TBZ;
else if (strcmp(format, "tgz") == 0)
fmt = TGZ;
else if (strcmp(format, "tar") == 0)
fmt = TAR;
else {
warnx("unknown format %s, using txz", format);
fmt = old ? TBZ : TXZ;
}
}
if (metadatadir == NULL && manifest == NULL) {
if (old) {
warnx("Can only create an old package format"
" out of a staged directory");
return (EX_SOFTWARE);
}
return (pkg_create_matches(argc, argv, match, fmt, outdir,
overwrite) == EPKG_OK ? EX_OK : EX_SOFTWARE);
} else if (metadatadir != NULL) {
return (pkg_create_staged(outdir, fmt, rootdir, metadatadir,
plist, old) == EPKG_OK ? EX_OK : EX_SOFTWARE);
} else { /* (manifest != NULL) */
return (pkg_create_from_manifest(outdir, fmt, rootdir,
manifest, old) == EPKG_OK ? EX_OK : EX_SOFTWARE);
}
}