Skip to content

Commit 95bd2dd

Browse files
committed
Update
1 parent 6032ebd commit 95bd2dd

File tree

5 files changed

+59
-62
lines changed

5 files changed

+59
-62
lines changed

client.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ class project {
127127
it.key().c_str());
128128
owner.sock.send("removeDir " + path + it.key());
129129
owner.sock.send("createFile " + path + it.key());
130-
owner.sock.sendFile(prjPath + path + it.key(), progressBar);
130+
if (owner.sock.recv() == "ok")
131+
owner.sock.sendFile(prjPath + path + it.key(),
132+
&progressBar);
131133
} else if (newjs[it.key()].is_object()) {
132134
compJson(it.value(), newjs[it.key()],
133135
path + it.key() + '/');
134136
} else if (it.value() != newjs[it.key()]) {
135137
printw("File ./%s%s was changed.\n", path.c_str(),
136138
it.key().c_str());
137139
owner.sock.send("createFile " + path + it.key());
138-
owner.sock.sendFile(prjPath + path + it.key(), progressBar);
140+
if (owner.sock.recv() == "ok")
141+
owner.sock.sendFile(prjPath + path + it.key(),
142+
&progressBar);
139143
}
140144
} else {
141145
if (it.value().is_object() || it.value().is_null()) {
@@ -161,7 +165,9 @@ class project {
161165
printw("File ./%s%s was created.\n", path.c_str(),
162166
it.key().c_str());
163167
owner.sock.send("createFile " + path + it.key());
164-
owner.sock.sendFile(prjPath + path + it.key(), progressBar);
168+
if (owner.sock.recv() == "ok")
169+
owner.sock.sendFile(prjPath + path + it.key(),
170+
&progressBar);
165171
}
166172
}
167173
}
@@ -213,7 +219,7 @@ class project {
213219
fs::create_directory(prjPath + path);
214220
} else if (action == "createFile") {
215221
printw("File %s was created\n", path.c_str());
216-
owner.sock.recvFile(prjPath + path, progressBar);
222+
owner.sock.recvFile(prjPath + path, &progressBar);
217223
}
218224
}
219225
}

server.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class client {
9090
};
9191

9292
class project {
93+
private:
94+
void downloadHelp(const string& path) {
95+
for (const fs::directory_entry& entry : fs::directory_iterator(path)) {
96+
string filePath = entry.path().string();
97+
if (entry.is_directory()) {
98+
owner.sock.send("createDir " +
99+
filePath.substr(prjPath.length()));
100+
downloadHelp(filePath);
101+
} else {
102+
owner.sock.send("createFile " +
103+
filePath.substr(prjPath.length()));
104+
owner.sock.sendFile(filePath);
105+
}
106+
}
107+
}
108+
93109
public:
94110
client owner;
95111
string prjPath;
@@ -167,18 +183,21 @@ class project {
167183
sqlite3_finalize(stmt);
168184
while ((command = owner.sock.recv())[0] != '{') {
169185
string action = command.substr(0, command.find(' '));
170-
if (action == "createDir")
171-
fs::create_directory(
172-
prjPath + command.substr(command.find(' ') + 1));
173-
else if (action == "removeDir")
174-
fs::remove_all(prjPath +
175-
command.substr(command.find(' ') + 1));
176-
else if (action == "createFile")
177-
owner.sock.recvFile(
178-
prjPath + command.substr(command.find(' ') + 1));
179-
else if (action == "removeFile")
180-
fs::remove(prjPath +
181-
command.substr(command.find(' ') + 1));
186+
string path =
187+
prjPath + command.substr(command.find(' ') + 1);
188+
if (path.find("../") != string::npos)
189+
owner.sock.send("not ok");
190+
else {
191+
owner.sock.send("ok");
192+
if (action == "createDir")
193+
fs::create_directory(path);
194+
else if (action == "removeDir")
195+
fs::remove_all(path);
196+
else if (action == "createFile")
197+
owner.sock.recvFile(path);
198+
else if (action == "removeFile")
199+
fs::remove(path);
200+
}
182201
}
183202
sqlite3_prepare_v2(db,
184203
"update projects set dirTree=? where id=?;",
@@ -191,19 +210,9 @@ class project {
191210
}
192211
}
193212

194-
void download(const string& path) {
195-
for (const fs::directory_entry& entry : fs::directory_iterator(path)) {
196-
string filePath = entry.path().string();
197-
if (entry.is_directory()) {
198-
owner.sock.send("createDir " +
199-
filePath.substr(prjPath.length()));
200-
download(filePath);
201-
} else {
202-
owner.sock.send("createFile " +
203-
filePath.substr(prjPath.length()));
204-
owner.sock.sendFile(filePath);
205-
}
206-
}
213+
void download() {
214+
downloadHelp(prjPath);
215+
owner.sock.send("done");
207216
}
208217

209218
project(client& x, const string& y) : owner(x), prjPath(y) {}
@@ -227,17 +236,15 @@ void handleClient(client client, sqlite3* db, string path) {
227236
project.open(db);
228237
} else if (command == "downloadPrj") {
229238
project.set(db);
230-
project.download(project.prjPath);
231-
client.sock.send("done");
239+
project.download();
232240
project.open(db);
233241
}
234242
close(client.sock.sock);
235243
cout << "[-] Client disconnected." << endl;
236244
}
237245

238246
int main(int argc, char* argv[]) {
239-
string path(argv[0]);
240-
path = path.substr(0, path.rfind('/') + 1);
247+
string path = fs::canonical(argv[0]).parent_path().string() + '/';
241248

242249
sqlite3* db;
243250
if (sqlite3_open((path + "users.db").c_str(), &db) != SQLITE_OK) {

sr.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ class clsSock {
1717
::send(sock, msg.c_str(), msgSize, 0);
1818
}
1919
string recv() {
20+
short msgSize;
21+
::recv(sock, (char*)&msgSize, sizeof(msgSize), 0);
2022
string res;
2123
char buffer[1024];
22-
::recv(sock, buffer, sizeof(short), 0);
23-
short msgSize;
24-
memcpy(&msgSize, buffer, sizeof(short));
2524
while (msgSize) {
2625
memset(buffer, 0, sizeof(buffer));
2726
msgSize -=
@@ -48,6 +47,7 @@ class clsSock {
4847
memset(buffer, 0, sizeof(buffer));
4948
if (callback) callback(fileSize - bytesLeft, fileSize);
5049
}
50+
input.close();
5151
}
5252

5353
void recvFile(const string& path,
@@ -59,12 +59,13 @@ class clsSock {
5959
long bytesLeft = fileSize;
6060
while (bytesLeft) {
6161
memset(buffer, 0, sizeof(buffer));
62-
int bytesReceived =
63-
::recv(sock, buffer, min(bytesLeft, (long)sizeof(buffer)), 0);
64-
output.write(buffer, bytesReceived);
65-
bytesLeft -= bytesReceived;
62+
int bytesToRecv = min(bytesLeft, (long)sizeof(buffer));
63+
::recv(sock, buffer, bytesToRecv, MSG_WAITALL);
64+
output.write(buffer, bytesToRecv);
65+
bytesLeft -= bytesToRecv;
6666
if (callback) callback(fileSize - bytesLeft, fileSize);
6767
}
68+
output.close();
6869
}
6970

7071
clsSock() : sock(socket(AF_INET, SOCK_STREAM, 0)) {}

test/downtest.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main(int argc, char* argv[]) {}

0 commit comments

Comments
 (0)