-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
137 lines (134 loc) · 3.84 KB
/
Copy pathexecute.c
File metadata and controls
137 lines (134 loc) · 3.84 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "structs.h"
int reconnectToSS(ss_info *ss_struct)
{
int ss_socket = socket(AF_INET, SOCK_STREAM, 0);
if (ss_socket < 0)
{
perror("[-]Socket error");
return -1;
}
struct sockaddr_in ss_addr;
memset(&ss_addr, '\0', sizeof(ss_addr));
ss_addr.sin_family = AF_INET;
ss_addr.sin_port = ss_struct->port_no_ns;
ss_addr.sin_addr.s_addr = inet_addr(ss_struct->ip_addr);
int n = connect(ss_socket, (struct sockaddr *)&ss_addr, sizeof(ss_addr));
if (n < 0)
{
perror("[-]Connection error");
close(ss_socket);
return -1;
}
return ss_socket;
}
void execute(char *command, int client_socket, struct LRUcache *lruQueue, struct TrieNode *root)
{
char input[1024];
strcpy(input, command);
char *token = strtok(command, "\t\n");
if (strcpy(token, "COPY") == 0)
{
// copy
token = strtok(NULL, "\t\n");
ss_info *ss_struct_1 = getFromLRUcache(lruQueue, token);
if (ss_struct_1 == NULL)
{
ss_struct_1 = search(root, token);
enqueue(lruQueue, token, ss_struct_1);
}
if (ss_struct_1 == NULL)
{
perror("File/Directory unavailable");
}
char path1[1024];
strcpy(path1, token);
token = strtok(NULL, "\t\n");
ss_info *ss_struct_2 = getFromLRUcache(lruQueue, token);
if (ss_struct_2 == NULL)
{
ss_struct_2 = search(root, token);
enqueue(lruQueue, token, ss_struct_2);
}
if (ss_struct_2 == NULL)
{
perror("File/Directory unavailable");
}
// in ss if there only path (no create or delete when tokenized) -> copy
int ss_socket=reconnectToSS(ss_struct_1);
if (ss_socket != -1)
{
printf("Connected to SS.\n");
int n = send(ss_socket, path1, sizeof(path1), 0);
if (n < 0)
{
perror("[-]Send error");
exit(1);
}
}
get(path1, ss_struct_1);
reconnectToSS(ss_struct_1);
if (ss_socket != -1)
{
printf("Connected to SS.\n");
int n = send(ss_socket, path1, sizeof(path1), 0);
if (n < 0)
{
perror("[-]Send error");
exit(1);
}
}
put(token, ss_struct_2);
}
else if (strcpy(token, "create") == 0 || strcpy(token, "delete") == 0)
{
// delete or create
token = strtok(NULL, "\t\n");
ss_info *ss_struct = getFromLRUcache(lruQueue, token);
if (ss_struct == NULL)
{
ss_struct = search(root, token);
enqueue(lruQueue, token, ss_struct);
}
if (ss_struct == NULL)
{
perror("File/Directory unavailable");
}
// establish connection with ss -> send create/delete filepath
int ss_socket = reconnectToSS(ss_struct);
if (ss_socket != -1)
{
printf("Connected to SS.\n");
int n = send(ss_socket, input, sizeof(input), 0);
if (n < 0)
{
perror("[-]Send error");
exit(1);
}
}
}
else
{
token = strtok(NULL, "\t\n");
ss_info *ss_struct = getFromLRUcache(lruQueue, token);
if (ss_struct == NULL)
{
ss_struct = search(root, token);
enqueue(lruQueue, token, ss_struct);
}
if (ss_struct == NULL)
{
perror("File/Directory unavailable");
}
int n = send(client_socket, ss_struct, sizeof(ss_info), 0);
if (n == -1)
{
perror("[-]send error");
exit(1);
}
}
}