aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/geniza-drive.rs
blob: 385ec3d1df72f485f9d86ba61ad06a913c54c817 (plain)
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
// Free Software under GPL-3.0, see LICENSE
// Copyright 2017 Bryan Newbold

extern crate clap;
extern crate env_logger;
#[macro_use]
extern crate error_chain;
extern crate geniza;

#[cfg(test)]
extern crate assert_cli;

use geniza::*;
use std::path::Path;
use clap::{App, Arg, SubCommand};

fn run() -> Result<()> {
    env_logger::init().unwrap();

    let matches = App::new("geniza-drive")
        .version(env!("CARGO_PKG_VERSION"))
        .arg(Arg::with_name("dat-dir")
            .short("d")
            .long("dat-dir")
            .value_name("PATH")
            .help("dat drive directory")
            .default_value(".dat")
            .takes_value(true))
        .subcommand(
            SubCommand::with_name("init")
                .about("Creates a blank drive")
        )
        .subcommand(
            SubCommand::with_name("ls")
                .about("Lists current files in this dat")
        )
        .subcommand(
            SubCommand::with_name("cat")
                .about("Prints a file (as a string) to stdout")
                .arg_from_usage("<FILE> 'file to add'")
        )
        .subcommand(
            SubCommand::with_name("import-file")
                .about("Adds an indivudal file to the dat")
                .arg_from_usage("<FILE> 'file to add'")
                .arg_from_usage("--target <path> 'path to import the file to (if not top level)'")
        )
        .subcommand(
            SubCommand::with_name("export-file")
                .about("Copies a file from dat archive to local disk")
                .arg_from_usage("<FILE> 'file to export'")
                .arg_from_usage("--target <path> 'path to save the file to (if not same name)'")
        )
        .subcommand(
            SubCommand::with_name("import-dir")
                .about("Adds a directory (recursively) to the dat")
                .arg_from_usage("<DIR> 'directory to add'")
                .arg_from_usage("--target <path> 'path to import the file to (if not top level)'")
        )
        .subcommand(
            SubCommand::with_name("export-dir")
                .about("Copies a directory (recursively) from dat archive to local disk")
                .arg_from_usage("<DIR> 'directory to export'")
                .arg_from_usage("--target <path> 'path to save the directory to (if not same name)'")
        )
        .subcommand(
            SubCommand::with_name("log")
                .about("History of additions/deletions from this dat")
        )
        .subcommand(
            SubCommand::with_name("verify")
                .about("Checks signatures et al")
        )
        .subcommand(
            SubCommand::with_name("dump-entries")
                .about("Dump all entries in a debug-friendly format")
        )
        .subcommand(
            SubCommand::with_name("copy")
                .about("Internally copies a file in a dat archive")
                .arg_from_usage("<FROM> 'path to copy from'")
                .arg_from_usage("<TO> 'path to copy from'")
        )
        .subcommand(
            SubCommand::with_name("remove")
                .about("Deletes a file path from dat archive")
                .arg_from_usage("<FILE> 'file to delete'")
        )
        .subcommand(
            SubCommand::with_name("remove-dir-all")
                .about("Recursively deletes a directory from the dat archive")
                .arg_from_usage("<PATH> 'directory to delete'")
        )
        .get_matches();

    let dir = Path::new(matches.value_of("dat-dir").unwrap());
    match matches.subcommand() {
        ("init", Some(_subm)) => {
            let _drive = DatDrive::create(dir)?;
            // TODO: print public key in hex
            println!("Done!");
        }
        ("ls", Some(_subm)) => {
            let mut drive = DatDrive::open(dir, false)?;
            for entry in drive.read_dir_recursive("/") {
                let entry = entry?;
                println!("{}", entry.path.display());
            }
        }
        ("cat", Some(subm)) => {
            let path = Path::new(subm.value_of("FILE").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            let data = drive.read_file_bytes(&path)?;
            // TODO: just write to stdout
            let s = String::from_utf8(data).unwrap();
            println!("{}", s);
        }
        ("import-file", Some(subm)) => {
            let path = Path::new(subm.value_of("FILE").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            let fpath = match subm.value_of("target") {
                None => Path::new("/").join(path.file_name().unwrap()),
                Some(p) => Path::new("/").join(p)
            };
            drive.import_file(&path, &fpath)?;

        }
        ("export-file", Some(subm)) => {
            let path = Path::new(subm.value_of("FILE").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            let fpath = match subm.value_of("target") {
                None => Path::new("/").join(path.file_name().unwrap()),
                Some(p) => Path::new("/").join(p)
            };
            drive.export_file(&path, &fpath)?;
        }
        ("import-dir", Some(subm)) => {
            let path = Path::new(subm.value_of("DIR").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            let fpath = match subm.value_of("target") {
                None => Path::new("/").join(path.file_name().unwrap()),
                Some(p) => Path::new("/").join(p)
            };
            drive.import_dir_all(&path, &fpath)?;

        }
        ("export-dir", Some(subm)) => {
            let path = Path::new(subm.value_of("DIR").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            let fpath = match subm.value_of("target") {
                None => Path::new("/").join(path.file_name().unwrap()),
                Some(p) => Path::new("/").join(p)
            };
            drive.export_dir(&path, &fpath)?;
        }
        ("log", Some(_subm)) => {
            let mut drive = DatDrive::open(dir, false)?;
            for entry in drive.history(0) {
                let entry = entry?;
                if let Some(stat) = entry.stat {
                    if stat.get_blocks() == 0 {
                        println!("{}\t[chg]  {}",
                            entry.index, entry.path.display());
                    } else {
                        println!("{}\t[put]  {}\t{} bytes ({} blocks)",
                            entry.index, entry.path.display(), stat.get_size(), stat.get_blocks());
                    }
                } else {
                    println!("{}\t[del]  {}",
                        entry.index, entry.path.display());
                }
            }
        }
        ("verify", Some(_subm)) => {
            let mut drive = DatDrive::open(dir, false)?;
            println!("{:?}", drive.verify());
        }
        ("dump-entries", Some(_subm)) => {
            let mut drive = DatDrive::open(dir, false)?;
            for entry in drive.history(0) {
                let entry = entry?;
                println!("{}\tpath: {}",
                    entry.index, entry.path.display());
                println!("\tchildren: {:?}",
                    entry.children);
                if let Some(_) = entry.stat {
                    println!("\tstat: Some (add/change)");
                } else {
                    println!("\tstat: None (delete)");
                }
            }
        }
        ("copy", Some(subm)) => {
            let from_path= Path::new(subm.value_of("FROM").unwrap());
            let to_path= Path::new(subm.value_of("FROM").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            drive.copy_file(&from_path, &to_path)?;
        }
        ("remove", Some(subm)) => {
            let path = Path::new(subm.value_of("FILE").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            drive.remove_file(&path)?;
        }
        ("remove-dir-all", Some(subm)) => {
            let path = Path::new(subm.value_of("FILE").unwrap());
            let mut drive = DatDrive::open(dir, true)?;
            drive.remove_dir_all(&path)?;
        }
        _ => {
            println!("Missing or unimplemented command!");
            println!("{}", matches.usage());
            ::std::process::exit(-1);
        }
    }
    Ok(())
}

quick_main!(run);

#[test]
fn test_drive_cmd() {

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "/non-existant-dir", "ls"])
        .fails();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/simple/.dat", "ls"])
        .stdout().contains("README.md")
        .succeeds();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/tree/.dat", "ls"])
        .stdout().contains("Cantharellu")
        .succeeds();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/alphabet/.dat", "cat", "/c"])
        .stdout().is("c")
        .succeeds();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/simple/.dat", "log"])
        .stdout().contains("Felidae")
        .succeeds();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/tree/.dat", "verify"])
        .succeeds();

    assert_cli::Assert::cargo_binary("geniza-drive")
        .with_args(&["-d", "test-data/dat/tree/.dat", "dump-entries"])
        .succeeds();

}