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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "stlfile.h"
#include "util.h"
#define MAXFILESIZE 1000000
struct command {
const char *name;
void (*func)(const char *);
const char *desc;
};
int save_submission(struct parseinfo *info, char *data, int len);
void cat_cmd(const char *arg);
void help_cmd(const char *arg);
void exit_cmd(const char *arg);
void echo_cmd(const char *arg);
void upload_cmd(const char *arg);
void search_cmd(const char *arg);
void list_cmd(const char *arg);
void auth_cmd(const char *arg);
void cleanexit();
struct command commands[] = {
{ "cat", cat_cmd, "Cat cmd go prrrrr." },
{ "help", help_cmd, "You already know what this does." },
{ "exit", exit_cmd, "Closes the session." },
{ "echo", echo_cmd, "Repeat after me!" },
{ "upload", upload_cmd, "Upload an STL file to analyze." },
{ "search", search_cmd, "Search for an STL file by model name." },
{ "list", list_cmd, "List your uploaded files." },
{ "auth", auth_cmd, "Login to upload files to a private dir." }
};
struct parseinfo cached = { 0 };
char *resultdir;
int echo = 0, loggedin = 0;
int
save_submission(struct parseinfo *info, char *stldata, int stlsize)
{
char *dirpath = NULL, *infopath = NULL, *modeldir = NULL,
*indexpath = NULL, *modelpath = NULL;
FILE *f = NULL;
int status = OK;
modeldir = aprintf("%s%s-%i", loggedin ? "." : "",
info->hash, time(NULL));
dirpath = aprintf("%s/%s", resultdir, modeldir);
if (mkdir(dirpath, S_IRWXU | S_IRWXG | S_IRWXO)) goto fail;
modelpath = aprintf("%s/%s", dirpath, "model");
if (!(f = fopen(modelpath, "w+"))) goto fail;
if (fwrite(stldata, 1, stlsize, f) != stlsize) goto fail;
FCLOSE(f);
infopath = aprintf("%s/%s", dirpath, "info");
if (!(f = fopen(infopath, "w+"))) goto fail;
if (save_info(info, f) != OK) goto fail;
FCLOSE(f);
indexpath = aprintf("%s/.index", resultdir);
if (!(f = fopen(indexpath, "a+"))) goto fail;
flock(fileno(f), LOCK_EX);
fwrite(modeldir, 1, strlen(modeldir), f);
fputc('\n', f);
flock(fileno(f), LOCK_UN);
FCLOSE(f);
exit:
FCLOSE(f);
free(dirpath);
free(modelpath);
free(infopath);
free(indexpath);
free(modeldir);
return status;
fail:
if (infopath) remove(infopath);
if (modelpath) remove(modelpath);
if (dirpath) remove(dirpath);
status = FAIL;
goto exit;
}
int
handle_download(const char *scandir)
{
char *infopath = NULL, *modelpath = NULL;
size_t i, size;
int status = OK;
FILE *f;
infopath = aprintf("%s/%s", scandir, "info");
if (!(f = fopen(infopath, "r"))) {
ERR("Selected result is missing!\n");
goto fail;
}
free_info(&cached);
if (load_info(&cached, f) != OK) {
ERR("Failed to parse info file!\n");
goto fail;
}
FCLOSE(f);
print_info(&cached);
if (strchr(ask("> Download model? "), 'y')) {
modelpath = aprintf("%s/%s", scandir, "model");
if (!(f = fopen(modelpath, "r"))) {
ERR("Failed to access file!\n");
goto fail;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size >= MAXFILESIZE) {
ERR("File is too large!\n");
goto fail;
}
printf("Here you go.. (%liB)\n", size);
while ((i = getc(f)) != EOF)
putc(i, stdout);
FCLOSE(f);
}
exit:
if (f) fclose(f);
free(infopath);
free(modelpath);
return status;
fail:
status = FAIL;
goto exit;
}
void
motd()
{
char linebuf[80];
int msgc, msgi, i;
FILE *f;
if (!(f = fopen("msgs/motd", "r"))) return;
if (!fgets(linebuf, sizeof(linebuf), f))
goto exit;
srand(time(NULL));
if ((msgc = atoi(linebuf))) {
msgi = rand() % msgc;
for (i = 0; i < msgi + 1; i++)
if (!fgets(linebuf, sizeof(linebuf), f))
return;
printf("%s\n", linebuf);
}
exit:
fclose(f);
}
void
cat_cmd(const char *arg)
{
if (arg && !strncmp(arg, "flag", 4))
dump("msgs/cat_flag");
else
printf("meow\n");
}
void
help_cmd(const char *arg)
{
int i;
for (i = 0; arg && i < ARRSIZE(commands); i++) {
if (!strcmp(commands[i].name, arg)) {
printf("%s\n", commands[i].desc);
return;
}
}
printf("Available commands:\n");
for (i = 0; i < ARRSIZE(commands); i++)
printf("%s%s", i ? " " : "", commands[i].name);
printf("\n");
}
void
exit_cmd(const char *arg)
{
printf("bye!\n");
exit(0);
}
void
echo_cmd(const char *arg)
{
echo ^= 1;
printf("Echo is %s\n", echo ? "enabled" : "disabled");
}
void
upload_cmd(const char *arg)
{
char *end, *contents = NULL, *modelname = NULL;
const char *resp;
size_t len;
modelname = checkp(strdup(ask("> Model name: ")));
if (!strlen(modelname)) {
ERR("Empty model names are not allowed");
goto exit;
}
resp = ask("> File size: ");
len = strtoul(resp, &end, 10);
if (len <= 0 || len >= MAXFILESIZE || *end) {
ERR("Invalid file length!\n");
goto exit;
}
printf("Ok! Im listening..\n");
contents = checkp(malloc(len + 1));
if (fread(contents, 1, len, stdin) != len) {
ERR("Not enough data received!\n");
goto exit;
}
contents[len] = '\0';
if ((cached.valid = parse_file(&cached, contents, len, &modelname))) {
if (save_submission(&cached, contents, len) != OK)
ERR("Failed to save your submission!\n");
else
printf("Your file was saved with ID %s!\n", cached.hash);
}
exit:
free(contents);
free(modelname);
}
void
search_cmd(const char *arg)
{
const char *hash, *resp = NULL;
char *indexpath = NULL, *filename = NULL,
*seldir = NULL;
int i, c, matchlen, reslen;
FILE *f;
/* either choose cached or request new entry */
if (arg && !strcmp(arg, "last")) {
if (!cached.valid) {
ERR("No cached info report available\n");
return;
}
hash = cached.hash;
} else {
hash = mhash(arg ? arg : ask("> Model name: "), -1);
}
/* open and lock index file access */
indexpath = aprintf("%s/.index", resultdir);
if (!(f = fopen(indexpath, "r"))) {
ERR("Sorry, no files currently available\n");
goto exit;
}
flock(fileno(f), LOCK_SH);
/* output lines that have hash as prefix */
reslen = matchlen = 0;
filename = aprintf("%s%s", loggedin ? "." : "", hash);
while ((c = fgetc(f)) > 0) {
if (c == '\n') {
matchlen = 0;
} else if (!matchlen && (c == '.') != loggedin) {
matchlen = -1;
} else if (matchlen >= 0 && c == filename[matchlen]) {
matchlen += 1;
if (matchlen == strlen(filename)) {
fseek(f, -matchlen, SEEK_CUR);
putchar(' ');
while ((c = fgetc(f)) > 0 && c != '\n')
putchar(c);
putchar('\n');
matchlen = 0;
reslen += 1;
}
} else {
matchlen = -1;
}
}
flock(fileno(f), LOCK_UN);
fclose(f);
if (!reslen) {
ERR("Sorry, no files matching that name\n");
goto exit;
}
/* allow user to choose files to access */
while (1) {
resp = ask("> Enter %s [q to quit]: ",
resp ? "another" : "hash");
if (strchr(resp, 'q')) break;
if (checkalph(resp, ".abcdef0123456789-") != OK) {
ERR("Invalid model id specified\n");
goto exit;
}
seldir = aprintf("%s/%s", resultdir, resp);
if (handle_download(seldir) != OK) goto exit;
FREE(seldir);
}
exit:
free(filename);
free(seldir);
free(indexpath);
return;
}
void
list_cmd(const char *arg)
{
struct parseinfo info;
char buf[256], *path;
FILE *f, *fn;
if (!loggedin) {
ERR("Not logged in!\n");
return;
}
path = aprintf("%s/.index", resultdir);
if (!(f = fopen(path, "r"))) {
ERR("Failed to get files index\n");
free(path);
return;
}
free(path);
flock(fileno(f), LOCK_SH);
while (fgets(buf, sizeof(buf), f)) {
if (*buf && buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
printf(">> %s\n", buf);
path = aprintf("%s/%s/info", resultdir, buf);
if ((fn = fopen(path, "r")) && load_info(&info, fn) == OK) {
print_info(&info);
free_info(&info);
} else {
ERR("Failed to read file info!\n");
}
if (fn) fclose(fn);
free(path);
}
flock(fileno(f), LOCK_UN);
fclose(f);
}
void
auth_cmd(const char *arg)
{
const char *hash;
char *ndir, *indexpath;
int ret, existed;
FILE *f;
if (loggedin) {
ERR("Already logged in!\n");
return;
}
existed = 0;
hash = mhash(arg ? arg : ask("> Enter a password: "), -1);
ndir = aprintf("%s/.%s", resultdir, hash);
ret = mkdir(ndir, S_IRWXU | S_IRWXG | S_IRWXO);
if (!ret) {
printf("Logged in with ID %s!\n", hash);
} else if (ret && errno == EEXIST) {
existed = 1;
printf("Logged in with ID %s!\nWelcome back!\n", hash);
} else {
ERR("Auth failed!\n");
return;
}
if (!existed) {
indexpath = aprintf("%s/.index", resultdir);
if (!(f = fopen(indexpath, "a+"))) {
free(indexpath);
ERR("Auth failed!\n");
return;
}
flock(fileno(f), LOCK_EX);
fputc('.', f);
fwrite(hash, 1, strlen(hash), f);
fputc('\n', f);
flock(fileno(f), LOCK_UN);
fclose(f);
free(indexpath);
}
free(resultdir);
resultdir = ndir;
loggedin = 1;
free_info(&cached);
}
void
cleanexit()
{
free_info(&cached);
free(resultdir);
}
int
main()
{
const char *cmd, *envstr;
int exit, i, cmdlen;
char *cp, *arg;
if (!(envstr = getenv("RESULTDIR")))
die("RESULTDIR not defined\n");
resultdir = checkp(strdup(envstr));
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
atexit(cleanexit);
dump("msgs/banner");
motd();
exit = 0;
while (!exit) {
errno = 0;
cmd = ask("\r$ ");
if (!*cmd && errno == EBADMSG) break;
if (!*cmd) continue;
cp = strchr(cmd, ' ');
arg = cp ? cp + 1 : NULL;
cmdlen = cp ? cp - cmd : strlen(cmd);
for (i = 0; i < ARRSIZE(commands); i++) {
if (!strncmp(commands[i].name, cmd, cmdlen)
&& !commands[i].name[cmdlen]) {
commands[i].func(arg);
break;
}
}
if (i == ARRSIZE(commands) && strlen(cmd) != 0)
ERR("No such command!\n");
}
return EXIT_SUCCESS;
}
|