#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <pkg.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include "pkgcli.h"
void
usage_register(void)
{
fprintf(stderr, "Usage: pkg register [-dtN] [-i <input-path>]"
" [-f <plist-file>] -m <metadatadir>\n");
fprintf(stderr, " pkg register [-dtN] [-i <input_path>]"
" -M <manifest>\n\n");
fprintf(stderr, "For more information see 'pkg help register'.\n");
}
int
exec_register(int argc, char **argv)
{
struct pkg *pkg = NULL;
struct pkgdb *db = NULL;
const char *plist = NULL;
const char *mdir = NULL;
const char *mfile = NULL;
const char *input_path = NULL;
const char *location = NULL;
bool testing_mode = false;
bool reg_in_db = true;
int ch;
int ret = EPKG_OK;
int retcode = EXIT_SUCCESS;
struct option longopts[] = {
{ "automatic", no_argument, NULL, 'A' },
{ "debug", no_argument, NULL, 'd' },
{ "manifest", required_argument, NULL, 'M' },
{ "metadata", required_argument, NULL, 'm' },
{ "no-registration", no_argument, NULL, 'N' },
{ "plist", required_argument, NULL, 'f' },
{ "relocate", required_argument, NULL, 1 },
{ "root", required_argument, NULL, 'i' },
{ "test", no_argument, NULL, 't' },
{ NULL, 0, NULL, 0},
};
if (pkg_new(&pkg, PKG_INSTALLED) != EPKG_OK)
err(EXIT_FAILURE, "malloc");
while ((ch = getopt_long(argc, argv, "+Adf:i:M:m:Nt", longopts, NULL)) != -1) {
switch (ch) {
case 'A':
case 'd':
pkg_set(pkg, PKG_ATTR_AUTOMATIC, (bool)true);
break;
case 'f':
plist = optarg;
break;
case 'i':
input_path = optarg;
break;
case 'M':
mfile = optarg;
break;
case 'm':
mdir = optarg;
break;
case 'N':
reg_in_db = false;
break;
case 't':
testing_mode = true;
break;
case 1:
location = optarg;
break;
default:
warnx("Unrecognised option -%c\n", ch);
usage_register();
pkg_free(pkg);
return (EXIT_FAILURE);
}
}
retcode = pkgdb_access(PKGDB_MODE_READ |
PKGDB_MODE_WRITE |
PKGDB_MODE_CREATE,
PKGDB_DB_LOCAL);
if (retcode == EPKG_ENOACCESS) {
warnx("Insufficient privileges to register packages");
pkg_free(pkg);
return (EXIT_FAILURE);
} else if (retcode != EPKG_OK) {
pkg_free(pkg);
return (EXIT_FAILURE);
}
if (mfile != NULL && mdir != NULL) {
warnx("Cannot use both -m and -M together");
usage_register();
pkg_free(pkg);
return (EXIT_FAILURE);
}
if (mfile == NULL && mdir == NULL) {
warnx("One of either -m or -M flags is required");
usage_register();
pkg_free(pkg);
return (EXIT_FAILURE);
}
if (testing_mode && input_path != NULL) {
warnx("-i incompatible with -t option");
usage_register();
pkg_free(pkg);
return (EXIT_FAILURE);
}
ret = pkg_load_metadata(pkg, mfile, mdir, plist, input_path, testing_mode);
if (ret != EPKG_OK) {
pkg_free(pkg);
return (EXIT_FAILURE);
}
if (reg_in_db && pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
pkg_free(pkg);
return (EXIT_FAILURE);
}
if (db != NULL && pkgdb_obtain_lock(db, PKGDB_LOCK_EXCLUSIVE) != EPKG_OK) {
pkgdb_close(db);
pkg_free(pkg);
warnx("Cannot get an exclusive lock on a database, it is locked by another process");
return (EXIT_FAILURE);
}
retcode = pkg_add_port(db, pkg, input_path, location, testing_mode);
if (retcode == EPKG_OK && messages != NULL) {
fflush(messages->fp);
printf("%s\n", messages->buf);
}
pkg_free(pkg);
if (db != NULL)
pkgdb_release_lock(db, PKGDB_LOCK_EXCLUSIVE);
return (retcode != EPKG_OK ? EXIT_FAILURE : EXIT_SUCCESS);
}