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
use clap::{App, AppSettings, Arg, SubCommand};
use failure::bail;
use file_protocol::{FileProtocol, FileProtocolConfig, State};
use log::{error, info};
use simplelog::*;
use std::path::Path;
use std::time::Duration;

fn upload(
    host_ip: &str,
    remote_addr: &str,
    source_path: &str,
    target_path: &str,
    prefix: Option<String>,
    chunk_size: usize,
    hold_count: u16,
) -> Result<(), failure::Error> {
    let f_config = FileProtocolConfig::new(prefix, chunk_size, hold_count);
    let f_protocol = FileProtocol::new(host_ip, remote_addr, f_config);

    info!(
        "Uploading local:{} to remote:{}",
        &source_path, &target_path
    );

    // Copy file to upload to temp storage. Calculate the hash and chunk info
    let (hash, num_chunks, mode) = f_protocol.initialize_file(&source_path)?;

    // Generate channel id for transaction
    let channel = f_protocol.generate_channel()?;

    // Tell our destination the hash and number of chunks to expect
    f_protocol.send_metadata(channel, &hash, num_chunks)?;

    // Send export command for file
    f_protocol.send_export(channel, &hash, &target_path, mode)?;

    // Start the engine to send the file data chunks
    f_protocol.message_engine(
        |d| f_protocol.recv(Some(d)),
        Duration::from_secs(2),
        &State::Transmitting,
    )?;
    Ok(())
}

fn download(
    host_ip: &str,
    remote_addr: &str,
    source_path: &str,
    target_path: &str,
    prefix: Option<String>,
    chunk_size: usize,
    hold_count: u16,
) -> Result<(), failure::Error> {
    let f_config = FileProtocolConfig::new(prefix, chunk_size, hold_count);
    let f_protocol = FileProtocol::new(host_ip, remote_addr, f_config);

    info!(
        "Downloading remote: {} to local: {}",
        source_path, target_path
    );

    // Generate channel id for transaction
    let channel = f_protocol.generate_channel()?;

    // Send our file request to the remote addr and verify that it's
    // going to be able to send it
    f_protocol.send_import(channel, source_path)?;

    // Wait for the request reply.
    // Note/TODO: We don't use a timeout here because we don't know how long it will
    // take the server to prepare the file we've requested.
    // Larger files (> 100MB) can take over a minute to process.
    let reply = match f_protocol.recv(None) {
        Ok(message) => message,
        Err(error) => bail!("Failed to import file: {}", error),
    };

    let state = f_protocol.process_message(
        reply,
        &State::StartReceive {
            path: target_path.to_string(),
        },
    )?;

    f_protocol.message_engine(|d| f_protocol.recv(Some(d)), Duration::from_secs(2), &state)?;
    Ok(())
}

fn cleanup(
    host_ip: &str,
    remote_addr: &str,
    hash: Option<String>,
    prefix: Option<String>,
    chunk_size: usize,
    hold_count: u16,
) -> Result<(), failure::Error> {
    match &hash {
        Some(s) => info!("Requesting remote cleanup of temp storage for hash {}", s),
        None => info!("Requesting remote cleanup of all temp storage"),
    }

    let f_config = FileProtocolConfig::new(prefix, chunk_size, hold_count);
    let f_protocol = FileProtocol::new(host_ip, remote_addr, f_config);

    // Generate channel ID for transaction
    let channel = f_protocol.generate_channel()?;

    // Send our cleanup request to the remote addr and verify that it's
    // going to be able to send it
    f_protocol.send_cleanup(channel, hash)?;

    Ok(())
}

fn main() {
    CombinedLogger::init(vec![
        TermLogger::new(LevelFilter::Info, Config::default()).unwrap()
    ])
    .unwrap();

    info!("Starting file transfer client");

    let args = App::new("File transfer client")
        .subcommand(
            SubCommand::with_name("upload")
                .about("Initiates upload of local file")
                .arg(
                    Arg::with_name("source_path")
                        .help("Local file path to upload")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("target_path")
                        .help("Destination path on remote target")
                        .takes_value(true),
                ),
        )
        .subcommand(
            SubCommand::with_name("download")
                .about("Requests download of remote file")
                .arg(
                    Arg::with_name("source_path")
                        .help("Remote file path to download")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("target_path")
                        .help("Local destination path")
                        .takes_value(true),
                ),
        )
        .subcommand(
            SubCommand::with_name("cleanup")
                .about("Requests cleanup of remote temporary storage")
                .arg(
                    Arg::with_name("hash")
                        .help("Specific file storage to clean up")
                        .takes_value(true),
                ),
        )
        .arg(
            Arg::with_name("host_ip")
                .short("h")
                .takes_value(true)
                .default_value("0.0.0.0"),
        )
        .arg(
            Arg::with_name("remote_ip")
                .short("-r")
                .takes_value(true)
                .default_value("0.0.0.0"),
        )
        .arg(
            Arg::with_name("remote_port")
                .short("-p")
                .takes_value(true)
                .default_value("7000"),
        )
        .arg(
            Arg::with_name("storage_prefix")
                .short("-s")
                .takes_value(true)
                .default_value("file-storage"),
        )
        .arg(
            Arg::with_name("chunk_size")
                .short("-c")
                .takes_value(true)
                .default_value("4096"),
        )
        .arg(
            Arg::with_name("hold_count")
                .short("-t")
                .takes_value(true)
                .default_value("6"),
        )
        .setting(AppSettings::SubcommandRequiredElseHelp)
        .setting(AppSettings::DeriveDisplayOrder)
        .get_matches();

    let host_ip = args.value_of("host_ip").unwrap();
    let remote_addr = format!(
        "{}:{}",
        args.value_of("remote_ip").unwrap(),
        args.value_of("remote_port").unwrap()
    );
    let chunk_size: usize = args.value_of("chunk_size").unwrap().parse().unwrap();
    let hold_count: u16 = args.value_of("hold_count").unwrap().parse().unwrap();
    let storage_prefix = args.value_of("storage_prefix").unwrap().to_string();

    let result = match args.subcommand_name() {
        Some("upload") => {
            let upload_args = args.subcommand_matches("upload").unwrap();
            let source_path = upload_args.value_of("source_path").unwrap();
            let target_path = match upload_args.value_of("target_path") {
                Some(path) => path.to_owned(),
                None => Path::new(&source_path)
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .into_owned(),
            };

            upload(
                host_ip,
                &remote_addr,
                &source_path,
                &target_path,
                Some(storage_prefix),
                chunk_size,
                hold_count,
            )
        }
        Some("download") => {
            let download_args = args.subcommand_matches("download").unwrap();
            let source_path = download_args.value_of("source_path").unwrap();
            let target_path = match download_args.value_of("target_path") {
                Some(path) => path.to_owned(),
                None => Path::new(&source_path)
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .into_owned(),
            };

            download(
                host_ip,
                &remote_addr,
                &source_path,
                &target_path,
                Some(storage_prefix),
                chunk_size,
                hold_count,
            )
        }
        Some("cleanup") => {
            let hash = args
                .subcommand_matches("cleanup")
                .unwrap()
                .value_of("hash")
                .to_owned()
                .map(|v| v.to_owned());
            cleanup(
                host_ip,
                &remote_addr,
                hash,
                Some(storage_prefix),
                chunk_size,
                hold_count,
            )
        }
        _ => panic!("Invalid command"),
    };

    if let Err(err) = result {
        error!("Operation failed: {}", err);
    } else {
        info!("Operation successful");
    }
}