File: compress_base_fns.c

package info (click to toggle)
openmpi 4.1.0-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 126,560 kB
  • sloc: ansic: 685,465; makefile: 42,952; f90: 19,220; sh: 7,002; java: 6,360; perl: 3,524; cpp: 2,227; python: 1,350; lex: 989; fortran: 61; tcl: 12
file content (135 lines) | stat: -rw-r--r-- 2,975 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2004-2010 The Trustees of Indiana University.
 *                         All rights reserved.
 * Copyright (c) 2015      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <string.h>
#include <sys/wait.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif  /* HAVE_FCNTL_H */
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#include "opal/mca/mca.h"
#include "opal/mca/base/base.h"
#include "opal/include/opal/constants.h"
#include "opal/util/os_dirpath.h"
#include "opal/util/output.h"
#include "opal/util/argv.h"

#include "opal/mca/compress/compress.h"
#include "opal/mca/compress/base/base.h"

/******************
 * Local Function Defs
 ******************/

/******************
 * Object stuff
 ******************/

int opal_compress_base_tar_create(char ** target)
{
    int exit_status = OPAL_SUCCESS;
    char *tar_target = NULL;
    char **argv = NULL;
    pid_t child_pid = 0;
    int status = 0;

    asprintf(&tar_target, "%s.tar", *target);

    child_pid = fork();
    if( 0 == child_pid ) { /* Child */
        char *cmd;
        asprintf(&cmd, "tar -cf %s %s", tar_target, *target);

        argv = opal_argv_split(cmd, ' ');
        status = execvp(argv[0], argv);

        opal_output(0, "compress:base: Tar:: Failed to exec child [%s] status = %d\n", cmd, status);
        exit(OPAL_ERROR);
    }
    else if(0 < child_pid) {
        waitpid(child_pid, &status, 0);

        if( !WIFEXITED(status) ) {
            exit_status = OPAL_ERROR;
            goto cleanup;
        }

        free(*target);
        *target = strdup(tar_target);
    }
    else {
        exit_status = OPAL_ERROR;
        goto cleanup;
    }

 cleanup:
    if( NULL != tar_target ) {
        free(tar_target);
    }

    return exit_status;
}

int opal_compress_base_tar_extract(char ** target)
{
    int exit_status = OPAL_SUCCESS;
    char **argv = NULL;
    pid_t child_pid = 0;
    int status = 0;

    child_pid = fork();
    if( 0 == child_pid ) { /* Child */
        char *cmd;
        asprintf(&cmd, "tar -xf %s", *target);

        argv = opal_argv_split(cmd, ' ');
        status = execvp(argv[0], argv);

        opal_output(0, "compress:base: Tar:: Failed to exec child [%s] status = %d\n", cmd, status);
        exit(OPAL_ERROR);
    }
    else if(0 < child_pid) {
        waitpid(child_pid, &status, 0);

        if( !WIFEXITED(status) ) {
            exit_status = OPAL_ERROR;
            goto cleanup;
        }

        /* Strip off the '.tar' */
        (*target)[strlen(*target)-4] = '\0';
    }
    else {
        exit_status = OPAL_ERROR;
        goto cleanup;
    }

 cleanup:

    return exit_status;
}

/******************
 * Local Functions
 ******************/