aboutsummaryrefslogtreecommitdiff
path: root/src/cmd_fsops.cc
blob: 5133313d9eb30cdb167dfea1d5e4bd73d4c214cb (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
#include "cmd.h"
#include "utils.h"
#include "entry.h"
#include <filesystem>
#include <sstream>

namespace fs = std::filesystem;

namespace cmd {
    std::string list(const fs::path& p) {
        std::stringstream text;
        if(p.empty()) {
            // Print read-only dirs
            for(std::string name : getVirtDirs()) {
                text << name << "/ (read only)" << std::endl;
            }
        }
        fs::path truePath = getTruePath(p);
        if(fs::directory_entry(truePath).is_regular_file()) {
            text << utils::instantiate<entry::Entry>(truePath)->getText() << std::endl;
        }
        else if(fs::directory_entry(truePath).is_directory()) {
            for(fs::directory_entry de : fs::directory_iterator(truePath)) {
                if(de.is_directory()) {
                    text << de.path().filename().string() << "/" << std::endl;
                } else {
                    text << de.path().stem().string() << std::endl;
                }
            }
        }
        else {
            text << "Unknown path " << p << std::endl;
        }
        return text.str();
    }

    std::string list(std::vector<std::string> args) {
        std::stringstream text;
        if(args.empty()) {
            text << list("");
        } else {
            for(std::string dir : args) {
                text << list(dir);
            }
        }
        return text.str();
    }

    std::string mkdir(std::vector<std::string> args) {
        for(std::string s : args) {
            fs::create_directories(getTruePath(s));
        }
        return "";
    }

    void cp(fs::path src, fs::path dest) {
        if(fs::directory_entry(src).is_regular_file()) {
            utils::saveJson(utils::instantiate<entry::Entry>(src)->serialize(), dest);
        } else {
            mkdir({dest});
            for(fs::directory_entry de : fs::directory_iterator(src)) {
                cp(de.path(), dest / de.path().filename());
            }
        }
    }

    std::string cp(std::vector<std::string> args) {
        // Operate by intantiating and saving
        // We do recursive!
        cp(getTruePath(args[0]), getTruePath(args[1]));
        return "";
    }

    std::string mv(std::vector<std::string> args) {
        fs::rename(getTruePath(args[0]), getTruePath(args[1]));
        return "";
    }

    std::string rm(std::vector<std::string> args) {
        for(std::string s : args) {
            fs::remove_all(getTruePath(s));
        }
        return "";
    }
}