root/opal/mca/compress/gzip/compress_gzip_module.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_compress_gzip_module_init
  2. opal_compress_gzip_module_finalize
  3. opal_compress_gzip_compress
  4. opal_compress_gzip_compress_nb
  5. opal_compress_gzip_decompress
  6. opal_compress_gzip_decompress_nb
  7. is_directory

   1 /*
   2  * Copyright (c) 2004-2010 The Trustees of Indiana University.
   3  *                         All rights reserved.
   4  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
   5  *
   6  * Copyright (c) 2014 Cisco Systems, Inc.  All rights reserved.
   7  * Copyright (c) 2015      Research Organization for Information Science
   8  *                         and Technology (RIST). All rights reserved.
   9  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
  10  * $COPYRIGHT$
  11  *
  12  * Additional copyrights may follow
  13  *
  14  * $HEADER$
  15  */
  16 
  17 #include "opal_config.h"
  18 
  19 #include <string.h>
  20 #include <sys/types.h>
  21 #include <sys/wait.h>
  22 #include <sys/stat.h>
  23 #if HAVE_UNISTD_H
  24 #include <unistd.h>
  25 #endif  /* HAVE_UNISTD_H */
  26 
  27 #include "opal/util/opal_environ.h"
  28 #include "opal/util/output.h"
  29 #include "opal/util/argv.h"
  30 #include "opal/util/opal_environ.h"
  31 #include "opal/util/printf.h"
  32 
  33 #include "opal/constants.h"
  34 #include "opal/util/basename.h"
  35 
  36 #include "opal/mca/compress/compress.h"
  37 #include "opal/mca/compress/base/base.h"
  38 #include "opal/runtime/opal_cr.h"
  39 
  40 #include "compress_gzip.h"
  41 
  42 static bool is_directory(char *fname );
  43 
  44 int opal_compress_gzip_module_init(void)
  45 {
  46     return OPAL_SUCCESS;
  47 }
  48 
  49 int opal_compress_gzip_module_finalize(void)
  50 {
  51     return OPAL_SUCCESS;
  52 }
  53 
  54 int opal_compress_gzip_compress(char * fname, char **cname, char **postfix)
  55 {
  56     pid_t child_pid = 0;
  57     int status = 0;
  58 
  59     opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
  60                         "compress:gzip: compress(%s)",
  61                         fname);
  62 
  63     opal_compress_gzip_compress_nb(fname, cname, postfix, &child_pid);
  64     waitpid(child_pid, &status, 0);
  65 
  66     if( WIFEXITED(status) ) {
  67         return OPAL_SUCCESS;
  68     } else {
  69         return OPAL_ERROR;
  70     }
  71 }
  72 
  73 int opal_compress_gzip_compress_nb(char * fname, char **cname, char **postfix, pid_t *child_pid)
  74 {
  75     char **argv = NULL;
  76     char * base_fname = NULL;
  77     char * dir_fname = NULL;
  78     int status;
  79     bool is_dir;
  80 
  81     is_dir = is_directory(fname);
  82 
  83     *child_pid = fork();
  84     if( *child_pid == 0 ) { /* Child */
  85         char * cmd = NULL;
  86 
  87         dir_fname  = opal_dirname(fname);
  88         base_fname = opal_basename(fname);
  89 
  90         chdir(dir_fname);
  91 
  92         if( is_dir ) {
  93 #if 0
  94             opal_compress_base_tar_create(&base_fname);
  95             opal_asprintf(cname, "%s.gz", base_fname);
  96             opal_asprintf(&cmd, "gzip %s", base_fname);
  97 #else
  98             opal_asprintf(cname, "%s.tar.gz", base_fname);
  99             opal_asprintf(&cmd, "tar -zcf %s %s", *cname, base_fname);
 100 #endif
 101         } else {
 102             opal_asprintf(cname, "%s.gz", base_fname);
 103             opal_asprintf(&cmd, "gzip %s", base_fname);
 104         }
 105 
 106         opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
 107                             "compress:gzip: compress_nb(%s -> [%s])",
 108                             fname, *cname);
 109         opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
 110                             "compress:gzip: compress_nb() command [%s]",
 111                             cmd);
 112 
 113         argv = opal_argv_split(cmd, ' ');
 114         status = execvp(argv[0], argv);
 115 
 116         opal_output(0, "compress:gzip: compress_nb: Failed to exec child [%s] status = %d\n", cmd, status);
 117         exit(OPAL_ERROR);
 118     }
 119     else if( *child_pid > 0 ) {
 120         if( is_dir ) {
 121             *postfix = strdup(".tar.gz");
 122         } else {
 123             *postfix = strdup(".gz");
 124         }
 125         opal_asprintf(cname, "%s%s", fname, *postfix);
 126 
 127     }
 128     else {
 129         return OPAL_ERROR;
 130     }
 131 
 132     return OPAL_SUCCESS;
 133 }
 134 
 135 int opal_compress_gzip_decompress(char * cname, char **fname)
 136 {
 137     pid_t child_pid = 0;
 138     int status = 0;
 139 
 140     opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
 141                         "compress:gzip: decompress(%s)",
 142                         cname);
 143 
 144     opal_compress_gzip_decompress_nb(cname, fname, &child_pid);
 145     waitpid(child_pid, &status, 0);
 146 
 147     if( WIFEXITED(status) ) {
 148         return OPAL_SUCCESS;
 149     } else {
 150         return OPAL_ERROR;
 151     }
 152 }
 153 
 154 int opal_compress_gzip_decompress_nb(char * cname, char **fname, pid_t *child_pid)
 155 {
 156     char **argv = NULL;
 157     char * dir_cname = NULL;
 158     pid_t loc_pid = 0;
 159     int status;
 160     bool is_tar = false;
 161 
 162     if( 0 == strncmp(&(cname[strlen(cname)-7]), ".tar.gz", strlen(".tar.gz")) ) {
 163         is_tar = true;
 164     }
 165 
 166     *fname = strdup(cname);
 167     if( is_tar ) {
 168         /* Strip off '.tar.gz' */
 169         (*fname)[strlen(cname)-7] = '\0';
 170     } else {
 171         /* Strip off '.gz' */
 172         (*fname)[strlen(cname)-3] = '\0';
 173     }
 174 
 175     opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
 176                         "compress:gzip: decompress_nb(%s -> [%s])",
 177                         cname, *fname);
 178 
 179     *child_pid = fork();
 180     if( *child_pid == 0 ) { /* Child */
 181         char * cmd;
 182         dir_cname  = opal_dirname(cname);
 183 
 184         chdir(dir_cname);
 185 
 186         /* Fork(gunzip) */
 187         loc_pid = fork();
 188         if( loc_pid == 0 ) { /* Child */
 189             opal_asprintf(&cmd, "gunzip %s", cname);
 190 
 191             opal_output_verbose(10, mca_compress_gzip_component.super.output_handle,
 192                                 "compress:gzip: decompress_nb() command [%s]",
 193                                 cmd);
 194 
 195             argv = opal_argv_split(cmd, ' ');
 196             status = execvp(argv[0], argv);
 197 
 198             opal_output(0, "compress:gzip: decompress_nb: Failed to exec child [%s] status = %d\n", cmd, status);
 199             exit(OPAL_ERROR);
 200         }
 201         else if( loc_pid > 0 ) { /* Parent */
 202             waitpid(loc_pid, &status, 0);
 203             if( !WIFEXITED(status) ) {
 204                 opal_output(0, "compress:gzip: decompress_nb: Failed to bunzip the file [%s] status = %d\n", cname, status);
 205                 exit(OPAL_ERROR);
 206             }
 207         }
 208         else {
 209             exit(OPAL_ERROR);
 210         }
 211 
 212         /* tar_decompress */
 213         if( is_tar ) {
 214             /* Strip off '.gz' leaving just '.tar' */
 215             cname[strlen(cname)-3] = '\0';
 216             opal_compress_base_tar_extract(&cname);
 217         }
 218 
 219         /* Once this child is done, then directly exit */
 220         exit(OPAL_SUCCESS);
 221     }
 222     else if( *child_pid > 0 ) {
 223         ;
 224     }
 225     else {
 226         return OPAL_ERROR;
 227     }
 228 
 229     return OPAL_SUCCESS;
 230 }
 231 
 232 static bool is_directory(char *fname ) {
 233     struct stat file_status;
 234     int rc;
 235 
 236     if(0 != (rc = stat(fname, &file_status) ) ) {
 237         return false;
 238     }
 239     if(S_ISDIR(file_status.st_mode)) {
 240         return true;
 241     }
 242 
 243     return false;
 244 }

/* [<][>][^][v][top][bottom][index][help] */