-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback.hpp
193 lines (164 loc) · 4.58 KB
/
rollback.hpp
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
namespace gitrollback
{
#include <iostream>
#include <bits/stdc++.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <sys/types.h>
#include <fstream>
using namespace std;
#define INDEX_FILE "index.txt"
#define VERSION_FILE "version_no.txt"
string current_path_global;
char cwd[PATH_MAX];
string mygit_path;
string vcpath;
string vc;
string indexpath;
vector<string> filelist;
map<string, string> filemap;
using namespace std;
map<string, string> get_map_from(string path)
{ cout<<"entered getmap func"<<endl;
map<string, string> toret;
fstream f(path, std::ios_base::in);
if (f.is_open())
{
string line;
while (getline(f, line))
{
stringstream ss(line);
string filename = "";
string sha = "";
ss >> filename;
ss >> sha;
toret[filename] = sha;
}
}
else
{
perror("Unable to open file information get_map_from function");
exit(1);
}
return toret;
}
vector<string> get_files_from(string path)
{
vector<string> toret;
struct dirent *entry;
DIR *dir = opendir(path.c_str());
if (dir == NULL)
{
cout << "Error in opening Directory";
exit(EXIT_FAILURE);
}
while ((entry = readdir(dir)) != NULL)
{
//cout << entry->d_name << endl;
string fname = entry->d_name;
if (fname == "." || fname == ".." || fname == ".mygit" || fname == "a.out" || fname == ".vscode" || fname=="mygit")
{
continue;
}
else
{
toret.push_back(fname);
}
}
closedir(dir);
return toret;
}
//2 -> vno >= currVer, 1 -> vno<currver and succ terminated
int roll_back(int curr_ver)
{
int to_ver_no = curr_ver-1;
string lastverno = to_string(to_ver_no);
mygit_path = "";
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
mygit_path = cwd;
mygit_path += "/.mygit/";
}
else
{
perror("Unable to get current working directory!\n");
exit(1);
}
vcpath = mygit_path + VERSION_FILE;
fstream vcfile(vcpath, std::ios_base::in);
vcfile >> vc;
indexpath = mygit_path + vc + "/" + INDEX_FILE;
string currverpath = mygit_path+vc;
DIR* folder = opendir(currverpath.c_str());
struct dirent *next_file;
char filepath[PATH_MAX];
while ( (next_file = readdir(folder)) != NULL )
{
// build the path for each file in the folder
sprintf(filepath, "%s/%s", currverpath.c_str(), next_file->d_name);
remove(filepath);
}
closedir(folder);
string lastverpath = mygit_path+lastverno;
string cmd2 = "cp -a "+lastverpath+"/. "+currverpath;
system(cmd2.c_str());
filemap = get_map_from(indexpath);
current_path_global = mygit_path+"global/";
for (auto i=filemap.begin();i!=filemap.end();i++){
string ifirst = i->first;
string sourcefilepath = current_path_global+ifirst;
string destfilepath = currverpath+"/"+ifirst;
ifstream ifile(sourcefilepath, ios::in);
ofstream ofile(destfilepath, ios::out);
if (!ifile.is_open()) {
cout << "file not found";
}
else {
ofile << ifile.rdbuf();
}
}
//run patch command for all patch fikes
for (auto i=filemap.begin();i!=filemap.end();i++){
string ifirst = i->first;
string sourcefilepath = currverpath+"/"+ifirst;
string patchfilepath = currverpath+"/"+ifirst+".patch";
string cmd = "patch "+sourcefilepath + " " + patchfilepath;
system(cmd.c_str());
}
for (auto i=filemap.begin();i!=filemap.end();i++){
string ifirst = i->first;
string patchfilepath = currverpath+"/"+ifirst+".patch";
remove(patchfilepath.c_str());
}
folder = opendir(currverpath.c_str());
char fpathlocal[PATH_MAX];
while ( (next_file = readdir(folder)) != NULL )
{
// build the path for each file in the folder
sprintf(fpathlocal, "%s/%s", cwd, next_file->d_name);
if(fpathlocal=="a.out") continue;
remove(fpathlocal);
}
cout<<"yaha ho gya!!"<<endl;
closedir(folder);
for (auto i=filemap.begin();i!=filemap.end();i++){
string ifirst = i->first;
if(ifirst=="index.txt")continue;
string sourcefilepath = currverpath+"/"+ifirst;
string cwds =cwd;
string destfilepath = cwds+"/"+ifirst;
ifstream ifile(sourcefilepath, ios::in);
ofstream ofile(destfilepath, ios::out);
if (!ifile.is_open()) {
cout << "file not found";
}
else {
ofile << ifile.rdbuf();
}
}
return 1;
}
}
}