-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipboard.cpp
235 lines (196 loc) · 6.27 KB
/
Clipboard.cpp
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
#include <string>
#include <vector>
#include <tuple>
#include <cstddef>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
#include "file_functions.h"
#include "Clipboard.h"
using namespace std;
namespace fs = boost::filesystem;
bool Clipboard::add(const vector<string> &files, bool recursive, vector<string> &messages){
for(auto const file : files){
add(file, recursive, messages);
}
return true;
}
bool Clipboard::add(const string &filename, bool recursive, vector<string> &messages){
fs::path path(filename);
boost::system::error_code ec;
fs::file_status fstatus = fs::symlink_status(path, ec);
if(fs::status_known(fstatus)){
path = file_functions::getCanonicalPathToSymlink(path, ec);
}
if(ec.value() != boost::system::errc::success){
messages.push_back("cannot access " + filename + ": " + ec.message());
return false;
}
fs::path currentPath("");
TreeNode *current = tree_.get();
auto targetIt = --path.end(); // the iterator pointing to the filename
for(auto it = path.begin(); it != path.end(); ++it){
const fs::path &p = *it;
currentPath /= p;
if(current->recursive())
break;
// get the next node on the path
TreeNode::iterator childIt = current->find(p.string());
if(childIt == current->end()){ // child not found => create it
childIt = current->add(new TreeNode(p.string(), false));
}
current = childIt->second.get();
if(it == targetIt){
current->recursive(recursive);
current->inClipboard(true);
break;
}
}
return true;
}
bool Clipboard::remove(const vector<string> &files, bool recursive,
vector<string> &messages){
for(auto const file : files){
remove(file, recursive, messages);
}
return true;
}
bool Clipboard::remove(const string &filename, bool recursive, vector<string> &messages){
boost::system::error_code ec;
fs::path path = file_functions::getCanonicalPathToSymlink(filename, ec);
if(ec.value() != boost::system::errc::success){
path = file_functions::normalizePath(filename);
messages.push_back("cannot access " + filename + ": " + ec.message() +
"; using " + path.string());
}
fs::path currentPath("");
TreeNode *current = tree_.get();
auto targetIt = --path.end(); // the iterator pointing to the filename
for(auto it = path.begin(); it != path.end(); ++it){
const fs::path &p = *it;
if(current->recursive()){
messages.push_back(filename + " cannot be removed, because " +
currentPath.string() + " is marked as recursive");
return false;
}
TreeNode::iterator childIt = current->find(p.string());
if(childIt == current->end()) break;
if(it != targetIt){
current = childIt->second.get();
}else{
// we found the file we were looking for!
if(!childIt->second->empty() && !childIt->second->recursive() && !recursive){
// it has children, so we have to let it live
childIt->second->inClipboard(false);
}else{
current->remove(childIt);
}
}
currentPath /= p;
}
// clear parent directories that are not in the clipboard
while(current != nullptr && current->parent() != nullptr &&
current->empty() && !current->inClipboard()){
TreeNode *parent = current->parent();
parent->remove(current->name());
current = parent;
}
return true;
}
bool Clipboard::directoryListing(const fs::path &_path,
directoryListing_t &files, bool &recursive, vector<string> &messages){
boost::system::error_code ec;
fs::path path = fs::canonical(_path, ec);
if(ec.value() != boost::system::errc::success){
messages.push_back("cannot access " + _path.string() + ": " + ec.message());
return false;
}
fs::path currentPath("");
TreeNode *currentDir = tree_.get();
for(auto it = path.begin(); it != path.end(); ++it){
const fs::path &p = *it;
TreeNode::iterator childIt = currentDir->find(p.string());
if(childIt==currentDir->end())
return true;
currentPath /= p;
currentDir = childIt->second.get();
if(currentDir->recursive()){
recursive = true;
return true;
}
}
recursive = false;
for(auto const &f : *currentDir){
files.push_back(make_tuple(f.first, f.second->inClipboard(), f.second->recursive()));
}
return true;
}
bool Clipboard::stash(std::vector<std::string> &messages){
if(tree_->empty()){
messages.push_back("nothing to stash");
return false;
}
stash_.push_front(move(tree_));
tree_.reset(new TreeNode());
return true;
}
bool Clipboard::unstash(vector<string> &messages){
if(!tree_->empty()){
messages.push_back("cannot unstash: clipboard is not empty");
return false;
}
if(stash_.empty()){
messages.push_back("cannot unstash: no stash found");
return false;
}
tree_ = move(stash_.front());
stash_.pop_front();
return true;
}
bool Clipboard::unstash(size_t stashId, vector<string> &messages){
if(stashId==0)
return unstash(messages);
if(!tree_->empty()){
messages.push_back("cannot unstash: clipboard is not empty");
return false;
}
if(stash_.size()<stashId+1){
messages.push_back("cannot unstash: no stash #" + to_string(stashId));
return false;
}
tree_ = move(stash_[stashId]);
stash_.erase(stash_.begin() + stashId);
return true;
}
vector<string> Clipboard::listStash(){
vector<string> list;
for(const auto &tree : stash_){
list.push_back(lowestCommonAncestor(*tree.get()).string());
}
return move(list);
}
bool Clipboard::dropStash(vector<string> &messages){
if(stash_.empty()){
messages.push_back("cannot drop stash: no stash found");
return false;
}
stash_.pop_front();
return true;
}
bool Clipboard::dropStash(size_t stashId, vector<string> &messages){
if(stash_.size()<stashId+1){
messages.push_back("cannot drop stash #" + to_string(stashId) + ": no such stash");
return false;
}
stash_.erase(stash_.begin() + stashId);
return true;
}
fs::path Clipboard::lowestCommonAncestor(const TreeNode &tree){
fs::path path(tree.name());
const TreeNode *currentDir = &tree;
while(currentDir->size()==1 && !currentDir->inClipboard()){
const TreeNode *f = currentDir->begin()->second.get();
path /= f->name();
currentDir = f;
}
return path;
}