Skip to content

Commit bdd42a0

Browse files
committed
delete feature
1 parent 196f02a commit bdd42a0

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

server.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ void* processClient(void* arg) {
137137
continue;
138138
}
139139
session->commit(command[1], connFd, command[2]);
140+
} else if(command[0] == "delete") {
141+
if(command.size() != 3) {
142+
write(connFd, "Usage: delete <filename.txt>", 28);
143+
continue;
144+
}
145+
session->deleteFile(command[1], connFd, command[2]);
140146
} else {
141147
cout << "Enter a valid command!" << endl;
142148
}

server_session.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ void ServerSession::addFile(string filename, int connFd, string username) {
209209
commit(filename, connFd, username);
210210
}
211211

212+
void ServerSession::deleteFile(string filename, int connFd, string username) {
213+
pthread_mutex_lock(&sessionLock);
214+
if(filesMap.find(filename) == filesMap.end()) {
215+
write(connFd, "Given filename does not exists!", 29);
216+
pthread_mutex_unlock(&sessionLock);
217+
return;
218+
}
219+
220+
FileMetaData fileMetaData = filesMap[filename];
221+
if(remove(fileMetaData.path.c_str()) == 0)
222+
write(connFd, "OK", 2);
223+
else
224+
write(connFd, "Err: Unable to delete file", 26);
225+
pthread_mutex_unlock(&sessionLock);
226+
}
227+
212228
string ServerSession:: listall() {
213229
unordered_map<string, FileMetaData>:: iterator it;
214230
string resp = "";

server_session.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ServerSession {
3434
const char* authenticateUser(User, string);
3535
void checkout(string, int, string);
3636
void commit(string, int, string);
37+
void deleteFile(string, int, string);
3738
void addFile(string, int, string);
3839
void quit(int, string);
3940
string listall();

0 commit comments

Comments
 (0)