-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpdf.c
More file actions
331 lines (274 loc) · 10.1 KB
/
Spdf.c
File metadata and controls
331 lines (274 loc) · 10.1 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
//Defining port no. for spdf server
#define PORT 16458
#define BUFSIZE 1024 //Defining Buffer size
void error(const char *msg) {
perror(msg);
exit(1);
}
//---------------------------------------------------Handling Upload file-------------------------------------------
// Created a ufile handler for handling ufile command for .txt files
void handle_upload(int newsockfd, char *filename, char *destination) {
char buffer[BUFSIZE];
int n;
// Remove any trailing newline from destination path
destination[strcspn(destination, "\n")] = 0;
char fullpath[BUFSIZE]; // array for storing the file path
//creating path using snprintf and creating directory stxt for storing .txt files
snprintf(fullpath, sizeof(fullpath), "%s/%s", destination, filename);
printf("Uploading File to directory %s\n", fullpath);
// Ensure the destination directory exists
struct stat st = {0};
if (stat(destination, &st) == -1) {
printf("%s Directory does not exist and therefore, creating the directory \n", destination);
// Create directories recursively for ex smain/folder1/folder2
char tmp[BUFSIZE];
snprintf(tmp, sizeof(tmp), "%s", destination);
for (char *p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
mkdir(tmp, 0700);
*p = '/';
}
}
mkdir(destination, 0700); // Created the final directory where we have to upload the file
}
// Open the file in write binary mode
FILE *fp = fopen(fullpath, "wb");
if (fp == NULL) {
perror("Error in opening the file");
close(newsockfd);
return;
}
// Receive file data from Smain
int total_bytes_received = 0;
while ((n = read(newsockfd, buffer, BUFSIZE)) > 0) {
fwrite(buffer, sizeof(char), n, fp);
total_bytes_received += n;
}
fclose(fp);
printf("File received: %s (%d bytes)\n", fullpath, total_bytes_received);
// Send acknowledgment back to Smain
n = write(newsockfd, "File uploaded successfully.\n", 25);
if (n < 0) error("Error in pdf file while writing to server socket");
close(newsockfd);
}
//-------------------------------------------------------------------------------------------------------
//handling dfile command for downloading file
void handle_dfile_Command(int newsockfd, char *filepath) {
char buffer[BUFSIZE];
FILE *fp;
int n;
printf("Performing download command: %s\n", filepath);
// Open the file to read bytes mode
fp = fopen(filepath, "rb");
if (fp == NULL) {
perror("Error while opening the file for download command");
write(newsockfd, "File not found\n", 16);
close(newsockfd);
return;
}
printf("Transferring file: %s\n", filepath);
// Sending file data to Smain server
while ((n = fread(buffer, sizeof(char), BUFSIZE, fp)) > 0) {
if (write(newsockfd, buffer, n) < 0) {
perror("Error in download file while writing to server socket");
break;
}
}
fclose(fp);
printf("File transferred successfully.\n");
shutdown(newsockfd, SHUT_WR);
}
void replace_smain_with_spdf(char *filepath) {
char *pos = strstr(filepath, "smain");
if (pos != NULL) {
char temp[BUFSIZE];
strncpy(temp, filepath, pos - filepath);
temp[pos - filepath] = '\0';
strcat(temp, "spdf");
strcat(temp, pos + strlen("smain"));
strcpy(filepath, temp);
}
}
//------------------------------------------------handle_rmfile------------------------------
//Function for handling remove file function
void handle_rmfile(int newsockfd, char *filepath) {
replace_smain_with_spdf(filepath);
//Using remove() for removing the file
if (remove(filepath) == 0) {
printf("File %s deleted successfully.\n", filepath);
//Writing message to socket_FD
write(newsockfd, "File deleted successfully.\n", 28);
} else {
perror("Error while deleting the file for remove command");
write(newsockfd, "File was not deleted.\n", 23);
}
close(newsockfd);
}
//------------------------------------------------------------------------
//Handling dtar command
void handle_dtar_command(int newsockfd, char *filetype) {
char tarname[BUFSIZE];
char command[BUFSIZE];
//Creating tarname according to the naming convention
snprintf(tarname, sizeof(tarname), "%sfiles.tar", filetype + 1); // Skip the '.' in the filetype
// Confirming tarname does not have any whitespaces or newlines
tarname[strcspn(tarname, "\n")] = 0;
tarname[strcspn(tarname, "\r")] = 0;
// Construct the command to create the tar file
snprintf(command, sizeof(command), "tar -cvf %s $(find ~/spdf -type f -name '*%s')", tarname, filetype);
printf("Executing command: %s\n", command);
system(command);
// Send the tar file back to Smain
FILE *fp = fopen(tarname, "rb");
if (fp == NULL) {
perror("Error while opening the tar file for dtar command");
write(newsockfd, "Tar file was not created\n", 26);
close(newsockfd);
return;
}
// Sending the tar file data back to Smain
char buffer[BUFSIZE];
int n;
while ((n = fread(buffer, sizeof(char), BUFSIZE, fp)) > 0) {
if (write(newsockfd, buffer, n) < 0) {
perror("Error while writing the tar file for dtar command");
break;
}
}
fclose(fp);
remove(tarname); // Remove the tar file after sending it to Smain
printf("Tar file %s transferred successfully\n", tarname);
shutdown(newsockfd, SHUT_WR);
}
//-------------------------------------Handle Display pathname
void handle_display_request(int newsockfd, char *directory) {
DIR *d;
struct dirent *dir;
char buffer[BUFSIZE] = {0};
// Handle .txt files by replacing "smain" with "spdf"
char directory_txt[BUFSIZE];
strcpy(directory_txt, directory);
replace_smain_with_spdf(directory_txt);
printf("%s",directory_txt);
//handle .pdf files
d = opendir(directory_txt);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG && strcmp(strrchr(dir->d_name, '.'), ".pdf") == 0) {
strcat(buffer, dir->d_name);
strcat(buffer, "\n");
}
}
closedir(d);
} else {
perror("Error while opening stext directory for display command");
}
write(newsockfd, buffer, strlen(buffer));
shutdown(newsockfd, SHUT_WR);
}
// Handle Client function
void handle_client(int newsockfd) {
char buffer[BUFSIZE];
int n;
// Receiving command from Smain
bzero(buffer, BUFSIZE);
n = read(newsockfd, buffer, BUFSIZE - 1);
if (n < 0) error("Error while reading from socket for handle client");
printf("Command received from smain: %s\n", buffer);
// handling for ufile command
if (strncmp(buffer, "ufile", 5) == 0) {
// Get filename and destination path
char *filename = strtok(buffer + 6, " ");
char *destination = strtok(NULL, " ");
if (!filename || !destination) {
printf("Invalid format for the command.\n");
close(newsockfd);
return;
}
handle_upload(newsockfd, filename, destination);
}
//Handling for dfile command
else if (strncmp(buffer, "dfile", 5) == 0) {
// Extract the file path
char *filepath = strtok(buffer + 6, " ");
if (!filepath) {
write(newsockfd, "Invalid format for the command.\n", 24);
close(newsockfd);
return;
}
handle_dfile_Command(newsockfd, filepath);
}
//Handling for rmfile command
else if (strncmp(buffer, "rmfile", 6) == 0) {
char *filepath = strtok(buffer + 7, " ");
if (!filepath) {
write(newsockfd, "Invalid format for the command.\n", 24);
close(newsockfd);
return;
}
handle_rmfile(newsockfd, filepath);
}
//handling for dtar command
else if (strncmp(buffer, "dtar", 4) == 0) {
// Extract the file type and handle the tar creation
char *filetype = strtok(buffer + 5, " ");
if (filetype) {
handle_dtar_command(newsockfd, filetype);
} else {
write(newsockfd, "Invalid format for the command.\n", 29);
}
}
//Handling for display file command
else if (strncmp(buffer, "display", 7) == 0) {
// Extract the directory path
char *directory = strtok(buffer + 8, " ");
if (!directory) {
write(newsockfd, "Invalid format for the command.\n", 24);
close(newsockfd);
return;
}
handle_display_request(newsockfd, directory);
}
//print error if invalid command format received
else {
printf("Invalid format for the command.\n");
write(newsockfd, "Invalid format for the command.\n", 24);
}
close(newsockfd);
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("Error while opening the socket.");
// Bind socket to port
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("Error while binding socket with the port");
listen(sockfd, 5);
printf("spdf listening on port %d\n", PORT);
// Accept connections
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("Error while accepting the connection request.");
printf("spdf successfully connected with smain\n");
handle_client(newsockfd);
}
close(sockfd);
return 0;
}