-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_queries.c
346 lines (297 loc) · 8.89 KB
/
helper_queries.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "helper.h"
void view_files(int clientSocket)
{
char buffer[MAX_SIZE];
ssize_t bytesRead;
char fileNames[MAX_FILES][MAX_FILENAME_SIZE];
int fileSizes[MAX_FILES];
int fileCount = 0;
int totalSize = 0;
while ((bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0)) > 0)
{
buffer[bytesRead] = '\0';
char *line = strtok(buffer, "\n");
while (line != NULL)
{
char fileName[MAX_FILENAME_SIZE];
int fileSize;
if (sscanf(line, "%s - %d", fileName, &fileSize) == 2)
{
strncpy(fileNames[fileCount], fileName, MAX_FILENAME_SIZE);
fileSizes[fileCount] = fileSize;
totalSize += fileSize;
fileCount++;
}
line = strtok(NULL, "\n");
}
}
if (bytesRead < 0)
{
perror("Error receiving data");
}
else
{
printf("\nFile data received successfully\n\n");
for (int i = 0; i < fileCount; i++)
{
printf("File Name: %s - FileSize: %d\n", fileNames[i], fileSizes[i]);
}
printf("\nTotal File Size: %d\n\n", totalSize);
}
}
void downloadFile(int clientSocket, const char *fileName)
{
ssize_t sentBytes = send(clientSocket, fileName, strlen(fileName), 0);
if (sentBytes < 0)
{
perror("Error sending file name to server");
return;
}
printf("Receiving file data for: %s\n", fileName);
char *encoded_data = (char *)malloc(MAX_SIZE);
if (encoded_data == NULL)
{
perror("Memory allocation failed");
return;
}
ssize_t total_bytes_received = 0;
ssize_t bytesRead;
FILE *tempFile = fopen("tempfile", "w");
if (tempFile == NULL)
{
perror("Error opening temp file for writing");
free(encoded_data);
return;
}
while ((bytesRead = recv(clientSocket, encoded_data + total_bytes_received, MAX_SIZE - total_bytes_received, 0)) > 0)
{
fwrite(encoded_data + total_bytes_received, 1, bytesRead, tempFile);
total_bytes_received += bytesRead;
}
fclose(tempFile);
if (bytesRead < 0)
{
perror("Error receiving file data");
free(encoded_data);
return;
}
printf("\nDecoding and writing file: %s\n", fileName);
int received_file_size = getDecodedFileSize("tempfile");
//printf("\n%d\n", received_file_size);
char *decoded_data = rle_decode(encoded_data, received_file_size, fileName); // Decoding data and writing to final file
if (decoded_data == NULL)
{
printf("Error decoding received data.\n");
}
else
{
printf("File downloaded and saved successfully.\n");
}
free(encoded_data);
free(decoded_data);
}
void uploadFile(int clientSocket, const char *filePath)
{
int encoded_length;
char *encoded = rle_encode(filePath, &encoded_length);
if (encoded == NULL)
{
perror("Error encoding file");
return;
}
const char *fileName = strrchr(filePath, '/');
fileName = fileName ? fileName + 1 : filePath;
ssize_t sentBytes = send(clientSocket, fileName, strlen(fileName), 0);
if (sentBytes < 0)
{
perror("Error sending file name to server");
free(encoded);
return;
}
char response[256];
ssize_t receivedBytes = recv(clientSocket, response, sizeof(response) - 1, 0);
if (receivedBytes < 0)
{
perror("Error receiving acknowledgment from server");
free(encoded);
return;
}
response[receivedBytes] = '\0';
FILE *encoded_file = fopen("encoded_data.txt", "r");
if (encoded_file == NULL)
{
perror("Error opening encoded data file");
free(encoded);
return;
}
char buffer[1024];
while ((receivedBytes = fread(buffer, sizeof(char), sizeof(buffer), encoded_file)) > 0)
{
sentBytes = send(clientSocket, buffer, receivedBytes, 0);
if (sentBytes < 0)
{
perror("Error sending encoded file data");
break;
}
}
fclose(encoded_file);
if (sentBytes >= 0)
{
printf("Encoded file data uploaded successfully.\n");
}
if (remove("encoded_data.txt") != 0)
{
perror("Error deleting encoded_data.txt file");
}
free(encoded);
}
void deleteFileRequest(int clientSocket, const char *fileName)
{
char serverResponse[1024];
ssize_t bytesRead;
if (send(clientSocket, fileName, strlen(fileName), 0) < 0)
{
perror("Error sending filename to server");
return;
}
printf("Filename '%s' sent to server.\n", fileName);
while ((bytesRead = recv(clientSocket, serverResponse, sizeof(serverResponse) - 1, 0)) > 0)
{
serverResponse[bytesRead] = '\0';
printf("Server response: %s\n", serverResponse);
if (strstr(serverResponse, "File is Found !!") || strstr(serverResponse, "File not Found") || strstr(serverResponse, "Error"))
{
break;
}
}
if (bytesRead < 0)
{
perror("Error receiving response from server");
}
}
void update_file(int clientSocket, const char *filePath)
{
int encoded_length;
char *encoded = rle_encode(filePath, &encoded_length);
if (encoded == NULL)
{
perror("Error encoding file");
return;
}
const char *fileName = strrchr(filePath, '/');
fileName = fileName ? fileName + 1 : filePath;
ssize_t sentBytes = send(clientSocket, fileName, strlen(fileName), 0);
if (sentBytes < 0)
{
perror("Error sending file name to server");
free(encoded);
return;
}
char response[256];
ssize_t receivedBytes = recv(clientSocket, response, sizeof(response) - 1, 0);
if (receivedBytes < 0)
{
perror("Error receiving acknowledgment from server");
free(encoded);
return;
}
response[receivedBytes] = '\0';
FILE *encoded_file = fopen("encoded_data.txt", "r");
if (encoded_file == NULL)
{
perror("Error opening encoded data file");
free(encoded);
return;
}
char buffer[1024];
while ((receivedBytes = fread(buffer, sizeof(char), sizeof(buffer), encoded_file)) > 0)
{
sentBytes = send(clientSocket, buffer, receivedBytes, 0);
if (sentBytes < 0)
{
perror("Error sending encoded file data");
break;
}
}
fclose(encoded_file);
if (sentBytes >= 0)
{
printf("Encoded file data uploaded successfully.\n");
}
if (remove("encoded_data.txt") != 0)
{
perror("Error deleting encoded_data.txt file");
}
free(encoded);
}
void replace_file(int clientSocket, const char *filePath)
{
int encoded_length;
char *encoded = rle_encode(filePath, &encoded_length);
if (encoded == NULL)
{
perror("Error encoding file");
return;
}
// Notify the server that we're ready to send the file content
char response[256];
ssize_t receivedBytes = recv(clientSocket, response, sizeof(response) - 1, 0);
if (receivedBytes < 0)
{
perror("Error receiving acknowledgment from server");
free(encoded);
return;
}
response[receivedBytes] = '\0';
// Open the encoded file (assumed encoded data is saved in "encoded_data.txt")
FILE *encoded_file = fopen("encoded_data.txt", "r");
if (encoded_file == NULL)
{
perror("Error opening encoded data file");
free(encoded);
return;
}
// Send the encoded file content in chunks to the server
char buffer[1024];
ssize_t sentBytes;
while ((receivedBytes = fread(buffer, sizeof(char), sizeof(buffer), encoded_file)) > 0)
{
sentBytes = send(clientSocket, buffer, receivedBytes, 0);
if (sentBytes < 0)
{
perror("Error sending encoded file data");
break;
}
}
// Close the file after sending its content
fclose(encoded_file);
// Check if the file was successfully sent
if (sentBytes >= 0)
{
printf("Encoded file data Replaced successfully.\n");
}
// Remove the encoded file after successful transmission
if (remove("encoded_data.txt") != 0)
{
perror("Error deleting encoded_data.txt file");
}
// Free the allocated memory for the encoded data
free(encoded);
}
void handleFileExistsResponse(int serverSocket, const char *fileName)
{
// Prompt user for input (1 for Yes, 0 for No)
char userInput[2];
printf("\nFile already exists, would you like to replace the file in the destination? (Enter 1 for Yes, 0 for No): ");
scanf("%1s", userInput);
send(serverSocket, userInput, 1, 0);
if (userInput[0] == '1')
{
update_file(serverSocket, fileName); // function to update the file
}
else
{
// printf("File replacement canceled.\n");
replace_file(serverSocket, fileName);
}
}