-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
162 lines (142 loc) · 4.95 KB
/
utils.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
/* Created by justanhduc on 11/6/20. */
#include <arpa/inet.h>
#include <cstring>
#include <fstream>
#include <sys/socket.h>
#include "utils.h"
Connection::Connection(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open())
logger.log(ERROR, __FILE__, __LINE__, "Cannot open host file");
else {
file >> host >> port;
}
}
void Environment::setEnv() {
auto itFlag = envFlags.begin();
auto itVal = envFlagVals.begin();
for (itFlag = envFlags.begin(), itVal = envFlagVals.begin();
(itFlag != envFlags.end()) && (itVal != envFlagVals.end());
++itFlag, ++itVal)
env[*itFlag] = *itVal;
}
void Environment::parseFlag(std::string flag) {
strings envs = splitString(std::move(flag), ";");
for (auto &it: envs) {
strings flagsVals = splitString(std::move(it), "=");
envFlags.push_back(flagsVals[0]);
envFlagVals.push_back(flagsVals[1]);
}
}
strings splitString(std::string s, const std::string &delimiter) {
strings ss;
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
ss.push_back(token);
s.erase(0, pos + delimiter.length());
}
ss.push_back(s);
return ss;
}
int Argument::parseOpts(int argc, strings &argv) {
int c;
int cmdIdx = 1;
int optionIdx = 0;
cstrings cargv = stringsToCstring(argv);
/* Parse options */
while (true) {
c = getopt_long(argc, cargv.data(), "H:", longOptions, &optionIdx);
if (c == -1) {
break;
}
switch (c) {
case 0:
if (strcmp(longOptions[optionIdx].name, "cd") == 0) {
currentDir = optarg;
cmdIdx += 2;
} else if (strcmp(longOptions[optionIdx].name, "env") == 0) {
env.parseFlag(optarg);
cmdIdx += 2;
} else if (strcmp(longOptions[optionIdx].name, "auto_server") == 0) {
cmdIdx += 1;
} else if (strcmp(longOptions[optionIdx].name, "exclude") == 0 ||
strcmp(longOptions[optionIdx].name, "sync") == 0 ||
strcmp(longOptions[optionIdx].name, "host") == 0 ||
strcmp(longOptions[optionIdx].name, "sync_dest") == 0 ||
strcmp(longOptions[optionIdx].name, "include") == 0 ||
strcmp(longOptions[optionIdx].name, "ln") == 0) {
cmdIdx += 2;
} else if (strcmp(longOptions[optionIdx].name, "kill") == 0) {
action = KILL_SERVER;
}
break;
case 'H':
cmdIdx += 2;
break;
}
}
return cmdIdx;
}
cstrings stringsToCstring(strings &str) {
cstrings cstrings;
cstrings.reserve(str.size());
for (auto &s: str)
cstrings.push_back(&s[0]);
return cstrings;
}
void sendString(int fd, const std::string &string) {
int res;
uint32_t size = htonl(string.length());
res = send(fd, &size, sizeof(uint32_t), MSG_CONFIRM);
if (res < 0)
logger.log(ERROR, __FILE__, __LINE__, "Cannot send package");
res = send(fd, string.c_str(), string.size(), MSG_CONFIRM);
if (res < 0)
logger.log(ERROR, __FILE__, __LINE__, "Cannot send package");
}
std::string recvString(int fd) {
int res;
uint32_t size;
res = recv(fd, &size, sizeof(uint32_t), 0);
if (res == -1)
logger.log(ERROR, __FILE__, __LINE__, "Cannot receive package");
size = ntohl(size);
std::vector<char> rcvBuf; // Allocate a receive buffer
rcvBuf.resize(size, 0x00); // with the necessary size
res = recv(fd, &(rcvBuf[0]), size, 0); // Receive the string data
if (res == -1)
logger.log(ERROR, __FILE__, __LINE__, "Cannot receive package");
std::string string;
string.assign(&(rcvBuf[0]), rcvBuf.size());
return string;
}
std::string buildTsCommand(strings &argv, int index) {
std::string cmd = "ts ";
for (auto it = argv.begin() + index; it != argv.end(); ++it) {
std::string tmp(it->c_str()); // truncate \00 in it
cmd += tmp;
cmd += " ";
}
return cmd;
}
void Environment::setGetoptEnv() {
env["POSIXLY_CORRECT_BAK"] = env["POSIXLY_CORRECT"];
env["POSIXLY_CORRECT"] = "YES";
}
void Environment::unsetGetoptEnv() {
env["POSIXLY_CORRECT"] = env["POSIXLY_CORRECT_BAK"];
env.erase("POSIXLY_CORRECT_BAK");
}
bool isBooted(boost::asio::io_context &io_context,
const std::string &filename) {
Argument arg(filename);
boost::system::error_code error;
tcp::socket socket(io_context);
boost::asio::ip::address ip_address =
boost::asio::ip::address::from_string("127.0.0.1", error);
tcp::endpoint endpoint(ip_address, arg.conn.port);
socket.connect(endpoint, error);
return !(error);
}