/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2021 Olaf Wintermann. All rights reserved. * * 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. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "main.h" #include "srvctrl.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define OPTSTR "hlpsuv" static char *cfgdir; static char *copydir; int main(int argc, char** argv) { int ret = 1; extern char *optarg; extern int optind, opterr, optopt; CPSettings settings; memset(&settings, 0, sizeof(CPSettings)); int help = 0; int version = 0; int list = 0; // list copying processes int c; while((c = getopt(argc, argv, OPTSTR)) != -1) { switch(c) { case 'l': list = 1; break; case 'p': settings.pause = 1; break; case 's': settings.printsocket = 1; break; case 'u': settings.url = 1; break; case 'v': version = 1; break; } } int ac = argc - optind; if(list) { // list command } else if(help) { // print help } else if(version) { // print version } else if(ac == 2) { // copy settings.from = argv[optind]; settings.to = argv[optind+1]; ret = mzcp_copy(&settings); } else { // print usage } return ret; } static int check_dir(const char *path) { struct stat s; if(stat(path, &s)) { if(errno == ENOENT) { if(mkdir(path, S_IRWXU)) { fprintf(stderr, "Cannot create %s: %s", path, strerror(errno)); return 1; } } else { fprintf(stderr, "Cannot access %s: %s", path, strerror(errno)); return 1; } } return 0; } static int check_configdir(void) { char *home = getenv(MZCP_ENV_HOME); char *base = util_concat_path(home, MZCP_CFG_BASE); if(check_dir(base)) return 1; free(base); cfgdir = util_concat_path(home, MZCP_CFG_DIR); if(check_dir(cfgdir)) return 1; copydir = util_concat_path(mzcp_get_cfgdir(), MZCP_COPY_DIR); if(check_dir(copydir)) return 1; return 0; } const char* mzcp_get_cfgdir(void) { return cfgdir; } const char* mzcp_get_copydir(void) { return copydir; } int mzcp_copy(CPSettings *settings) { int ret = 0; if(check_configdir()) { return 2; } if(create_control_socket()) { return 3; } if(settings->printsocket) { printf("%s\n", mzcp_get_socketpath()); } else { printf("copy %s to %s\n", settings->from, settings->to); if(settings->pause) { printf("pause\n"); } } //pid_t p = fork(); pid_t p = 0; if(p == 0) { //close(0); //close(1); //close(2); ret = mzcp_srvctrl(settings); } return ret; }