-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
417 lines (336 loc) · 11 KB
/
file.c
File metadata and controls
417 lines (336 loc) · 11 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "storage_server.h"
#include "file.h"
// Some global variables
char File_Msg[BUFFER_LENGTH];
char error_message_file[ERROR_BUFFER_LENGTH];
File* fileHead = NULL;
File* fileTail = NULL;
//////////////////////////////// FILE STRUCT FUNCTIONS ///////////////////////
// Check if the file path exists (returns 1 is yes, otherwise returns 0)
int checkFilePathExists(char* path){
File* ptr = fileHead;
while(ptr != NULL) {
if(strcmp(path, ptr->filepath)==0) return 1;
ptr=ptr->next;
}
return 0;
}
// Check if the filepath is valid and stored in file struct
int validateFilePath(char* filepath, char* operation_no)
{
/* Function also locks the file based on the operation number */
File* ptr = fileHead;
while(ptr != NULL) {
if(strcmp(filepath, ptr->filepath)==0) break;
ptr=ptr->next;
}
if(ptr == NULL) return ERROR_PATH_DOES_NOT_EXIST;
if(strcmp(operation_no, READ_FILE)==0){
pthread_mutex_lock(&ptr->read_write_lock);
//Someone is already writing to the file
if(ptr->read_write == 1) {
pthread_mutex_unlock(&ptr->read_write_lock);
return ERROR_A_CLIENT_ALREADY_WRITING_TO_FILE;
}
else ptr->read_write = 0;
// Incrementing the reader count
pthread_mutex_lock(&ptr->get_reader_count_lock);
ptr->reader_count++;
// printf("Count: %d\n", ptr->reader_count);
pthread_mutex_unlock(&ptr->get_reader_count_lock);
pthread_mutex_unlock(&ptr->read_write_lock);
return VALID;
}
// File path matches and operation is to write
else if(strcmp(operation_no, WRITE_FILE) == 0)
{
pthread_mutex_lock(&ptr->read_write_lock);
// Someone is already reading the file
if(ptr->read_write == 0){
pthread_mutex_unlock(&ptr->read_write_lock);
return ERROR_A_CLIENT_ALREADY_READING_THE_FILE;
}
else ptr->read_write = 1;
pthread_mutex_unlock(&ptr->read_write_lock);
return VALID;
}
else {
return VALID;
}
}
// Decrease the reader count of a particular file
void decreaseReaderCount(char* path)
{
File* file = fileHead;
while(file != NULL) {
if(strcmp(path, file->filepath)==0)
{
pthread_mutex_lock(&file->read_write_lock);
pthread_mutex_lock(&file->get_reader_count_lock);
// printf("Filename: %s\n", file->filepath);
// printf("Reader_count: %d\n", file->reader_count);
if(file->read_write != 1 && --file->reader_count == 0){
file->read_write = -1;
}
pthread_mutex_unlock(&file->get_reader_count_lock);
pthread_mutex_unlock(&file->read_write_lock);
return;
}
file = file->next;
}
return;
}
// Function to add a File to the file struct
int addFile(char* path, int check)
{
// Check if the path is not already stored in the storage server
if(check && checkFilePathExists(path)) {
printf("FILE already exists in Accessible path list\n");
return ERROR_FILE_ALREADY_EXISTS;
}
// Creating a new File struct if the path is not already stored
File* newFile = (File*)malloc(sizeof(File));
strcpy(newFile->filepath, path);
newFile->read_write = -1;
newFile->reader_count = 0;
newFile->next = NULL;
if(pthread_mutex_init(&newFile->get_reader_count_lock, NULL) < 0 ||
pthread_mutex_init(&newFile->read_write_lock, NULL)< 0)
{
printf("[-] Error addFile(): Unable to initialize mutex lock\n");
return STORAGE_SERVER_DOWN;
}
// Adding it to the File struct linked list
if(fileHead == NULL){
fileHead = newFile;
fileTail = newFile;
return 0;
}
fileTail->next = newFile;
fileTail = newFile;
return 0;
}
// Function to remove the file from file struct
int removeFile(char* path){
File* ptr = fileHead, *ptr2;
if(ptr == NULL) return 0;
while(ptr->next != NULL) {
if(strcmp(path, ptr->next->filepath)==0) {
ptr2 = ptr->next;
ptr->next = ptr2->next;
free(ptr2);
return 1;
}
ptr=ptr->next;
}
return 0;
}
// Function to clean up the file struct
void cleanUpFileStruct(){
File* ptr = fileHead, *ptr2;
if(ptr == NULL) return;
while(ptr != NULL) {
ptr2 = ptr->next;
free(ptr);
ptr=ptr2;
}
}
// Opening the file lock from writing
void openWriteLock(char* path){
File* file = fileHead;
while(file != NULL) {
if(strcmp(path, file->filepath) == 0){
pthread_mutex_lock(&file->read_write_lock);
file->read_write = -1;
pthread_mutex_unlock(&file->read_write_lock);
return;
}
file = file->next;
}
}
////////////////////////////// ACTUAL FILE FUNCTIONS ////////////////////////////
// Check the existence of a path and whether it corresponds to a file/directory
// returns 0 in case of files and 1 in case of directories, -1 in case it does not exist
int checkFileType(char* path)
{
struct stat path_stat;
if(stat(path, &path_stat)){
perror("[-] Error checkFileType(): Unable to get file stat");
return -1;
}
if(S_ISREG(path_stat.st_mode)) return 0;
else if(S_ISDIR(path_stat.st_mode)) return 1;
return -1;
}
// Returns 1 is the file exists
int fileExists(char *filename) {
return access(filename, F_OK) != -1;
}
// Function to create a File
void createFile(char* path, char* response)
{
if(fileExists(path)){
sprintf(response, "%d", ERROR_FILE_ALREADY_EXISTS);
}
else{
FILE* file = fopen(path, "w");
if(file == NULL){
sprintf(response, "%d", ERROR_CREATING_FILE);
}
else{
fclose(file);
addFile(path, 0);
strcpy(response, VALID_STRING);
}
}
}
// Function to get the meta data(additional information) about the file
void getFileMetaData(char* filepath, int clientSocketID)
{
struct stat fileStat;
char buffer[1000];
// Use the stat function to retrieve file metadata
if (stat(filepath, &fileStat) == 0) {
// Format file metadata into a single string
// filepath : filesize : file_permissions : last_access_time : last_modification_time : creation_time
int n = sprintf(buffer, "%s:%ld:%o:%s:%s:%s", filepath, (long)fileStat.st_size, (unsigned int)(fileStat.st_mode & 0777),
ctime(&fileStat.st_atime), ctime(&fileStat.st_mtime), ctime(&fileStat.st_ctime));
if (n < 0) {
perror("[-] Error formatting file metadata");
sprintf(buffer, "%d", STORAGE_SERVER_ERROR);
}
}
else {
perror("[-] Stat: Unable to get file stat");
sprintf(buffer, "%d", ERROR_GETTING_FILE_PERMISSIONS);
}
// Send the formatted metadata or error buffer
if (sendData(clientSocketID, buffer)){
printf("[-] Error sending file metadata\n");
}
}
// Function to delete a File
void deleteFile(char *filename, char* response)
{
// File does not exist
if (access(filename, F_OK) != 0) {
sprintf(response, "%d", ERROR_FILE_DOES_NOT_EXIST);
return;
}
else strcpy(response, VALID_STRING);
// Check for permission [TODO]
if(remove(filename) == 0) {
strcpy(response, VALID_STRING);
removeFile(filename);
}
else
sprintf(response, "%d", ERROR_UNABLE_TO_DELETE_FILE);
}
// Function to download a file
void DownloadFile(int serverSocket, char* filename)
{
char buffer[BUFFER_LENGTH];
if(nonBlockingRecv(serverSocket, buffer)) return;
if(strcmp(buffer, VALID_STRING) != 0){
printError(buffer);
return;
}
if(sendConfirmation(serverSocket)) return;
FILE* file = fopen(filename, "w");
if(!file){
printf("Unable to open the FILE %s for writing\n", filename);
return;
}
// Receiving the FILE DATA
while(1){
int status = nonBlockingRecv(serverSocket, buffer);
if(status){
printf("Error downloadFile(): Unable to received file content");
fclose(file);
return;
}
if(sendConfirmation(serverSocket)){
printf("Error downloadFile(): Error sending confirmation");
fclose(file);
return;
}
if(strstr(buffer, "COMPLETE") != NULL) {
break;
}
else if(fprintf(file, "%s", buffer) < 0){
printf("Error downloadFile(): Unable to write to the file");
fclose(file);
return;
}
// printf("%s\n", buffer);
}
fclose(file);
printf("File %s downloaded successfully.\n", filename);
}
// Function to upload a file
int UploadFile(int clientSocket, char* filename)
{
// Open the file for reading on the server side
FILE *file = fopen(filename, "r");
bzero(error_message_file, ERROR_BUFFER_LENGTH);
if (!file) sprintf(error_message_file, "%d", ERROR_OPENING_FILE);
else sprintf(error_message_file, VALID_STRING);
if(sendDataAndReceiveConfirmation(clientSocket, error_message_file)){
perror("[-] Error UploadingFile(): Unable to send file opened message");
fclose(file);
return -1;
}
if(!file) fclose(file);
else {
char buffer[BUFFER_LENGTH] = {'\0'};
while (fgets(buffer, BUFFER_LENGTH, file) != NULL){
if (sendDataAndReceiveConfirmation(clientSocket, buffer)){
perror("[-] Error sending file");
fclose(file);
return -1;
}
}
if (sendData(clientSocket, "COMPLETE")){
perror("[-] Error sending COMPLETE");
fclose(file);
return -1;
}
fclose(file);
printf("File %s sent successfully.\n", filename);
}
return 0;
}
void copyFile(char* ss_path, char* response)
{
char IP_address[20], path[MAX_PATH_LENGTH], path2[MAX_PATH_LENGTH];
int PORT = 0;
if(sscanf(ss_path, "%[^:]:%d:%[^:]:%[^:]", IP_address, &PORT, path, path2) != 4){
printf("[-] Error parsing the SS_Path string\n");
return;
}
printf("Inside\n");
char filename[1000];
extractFileName(path, filename);
if(path2[strlen(path2)-1]=='/'){
strcat(path2, filename);
}
else{
strcat(path2, "/");
strcat(path2, filename);
}
printf("Path2: %s\n", path2);
int copySocket = connectToServer(IP_address, PORT);
printf("[+] Connected to copy server\n");
if(sendDataAndReceiveConfirmation(copySocket, COPY_FILES)){
printf("[-] Error sending Copying Operation number");
return;
}
if(sendDataAndReceiveConfirmation(copySocket, path2)){
printf("[-] Error sending Copying Operation number");
return;
}
UploadFile(copySocket, path);
close(copySocket);
}
void copyDirectory(char* ss_path, char* response){
}