cmprss 0.2.0

A compression multi-tool for the command line.
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
mod bzip2;
mod gzip;
mod progress;
mod tar;
mod utils;
mod xz;

use bzip2::{Bzip2, Bzip2Args};
use clap::{Parser, Subcommand};
use gzip::{Gzip, GzipArgs};
use is_terminal::IsTerminal;
use std::path::{Path, PathBuf};
use std::{io, vec};
use tar::{Tar, TarArgs};
use utils::*;
use xz::{Xz, XzArgs};

/// A compression multi-tool
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct CmprssArgs {
    /// Format
    #[command(subcommand)]
    format: Option<Format>,

    // Base arguments for the non-subcommand behavior
    #[clap(flatten)]
    pub base_args: CommonArgs,
}

#[derive(Subcommand, Debug)]
enum Format {
    /// tar archive format
    Tar(TarArgs),

    /// gzip compression
    #[clap(visible_alias = "gz")]
    Gzip(GzipArgs),

    /// xz compression
    Xz(XzArgs),

    /// bzip2 compression
    #[clap(visible_alias = "bz2")]
    Bzip2(Bzip2Args),
}

/// Get the input filename or return a default file
/// This file will be used to generate the output filename
fn get_input_filename(input: &CmprssInput) -> Result<&Path, io::Error> {
    match input {
        CmprssInput::Path(paths) => {
            if paths.is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    "error: no input specified",
                ));
            }
            Ok(paths.first().unwrap())
        }
        CmprssInput::Pipe(_) => Ok(Path::new("archive")),
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum Action {
    Compress,
    Extract,
    Unknown,
}

/// Defines a single compress/extract action to take.
#[derive(Debug)]
struct Job {
    compressor: Box<dyn Compressor>,
    input: CmprssInput,
    output: CmprssOutput,
    action: Action,
}

/// Get a compressor from a filename
fn get_compressor_from_filename(filename: &Path) -> Option<Box<dyn Compressor>> {
    // TODO: Support multi-level files, like tar.gz
    let compressors: Vec<Box<dyn Compressor>> = vec![
        Box::<Tar>::default(),
        Box::<Gzip>::default(),
        Box::<Xz>::default(),
        Box::<Bzip2>::default(),
    ];
    compressors.into_iter().find(|c| c.is_archive(filename))
}

/// Convert an input path into a Path
fn get_path(input: &str) -> Option<PathBuf> {
    let path = PathBuf::from(input);
    if !path.try_exists().unwrap_or(false) {
        return None;
    }
    Some(path)
}

/// Guess compressor/action from the two filenames
/// The compressor may already be given
fn guess_from_filenames(
    input: &[PathBuf],
    output: &Path,
    compressor: Option<Box<dyn Compressor>>,
) -> (Option<Box<dyn Compressor>>, Action) {
    if input.len() != 1 {
        if let Some(guessed_compressor) = get_compressor_from_filename(output) {
            return (Some(guessed_compressor), Action::Compress);
        }
        // In theory we could be extracting multiple files to a directory
        // We'll fail somewhere else if that's not the case
        return (compressor, Action::Extract);
    }
    let input = input.first().unwrap();

    let guessed_compressor = get_compressor_from_filename(output);
    let guessed_extractor = get_compressor_from_filename(input);
    let guessed_compressor_name = if let Some(c) = &guessed_compressor {
        c.name()
    } else {
        ""
    };
    let guessed_extractor_name = if let Some(e) = &guessed_extractor {
        e.name()
    } else {
        ""
    };

    if let Some(c) = &compressor {
        if guessed_compressor_name == c.name() {
            return (compressor, Action::Compress);
        } else if guessed_extractor_name == c.name() {
            return (compressor, Action::Extract);
        } else {
            // Default to compressing
            return (compressor, Action::Compress);
        }
    }

    match (guessed_compressor, guessed_extractor) {
        (None, None) => (None, Action::Unknown),
        (Some(c), None) => (Some(c), Action::Compress),
        (None, Some(e)) => (Some(e), Action::Extract),
        (Some(c), Some(e)) => {
            if c.name() == e.name() {
                return (Some(c), Action::Unknown);
            }
            // Compare the input and output extensions to see if one has an extra extension
            let input_file = input.file_name().unwrap().to_str().unwrap();
            let input_ext = input.extension().unwrap_or_default();
            let output_file = output.file_name().unwrap().to_str().unwrap();
            let output_ext = output.extension().unwrap_or_default();
            let guessed_output = input_file.to_string() + "." + output_ext.to_str().unwrap();
            let guessed_input = output_file.to_string() + "." + input_ext.to_str().unwrap();
            if guessed_output == output_file {
                (Some(c), Action::Compress)
            } else if guessed_input == input_file {
                (Some(e), Action::Extract)
            } else {
                (None, Action::Unknown)
            }
        }
    }
}

