From 9ccfdb99906f84cc43c203b76d69b716b94ab0d1 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 30 Sep 2015 14:22:59 +0200 Subject: [PATCH] Modernize tix temporary files and directory creation. --- build-aux/build-ports.sh | 5 ----- build-aux/clean-ports.sh | 5 ----- tix/porttix-create.cpp | 8 ++++---- tix/srctix-create.cpp | 14 +++++++------- tix/tix-build.cpp | 38 +++++++++++++++++--------------------- tix/util.h | 16 ++++++++++++---- 6 files changed, 40 insertions(+), 46 deletions(-) diff --git a/build-aux/build-ports.sh b/build-aux/build-ports.sh index 7b918528..328346bf 100755 --- a/build-aux/build-ports.sh +++ b/build-aux/build-ports.sh @@ -42,11 +42,6 @@ SYSROOT=$(make_dir_path_absolute "$SYSROOT") SORTIX_PORTS_DIR=$(make_dir_path_absolute "$SORTIX_PORTS_DIR") SORTIX_REPOSITORY_DIR=$(make_dir_path_absolute "$SORTIX_REPOSITORY_DIR") -# Create a temporary directory in which out-of-directory builds will happen. -if [ -z "$BUILDTMP" ]; then - export BUILDTMP=$(mktemp -d) -fi - # Decide the optimization options with which the ports will be built. if [ -z "${OPTLEVEL+x}" ]; then OPTLEVEL="-Os"; fi if [ -z "${PORTS_OPTLEVEL+x}" ]; then PORTS_OPTLEVEL="$OPTLEVEL"; fi diff --git a/build-aux/clean-ports.sh b/build-aux/clean-ports.sh index 2374697f..3438e0b5 100755 --- a/build-aux/clean-ports.sh +++ b/build-aux/clean-ports.sh @@ -23,11 +23,6 @@ fi # Make paths absolute for later use. SORTIX_PORTS_DIR=$(make_dir_path_absolute "$SORTIX_PORTS_DIR") -# Create a temporary directory in which out-of-directory builds will happen. -if [ -z "$BUILDTMP" ]; then - export BUILDTMP=$(mktemp -d) -fi - # Detect all packages. get_all_packages() { for PACKAGE in $(ls "$SORTIX_PORTS_DIR"); do diff --git a/tix/porttix-create.cpp b/tix/porttix-create.cpp index 4f7fd7ce..3f9381d6 100644 --- a/tix/porttix-create.cpp +++ b/tix/porttix-create.cpp @@ -74,7 +74,7 @@ int main(int argc, char* argv[]) char* input_tarball_path = NULL; char* output_directory = strdup("."); char* output = NULL; - char* tmp = strdup(getenv_def("TMP", "/tmp")); + char* tmp = strdup(getenv_def("TMPDIR", "/tmp")); const char* argv0 = argv[0]; for ( int i = 0; i < argc; i++ ) @@ -155,9 +155,9 @@ int main(int argc, char* argv[]) if ( !output ) 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, 0755) != 0 ) - error(1, errno, "mkdir: `%s'", tmp_root); + char* tmp_root = print_string("%s/porttix.XXXXXX", tmp); + if ( !mkdtemp(tmp_root) ) + error(1, errno, "mkdtemp: `%s'", tmp_root); on_exit(cleanup_file_or_directory, tmp_root); diff --git a/tix/srctix-create.cpp b/tix/srctix-create.cpp index f723b6c3..a4927fa8 100644 --- a/tix/srctix-create.cpp +++ b/tix/srctix-create.cpp @@ -68,7 +68,7 @@ int main(int argc, char* argv[]) { char* output_directory = strdup("."); char* output = NULL; - char* tmp = strdup(getenv_def("TMP", "/tmp")); + char* tmp = strdup(getenv_def("TMPDIR", "/tmp")); const char* argv0 = argv[0]; for ( int i = 0; i < argc; i++ ) @@ -120,9 +120,9 @@ 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, 0755) != 0 ) - error(1, errno, "mkdir: `%s'", tmp_in_root); + char* tmp_in_root = print_string("%s/srctixin.XXXXXX", tmp); + if ( !mkdtemp(tmp_in_root) ) + error(1, errno, "mkdtemp: `%s'", tmp_in_root); on_exit(cleanup_file_or_directory, tmp_in_root); if ( fork_and_wait_or_death() ) @@ -150,9 +150,9 @@ int main(int argc, char* argv[]) error(1, errno, "`%s'", porttixinfo_path); } - char* tmp_out_root = print_string("%s/tmppid.%ju.out", tmp, (uintmax_t) getpid()); - if ( mkdir_p(tmp_out_root, 0755) != 0 ) - error(1, errno, "mkdir: `%s'", tmp_out_root); + char* tmp_out_root = print_string("%s/srctixout.XXXXXX", tmp); + if ( !mkdtemp(tmp_out_root) ) + error(1, errno, "mkdtemp: `%s'", tmp_out_root); on_exit(cleanup_file_or_directory, tmp_out_root); char* package_name = NULL; diff --git a/tix/tix-build.cpp b/tix/tix-build.cpp index 84eb8900..69b76bd9 100644 --- a/tix/tix-build.cpp +++ b/tix/tix-build.cpp @@ -239,9 +239,9 @@ 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, 0755) != 0 ) - error(1, errno, "mkdir: `%s'", bindir); + char* bindir = print_string("%s/bin.XXXXXX", minfo->tmp); + if ( !mkdtemp(bindir) ) + error(1, errno, "mkdtemp: `%s'", bindir); on_exit(cleanup_file_or_directory, strdup(bindir)); @@ -582,10 +582,10 @@ void BuildPackage(metainfo_t* minfo) bool use_build_dir = parse_boolean(use_build_dir_var); if ( use_build_dir ) { - minfo->build_dir = print_string("%s/tmppid.%ju", minfo->tmp, - (uintmax_t) getpid()); - if ( mkdir_p(minfo->build_dir, 0755) != 0 ) - error(1, errno, "mkdir: `%s'", minfo->build_dir); + minfo->build_dir = print_string("%s/build.XXXXXX", minfo->tmp); + if ( !mkdtemp(minfo->build_dir) ) + error(1, errno, "mkdtemp: `%s'", minfo->build_dir); + on_exit(cleanup_file_or_directory, strdup(minfo->build_dir)); } else minfo->build_dir = strdup(minfo->package_dir); @@ -623,18 +623,14 @@ void BuildPackage(metainfo_t* minfo) if ( SHOULD_DO_BUILD_STEP(BUILD_STEP_BUILD, minfo) ) Make(minfo, build_target, NULL, true, subdir); - char* tardir_rel = print_string("%s/%s", minfo->tmp, "tmp-tixbuild"); - char* destdir_rel = print_string("%s/%s", minfo->tmp, "tmp-tixbuild/data"); - 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"); + char* tardir_rel = print_string("%s/%s", minfo->tmp, "tix.XXXXXX"); + if ( !mkdtemp(tardir_rel) ) + error(1, errno, "mkdtemp: `%s'", tardir_rel); + on_exit(cleanup_file_or_directory, strdup(tardir_rel)); - while ( mkdir(tardir_rel, 0755) != 0 ) - { - if ( errno != EEXIST ) - error(1, errno, "mkdir: `%s'", tardir_rel); - if ( rmdir(tardir_rel) != 0 ) - error(1, errno, "rmdir: `%s'", tardir_rel); - } + char* destdir_rel = print_string("%s/%s", tardir_rel, "data"); + char* tixdir_rel = print_string("%s/%s", tardir_rel, "tix"); + char* tixinfo_rel = print_string("%s/%s", tardir_rel, "tix/tixinfo"); if ( mkdir(destdir_rel, 0755) != 0 ) error(1, errno, "mkdir: `%s'", destdir_rel); @@ -734,7 +730,7 @@ void BuildPackage(metainfo_t* minfo) unlink(tixinfo_rel); rmdir(destdir_rel); rmdir(tixdir_rel); - rmdir(tardir_rel); + //rmdir(tardir_rel); // Keep around to avoid on_exit handler race. free(tardir_rel); free(destdir_rel); @@ -822,7 +818,7 @@ int main(int argc, char* argv[]) metainfo_t minfo; memset(&minfo, 0, sizeof(minfo)); minfo.build = NULL; - minfo.destination = NULL; + minfo.destination = strdup("."); minfo.host = NULL; char* generation_string = strdup(DEFAULT_GENERATION); minfo.makeflags = strdup_null(getenv_def("MAKEFLAGS", NULL)); @@ -832,7 +828,7 @@ int main(int argc, char* argv[]) minfo.sysroot = NULL; minfo.target = NULL; minfo.tar = strdup("tar"); - minfo.tmp = strdup(getenv_def("BUILDTMP", ".")); + minfo.tmp = strdup(getenv_def("TMPDIR", "/tmp")); char* start_step_string = strdup("start"); char* end_step_string = strdup("end"); diff --git a/tix/util.h b/tix/util.h index 5da5a488..a553d6ec 100644 --- a/tix/util.h +++ b/tix/util.h @@ -776,20 +776,28 @@ void cleanup_file_or_directory(int, void* path_ptr) { if ( original_pid != getpid() ) return; - const char* path = (const char*) path_ptr; - if ( fork_and_wait_or_death(false) ) + pid_t pid = fork(); + if ( pid < 0 ) + { + error(0, errno, "fork"); + return; + } + if ( pid == 0 ) { const char* cmd_argv[] = { "rm", "-rf", "--", - path, + (const char*) path_ptr, NULL, }; execvp(cmd_argv[0], (char* const*) cmd_argv); - error(127, errno, "%s", cmd_argv[0]); + error(0, errno, "%s", cmd_argv[0]); + _exit(127); } + int code; + waitpid(pid, &code, 0); } mode_t get_umask_value()