@@ -90,6 +90,22 @@ class client {
9090};
9191
9292class 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
238246int 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) {
0 commit comments