summaryrefslogtreecommitdiffstats
path: root/src/commands/file_ops/paste_cut.rs
blob: 4fa171d9f02e88364cbde3418215c38ec10e1d56 (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
use std::fs;
use std::path::Path;
use std::sync::mpsc;
use std::thread;

use crate::context::JoshutoContext;
use crate::io::{IOWorkerThread, Options};

use super::local_state::LocalState;
use super::name_resolution::rename_filename_conflict;

pub fn recursive_cut(dest: &Path, src: &Path, options: &Options) -> std::io::Result<u64> {
    let mut dest_buf = dest.to_path_buf();
    if let Some(s) = src.file_name() {
        dest_buf.push(s);
    }
    rename_filename_conflict(&mut dest_buf);
    if !src.is_dir() {
        let metadata = src.metadata()?;
        if fs::rename(src, dest_buf.as_path()).is_err() {
            fs::copy(src, dest_buf.as_path())?;
            fs::remove_file(src)?;
        }
        Ok(metadata.len())
    } else {
        match fs::rename(src, dest_buf.as_path()) {
            Ok(_) => {
                let metadata = dest_buf.metadata()?;
                Ok(metadata.len())
            }
            Err(_) => {
                fs::create_dir(dest_buf.as_path())?;
                let mut total = 0;
                for entry in fs::read_dir(src)? {
                    let entry = entry?;
                    let entry_path = entry.path();
                    total += recursive_cut(dest_buf.as_path(), entry_path.as_path(), options)?;
                }
                fs::remove_dir(src)?;
                Ok(total)
            }
        }
    }
}

pub fn paste_cut(
    context: &mut JoshutoContext,
    options: Options,
) -> std::io::Result<IOWorkerThread> {
    let paths = LocalState::take_selected_files()
        .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "no files selected"))?;
    if paths.is_empty() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "no files selected",
        ));
    }

    let tab_dest = context.curr_tab_index;
    let thread_dest = context.tabs[tab_dest].curr_path.clone();
    let dest = thread_dest.clone();
    let src = paths[0].parent().unwrap().to_path_buf();

    let (tx_start, rx_start) = mpsc::channel();
    let (tx, rx) = mpsc::channel();

    let handle: thread::JoinHandle<std::io::Result<u64>> =
        thread::spawn(move || match rx_start.recv() {
            Ok(_) => {
                let mut total = 0;
                for path in paths {
                    total += recursive_cut(thread_dest.as_path(), path.as_path(), &options)?;
                    tx.send(total);
                }
                Ok(total)
            }
            Err(_) => Ok(0),
        });

    let thread = IOWorkerThread {
        src,
        dest,
        handle,
        tx_start,
        rx,
    };

    Ok(thread)
}