/// Parse the common args and determine the details of the job requested
fn get_job(
    compressor: Option<Box<dyn Compressor>>,
    common_args: &CommonArgs,
) -> Result<Job, io::Error> {
    let mut compressor = compressor;
    let mut action = {
        if common_args.compress {
            Action::Compress
        } else if common_args.extract || common_args.decompress {
            Action::Extract
        } else {
            Action::Unknown
        }
    };

    let mut inputs = Vec::new();
    if let Some(in_file) = &common_args.input {
        match get_path(in_file) {
            Some(path) => inputs.push(path),
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    "Specified input path does not exist",
                ));
            }
        }
    }

    let mut output = match &common_args.output {
        Some(output) => {
            let path = Path::new(output);
            if path.try_exists()? && !path.is_dir() {
                // Output path exists, bail out
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    "Specified output path already exists",
                ));
            }
            Some(path)
        }
        None => None,
    };

    // Process the io_list, check if there is an output first
    let mut io_list = common_args.io_list.clone();
    if output.is_none() {
        if let Some(possible_output) = common_args.io_list.last() {
            let path = Path::new(possible_output);
            if !path.try_exists()? {
                // Use the given path if it doesn't exist
                output = Some(path);
                io_list.pop();
            } else if path.is_dir() {
                match action {
                    Action::Compress => {
                        // A directory can potentially be a target output location or
                        // an input, for now assume it is an input.
                    }
                    Action::Extract => {
                        // Can extract to a directory, and it wouldn't make any sense as an input
                        output = Some(path);
                        io_list.pop();
                    }
                    _ => {
                        // TODO: don't know if this is an input or output, assume we're compressing this directory
                        // This does cause problems for inferencing "cat archive.tar | cmprss tar ."
                        // Probably need to add some special casing
                    }
                };
            } else {
                // TODO: check for scenarios where we want to append to an existing archive
            }
        }
    }

    // Validate the specified inputs
    // Everything in the io_list should be an input
    for input in &io_list {
        if let Some(path) = get_path(input) {
            inputs.push(path);
        } else {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "Specified input path does not exist",
            ));
        }
    }

    // Fallback to stdin/stdout if we're missing files
    let cmprss_input = match inputs.is_empty() {
        true => {
            if !std::io::stdin().is_terminal()
                && !&common_args.ignore_pipes
                && !&common_args.ignore_stdin
            {
                CmprssInput::Pipe(std::io::stdin())
            } else {
                return Err(io::Error::new(io::ErrorKind::Other, "No specified input"));
            }
        }
        false => CmprssInput::Path(inputs),
    };

    let cmprss_output = match output {
        Some(path) => CmprssOutput::Path(path.to_path_buf()),
        None => {
            if !std::io::stdout().is_terminal()
                && !&common_args.ignore_pipes
                && !&common_args.ignore_stdout
            {
                CmprssOutput::Pipe(std::io::stdout())
            } else {
                match action {
                    Action::Compress => {
                        if compressor.is_none() {
                            return Err(io::Error::new(
                                io::ErrorKind::Other,
                                "Must specify a compressor",
                            ));
                        }
                        CmprssOutput::Path(PathBuf::from(
                            compressor
                                .as_ref()
                                .unwrap()
                                .default_compressed_filename(get_input_filename(&cmprss_input)?),
                        ))
                    }
                    Action::Extract => {
                        if compressor.is_none() {
                            compressor =
                                get_compressor_from_filename(get_input_filename(&cmprss_input)?);
                            if compressor.is_none() {
                                return Err(io::Error::new(
                                    io::ErrorKind::Other,
                                    "Must specify a compressor",
                                ));
                            }
                        }
                        CmprssOutput::Path(PathBuf::from(
                            compressor
                                .as_ref()
                                .unwrap()
                                .default_extracted_filename(get_input_filename(&cmprss_input)?),
                        ))
                    }
                    Action::Unknown => {
                        if compressor.is_none() {
                            // Can still work if the input is an archive
                            compressor =
                                get_compressor_from_filename(get_input_filename(&cmprss_input)?);
                            if compressor.is_none() {
                                return Err(io::Error::new(
                                    io::ErrorKind::Other,
                                    "Must specify a compressor",
                                ));
                            }
                            action = Action::Extract;
                            CmprssOutput::Path(PathBuf::from(
                                compressor
                                    .as_ref()
                                    .unwrap()
                                    .default_extracted_filename(get_input_filename(&cmprss_input)?),
                            ))
                        } else {
                            // We know the compressor, does the input have the same extension?
                            if let Some(compressor_from_input) =
                                get_compressor_from_filename(get_input_filename(&cmprss_input)?)
                            {
                                if compressor.as_ref().unwrap().name()
                                    == compressor_from_input.name()
                                {
                                    action = Action::Extract;
                                    CmprssOutput::Path(PathBuf::from(
                                        compressor.as_ref().unwrap().default_extracted_filename(
                                            get_input_filename(&cmprss_input)?,
                                        ),
                                    ))
                                } else {
                                    action = Action::Compress;
                                    CmprssOutput::Path(PathBuf::from(
                                        compressor.as_ref().unwrap().default_compressed_filename(
                                            get_input_filename(&cmprss_input)?,
                                        ),
                                    ))
                                }
                            } else {
                                action = Action::Compress;
                                CmprssOutput::Path(PathBuf::from(
                                    compressor.as_ref().unwrap().default_compressed_filename(
                                        get_input_filename(&cmprss_input)?,
                                    ),
                                ))
                            }
                        }
                    }
                }
            }
        }
    };

    // If we don't have the compressor/action, we can attempt to infer
    if compressor.is_none() || action == Action::Unknown {
        match action {
            Action::Compress => {
                // Look at the output name
                // TODO: tar.gz ??
                if let CmprssOutput::Path(path) = &cmprss_output {
                    compressor = get_compressor_from_filename(path);
                }
            }
            Action::Extract => {
                // Look at the input name
                if let CmprssInput::Path(paths) = &cmprss_input {
                    if paths.len() != 1 {
                        // Can't guess if there are multiple inputs
                        return Err(io::Error::new(
                            io::ErrorKind::Other,
                            "Can't guess compressor with multiple inputs",
                        ));
                    }
                    compressor = get_compressor_from_filename(paths.first().unwrap());
                }
            }
            Action::Unknown => match (&cmprss_input, &cmprss_output) {
                (CmprssInput::Pipe(_), CmprssOutput::Path(path)) => {
                    if compressor.is_none() {
                        compressor = get_compressor_from_filename(path);
                        if compressor.is_some() {
                            action = Action::Compress;
                        } else {
                            return Err(io::Error::new(
                                io::ErrorKind::Other,
                                "Can't guess compressor to use",
                            ));
                        }
                    } else if compressor.as_ref().unwrap().name()
                        == get_compressor_from_filename(path).unwrap().name()
                    {
                        action = Action::Compress;
                    } else {
                        action = Action::Extract;
                    }
                }
                (CmprssInput::Path(paths), CmprssOutput::Pipe(_)) => {
                    if compressor.is_none() {
                        if paths.len() != 1 {
                            return Err(io::Error::new(
                                io::ErrorKind::Other,
                                "Can't guess compressor with multiple inputs",
                            ));
                        }
                        compressor = get_compressor_from_filename(paths.first().unwrap());
                        if compressor.is_some() {
                            action = Action::Extract;
                        } else {
                            return Err(io::Error::new(
                                io::ErrorKind::Other,
                                "Can't guess compressor to use",
                            ));
                        }
                    } else if let Some(c) = get_compressor_from_filename(paths.first().unwrap()) {
                        if compressor.as_ref().unwrap().name() == c.name() {
                            action = Action::Extract;
                        } else {
                            action = Action::Compress;
                        }
                    } else {
                        action = Action::Compress;
                    }
                }
                (CmprssInput::Pipe(_), CmprssOutput::Pipe(_)) => {
                    action = Action::Compress;
                }
                (CmprssInput::Path(paths), CmprssOutput::Path(path)) => {
                    let (guessed_compressor, guessed_action) =
                        guess_from_filenames(paths, path, compressor);
                    compressor = guessed_compressor;
                    action = guessed_action;
                }
            },
        }
    }

    if compressor.is_none() {
        return Err(io::Error::new(
            io::ErrorKind::Other,
            "Could not determine compressor to use",
        ));
    }
    if action == Action::Unknown {
        return Err(io::Error::new(
            io::ErrorKind::Other,
            "Could not determine action to take",
        ));
    }

    Ok(Job {
        compressor: compressor.unwrap(),
        input: cmprss_input,
        output: cmprss_output,
        action,
    })
}

fn command(compressor: Option<Box<dyn Compressor>>, args: &CommonArgs) -> Result<(), io::Error> {
    let job = get_job(compressor, args)?;

    match job.action {
        Action::Compress => job.compressor.compress(job.input, job.output)?,
        Action::Extract => job.compressor.extract(job.input, job.output)?,
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "Unknown action requested",
            ));
        }
    };

    Ok(())
}

fn main() {
    let args = CmprssArgs::parse();
    match args.format {
        Some(Format::Tar(a)) => command(Some(Box::new(Tar::new(&a))), &a.common_args),
        Some(Format::Gzip(a)) => command(Some(Box::new(Gzip::new(&a))), &a.common_args),
        Some(Format::Xz(a)) => command(Some(Box::new(Xz::new(&a))), &a.common_args),
        Some(Format::Bzip2(a)) => command(Some(Box::new(Bzip2::new(&a))), &a.common_args),
        _ => command(None, &args.base_args),
    }
    .unwrap_or_else(|e| {
        eprintln!("ERROR(cmprss): {}", e);
        std::process::exit(1);
    });
}