1 /*
2  * Copyright (c) 2004-2010 The Trustees of Indiana University.
3  *                         All rights reserved.
4  * Copyright (c) 2015      Research Organization for Information Science
5  *                         and Technology (RIST). All rights reserved.
6  *
7  * $COPYRIGHT$
8  *
9  * Additional copyrights may follow
10  *
11  * $HEADER$
12  */
13 
14 #include "opal_config.h"
15 
16 #include <string.h>
17 #include <sys/wait.h>
18 #if HAVE_SYS_TYPES_H
19 #include <sys/types.h>
20 #endif
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif  /* HAVE_FCNTL_H */
27 #ifdef HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30 
31 #include "opal/mca/mca.h"
32 #include "opal/mca/base/base.h"
33 #include "opal/include/opal/constants.h"
34 #include "opal/util/os_dirpath.h"
35 #include "opal/util/output.h"
36 #include "opal/util/argv.h"
37 
38 #include "opal/mca/compress/compress.h"
39 #include "opal/mca/compress/base/base.h"
40 
41 /******************
42  * Local Function Defs
43  ******************/
44 
45 /******************
46  * Object stuff
47  ******************/
48 
opal_compress_base_tar_create(char ** target)49 int opal_compress_base_tar_create(char ** target)
50 {
51     int exit_status = OPAL_SUCCESS;
52     char *tar_target = NULL;
53     char **argv = NULL;
54     pid_t child_pid = 0;
55     int status = 0;
56 
57     asprintf(&tar_target, "%s.tar", *target);
58 
59     child_pid = fork();
60     if( 0 == child_pid ) { /* Child */
61         char *cmd;
62         asprintf(&cmd, "tar -cf %s %s", tar_target, *target);
63 
64         argv = opal_argv_split(cmd, ' ');
65         status = execvp(argv[0], argv);
66 
67         opal_output(0, "compress:base: Tar:: Failed to exec child [%s] status = %d\n", cmd, status);
68         exit(OPAL_ERROR);
69     }
70     else if(0 < child_pid) {
71         waitpid(child_pid, &status, 0);
72 
73         if( !WIFEXITED(status) ) {
74             exit_status = OPAL_ERROR;
75             goto cleanup;
76         }
77 
78         free(*target);
79         *target = strdup(tar_target);
80     }
81     else {
82         exit_status = OPAL_ERROR;
83         goto cleanup;
84     }
85 
86  cleanup:
87     if( NULL != tar_target ) {
88         free(tar_target);
89     }
90 
91     return exit_status;
92 }
93 
opal_compress_base_tar_extract(char ** target)94 int opal_compress_base_tar_extract(char ** target)
95 {
96     int exit_status = OPAL_SUCCESS;
97     char **argv = NULL;
98     pid_t child_pid = 0;
99     int status = 0;
100 
101     child_pid = fork();
102     if( 0 == child_pid ) { /* Child */
103         char *cmd;
104         asprintf(&cmd, "tar -xf %s", *target);
105 
106         argv = opal_argv_split(cmd, ' ');
107         status = execvp(argv[0], argv);
108 
109         opal_output(0, "compress:base: Tar:: Failed to exec child [%s] status = %d\n", cmd, status);
110         exit(OPAL_ERROR);
111     }
112     else if(0 < child_pid) {
113         waitpid(child_pid, &status, 0);
114 
115         if( !WIFEXITED(status) ) {
116             exit_status = OPAL_ERROR;
117             goto cleanup;
118         }
119 
120         /* Strip off the '.tar' */
121         (*target)[strlen(*target)-4] = '\0';
122     }
123     else {
124         exit_status = OPAL_ERROR;
125         goto cleanup;
126     }
127 
128  cleanup:
129 
130     return exit_status;
131 }
132 
133 /******************
134  * Local Functions
135  ******************/
136