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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (C) 2019 O.S. Systems Sofware LTDA
//
// SPDX-License-Identifier: MIT OR Apache-2.0

/*! The library provide tools for handling compressed and archive files

# Examples
```
let dir = tempfile::tempdir().unwrap();
uncompress("tests/fixtures/tree.tar.gz", dir.path(), Kind::TarGZip).unwrap();
```
*/

mod uncompress;

use std::path::Path;

/// Type of compressed file or archive
#[derive(Copy, Clone, Debug)]
pub enum Kind {
    TarGZip,
    TarBZip2,
    TarXz,
    TarLZMA,
    TarLZip,
    Tar,

    GZip,
    BZip2,
    Xz,
    LZip,
    LZMA,
}

/// Uncompress a archive of known [Kind](Kind) pointed by source to target location for file
pub fn uncompress<P1, P2>(source: P1, target: P2, kind: Kind) -> Result<(), failure::Error>
where
    P1: AsRef<Path>,
    P2: AsRef<Path>,
{
    let source = uncompress::Uncompress::new(source.as_ref());
    let target = target.as_ref();
    match kind {
        Kind::TarGZip => source.gz().tar(target),
        Kind::TarBZip2 => source.bz2().tar(target),
        Kind::TarXz => source.xz().tar(target),
        Kind::TarLZMA => source.lzma().tar(target),
        Kind::TarLZip => source.lz().tar(target),
        Kind::Tar => source.tar(target),

        Kind::GZip => source.gz().file(target),
        Kind::BZip2 => source.bz2().file(target),
        Kind::Xz => source.xz().file(target),
        Kind::LZMA => source.lzma().file(target),
        Kind::LZip => source.lz().file(target),
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::os::unix::fs::MetadataExt;
    use tempfile;

    fn assert_tree(p: &Path) {
        let leaf1 = p
            .join("tree/branch1/leaf")
            .metadata()
            .expect("tree/branch1/leaf not found in extracted directory structure");
        assert_eq!(leaf1.mode() % 0o1000, 0o664);
        assert_eq!(leaf1.uid(), 1000);
        assert_eq!(leaf1.gid(), 1000);

        let leaf2 = p
            .join("tree/branch2/leaf")
            .metadata()
            .expect("tree/branch1/leaf not found in extracted directory structure");
        assert_eq!(leaf2.mode() % 0o1000, 0o664);
        assert_eq!(leaf2.uid(), 1000);
        assert_eq!(leaf2.gid(), 1000);
    }

    fn assert_file(p: &Path) {
        let tree = p
            .metadata()
            .expect("tree.tar not found in extracted directory");
        assert_eq!(tree.mode() % 0o1000, 0o664);
        assert_eq!(tree.uid(), 1000);
        assert_eq!(tree.gid(), 1000);
    }

    #[test]
    fn uncompress_tar_gz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar.gz", dir.path(), Kind::TarGZip)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_tar_bz2() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar.bz2", dir.path(), Kind::TarBZip2)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_tar_xz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar.xz", dir.path(), Kind::TarXz)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_tar_lzma() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar.lzma", dir.path(), Kind::TarLZMA)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_tar_lz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar.lz", dir.path(), Kind::TarLZip)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_tar() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress("tests/fixtures/tree.tar", dir.path(), Kind::Tar)
            .expect("Failed to uncompress file");
        assert_tree(dir.path())
    }

    #[test]
    fn uncompress_gz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress(
            "tests/fixtures/tree.tar.gz",
            &dir.path().join("tree.tar"),
            Kind::GZip,
        )
        .expect("Failed to uncompress file");
        assert_file(&dir.path().join("tree.tar"))
    }

    #[test]
    fn uncompress_bz2() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress(
            "tests/fixtures/tree.tar.bz2",
            &dir.path().join("tree.tar"),
            Kind::BZip2,
        )
        .expect("Failed to uncompress file");
        assert_file(&dir.path().join("tree.tar"))
    }

    #[test]
    fn uncompress_xz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress(
            "tests/fixtures/tree.tar.xz",
            &dir.path().join("tree.tar"),
            Kind::Xz,
        )
        .expect("Failed to uncompress file");
        assert_file(&dir.path().join("tree.tar"))
    }

    #[test]
    fn uncompress_lzma() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress(
            "tests/fixtures/tree.tar.lzma",
            &dir.path().join("tree.tar"),
            Kind::LZMA,
        )
        .expect("Failed to uncompress file");
        assert_file(&dir.path().join("tree.tar"))
    }

    #[test]
    fn uncompress_lz() {
        let dir = tempfile::tempdir().expect("Unable to create temp dir");
        uncompress(
            "tests/fixtures/tree.tar.lz",
            &dir.path().join("tree.tar"),
            Kind::LZip,
        )
        .expect("Failed to uncompress file");
        assert_file(&dir.path().join("tree.tar"))
    }
}