root/opal/mca/compress/bzip/compress_bzip_module.c

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

DEFINITIONS

This source file includes following definitions.
  1. opal_compress_bzip_module_init
  2. opal_compress_bzip_module_finalize
  3. opal_compress_bzip_compress
  4. opal_compress_bzip_compress_nb
  5. opal_compress_bzip_decompress
  6. opal_compress_bzip_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_bzip.h"
  41 
  42 static bool is_directory(char *fname );
  43 
  44 int opal_compress_bzip_module_init(void)
  45 {
  46     return OPAL_SUCCESS;
  47 }
  48 
  49 int opal_compress_bzip_module_finalize(void)
  50 {
  51     return OPAL_SUCCESS;
  52 }
  53 
  54 int opal_compress_bzip_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_bzip_component.super.output_handle,
  60                         "compress:bzip: compress(%s)",
  61                         fname);
  62 
  63     opal_compress_bzip_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_bzip_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;
  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.bz2", base_fname);
  96             opal_asprintf(&cmd, "bzip2 %s", base_fname);
  97 #else
  98             opal_asprintf(cname, "%s.tar.bz2", base_fname);
  99             opal_asprintf(&cmd, "tar -jcf %s %s", *cname, base_fname);
 100 #endif
 101         } else {
 102             opal_asprintf(cname, "%s.bz2", base_fname);
 103             opal_asprintf(&cmd, "bzip2 %s", base_fname);
 104         }
 105 
 106         opal_output_verbose(10, mca_compress_bzip_component.super.output_handle,
 107                             "compress:bzip: compress_nb(%s -> [%s])",
 108                             fname, *cname);
 109         opal_output_verbose(10, mca_compress_bzip_component.super.output_handle,
 110                             "compress:bzip: 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:bzip: 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.bz2");
 122         } else {
 123             *postfix = strdup(".bz2");
 124         }
 125         opal_asprintf(cname, "%s%s", fname, *postfix);
 126     }
 127     else {
 128         return OPAL_ERROR;
 129     }
 130 
 131     return OPAL_SUCCESS;
 132 }
 133 
 134 int opal_compress_bzip_decompress(char * cname, char **fname)
 135 {
 136     pid_t child_pid = 0;
 137     int status = 0;
 138 
 139     opal_output_verbose(10, mca_compress_bzip_component.super.output_handle,
 140                         "compress:bzip: decompress(%s)",
 141                         cname);
 142 
 143     opal_compress_bzip_decompress_nb(cname, fname, &child_pid);
 144     waitpid(child_pid, &status, 0);
 145 
 146     if( WIFEXITED(status) ) {
 147         return OPAL_SUCCESS;
 148     } else {
 149         return OPAL_ERROR;
 150     }
 151 }
 152 
 153 int opal_compress_bzip_decompress_nb(char * cname, char **fname, pid_t *child_pid)
 154 {
 155     char **argv = NULL;
 156     char * dir_cname = NULL;
 157     pid_t loc_pid = 0;
 158     int status;
 159     bool is_tar = false;
 160 
 161     if( 0 == strncmp(&(cname[strlen(cname)-8]), ".tar.bz2", strlen(".tar.bz2")) ) {
 162         is_tar = true;
 163     }
 164 
 165     *fname = strdup(cname);
 166     if( is_tar ) {
 167         (*fname)[strlen(cname)-8] = '\0';
 168     } else {
 169         (*fname)[strlen(cname)-4] = '\0';
 170     }
 171 
 172     opal_output_verbose(10, mca_compress_bzip_component.super.output_handle,
 173                         "compress:bzip: decompress_nb(%s -> [%s])",
 174                         cname, *fname);
 175 
 176     *child_pid = fork();
 177     if( *child_pid == 0 ) { /* Child */
 178         dir_cname  = opal_dirname(cname);
 179 
 180         chdir(dir_cname);
 181 
 182         /* Fork(bunzip) */
 183         loc_pid = fork();
 184         if( loc_pid == 0 ) { /* Child */
 185             char * cmd;
 186             opal_asprintf(&cmd, "bunzip2 %s", cname);
 187 
 188             opal_output_verbose(10, mca_compress_bzip_component.super.output_handle,
 189                                 "compress:bzip: decompress_nb() command [%s]",
 190                                 cmd);
 191 
 192             argv = opal_argv_split(cmd, ' ');
 193             status = execvp(argv[0], argv);
 194 
 195             opal_output(0, "compress:bzip: decompress_nb: Failed to exec child [%s] status = %d\n", cmd, status);
 196             exit(OPAL_ERROR);
 197         }
 198         else if( loc_pid > 0 ) { /* Parent */
 199             waitpid(loc_pid, &status, 0);
 200             if( !WIFEXITED(status) ) {
 201                 opal_output(0, "compress:bzip: decompress_nb: Failed to bunzip the file [%s] status = %d\n", cname, status);
 202                 exit(OPAL_ERROR);
 203             }
 204         }
 205         else {
 206             exit(OPAL_ERROR);
 207         }
 208 
 209         /* tar_decompress */
 210         if( is_tar ) {
 211             /* Strip off '.bz2' leaving just '.tar' */
 212             cname[strlen(cname)-4] = '\0';
 213             opal_compress_base_tar_extract(&cname);
 214         }
 215 
 216         /* Once this child is done, then directly exit */
 217         exit(OPAL_SUCCESS);
 218     }
 219     else if( *child_pid > 0 ) {
 220         ;
 221     }
 222     else {
 223         return OPAL_ERROR;
 224     }
 225 
 226     return OPAL_SUCCESS;
 227 }
 228 
 229 static bool is_directory(char *fname ) {
 230     struct stat file_status;
 231     int rc;
 232 
 233     if(0 != (rc = stat(fname, &file_status) ) ) {
 234         return false;
 235     }
 236     if(S_ISDIR(file_status.st_mode)) {
 237         return true;
 238     }
 239 
 240     return false;
 241 }

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