aboutsummaryrefslogtreecommitdiff
path: root/lib/archive.c
blob: 6452647d95bc0b14c90cfdf7f8cfd996c9a91d10 (plain) (blame)
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
/**
 * @file archive.c
 */
#include "spm.h"

/**
 * Extract a single file from a tar archive into a directory
 *
 * @param archive path to tar archive
 * @param filename known path inside the archive to extract
 * @param destination where to extract file to (must exist)
 * @return
 */
int tar_extract_file(const char *_archive, const char* _filename, const char *_destination) {
    Process *proc = NULL;
    int status;
    char cmd[PATH_MAX];
    char *archive = strdup(_archive);
    if (!archive) {
        fprintf(SYSERROR);
        return -1;
    }
    char *filename = strdup(_filename);
    if (!filename) {
        fprintf(SYSERROR);
        return -1;
    }
    char *destination = strdup(_destination);
    if (!destination) {
        fprintf(SYSERROR);
        return -1;
    }

    strchrdel(archive, SHELL_INVALID);
    strchrdel(destination, SHELL_INVALID);
    strchrdel(filename, SHELL_INVALID);

    sprintf(cmd, "bsdtar -x -f \"%s\" -C \"%s\" \"%s\" 2>&1", archive, destination, filename);
    if (exists(archive) != 0) {
        fprintf(stderr, "unable to find archive: %s\n", archive);
        fprintf(SYSERROR);
        return -1;
    }

    shell(&proc, SHELL_OUTPUT, cmd);
    if (!proc) {
        fprintf(SYSERROR);
        return -1;
    }

    status = proc->returncode;
    if (status != 0) {
        fprintf(stderr, "%s\n", proc->output);
    }

    shell_free(proc);
    free(archive);
    free(filename);
    free(destination);
    return status;
}

/**
 *
 * @param _archive
 * @param _destination
 * @return
 */
int tar_extract_archive(const char *_archive, const char *_destination) {
    Process *proc = NULL;
    int status;
    char cmd[PATH_MAX];

    if (exists(_archive) != 0) {
        //fprintf(SYSERROR);
        return -1;
    }

    char *archive = strdup(_archive);
    if (!archive) {
        fprintf(SYSERROR);
        return -1;
    }
    char *destination = strdup(_destination);
    if (!destination) {
        fprintf(SYSERROR);
        return -1;
    }

    // sanitize archive
    strchrdel(archive, SHELL_INVALID);
    // sanitize destination
    strchrdel(destination, SHELL_INVALID);

    sprintf(cmd, "bsdtar -x -f %s -C %s 2>&1", archive, destination);
    shell(&proc, SHELL_OUTPUT, cmd);
    if (!proc) {
        fprintf(SYSERROR);
        free(archive);
        free(destination);
        return -1;
    }

    status = proc->returncode;
    if (status != 0 && SPM_GLOBAL.verbose) {
        fprintf(stderr, "%s", proc->output);
    }

    shell_free(proc);
    free(archive);
    free(destination);
    return status;
}