-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.cpp
98 lines (79 loc) · 1.87 KB
/
push.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
namespace gitpush
{
#include <unistd.h>
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include <dirent.h>
#include <iostream>
#define VERSION_FILE "version_no.txt"
#define INDEX_FILE "index.txt"
using namespace std;
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;
}
void cp(string src, string des)
{
ifstream fin;
ofstream fout;
fin.open(src, ios::in);
fout.open(des, ios::out);
string line;
while (fin)
{
getline(fin, line);
fout << line << endl;
}
fin.close();
fout.close();
}
int push()
{
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
perror("Unable to get current working directory!\n");
exit(1);
}
string push_path_dir = string(cwd) + "/push";
int mkdir_stat = mkdir(push_path_dir.c_str(), 0777);
if (mkdir_stat != 0 && errno != EEXIST)
{
cerr << "unable to make push directory " << strerror(errno) << endl;
return 0;
}
string pull_path_dir = string(cwd) + "/pull";
vector<string> filelist = get_files_from(pull_path_dir);
for (auto f : filelist)
{
string src = pull_path_dir + "/" + f;
string des = push_path_dir + "/" + f;
cp(src, des);
}
return 0;
}
}