Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2016-08-09 19:29:04 -04:00
|
|
|
* Copyright (c) 2013, 2015, 2016 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* tix.c
|
|
|
|
* Front end to the Tix package management system.
|
|
|
|
*/
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2016-08-09 19:29:04 -04:00
|
|
|
#include <err.h>
|
2013-03-26 07:40:43 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2014-01-20 12:30:13 -05:00
|
|
|
#include <signal.h>
|
2013-03-26 07:40:43 -04:00
|
|
|
#include <stdarg.h>
|
2016-02-28 18:10:59 -05:00
|
|
|
#include <stdbool.h>
|
2013-03-26 07:40:43 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char* collection;
|
|
|
|
char* tixdb_path;
|
|
|
|
string_array_t coll_conf;
|
|
|
|
string_array_t repo_list;
|
|
|
|
string_array_t inst_list;
|
2015-08-29 16:10:50 -04:00
|
|
|
bool reinstall;
|
2013-03-26 07:40:43 -04:00
|
|
|
} params_t;
|
|
|
|
|
2015-08-29 16:10:50 -04:00
|
|
|
char* FindPackageInRepository(params_t* params,
|
|
|
|
const char* repo,
|
|
|
|
const char* pkg_name)
|
2013-03-26 07:40:43 -04:00
|
|
|
{
|
2015-08-29 16:10:50 -04:00
|
|
|
char* repo_location;
|
|
|
|
if ( repo[0] == '/' )
|
|
|
|
repo_location = strdup(repo);
|
|
|
|
else
|
|
|
|
repo_location = join_paths(params->tixdb_path, repo);
|
|
|
|
char* tix_name = print_string("%s.tix.tar.xz", pkg_name);
|
|
|
|
char* tix_path = join_paths(repo_location, tix_name);
|
|
|
|
free(repo_location);
|
|
|
|
free(tix_name);
|
|
|
|
if ( !IsFile(tix_path) )
|
|
|
|
{
|
|
|
|
free(tix_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tix_path;
|
2013-03-26 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
2015-08-29 16:10:50 -04:00
|
|
|
char* FindPackage(params_t* params,
|
|
|
|
string_array_t* repositories,
|
|
|
|
const char* pkg_name)
|
2013-03-26 07:40:43 -04:00
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < repositories->length; i++ )
|
|
|
|
{
|
|
|
|
const char* repo = repositories->strings[i];
|
2015-08-29 16:10:50 -04:00
|
|
|
char* ret = FindPackageInRepository(params, repo, pkg_name);
|
2013-03-26 07:40:43 -04:00
|
|
|
if ( ret )
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_array_t GetPackageDependencies(params_t* params, const char* pkg_name)
|
|
|
|
{
|
|
|
|
string_array_t ret = string_array_make();
|
|
|
|
|
2015-08-29 16:10:50 -04:00
|
|
|
char* pkg_path = FindPackage(params, ¶ms->repo_list, pkg_name);
|
2013-03-26 07:40:43 -04:00
|
|
|
if ( !pkg_path )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "unable to locate package `%s'", pkg_name);
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
const char* tixinfo_path = "tix/tixinfo";
|
2015-08-29 15:34:06 -04:00
|
|
|
if ( !TarContainsFile(pkg_path, tixinfo_path) )
|
2016-08-09 19:29:04 -04:00
|
|
|
errx(1, "`%s' doesn't contain a `%s' file", pkg_path, tixinfo_path);
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
string_array_t tixinfo = string_array_make();
|
2015-08-29 15:34:06 -04:00
|
|
|
FILE* tixinfo_fp = TarOpenFile(pkg_path, tixinfo_path);
|
2013-03-26 07:40:43 -04:00
|
|
|
dictionary_append_file(&tixinfo, tixinfo_fp);
|
|
|
|
fclose(tixinfo_fp);
|
|
|
|
|
|
|
|
VerifyTixInformation(&tixinfo, pkg_path);
|
|
|
|
|
2016-02-28 18:10:59 -05:00
|
|
|
const char* deps = dictionary_get_def(&tixinfo, "pkg.runtime-deps", "");
|
2013-03-26 07:40:43 -04:00
|
|
|
string_array_append_token_string(&ret, deps);
|
2016-12-26 07:59:51 -05:00
|
|
|
const char* alias = dictionary_get_def(&tixinfo, "pkg.alias-of", "");
|
|
|
|
string_array_append_token_string(&ret, alias);
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
string_array_reset(&tixinfo);
|
|
|
|
|
|
|
|
free(pkg_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetPackageRecursiveDependencies(params_t* params, string_array_t* sofar,
|
|
|
|
const char* pkg_name)
|
|
|
|
{
|
|
|
|
if ( string_array_contains(sofar, pkg_name) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Avoid endless recursion by adding our package before the recursive call,
|
|
|
|
// in case we need to satisfy cyclic dependencies.
|
|
|
|
string_array_append(sofar, pkg_name);
|
|
|
|
|
|
|
|
string_array_t pkg_deps = GetPackageDependencies(params, pkg_name);
|
|
|
|
for ( size_t i = 0; i < pkg_deps.length; i++ )
|
|
|
|
if ( !string_array_contains(sofar, pkg_deps.strings[i]) )
|
|
|
|
GetPackageRecursiveDependencies(params, sofar, pkg_deps.strings[i]);
|
|
|
|
string_array_reset(&pkg_deps);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallPackageOfName(params_t* params, const char* pkg_name)
|
|
|
|
{
|
2015-08-29 16:10:50 -04:00
|
|
|
char* pkg_path = FindPackage(params, ¶ms->repo_list, pkg_name);
|
2013-03-26 07:40:43 -04:00
|
|
|
if ( !pkg_path )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "unable to locate package `%s'", pkg_name);
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
if ( fork_and_wait_or_death() )
|
|
|
|
{
|
|
|
|
const char* cmd_argv[] =
|
|
|
|
{
|
2015-08-29 15:34:06 -04:00
|
|
|
"tix-install",
|
2013-03-26 07:40:43 -04:00
|
|
|
"--collection", params->collection,
|
|
|
|
"--", pkg_path,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
execvp(cmd_argv[0], (char* const*) cmd_argv);
|
2016-08-09 19:29:04 -04:00
|
|
|
err(127, "`%s'", cmd_argv[0]);
|
2013-03-26 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(pkg_path);
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void help(FILE* fp, const char* argv0)
|
2013-03-26 07:40:43 -04:00
|
|
|
{
|
2015-08-29 16:10:50 -04:00
|
|
|
fprintf(fp, "Usage: %s [PREFIX] COMMAND ...\n", argv0);
|
2013-03-26 07:40:43 -04:00
|
|
|
fprintf(fp, "Front end to the Tix package management system.\n");
|
2015-08-29 16:10:50 -04:00
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, "Commands:\n");
|
|
|
|
fprintf(fp, " install [--reinstall] PACKAGE...\n");
|
2013-03-26 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2013-03-26 07:40:43 -04:00
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
2013-03-26 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
params_t params;
|
2015-08-29 16:10:50 -04:00
|
|
|
memset(¶ms, 0, sizeof(params));
|
2015-08-29 15:34:06 -04:00
|
|
|
params.collection = NULL;
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
const char* argv0 = argv[0];
|
|
|
|
for ( int i = 0; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2015-07-29 19:08:49 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
2013-03-26 07:40:43 -04:00
|
|
|
continue;
|
|
|
|
argv[i] = NULL;
|
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( arg[1] != '-' )
|
|
|
|
{
|
2016-02-28 18:10:59 -05:00
|
|
|
char c;
|
|
|
|
while ( (c = *++arg) ) switch ( c )
|
2013-03-26 07:40:43 -04:00
|
|
|
{
|
|
|
|
default:
|
2015-07-29 19:08:49 -04:00
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
|
|
|
help(stderr, argv0);
|
2013-03-26 07:40:43 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 19:08:49 -04:00
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
2013-03-26 07:40:43 -04:00
|
|
|
else if ( GET_OPTION_VARIABLE("--collection", ¶ms.collection) ) { }
|
2015-08-29 16:10:50 -04:00
|
|
|
else if ( !strcmp(arg, "--reinstall") )
|
|
|
|
params.reinstall = true;
|
2013-03-26 07:40:43 -04:00
|
|
|
else
|
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
|
|
|
help(stderr, argv0);
|
2013-03-26 07:40:43 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( argc == 1 )
|
|
|
|
{
|
2015-07-29 19:08:49 -04:00
|
|
|
help(stdout, argv0);
|
2013-03-26 07:40:43 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
ParseOptionalCommandLineCollectionPrefix(¶ms.collection, &argc, &argv);
|
|
|
|
VerifyCommandLineCollection(¶ms.collection);
|
|
|
|
|
|
|
|
params.tixdb_path = join_paths(params.collection, "tix");
|
|
|
|
|
|
|
|
char* coll_conf_path = join_paths(params.tixdb_path, "collection.conf");
|
|
|
|
params.coll_conf = string_array_make();
|
|
|
|
if ( !dictionary_append_file_path(¶ms.coll_conf, coll_conf_path) )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "`%s'", coll_conf_path);
|
2013-03-26 07:40:43 -04:00
|
|
|
VerifyTixCollectionConfiguration(¶ms.coll_conf, coll_conf_path);
|
|
|
|
free(coll_conf_path);
|
|
|
|
|
|
|
|
char* repo_list_path = join_paths(params.tixdb_path, "repository.list");
|
|
|
|
params.repo_list = string_array_make();
|
|
|
|
if ( !string_array_append_file_path(¶ms.repo_list, repo_list_path) )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "`%s'", repo_list_path);
|
2013-03-26 07:40:43 -04:00
|
|
|
free(repo_list_path);
|
|
|
|
|
|
|
|
char* inst_list_path = join_paths(params.tixdb_path, "installed.list");
|
|
|
|
params.inst_list = string_array_make();
|
|
|
|
if ( !string_array_append_file_path(¶ms.inst_list, inst_list_path) )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "`%s'", inst_list_path);
|
2013-03-26 07:40:43 -04:00
|
|
|
free(inst_list_path);
|
|
|
|
|
|
|
|
if ( argc == 1 )
|
2016-08-09 19:29:04 -04:00
|
|
|
errx(1, "error: no command specified.");
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
const char* cmd = argv[1];
|
|
|
|
if ( !strcmp(cmd, "install") )
|
|
|
|
{
|
|
|
|
if ( argc == 2 )
|
2016-08-09 19:29:04 -04:00
|
|
|
errx(1, "expected list of packages to install after `install'");
|
2013-03-26 07:40:43 -04:00
|
|
|
|
|
|
|
string_array_t work = string_array_make();
|
|
|
|
|
|
|
|
for ( int i = 2; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* pkg_name = argv[i];
|
|
|
|
if ( string_array_contains(¶ms.inst_list, pkg_name) )
|
|
|
|
{
|
2015-08-29 16:10:50 -04:00
|
|
|
if ( params.reinstall )
|
|
|
|
{
|
|
|
|
string_array_append(&work, pkg_name);
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-26 07:40:43 -04:00
|
|
|
printf("Package `%s' is already installed.\n", pkg_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetPackageRecursiveDependencies(¶ms, &work, pkg_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < work.length; i++ )
|
|
|
|
InstallPackageOfName(¶ms, work.strings[i]);
|
|
|
|
|
|
|
|
string_array_reset(&work);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown command: `%s'\n", argv0, cmd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|