/*- * Copyright 2010. Ivan Voras * * 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. * 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. */ /* * FreeBSD install - a package for the installation and maintainance * of non-core utilities. * * This is the "binary patch" utility main module. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #define PKG_PATCH_MAIN #include "pkg_patch.h" #include "mkpatch.h" #include "mkpatchdir.h" #include "applypatch.h" #include "updateweb.h" int argc; char **argv; enum PP_OP patch_op = PP_NONE; char *my_tmp = NULL; int flag_bsdiff = 0; char *pkg_repo_url = NULL; static void usage_short(void); static void proc_args(void); static void proc_setup(void); static void atexit_handler(void); static void usage_short() { printf("usage:\n"); printf("\t%s -c [-b] package_file_1 package_file_2 patch_file\n", argv[0]); printf("\t%s -a patch_file\n", argv[0]); printf("\t%s -m [-b] [-r url_repo] package_dir_1 package_dir_2 " "patch_dir\n", argv[0]); printf("\t%s -u [url]\n", argv[0]); } static void proc_args() { int ch; while ((ch = getopt(argc, argv, "abcfhmp:uv")) != -1) { switch (ch) { case 'a': patch_op = PP_APPLY; break; case 'b': flag_bsdiff = 1; break; case 'c': patch_op = PP_MKPATCH; break; case 'f': Force++; break; case 'h': usage_short(); exit(0); break; case 'm': patch_op = PP_MKPATCHDIR; break; case 'p': pkg_repo_url = strdup(optarg); break; case 'v': Verbose++; break; case 'u': patch_op = PP_UPDATEWEB; break; default: usage_short(); exit(1); } } argc -= optind; argv += optind; } /* * Process early setup things like making sure the environment is sane and * creating the work directory. */ void proc_setup() { if (access(_PATH_RM, X_OK) != 0) errx(1, "Cannot execute %s", _PATH_RM); if (access(_PATH_TAR, X_OK) != 0) errx(1, "Cannot execute %s", _PATH_TAR); if (access(_PATH_BSDIFF, X_OK) != 0) errx(1, "Cannot execute %s", _PATH_BSDIFF); asprintf(&my_tmp, "%spkg_patch.%d.%d", _PATH_TMP, getpid(), time(NULL)); if (mkdir(my_tmp, 0700) != 0) errx(1, "Cannot create working directory: %s", my_tmp); if (Verbose > 1) printf("Using temporary directory: %s\n", my_tmp); } static void atexit_handler(void) { if (Verbose < 2) rm_rf(my_tmp); else printf("Leaving temp directory behind: %s\n", my_tmp); } void cleanup(int __unused sig) { printf("cleanup() called\n"); } int main(int _argc, char **_argv) { argc = _argc; argv = _argv; proc_args(); if (patch_op == PP_NONE) errx(1, "No operation switch given, try '-h' for help"); atexit(atexit_handler); proc_setup(); switch (patch_op) { case PP_MKPATCH: if (argc < 3) errx(1, "Expecting 3 arguments: old_package_file " "new_package_file patch_file"); perform_mkpatch(argv[0], argv[1], argv[2]); break; case PP_APPLY: if (argc < 1) errx(1, "Expecting argument: patch filename"); perform_applypatch(argv[0]); break; case PP_MKPATCHDIR: if (argc < 3) errx(1, "Expecting 3 arguments: old_pkg_dir new_pkg_dir " "patch_pkg_dir"); perform_mkpatchdir(argv[0], argv[1], argv[2]); break; case PP_UPDATEWEB: if (argc > 0) perform_updateweb(argv[0]); else perform_updateweb(NULL); break; default: errx(1, "This should not happen - unknown patch_op"); } return (0); }