-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestoreSnapShot.cpp
316 lines (221 loc) · 6.75 KB
/
restoreSnapShot.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <fcntl.h> // for open()
#include <pwd.h>
#include <vector>
#include <time.h>
#include <fstream>
#include <string.h>
#include <unistd.h> //for getcwd()
#include <dirent.h> // opendir()
#include <sys/stat.h> // for stat
#include <thread>
#include <pthread.h>
#include <bits/stdc++.h>
#include <sstream>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include "compareSnapshot.h"
#define MDPath "./MetaDataFile/All_snapshot_details.txt"
#define MDLogFilePath "./LogFile/logfile.txt"
using namespace std;
class RestoreSnapShot{
public:
bool toLog = true;
char * getCurrentTime()
{
std::time_t result = std::time(nullptr);
return std::asctime(std::localtime(&result));
}
bool writeLog(string Data,int flag)
{
if(toLog==0)
return false;
bool success = false;
if(Data!="")
{
std::ofstream out;
out.open(MDLogFilePath, std::ios::app);
string strDate(getCurrentTime());
if(flag == 1)
{
out << "Information : " ;
}
if(flag == -1)
{
out << "Error : " ;
}
out << strDate << ":" << Data << endl;
out.close();
}
return success;
}
vector<string> split(char *phrase, string delimiter){
vector<string> list;
string s = string(phrase);
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != string::npos) {
token = s.substr(0, pos);
list.push_back(token);
s.erase(0, pos + delimiter.length());
}
list.push_back(s);
return list;
}
void processRestoreSnapShot(string sourcePath,string destinationPath){
SyncData syncDataObj;
string line;
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("Current working dir: %s\n", cwd);
}
syncDataObj.createSnapshotFunctionality(toCharArrayFromString(sourcePath),toCharArrayFromString(sourcePath));
vector<string> fileLines;
string sPath = sourcePath+"/.snapshot";
ifstream snapshot(sPath);
if(snapshot.is_open()){
while(getline(snapshot,line)){
fileLines.push_back(line);
}
}
fileLines[0] = destinationPath;
snapshot.close();
ofstream writeSnapshot;
writeSnapshot.open(sPath);
for(int i=0;i<fileLines.size();i++){
writeSnapshot<<fileLines[i]<<endl;
}
writeSnapshot.close();
// syncDataObj.createSnapshotFunctionality(toCharArrayFromString(destinationPath),toCharArrayFromString(destinationPath));
// syncDataObj.createSnapshotFunctionality(toCharArrayFromString(sourcePath),toCharArrayFromString(destinationPath));
// int status = remove((char *)sPath.c_str());
// if(status == -1){
// cout<<"Error while deleting .snapshot file in: "<<sPath<<endl;
// }
chdir(cwd);
vector<compareSnapshot> diffList;
// if(getcwd(cwd, sizeof(cwd)) != NULL)
// {
// printf("Current working dir: %s\n", cwd);
// }
diffList = syncDataObj.compareSnapshotFile(destinationPath,sourcePath);
syncDataObj.runTasks(diffList);
}
string getSnapShotLoaction(string source){
ifstream metadata(MDPath);
string destination = "";
string line;
if(metadata.is_open()){
while(getline(metadata,line)){
char *fileLine = new char [line.length()+1];
strcpy(fileLine, line.c_str());
vector<string> tokens = split(fileLine,"\t");
if(tokens[0] == source){
destination = tokens[1];
metadata.close();
return destination;
}
}
}else{
cout<<"Error opening metadata file\n";
}
metadata.close();
return destination;
}
bool restoreSnapShotInDestination(string strFolderPath){
remove(toCharArrayFromString(toCharArrayFromString(strFolderPath+"/.snapshot")));
// rename(toCharArrayFromString(strFolderPath+"/__tempBuffer_2.txt"),toCharArrayFromString(strFolderPath+"/.snapshot"));
rename(toCharArrayFromString(strFolderPath+"/__tempBuffer_2.txt"),toCharArrayFromString(strFolderPath+"/.snapshot"));
return true;
}
void restore(string source){
string destination = getSnapShotLoaction(source);
cout<<"destination "<<destination<<"\n";
if(!CreateTemporaryFileForSnapShot(destination)){
writeLog("Swapping .snapshot file ",-1);
}
if(destination!=""){
processRestoreSnapShot(source,destination);
}else{
cout<<"No snapshot found\n";
}
//restoring .snapshot file in destination folder
if(remove(toCharArrayFromString(source+"/.snapshot"))!=0){
writeLog("Source snapshot file remove error",-1);
}
if(!restoreSnapShotInDestination(destination)){
writeLog("Re-swapping .snapshot file ",-1);
}
}
//strPath - snapshot file path
// strFolderPath - snapshot file folder Path
bool CreateTemporaryFileForSnapShot(string strFolderPath)
{
string deleteline;
std::string line;
string strPath = strFolderPath+"/.snapshot";
std::ifstream file(strPath);
ofstream temp;
temp.open(strFolderPath+"/__tempBuffer.txt");
ofstream temp2;
temp2.open(strFolderPath+"/__tempBuffer_2.txt");
vector<string> tokens;
bool firstLine=false;
vector<string> fileContent;
while (getline(file,line))
{
fileContent.push_back(line);
temp2 << line << endl;
}
string tempLine = fileContent[0];
fileContent[0]=fileContent[1];
fileContent[1]=tempLine;
for(auto lineContent : fileContent)
{
temp << lineContent << endl;
}
temp.close();
temp2.close();
file.close();
char * strPath1;
strPath1 = (char*)malloc(strFolderPath.length()+1);
strcpy(strPath1,strFolderPath.c_str());
remove(toCharArrayFromString(toCharArrayFromString(strFolderPath+"/.snapshot")));
// rename(toCharArrayFromString(strFolderPath+"/__tempBuffer_2.txt"),toCharArrayFromString(strFolderPath+"/.snapshot"));
rename(toCharArrayFromString(strFolderPath+"/__tempBuffer.txt"),toCharArrayFromString(strFolderPath+"/.snapshot"));
return true;
}
char* toCharArrayFromString(string str)
{
// char charStr[str.length()+1];
char *charStr;
charStr = (char*)malloc(str.length()+1);
strcpy(charStr,str.c_str());
return charStr;
}
};
int main(int argc,char *argv[]){
// string source="/home/prakashjha/os/workarea/OS_Snapshot/repoSnapShot";
RestoreSnapShot restoreSnapShotObj;
restoreSnapShotObj.writeLog("---Entering into restoreSnapshot main function",1);
// string source;
// cout<<"enter source path\n";
// cin>>source;
if(argc<2){
restoreSnapShotObj.writeLog("Invalid input format",-1);
return -1;
}
restoreSnapShotObj.writeLog(to_string(argc),1);
restoreSnapShotObj.writeLog(argv[1],1);
string source(argv[1]);
//Step 1 :Fetch Destination Path From Metadata
// Step 2: Make SnapShot in reverse Order
//Step 3 : Call Compare SnapShot and runTask
// RestoreSnapShot restoreSnapShotObj;
restoreSnapShotObj.restore(source);
restoreSnapShotObj.writeLog("---Exiting from restoreSnapshot main function",1);
return 0;
}