-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.cpp
255 lines (214 loc) · 8.74 KB
/
manager.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "manager.h"
/*
* manager::manager():
* Initializes _symlinks, _encrypts, _repo
*/
manager::manager(config &configfile) :
_symlinks(configfile.get_folder_path(), "symlinks", std::map<std::string, std::string>()),
_encrypts(configfile.get_folder_path(), "encrypts", std::map<std::string, std::string>()),
_repo(configfile){
_gpg_recepient = configfile.config_values["gpg_recepient_name"];
}
/*
* manager::add_entry():
* adds an entry in _symlinks and sets up symlink to the file
*/
void manager::add_entry(const std::string &source, const std::string &label) {
bool entry_present = false;
//Check if entry with same label is present
if(!_symlinks.config_values[label].empty()){
std::cout << "Label already exists for " << _symlinks.config_values[label] << std::endl;
entry_present = true;
}
//Check if entry with same path is present
else{
for(auto p: _symlinks.config_values){
if(p.second == source){
std::cout << "Entry already exists for label " << p.first << std::endl;
entry_present = true;
break;
}
}
}
//If no conflicting entry is present
if(!entry_present){
//Update map and write configuration to file
_symlinks.config_values[label] = source;
_symlinks.write_config_file();
//ABSOLUTE path to where file has to be moved
std::string destination_path = _repo.path() + source.substr(1, source.length() - 1);
size_t i = destination_path.length() - 1, filename_size = 0;
while(destination_path[i] != '/'){
--i;
++filename_size;
}
++filename_size;
//Setup path if it does not exist
system(std::string("mkdir -p " + destination_path.substr(0, destination_path.length() - filename_size)).c_str());
//Move file to destination path
system(std::string("mv " + source + " " + destination_path).c_str());
//Create symlink
system(std::string("ln -s " + destination_path + " " + source).c_str());
std::cout << "Entry with label " << label << " at " << source << " added to tracking list" << std::endl;
update();
}
}
void manager::add_encrypted_entry(const std::string &source, const ::std::string &label) {
bool entry_present = false;
//Check if entry with same label is present
if(!_encrypts.config_values[label].empty()){
std::cout << "Label already exists for " << _encrypts.config_values[label] << std::endl;
entry_present = true;
}
//Check if entry with same path is present
else{
for(auto p: _encrypts.config_values){
if(p.second == source){
std::cout << "Entry already exists for label " << p.first << std::endl;
entry_present = true;
break;
}
}
}
//If no config entry is present
if(!entry_present){
//Update map and write configuration to file
_encrypts.config_values[label] = source;
_encrypts.write_config_file();
//ABSOLUTE path to where file has to be moved
std::string destination_path = _repo.path() + source.substr(1, source.length() - 1);
size_t i = destination_path.length() - 1, filename_size = 0;
while(destination_path[i] != '/'){
--i;
++filename_size;
}
++filename_size;
//Setup path if it does not exist
system(std::string("mkdir -p " + destination_path.substr(0, destination_path.length() - filename_size)).c_str());
destination_path += ".gpg";
//Copy encrypted version of configfile to repo
encrypt(_gpg_recepient, source, destination_path);
std::cout << "Encrypted entry with label " << label << " at " << source << " added to tracking list" << std::endl;
update();
}
}
/*
* manager::remove_entry():
* removes entry in _symlinks and restores file to original state
*/
void manager::remove_entry(const std::string &label) {
bool entry_found = false;
for(auto &p: _symlinks.config_values){
if(p.first == label) {
//path: Original ABSOLUTE path of file
auto path = p.second;
entry_found = true;
//remove symlink
system(std::string("rm " + path).c_str());
//destination: ABSOLUTE path where file is stored in repo
std::string destination_path = _repo.path() + path.substr(1, path.length() - 1);
//move file back to original path
system(std::string("mv " + destination_path + " " + path).c_str());
//untrack git file
//_repo.git_rm(destination_path);
//erase entry and update file
_symlinks.config_values.erase(label);
_symlinks.write_config_file();
std::cout << "Entry with label " << label << " at " << path << " removed from tracking list" << std::endl;
update();
break;
}
}
if(!entry_found){
std::cout << "No entry with label " << label << " found" << std::endl;
}
}
void manager::remove_encrypted_entry(const ::std::string &label) {
bool entry_found = false;
for(auto &p: _encrypts.config_values){
if(p.first == label) {
//path: Original ABSOLUTE path of file
auto path = p.second;
entry_found = true;
//destination: ABSOLUTE path where file is stored in repo
std::string destination_path = _repo.path() + path.substr(1, path.length() - 1) + ".gpg";
//move file back to original path
system(std::string("rm " + destination_path).c_str());
//untrack git file
//_repo.git_rm(destination_path);
//erase entry and update file
_encrypts.config_values.erase(label);
_encrypts.write_config_file();
std::cout << "Encrypted entry with label " << label << " at " << path << " removed from tracking list" << std::endl;
update();
break;
}
}
if(!entry_found){
std::cout << "No encrypted entry with label " << label << " found" << std::endl;
}
}
/*
* manager::update():
* encrypts all files set to encryption and
* adds, commits and pushes all changes to remote
*/
void manager::update() {
//Encrypt and copy all files set in _encrypts
for(auto &p: _encrypts.config_values){
std::string source = p.second;
if(source.length() > 1) {
//ABSOLUTE path to where file has to be moved
std::string destination_path = _repo.path() + source.substr(1, source.length() - 1) + ".gpg";
//Remove old version of encrypted file
system(std::string("rm " + destination_path).c_str());
//Copy encrypted version of configfile to repo
encrypt(_gpg_recepient, source, destination_path);
}
}
_repo.git_add();
_repo.git_commit();
_repo.git_push();
}
/*
* manager::deploy_all():
* symlinks all entries in _symlink to their positions. Used for rapid deployment
*/
void manager::deploy_all() {
//Create symlinks for all normal config files
for(auto &p: _symlinks.config_values) {
if (p.second.length() > 1) {
//If any other file exists at that path delete it
if(fexists(p.second)) {
system(std::string("rm " + p.second).c_str());
}
//destination: ABSOLUTE path where file is stored in repo
std::string destination_path = _repo.path() + p.second.substr(1, p.second.length() - 1);
//Create symlink
system(std::string("ln -s " + destination_path + " " + p.second).c_str());
std::cout << "Entry with label " << p.first << " at " << p.second << " recreated" << std::endl;
}
}
//Decrypt and copy back all encrypted files
for(auto &p: _encrypts.config_values){
if(p.second.length() > 1){
//If any other file exists at that path delete it
if(fexists(p.second)) {
system(std::string("rm " + p.second).c_str());
}
//destination: ABSOLUTE path where encrypted file is stored in repo
std::string destination_path = _repo.path() + p.second.substr(1, p.second.length() - 1) + ".gpg";
//Create decrypted file at p.second
decrypt(p.second, destination_path);
std::cout << "Encrypted entry with label " << p.first << " at " << p.second << " recreated" << std::endl;
}
}
}
/*
* manager::pass_to_git():
* passes unhandled commands directly to git
*/
void manager::pass_to_git(const std::string &command) {
std::cout << "git: " << std::flush;
system(std::string("git -C " + _repo.path() + " " + command).c_str());
}