-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfscli.c
346 lines (286 loc) · 11.2 KB
/
mfscli.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
// Version 3
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "mfs.h"
#include "ufs.h"
#define MFS_RW_BUFFER_SIZE 4096
#define LOG_SIZE 4096
char logBuffer[LOG_SIZE];
int verboseMode = 0;
void VERBOSE() {
if (verboseMode != 1) return;
printf("[VERBOSE] %s\n", logBuffer);
}
void INFO() {
printf("[INFO] %s\n", logBuffer);
}
void ERR() {
printf("[ERR] %s\n", logBuffer);
exit(-1);
}
int _connect(char *hostname, int port) {
sprintf(logBuffer, "Attemping to connect to %s:%d", hostname, port); INFO();
int rc = MFS_Init(hostname, port);
if (rc != 0) {
sprintf("MFS_Init failed for %s:%d", hostname, port); ERR();
}
return rc;
}
// assumes absolute path.
// exits on traversal failure
int _traverseToDirectory(char *path) {
assert(strlen(path) > 0);
assert(path[0] == '/');
path = strdup(path); // because strtok is destructive
char *dirname = strtok(path, "/");
// root directory is inode 0
int dirInode = 0;
while (dirname != NULL) {
if (strcmp(dirname, "") == 0) {
dirname = strtok(NULL, "/");
continue; // to handle // and trailing /
}
sprintf(logBuffer, "looking up child entry %s in parent directory (inode=%d)", dirname, dirInode); VERBOSE();
dirInode = MFS_Lookup(dirInode, dirname);
sprintf(logBuffer, "Found child entry with inode number %d", dirInode); VERBOSE();
if (dirInode == -1) {
sprintf(logBuffer, "Unable to enter %s", dirname); ERR();
exit(1);
};
dirname = strtok(NULL, "/");
}
free(path);
return dirInode;
}
int rfind(const char *haystack, char needle) {
int end = strlen(haystack) - 1;
for(int i = end; i >= 0; i--) {
if (haystack[i] == needle) return i;
}
return -1;
}
// can only be called on directories.
int perform_ls(char *path) {
int dirInode = _traverseToDirectory(path);
MFS_Stat_t stat;
int rc = MFS_Stat(dirInode, &stat);
if (rc == -1) {
sprintf(logBuffer, "Unable to call MFS_Stat on dir (inum=%d)", dirInode); ERR();
}
if (stat.type != UFS_DIRECTORY) {
sprintf(logBuffer, "The inode (%d) received for %s is not of directory type", dirInode, path); ERR();
}
int sz = stat.size;
assert(sz % sizeof(MFS_DirEnt_t) == 0);
int nentries = sz / sizeof(MFS_DirEnt_t);
MFS_DirEnt_t entries[nentries]; // should be safe to pass to MFS_Read because struct seems packed. No padding should be necessary.
assert(sizeof(entries) == (28+4)*nentries); // folks, students, if this assert fails, email me!
// Future me: the solution would be to read in X bytes specifically, and explicitly force-read entries by casting at required offsets.
sprintf(logBuffer, "Attempting to read %d children of %s", nentries, path); VERBOSE();
int offset = 0;
while (offset < sz) {
int toRead = sz - offset;
if (toRead > MFS_RW_BUFFER_SIZE) toRead = MFS_RW_BUFFER_SIZE;
rc = MFS_Read(dirInode, entries, offset, toRead);
if (rc == -1) {
sprintf(logBuffer, "MFS_Read failed"); ERR();
}
offset += toRead;
}
sprintf(logBuffer, "Fetched %d children. Here they are!", nentries); INFO();
for(int i = 0; i < nentries; i++) {
if (entries[i].inum < 0)
printf("Skipping entry: (inode=%d)", entries[i].inum);
else
printf("%s (inode=%d)\n", entries[i].name, entries[i].inum);
}
return 0;
}
int perform_insert(const char *fromPath, char *toPath) {
assert(strlen(toPath) > 0 && strlen(fromPath) > 0);
int toCopyFd = open(fromPath, O_RDONLY);
if (toCopyFd == -1) {
sprintf(logBuffer, "Unable to open provided file %s", fromPath); ERR();
}
// assumes toPath ends with filename to copy as
int fnameSep = rfind(toPath, '/');
char *dirPath = strndup(toPath, fnameSep);
char *fileName = toPath + fnameSep + 1;
int dirInode = _traverseToDirectory(dirPath);
sprintf(logBuffer, "Trying to create new file %s in %s", fileName, dirPath); VERBOSE();
int rc = MFS_Creat(dirInode, UFS_REGULAR_FILE, fileName);
if (rc == -1) {
sprintf(logBuffer, "Unable to create new file %s in %s", fileName, dirPath); ERR();
}
int newInode = MFS_Lookup(dirInode, fileName);
if (newInode == -1) {
sprintf(logBuffer, "Unable to fetch newly created inode number even though MFS_Creat was successful"); ERR();
}
sprintf(logBuffer, "Created new file with inode number %d", newInode); INFO();
char buffer[MFS_RW_BUFFER_SIZE];
memset(buffer, 0, MFS_RW_BUFFER_SIZE);
int readBytes = read(toCopyFd, buffer, MFS_RW_BUFFER_SIZE);
int offset = 0;
while (readBytes > 0) {
sprintf(logBuffer, "about to write %d bytes ", readBytes); VERBOSE();
int rc = MFS_Write(newInode, buffer, offset, readBytes);
offset += readBytes;
if (rc == -1) {
sprintf(logBuffer, "MFS_Write failed"); ERR();
}
sprintf(logBuffer, "Written %d bytes successfully", readBytes); VERBOSE();
readBytes = read(toCopyFd, buffer, MFS_RW_BUFFER_SIZE);
}
if (readBytes == -1) {
sprintf(logBuffer, "Error while reading input file"); ERR();
}
sprintf(logBuffer, "Completed all write operations. Written a total of %d bytes", offset); INFO();
free(dirPath);
return 0;
}
int perform_cat(char *path) {
int fnameSep = rfind(path, '/');
char *dirPath = strndup(path, fnameSep);
char *fileName = path + fnameSep + 1;
int dirInode = _traverseToDirectory(dirPath);
int fileInode = MFS_Lookup(dirInode, fileName);
if (fileInode == -1) {
sprintf(logBuffer, "Unable to lookup file %s in directory (inum=%d)", fileName, dirInode); ERR();
}
sprintf(logBuffer, "Trying to determine filesize"); VERBOSE();
MFS_Stat_t stat;
int rc = MFS_Stat(fileInode, &stat);
if (rc == -1) {
sprintf(logBuffer, "Unable to determine filesize. Stat failed for inum=%d", fileInode); ERR();
}
int sz = stat.size;
char *output = (char *) malloc(sz * sizeof(char));
memset(output, 0, sz);
sprintf(logBuffer, "Filesize=%d. Starting read", sz); INFO();
int offset = 0;
while (offset < sz) {
int count = sz - offset;
if (count > MFS_RW_BUFFER_SIZE) count = MFS_RW_BUFFER_SIZE;
sprintf(logBuffer, "Trying to read %d bytes from offset %d foi inum=%d", count, offset, fileInode); VERBOSE();
int rc = MFS_Read(fileInode, output + offset, offset, count);
if (rc == -1) {
sprintf(logBuffer, "MFS_Read failed for inum=%d offset=%d count=%d", fileInode, offset, count); ERR();
}
offset += count;
}
sprintf(logBuffer, "File contents (from next line): \n%s\n", output);
INFO();
free(output);
free(dirPath);
return 0;
}
// similar to mkdir -p. Just bulldoze through and call MFS_Creat for all
// subdirectories. If name already exists, should not overwrite.
int perform_mkdir(char *path) {
assert(strlen(path) > 0);
assert(path[0] == '/');
path = strdup(path); // because strtok is destructive.
char *dirname = strtok(path, "/");
// root directory is inode 0
int dirInode = 0;
while (dirname != NULL) { // assume root directory already exists. Creating further ones.
if (strcmp(dirname, "") == 0) {
dirname = strtok(NULL, "/");
continue; // to handle // and trailing /
}
sprintf(logBuffer, "calling MFS_Creat for %s in parent directory (inode=%d)", dirname, dirInode); VERBOSE();
int rc = MFS_Creat(dirInode, UFS_DIRECTORY, dirname);
if (rc == -1) {
sprintf(logBuffer, "Unable to create directory %s", dirname); ERR();
}
int newInode = MFS_Lookup(dirInode, dirname);
if (newInode == -1) {
sprintf(logBuffer, "Unable to fetch newly created directory inode even though MFS_Creat was successful"); ERR();
}
dirInode = newInode;
if (dirInode == -1) {
sprintf(logBuffer, "Unable to enter %s", dirname); ERR();
exit(1);
};
dirname = strtok(NULL, "/");
}
sprintf(logBuffer, "mkdir completed successfully"); INFO();
free(path);
return 0;
}
const char *usage = "mfscli usage: \n"
"Basic format: ./mfscli ip_of_server port <command> <args...>\n"
" If the server is on the same machine, use 127.0.0.1 as ip\n"
"\n"
"Verbose mode: you can run all commands of mfscli in verbose mode by \n"
" prepending MFS_VERBOSE=1.\n"
" for e.g. MFS_VERBOSE=1 ./mfscli 127.0.0.1 36000 ls /files/\n\n"
"Usage:\n"
" - ./mfscli 127.0.0.1 36000 insert /path/to/local/file/test.txt /files/test1.txt \n"
" This copies the file specified by first path into MFS with \n"
" the location specified by the second path.\n"
" First path refers to a file in your original filesystem (AFS) \n"
" Second path refers to a location in MFS.\n"
" The directory should exist in MFS for insert to succeed. \n"
"\n"
" - ./mfscli 127.0.0.1 36000 cat /files/test1.txt \n"
" similar to UNIX cat. Outputs content of /files/test1.txt. Issues \n"
" corresponding MSF_Read, MFS_Lookup, MFS_Stat calls for this. \n"
" Fails if file/path does not exist. \n"
"\n"
" - ./mfscli 127.0.0.1 36000 ls /files/ \n"
" Similar to UNIX ls. The path argument is for a location within MFS.\n"
" It should end with a directory. doing /files/test1.txt is not \n"
" supported.\n"
"\n"
" - ./mfscli 127.0.0.1 36000 mkdir /files/new/directory \n"
" This works similar to unix's mkdir -p. Basically it calls MFS_Creat \n"
" for each subdirectory. First MFS_Creat(files), then MFS_Creat(new) within \n"
" it and so on. Existing directories would ideally remain untouched \n"
" because MFS_Creat doesn't do anything and returns true for existing dirs\n"
"\n"
;
int _assert_argc(int argc, int expected) {
if (argc != expected) {
printf("Incorrect number of arguments! Run ./mfscli for usage help\n");
exit(0);
}
return 0;
}
int main(int argc, char *argv[]) {
memset(logBuffer, 0, LOG_SIZE);
// TODO: move to argparse
if (argc <= 3) { // bare minumum: ./mfscli host port
printf("%s", usage);
return -1;
}
char *verboseEnv = getenv("MFS_VERBOSE");
if (verboseEnv != NULL && strcmp(verboseEnv, "1") == 0)
verboseMode = 1;
_connect(argv[1], atoi(argv[2]));
char *cmd = argv[3];
if (strcmp(cmd, "insert") == 0) {
_assert_argc(argc, 3 + 3);
perform_insert(argv[4], argv[5]);
} else if (strcmp(cmd, "cat") == 0) {
_assert_argc(argc, 2 + 3);
perform_cat(argv[4]);
} else if (strcmp(cmd, "ls") == 0) {
_assert_argc(argc, 2 + 3);
perform_ls(argv[4]);
} else if (strcmp(cmd, "mkdir") == 0) {
_assert_argc(argc, 2 + 3);
perform_mkdir(argv[4]);
} else {
printf("Command not found! run ./mfscli for usage help\n");
return -1;
}
return 0;
}