From f4560a952790cdf5a8c4b6da869c40644ab0f8b4 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 29 Aug 2015 21:34:06 +0200 Subject: [PATCH] Remove tix tools command line interface cruft. This removes the ability to override standard shell utilities using environment variables. The standard names are invoked unconditionally and can be overridden using the standard approach of adding replacements to the PATH. Additionally environment variables like PREFIX and HOST are no longer honored as defaults for the --prefix and --host options. These features are removed because they've never been used and cause more trouble than they are worth. The tix collection option now defaults to the root directory to simplify common invocations. The tix-build prefix also now defaults to the empty prefix. Support installing multiple packages at once with tix-install. Tighten file and directory creation modes while here. Add --generation for forward compatibility. Silence tix-collection creation. Fix uninitialized getline invocations. Fix porttix-create buffer overflow. --- build-aux/build-ports.sh | 5 +- tix/porttix-create.cpp | 42 +++++++---------- tix/srctix-create.cpp | 28 ++++-------- tix/tix-build.cpp | 41 ++++++++++------- tix/tix-collection.cpp | 17 ++++--- tix/tix-install.cpp | 98 +++++++++++++++++++++------------------- tix/tix.cpp | 19 +++----- tix/util.h | 36 +++++++++------ 8 files changed, 143 insertions(+), 143 deletions(-) diff --git a/build-aux/build-ports.sh b/build-aux/build-ports.sh index a1cc0b50..37799ce5 100755 --- a/build-aux/build-ports.sh +++ b/build-aux/build-ports.sh @@ -134,8 +134,8 @@ mkdir -p "$SYSROOT" mkdir -p "$SORTIX_REPOSITORY_DIR" # Initialize Tix package management in the system root if absent. -[ -d "$SYSROOT/tix" ] || -tix-collection "$SYSROOT" create --platform=$HOST --prefix= --disable-multiarch +[ -e "$SYSROOT/tix/collection.conf" ] || +tix-collection "$SYSROOT" create --platform=$HOST --prefix= --disable-multiarch --generation=1 # Build all the packages (if needed) and otherwise install them. for PACKAGE in $PACKAGES; do @@ -149,6 +149,7 @@ for PACKAGE in $PACKAGES; do --prefix= \ --exec-prefix= \ --destination="$SORTIX_REPOSITORY_DIR" \ + --generation=1 \ "$SORTIX_PORTS_DIR/$PACKAGE" tix-install \ --collection="$SYSROOT" \ diff --git a/tix/porttix-create.cpp b/tix/porttix-create.cpp index feec20c3..4f7fd7ce 100644 --- a/tix/porttix-create.cpp +++ b/tix/porttix-create.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2015. This file is part of Tix. @@ -70,14 +70,10 @@ static void version(FILE* fp, const char* argv0) } int main(int argc, char* argv[]) { - char* cp = strdup(getenv_def("CP", "cp")); - char* diff = strdup(getenv_def("DIFF", "diff")); char* input_normalized_path = NULL; char* input_tarball_path = NULL; char* output_directory = strdup("."); char* output = NULL; - char* tar = strdup(getenv_def("TAR", "tar")); - char* tix_execdiff = strdup(getenv_def("TIX_EXECDIFF", "tix-execdiff")); char* tmp = strdup(getenv_def("TMP", "/tmp")); const char* argv0 = argv[0]; @@ -103,14 +99,10 @@ int main(int argc, char* argv[]) help(stdout, argv0), exit(0); else if ( !strcmp(arg, "--version") ) version(stdout, argv0), exit(0); - else if ( GET_OPTION_VARIABLE("--cp", &cp) ) { } - else if ( GET_OPTION_VARIABLE("--diff", &diff) ) { } else if ( GET_OPTION_VARIABLE("--normalized", &input_normalized_path) ) { } else if ( GET_OPTION_VARIABLE("--output-directory", &output_directory) ) { } else if ( GET_OPTION_VARIABLE("--output", &output) ) { } else if ( GET_OPTION_VARIABLE("--tarball", &input_tarball_path) ) { } - else if ( GET_OPTION_VARIABLE("--tar", &tar) ) { } - else if ( GET_OPTION_VARIABLE("--tix-execdiff", &tix_execdiff) ) { } else if ( GET_OPTION_VARIABLE("--tmp", &tmp) ) { } else { @@ -164,7 +156,7 @@ int main(int argc, char* argv[]) output = print_string("%s/%s.porttix.tar.xz", output_directory, package_name); char* tmp_root = print_string("%s/tmppid.%ju", tmp, (uintmax_t) getpid()); - if ( mkdir_p(tmp_root, 0777) != 0 ) + if ( mkdir_p(tmp_root, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tmp_root); on_exit(cleanup_file_or_directory, tmp_root); @@ -175,15 +167,15 @@ int main(int argc, char* argv[]) char* rel_normalized_path = print_string("%s.normalized", package_name); char* porttix_path = print_string("%s/%s", tmp_root, package_name); - if ( mkdir_p(porttix_path, 0777) != 0 ) + if ( mkdir_p(porttix_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", porttix_path); char* srctix_path = print_string("%s/%s", tmp_root, rel_srctix_path); - if ( mkdir_p(srctix_path, 0777) != 0 ) + if ( mkdir_p(srctix_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", srctix_path); char* normalized_path = print_string("%s/%s", tmp_root, rel_normalized_path); - if ( mkdir_p(normalized_path, 0777) != 0 ) + if ( mkdir_p(normalized_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", normalized_path); // Create the porttixinfo file. @@ -198,7 +190,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - cp, + "cp", "-HRT", "--preserve=timestamps,links", "--", @@ -222,7 +214,7 @@ int main(int argc, char* argv[]) error(1, errno, "chdir: `%s'", work_dir); const char* cmd_argv[] = { - tar, + "tar", "--create", "--xz", "--directory", input_normalized_path, @@ -243,7 +235,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - cp, + "cp", "-HRT", "--preserve=timestamps,links", "--", @@ -263,7 +255,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tar, + "tar", "--extract", "--directory", normalized_path, "--file", input_tarball_path, @@ -281,7 +273,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - cp, + "cp", "--", input_tarball_path, porttix_tarball_path, @@ -313,7 +305,7 @@ int main(int argc, char* argv[]) close(pipes[0]); const char* cmd_argv[] = { - tar, + "tar", "--list", "--file", porttix_tarball_path, "--strip-components=1", @@ -333,7 +325,7 @@ int main(int argc, char* argv[]) if ( line_len && line[line_len-1] == '\n' ) line[--line_len] = '\0'; const char* path = line; - while ( *path != '/' ) + while ( *path && *path != '/' ) path++; if ( *path == '/' ) path++; @@ -373,13 +365,13 @@ int main(int argc, char* argv[]) if ( fork_and_wait_or_death(false) ) { close(1); - if ( open(patch_path, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1 ) + if ( open(patch_path, O_WRONLY | O_CREAT | O_TRUNC, 0644) != 1 ) error(1, errno, "`%s'", patch_path); if ( chdir(tmp_root) != 0 ) error(1, errno, "chdir(`%s')", tmp_root); const char* cmd_argv[] = { - diff, + "diff", "--no-dereference", "-Naur", "--", @@ -397,13 +389,13 @@ int main(int argc, char* argv[]) char* patch_exec_path = join_paths(porttix_path, "patch.execpatch"); if ( fork_and_wait_or_death(false) ) { - if ( redirect(patch_exec_path, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 0 ) + if ( redirect(patch_exec_path, O_WRONLY | O_CREAT | O_TRUNC, 0644) != 0 ) error(1, errno, "`%s'", patch_exec_path); if ( chdir(tmp_root) != 0 ) error(1, errno, "chdir(`%s')", tmp_root); const char* cmd_argv[] = { - tix_execdiff, + "tix-execdiff", "--", rel_normalized_path, rel_srctix_path, @@ -424,7 +416,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tar, + "tar", "--create", "--xz", "--directory", tmp_root, diff --git a/tix/srctix-create.cpp b/tix/srctix-create.cpp index 841eba17..f723b6c3 100644 --- a/tix/srctix-create.cpp +++ b/tix/srctix-create.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2015. This file is part of Tix. @@ -68,10 +68,6 @@ int main(int argc, char* argv[]) { char* output_directory = strdup("."); char* output = NULL; - char* patch = strdup(getenv_def("PATCH", "patch")); - char* tar = strdup(getenv_def("TAR", "tar")); - char* tix_execpatch = strdup(getenv_def("TIX_EXECPATCH", "tix-execpatch")); - char* tix_rmpatch = strdup(getenv_def("TIX_RMPATCH", "tix-rmpatch")); char* tmp = strdup(getenv_def("TMP", "/tmp")); const char* argv0 = argv[0]; @@ -99,10 +95,6 @@ int main(int argc, char* argv[]) version(stdout, argv0), exit(0); else if ( GET_OPTION_VARIABLE("--output-directory", &output_directory) ) { } else if ( GET_OPTION_VARIABLE("--output", &output) ) { } - else if ( GET_OPTION_VARIABLE("--patch", &patch) ) { } - else if ( GET_OPTION_VARIABLE("--tar", &tar) ) { } - else if ( GET_OPTION_VARIABLE("--tix-execpatch", &tix_execpatch) ) { } - else if ( GET_OPTION_VARIABLE("--tix-rmpatch", &tix_rmpatch) ) { } else if ( GET_OPTION_VARIABLE("--tmp", &tmp) ) { } else { @@ -129,7 +121,7 @@ int main(int argc, char* argv[]) const char* porttix_path = argv[1]; char* tmp_in_root = print_string("%s/tmppid.%ju.in", tmp, (uintmax_t) getpid()); - if ( mkdir_p(tmp_in_root, 0777) != 0 ) + if ( mkdir_p(tmp_in_root, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tmp_in_root); on_exit(cleanup_file_or_directory, tmp_in_root); @@ -137,7 +129,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tar, + "tar", "--extract", "--directory", tmp_in_root, "--file", porttix_path, @@ -159,7 +151,7 @@ int main(int argc, char* argv[]) } char* tmp_out_root = print_string("%s/tmppid.%ju.out", tmp, (uintmax_t) getpid()); - if ( mkdir_p(tmp_out_root, 0777) != 0 ) + if ( mkdir_p(tmp_out_root, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tmp_out_root); on_exit(cleanup_file_or_directory, tmp_out_root); @@ -191,7 +183,7 @@ int main(int argc, char* argv[]) porttixinfo_path, parameter); package_name = strdup(parameter); srctix_path = join_paths(tmp_out_root, package_name); - if ( mkdir_p(srctix_path, 0777) != 0 ) + if ( mkdir_p(srctix_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", srctix_path); } else if ( !package_name ) @@ -207,7 +199,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tar, + "tar", "--extract", "--directory", srctix_path, "--file", tarball_path, @@ -229,7 +221,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tix_rmpatch, + "tix-rmpatch", "--directory", srctix_path, "--", rmpatch_path, @@ -250,7 +242,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - patch, + "patch", "--strip=1", "--silent", "--directory", srctix_path, @@ -272,7 +264,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tix_execpatch, + "tix-execpatch", "--directory", srctix_path, "--", execpatch_path, @@ -299,7 +291,7 @@ int main(int argc, char* argv[]) { const char* cmd_argv[] = { - tar, + "tar", "--create", "--xz", "--directory", tmp_out_root, diff --git a/tix/tix-build.cpp b/tix/tix-build.cpp index 19e87c89..84eb8900 100644 --- a/tix/tix-build.cpp +++ b/tix/tix-build.cpp @@ -100,6 +100,7 @@ typedef struct char* build; char* build_dir; char* destination; + int generation; char* host; char* make; char* makeflags; @@ -239,7 +240,7 @@ void emit_compiler_sysroot_cross_wrapper(metainfo_t* minfo, void emit_pkg_config_wrapper(metainfo_t* minfo) { char* bindir = print_string("%s/tmppid.%ju.bin", minfo->tmp, (uintmax_t) getpid()); - if ( mkdir_p(bindir, 0777) != 0 ) + if ( mkdir_p(bindir, 0755) != 0 ) error(1, errno, "mkdir: `%s'", bindir); on_exit(cleanup_file_or_directory, strdup(bindir)); @@ -298,7 +299,7 @@ void emit_pkg_config_wrapper(metainfo_t* minfo) if ( getenv("TIX_WARNINGS_DIR") ) { char* warnings_dir = print_string("%s/%s", getenv("TIX_WARNINGS_DIR"), minfo->package_name); - if ( mkdir(warnings_dir, 0777) == 0 || errno == EEXIST ) + if ( mkdir(warnings_dir, 0755) == 0 || errno == EEXIST ) { setenv("TIX_WARNINGS_DIR", warnings_dir, 1); } @@ -583,7 +584,7 @@ void BuildPackage(metainfo_t* minfo) { minfo->build_dir = print_string("%s/tmppid.%ju", minfo->tmp, (uintmax_t) getpid()); - if ( mkdir_p(minfo->build_dir, 0777) != 0 ) + if ( mkdir_p(minfo->build_dir, 0755) != 0 ) error(1, errno, "mkdir: `%s'", minfo->build_dir); } else @@ -616,8 +617,8 @@ void BuildPackage(metainfo_t* minfo) if ( !location_independent && !minfo->prefix ) error(1, 0, "error: %s is not location independent and you need to " - "specify the intended destination prefix using --prefix or " - "PREFIX", minfo->package_name); + "specify the intended destination prefix using --prefix", + minfo->package_name); if ( SHOULD_DO_BUILD_STEP(BUILD_STEP_BUILD, minfo) ) Make(minfo, build_target, NULL, true, subdir); @@ -627,7 +628,7 @@ void BuildPackage(metainfo_t* minfo) char* tixdir_rel = print_string("%s/%s", minfo->tmp, "tmp-tixbuild/tix"); char* tixinfo_rel = print_string("%s/%s", minfo->tmp, "tmp-tixbuild/tix/tixinfo"); - while ( mkdir(tardir_rel, 0777) != 0 ) + while ( mkdir(tardir_rel, 0755) != 0 ) { if ( errno != EEXIST ) error(1, errno, "mkdir: `%s'", tardir_rel); @@ -635,9 +636,9 @@ void BuildPackage(metainfo_t* minfo) error(1, errno, "rmdir: `%s'", tardir_rel); } - if ( mkdir(destdir_rel, 0777) != 0 ) + if ( mkdir(destdir_rel, 0755) != 0 ) error(1, errno, "mkdir: `%s'", destdir_rel); - if ( mkdir(tixdir_rel, 0777) != 0 ) + if ( mkdir(tixdir_rel, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tixdir_rel); char* destdir = canonicalize_file_name(destdir_rel); @@ -819,16 +820,18 @@ int main(int argc, char* argv[]) PurifyMakeflags(); metainfo_t minfo; + memset(&minfo, 0, sizeof(minfo)); minfo.build = NULL; - minfo.destination = strdup(getenv_def("TIX_BUILD_DESTINATION", ".")); + minfo.destination = NULL; minfo.host = NULL; + char* generation_string = strdup(DEFAULT_GENERATION); minfo.makeflags = strdup_null(getenv_def("MAKEFLAGS", NULL)); minfo.make = strdup(getenv_def("MAKE", "make")); - minfo.prefix = strdup_null(getenv_def("PREFIX", NULL)); - minfo.exec_prefix = strdup_null(getenv_def("EXEC_PREFIX", NULL)); - minfo.sysroot = strdup_null(getenv_def("SYSROOT", NULL)); + minfo.prefix = strdup(""); + minfo.exec_prefix = NULL; + minfo.sysroot = NULL; minfo.target = NULL; - minfo.tar = strdup(getenv_def("TAR", "tar")); + minfo.tar = strdup("tar"); minfo.tmp = strdup(getenv_def("BUILDTMP", ".")); char* start_step_string = strdup("start"); char* end_step_string = strdup("end"); @@ -860,6 +863,7 @@ int main(int argc, char* argv[]) else if ( GET_OPTION_VARIABLE("--destination", &minfo.destination) ) { } else if ( GET_OPTION_VARIABLE("--end", &end_step_string) ) { } else if ( GET_OPTION_VARIABLE("--host", &minfo.host) ) { } + else if ( GET_OPTION_VARIABLE("--generation", &generation_string) ) { } else if ( GET_OPTION_VARIABLE("--makeflags", &minfo.makeflags) ) { } else if ( GET_OPTION_VARIABLE("--make", &minfo.make) ) { } else if ( GET_OPTION_VARIABLE("--prefix", &minfo.prefix) ) { } @@ -877,6 +881,9 @@ int main(int argc, char* argv[]) } } + minfo.generation = atoi(generation_string); + free(generation_string); + if ( !(minfo.start_step = step_of_step_name(start_step_string)) ) { fprintf(stderr, "%s: no such step `%s'\n", argv0, start_step_string); @@ -924,17 +931,17 @@ int main(int argc, char* argv[]) free(minfo.target), minfo.target = NULL; if ( !minfo.build && !(minfo.build = GetBuildTriplet()) ) - error(1, errno, "unable to determine build, use --build or BUILD"); - if ( !minfo.host && !(minfo.host = strdup_null_if_content(getenv("HOST"))) ) + error(1, errno, "unable to determine build, use --build"); + if ( !minfo.host ) minfo.host = strdup(minfo.build); - if ( !minfo.target && !(minfo.target = strdup_null_if_content(getenv("TARGET"))) ) + if ( !minfo.target ) minfo.target = strdup(minfo.host); if ( minfo.prefix && !minfo.exec_prefix ) { // TODO: After releasing Sortix 1.0, switch to this branch that defaults the // exec-prefix to the prefix. -#if 0 +#if defined(__sortix__) minfo.exec_prefix = strdup(minfo.prefix); #else // Sortix 0.9 compatibility. minfo.exec_prefix = print_string("%s/%s", minfo.prefix, minfo.host); diff --git a/tix/tix-collection.cpp b/tix/tix-collection.cpp index 0fa52b07..aceb2e6c 100644 --- a/tix/tix-collection.cpp +++ b/tix/tix-collection.cpp @@ -46,7 +46,7 @@ static void help(FILE* fp, const char* argv0) { - fprintf(fp, "Usage: %s PREFIX [OPTION]... COMMAND\n", argv0); + fprintf(fp, "Usage: %s [PREFIX] [OPTION]... COMMAND\n", argv0); fprintf(fp, "Administer and configure a tix collection.\n"); } @@ -60,7 +60,7 @@ static void version(FILE* fp, const char* argv0) int main(int argc, char* argv[]) { - char* collection = strdup_null(getenv_def("TIX_COLLECTION", NULL)); + char* collection = NULL; char* platform = NULL; char* prefix = NULL; // TODO: After releasing Sortix 1.0, keep the --disable-multiarch option @@ -74,6 +74,7 @@ int main(int argc, char* argv[]) #else bool use_multiarch = true; #endif + char* generation_string = strdup(DEFAULT_GENERATION); const char* argv0 = argv[0]; for ( int i = 0; i < argc; i++ ) @@ -101,6 +102,7 @@ int main(int argc, char* argv[]) else if ( GET_OPTION_VARIABLE("--collection", &collection) ) { } else if ( GET_OPTION_VARIABLE("--platform", &platform) ) { } else if ( GET_OPTION_VARIABLE("--prefix", &prefix) ) { } + else if ( GET_OPTION_VARIABLE("--generation", &generation_string) ) { } else if ( !strcmp(arg, "--enable-multiarch") ) use_multiarch = true; else if ( !strcmp(arg, "--disable-multiarch") ) @@ -124,6 +126,10 @@ int main(int argc, char* argv[]) ParseOptionalCommandLineCollectionPrefix(&collection, &argc, &argv); VerifyCommandLineCollection(&collection); + int generation = atoi(generation_string); + free(generation_string); + (void) generation; + if ( !prefix ) prefix = strdup(collection); @@ -140,14 +146,14 @@ int main(int argc, char* argv[]) error(1, errno, "unable to determine platform, use --platform"); char* tix_path = join_paths(collection, "tix"); - if ( mkdir_p(tix_path, 0777) != 0 ) + if ( mkdir_p(tix_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tix_path); char* tixdb_path; if ( use_multiarch ) { tixdb_path = join_paths(tix_path, platform); - if ( mkdir_p(tixdb_path, 0777) != 0 ) + if ( mkdir_p(tixdb_path, 0755) != 0 ) error(1, errno, "mkdir: `%s'", tixdb_path); } else @@ -181,9 +187,6 @@ int main(int argc, char* argv[]) error(1, errno, "`%s'", inst_list_path); fclose(inst_list_fp); - printf("Created empty tix collection at `%s' with no repositories.\n", - collection); - return 0; } else diff --git a/tix/tix-install.cpp b/tix/tix-install.cpp index 918a388d..08744bc7 100644 --- a/tix/tix-install.cpp +++ b/tix/tix-install.cpp @@ -48,8 +48,8 @@ void TipTixCollection(const char* prefix) { - error(0, 0, "error: `%s' isn't a tix collection, use tix-collection before " - "installing packages.", prefix); + error(0, 0, "error: `%s' isn't a tix collection, use \"tix-collection %s " + "create\" before " "installing packages.", prefix, prefix); } void VerifyTixCollection(const char* prefix) @@ -108,7 +108,7 @@ bool IsPackageInstalled(const char* tixdb_path, const char* package) bool ret = false; char* line = NULL; - size_t line_size; + size_t line_size = 0; ssize_t line_len; while ( 0 < (line_len = getline(&line, &line_size, installed_list_fp)) ) { @@ -144,7 +144,7 @@ void MarkPackageAsInstalled(const char* tixdb_path, const char* package) static void help(FILE* fp, const char* argv0) { - fprintf(fp, "Usage: %s [OPTION]... --collection=PREFIX PACKAGE\n", argv0); + fprintf(fp, "Usage: %s [OPTION]... [--collection=PREFIX] PACKAGE...\n", argv0); fprintf(fp, "Install a tix into a tix collection.\n"); } @@ -156,12 +156,15 @@ static void version(FILE* fp, const char* argv0) fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n"); } +static char* collection = NULL; +static bool reinstall = false; +static char* tix_directory_path = NULL; + +void InstallPackage(const char* tix_path); + int main(int argc, char* argv[]) { - char* collection = strdup_null(getenv_def("TIX_COLLECTION", NULL)); - char* prefix = strdup_null(getenv_def("TIX_COLLECTION_PREFIX", NULL)); - char* tar = strdup(getenv_def("TAR", "tar")); - bool reinstall = false; + collection = strdup("/"); const char* argv0 = argv[0]; for ( int i = 0; i < argc; i++ ) @@ -187,8 +190,6 @@ int main(int argc, char* argv[]) else if ( !strcmp(arg, "--version") ) version(stdout, argv0), exit(0); else if ( GET_OPTION_VARIABLE("--collection", &collection) ) { } - else if ( GET_OPTION_VARIABLE("--prefix", &prefix) ) { } - else if ( GET_OPTION_VARIABLE("--tar", &tar) ) { } else if ( !strcmp(arg, "--reinstall") ) reinstall = true; else @@ -213,44 +214,37 @@ int main(int argc, char* argv[]) exit(1); } - if ( !collection && prefix ) - collection = strdup(prefix); - - if ( !collection ) - { - fprintf(stderr, "%s: no collection prefix specified, use --collection " - "or TIX_COLLECTION to specify where the package will " - "installed.\n", argv0); - exit(1); - } - - if ( !prefix ) - prefix = strdup(collection); - - if ( strcmp(collection, prefix) != 0 ) - error(1, 0, "error: desired collection `%s' isn't equal to desired " - "prefix `%s', which isn't supported (and dangerous).\n", - collection, prefix); - if ( !*collection ) + { + free(collection); collection = strdup("/"); + } VerifyTixCollection(collection); - char* tix_directory_path = join_paths(collection, "tix"); + tix_directory_path = join_paths(collection, "tix"); VerifyTixDirectory(collection, tix_directory_path); - char* tix_path = strdup(argv[1]); + for ( int i = 1; i < argc; i++ ) + InstallPackage(argv[i]); + + free(tix_directory_path); + + return 0; +} + +void InstallPackage(const char* tix_path) +{ if ( !IsFile(tix_path) ) error(1, errno, "`%s'", tix_path); const char* tixinfo_path = "tix/tixinfo"; - if ( !TarContainsFile(tar, tix_path, tixinfo_path) ) + if ( !TarContainsFile(tix_path, tixinfo_path) ) error(1, 0, "`%s' doesn't contain a `%s' file", tix_path, tixinfo_path); string_array_t tixinfo = string_array_make(); - FILE* tixinfo_fp = TarOpenFile(tar, tix_path, tixinfo_path); + FILE* tixinfo_fp = TarOpenFile(tix_path, tixinfo_path); dictionary_append_file(&tixinfo, tixinfo_fp); fclose(tixinfo_fp); @@ -258,20 +252,18 @@ int main(int argc, char* argv[]) assert(package_name); const char* package_prefix = dictionary_get(&tixinfo, "pkg.prefix"); + assert(package_prefix || !package_prefix); const char* package_platform = dictionary_get(&tixinfo, "tix.platform"); - assert(package_platform); + assert(package_platform || !package_platform); // TODO: After releasing Sortix 1.0, delete this compatibility. + // Then move this code to main, from here... char* tixdb_path = join_paths(tix_directory_path, package_platform); - if ( IsDirectory(tixdb_path ) ) - { - free(tix_directory_path); - } - else + if ( !IsDirectory(tixdb_path ) ) { free(tixdb_path); - tixdb_path = tix_directory_path; + tixdb_path = strdup(tix_directory_path); } VerifyTixDatabase(collection, tixdb_path); @@ -282,7 +274,14 @@ int main(int argc, char* argv[]) error(1, errno, "`%s'", coll_conf_path); VerifyTixCollectionConfiguration(&coll_conf, coll_conf_path); free(coll_conf_path); + // ... to here. (But see allocation cleanup below). + const char* coll_generation = dictionary_get(&coll_conf, "collection.generation"); + // TODO: After releasing Sortix 1.0, remove this compatibility. + if ( !coll_generation ) + coll_generation = "1"; + assert(coll_generation); + (void) coll_generation; const char* coll_prefix = dictionary_get(&coll_conf, "collection.prefix"); assert(coll_prefix); const char* coll_platform = dictionary_get(&coll_conf, "collection.platform"); @@ -290,7 +289,8 @@ int main(int argc, char* argv[]) bool already_installed = IsPackageInstalled(tixdb_path, package_name); if ( already_installed && !reinstall ) - error(1, 0, "error: package `%s' is already installed.", package_name); + error(1, 0, "error: package `%s' is already installed. Use --reinstall " + "to force reinstallation.", package_name); if ( package_prefix && strcmp(coll_prefix, package_prefix) != 0 ) { @@ -301,7 +301,7 @@ int main(int argc, char* argv[]) "--prefix=\"%s\".", coll_prefix); } - if ( strcmp(coll_platform, package_platform) != 0 ) + if ( package_platform && strcmp(coll_platform, package_platform) != 0 ) { error(0, errno, "error: `%s' is compiled with the platform `%s', but " "the destination collection has the platform `%s'.", @@ -310,8 +310,8 @@ int main(int argc, char* argv[]) "--host=%s\".", coll_platform); } - printf("Installing `%s' into `%s'...\n", package_name, prefix); - char* data_and_prefix = package_prefix && prefix[0] ? + printf("Installing `%s' into `%s'...\n", package_name, collection); + char* data_and_prefix = package_prefix && package_prefix[0] ? print_string("data%s", package_prefix) : strdup("data"); if ( fork_and_wait_or_death() ) @@ -319,9 +319,9 @@ int main(int argc, char* argv[]) size_t num_strips = count_tar_components(data_and_prefix); const char* cmd_argv[] = { - tar, + "tar", print_string("--strip-components=%zu", num_strips), - "-C", prefix, + "-C", collection, "--extract", "--file", tix_path, "--keep-directory-symlink", @@ -335,4 +335,10 @@ int main(int argc, char* argv[]) if ( !already_installed ) MarkPackageAsInstalled(tixdb_path, package_name); + + string_array_reset(&tixinfo); + + // TODO: After releasing Sortix 1.0, and done the above, move this to main. + string_array_reset(&coll_conf); + free(tixdb_path); } diff --git a/tix/tix.cpp b/tix/tix.cpp index c50ee944..b22429ca 100644 --- a/tix/tix.cpp +++ b/tix/tix.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2015. This file is part of Tix. @@ -49,9 +49,7 @@ typedef struct { char* collection; - char* tar; char* tixdb_path; - char* tix_install; string_array_t coll_conf; string_array_t repo_list; string_array_t inst_list; @@ -91,11 +89,11 @@ string_array_t GetPackageDependencies(params_t* params, const char* pkg_name) error(1, errno, "unable to locate package `%s'", pkg_name); const char* tixinfo_path = "tix/tixinfo"; - if ( !TarContainsFile(params->tar, pkg_path, tixinfo_path) ) + if ( !TarContainsFile(pkg_path, tixinfo_path) ) error(1, 0, "`%s' doesn't contain a `%s' file", pkg_path, tixinfo_path); string_array_t tixinfo = string_array_make(); - FILE* tixinfo_fp = TarOpenFile(params->tar, pkg_path, tixinfo_path); + FILE* tixinfo_fp = TarOpenFile(pkg_path, tixinfo_path); dictionary_append_file(&tixinfo, tixinfo_fp); fclose(tixinfo_fp); @@ -138,9 +136,8 @@ void InstallPackageOfName(params_t* params, const char* pkg_name) { const char* cmd_argv[] = { - params->tix_install, + "tix-install", "--collection", params->collection, - "--tar", params->tar, "--", pkg_path, NULL }; @@ -153,7 +150,7 @@ void InstallPackageOfName(params_t* params, const char* pkg_name) static void help(FILE* fp, const char* argv0) { - fprintf(fp, "Usage: %s PREFIX COMMAND [OPTION]...\n", argv0); + fprintf(fp, "Usage: %s [PREFIX] COMMAND [OPTION]...\n", argv0); fprintf(fp, "Front end to the Tix package management system.\n"); } @@ -168,9 +165,7 @@ static void version(FILE* fp, const char* argv0) int main(int argc, char* argv[]) { params_t params; - params.collection = strdup_null(getenv_def("TIX_COLLECTION", NULL)); - params.tar = strdup(getenv_def("TAR", "tar")); - params.tix_install = strdup("tix-install"); + params.collection = NULL; const char* argv0 = argv[0]; for ( int i = 0; i < argc; i++ ) @@ -196,8 +191,6 @@ int main(int argc, char* argv[]) else if ( !strcmp(arg, "--version") ) version(stdout, argv0), exit(0); else if ( GET_OPTION_VARIABLE("--collection", ¶ms.collection) ) { } - else if ( GET_OPTION_VARIABLE("--tar", ¶ms.tar) ) { } - else if ( GET_OPTION_VARIABLE("--tix-install", ¶ms.tix_install) ) { } else { fprintf(stderr, "%s: unknown option: %s\n", argv0, arg); diff --git a/tix/util.h b/tix/util.h index 7e89c9c9..4fc91ec0 100644 --- a/tix/util.h +++ b/tix/util.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2015. This file is part of Tix. @@ -25,6 +25,8 @@ #ifndef UTIL_H #define UTIL_H +#define DEFAULT_GENERATION "1" + bool does_path_contain_dotdot(const char* path) { size_t index = 0; @@ -262,7 +264,7 @@ char* token_string_of_string_array(const string_array_t* sa) void string_array_append_file(string_array_t* sa, FILE* fp) { char* entry = NULL; - size_t entry_size; + size_t entry_size = 0; ssize_t entry_length; while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) ) { @@ -345,7 +347,7 @@ void dictionary_normalize_entry(char* entry) void dictionary_append_file(string_array_t* sa, FILE* fp) { char* entry = NULL; - size_t entry_size; + size_t entry_size = 0; ssize_t entry_length; while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) ) { @@ -458,9 +460,6 @@ static void compact_arguments(int* argc, char*** argv) char* GetBuildTriplet() { - const char* env_host = getenv("BUILD"); - if ( env_host ) - return strdup(env_host); #if defined(__sortix__) && defined(__i386__) return strdup("i486-sortix"); #elif defined(__sortix__) && defined(__x86_64__) @@ -537,7 +536,7 @@ size_t count_tar_components(const char* path) // through the error stream for messages such as "file not found", which // can be hard to distinguish from the common "oh no, an error occured" // case in which we need to abort as well. -bool TarContainsFile(const char* tar, const char* archive, const char* file) +bool TarContainsFile(const char* archive, const char* file) { int pipes[2]; if ( pipe(pipes) ) @@ -550,7 +549,7 @@ bool TarContainsFile(const char* tar, const char* archive, const char* file) close(pipes[0]); const char* cmd_argv[] = { - tar, + "tar", "--list", "--file", archive, NULL @@ -595,7 +594,7 @@ bool TarContainsFile(const char* tar, const char* archive, const char* file) return ret; } -FILE* TarOpenFile(const char* tar, const char* archive, const char* file) +FILE* TarOpenFile(const char* archive, const char* file) { FILE* fp = tmpfile(); if ( !fp ) @@ -607,7 +606,7 @@ FILE* TarOpenFile(const char* tar, const char* archive, const char* file) fclose(fp); const char* cmd_argv[] = { - tar, + "tar", "--to-stdout", "--extract", "--file", archive, @@ -707,19 +706,26 @@ void ParseOptionalCommandLineCollectionPrefix(char** collection, int* argcp, { if ( 2 <= *argcp && IsCollectionPrefixRatherThanCommand((*argvp)[1]) ) { - free(*collection); - *collection = strdup((*argvp)[1]); + if ( !*collection ) + { + free(*collection); + *collection = strdup((*argvp)[1]); + } (*argvp)[1] = NULL; compact_arguments(argcp, argvp); } + else if ( !*collection ) + { + *collection = strdup("/"); + } } void VerifyCommandLineCollection(char** collection) { if ( !*collection ) error(1, 0, "error: you need to specify which tix collection to " - "administer using --collection or TIX_COLLECTION or giving " - "the prefix as the first argument."); + "administer using --collection or giving the prefix as the " + "first argument."); if ( !**collection ) { @@ -730,6 +736,7 @@ void VerifyCommandLineCollection(char** collection) char* collection_rel = *collection; if ( !(*collection = canonicalize_file_name(collection_rel)) ) error(1, errno, "canonicalize_file_name(`%s')", collection_rel); + free(collection_rel); } void VerifyTixCollectionConfiguration(string_array_t* info, const char* path) @@ -937,7 +944,6 @@ int recovery_execvp(const char* path, char* const* argv) const char* cmd_argv[] = { getenv_def("SHELL", "sh"), - "-i", NULL }; execvp(cmd_argv[0], (char* const*) cmd_argv);