From 1490268ea6ae75e5e9b78861c56dc9b04db903f3 Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Mon, 10 May 2021 17:18:28 +0200 Subject: extended query function to load file info, added poc test --- service/src/printdoc.c | 211 +++++++++++++++++++++++++++++-------------------- 1 file changed, 125 insertions(+), 86 deletions(-) (limited to 'service/src/printdoc.c') diff --git a/service/src/printdoc.c b/service/src/printdoc.c index 951f40b..3224724 100644 --- a/service/src/printdoc.c +++ b/service/src/printdoc.c @@ -10,99 +10,51 @@ #include "stlfile.h" #include "util.h" +#define MAXFILESIZE 7000 + struct command { const char *name; void (*func)(char *); }; -void search_cmd(char *arg); -void submit_cmd(char *arg); -void list_cmd(char *arg); +int save_submission(struct parseinfo *info, char *data, int len); + void cat_cmd(char *arg); +void list_cmd(char *arg); +void submit_cmd(char *arg); +void query_cmd(char *arg); struct command commands[] = { - { "submit", submit_cmd }, { "cat", cat_cmd }, { "help", list_cmd }, - { "search", search_cmd } + { "submit", submit_cmd }, + { "query", query_cmd }, }; -struct parseinfo lastrun; +struct parseinfo cached; const char *resultdir; -void -search_cmd(char *arg) -{ - char *end, *resultfile = NULL; - const char *hash; - struct dirent *de; - int i, which; - DIR *d = NULL; - - if (lastrun.valid) - hash = lastrun.hash; - else - hash = mhash(ask("What is the model name?\n"), -1); - - d = opendir("scans"); - if (!d) return; - - i = 0; - while ((de = readdir(d))) { - if (de->d_type != DT_DIR && !strpfcmp(hash, de->d_name)) { - printf("%i : %s\n", i, de->d_name); - i++; - } - } - - if (i == 0) { - fprintf(stderr, "Sorry, couldnt find a matching scan result!\n"); - goto cleanup; - } else { - which = strtoul(ask("Which of these results?\n"), &end, 10); - if (which >= i || which < 0 || *end) { - fprintf(stderr, "Invalid index!\n"); - goto cleanup; - } - } - - while ((de = readdir(d))) { - if (de->d_type != DT_DIR && !strpfcmp(hash, de->d_name)) { - if (i == which) { - resultfile = aprintf("scans/%s/info", strdup(de->d_name)); - break; - } - i++; - } - } - - if (resultfile) - dump(resultfile); - -cleanup: - free(resultfile); - closedir(d); -} - int -save_submission(char *stldata, int stlsize) +save_submission(struct parseinfo *info, char *stldata, int stlsize) { DIR *d; FILE *f; char *dirpath = NULL, *infopath = NULL, *modelpath = NULL; - dirpath = aprintf("%s/%s-%i", resultdir, lastrun.hash, time(NULL)); + dirpath = aprintf("%s/%s-%i", resultdir, info->hash, time(NULL)); 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); + f = NULL; infopath = aprintf("%s/%s", dirpath, "info"); if (!(f = fopen(infopath, "w+"))) goto fail; - if (save_info(&lastrun, f) != OK) goto fail; + if (save_info(info, f) != OK) goto fail; fclose(f); + f = NULL; free(dirpath); free(modelpath); @@ -111,6 +63,8 @@ save_submission(char *stldata, int stlsize) return OK; fail: + if (f) fclose(f); + remove(infopath); remove(modelpath); remove(dirpath); @@ -122,6 +76,26 @@ fail: return FAIL; } +void +cat_cmd(char *arg) +{ + if (arg && !strncmp(arg, "flag", 4)) + dump("msgs/cat_flag"); + else + printf("meow\n"); +} + +void +list_cmd(char *arg) +{ + int i; + + printf("Available commands:\n"); + for (i = 0; i < ARRSIZE(commands); i++) + printf("%s%s", i ? " " : "", commands[i].name); + printf("\n"); +} + void submit_cmd(char *arg) { @@ -129,48 +103,109 @@ submit_cmd(char *arg) char *end, *contents; size_t len; - bufp = ask("How large is your file?\n"); + bufp = ask("> How large is your file? "); len = strtoul(bufp, &end, 10); - if (len <= 0 || len >= 10000 || *end) { + if (len <= 0 || len >= MAXFILESIZE || *end) { fprintf(stderr, "Invalid file length!\n"); return; } - printf("Ok! Im listening..\n"); + printf("> Ok! Im listening..\n"); contents = checkp(malloc(len + 1)); fread(contents, 1, len, stdin); contents[len] = '\0'; - lastrun.valid = parse_file(&lastrun, contents, len); - - if (lastrun.valid) { - if (save_submission(contents, len) != OK) + if ((cached.valid = parse_file(&cached, contents, len))) { + if (save_submission(&cached, contents, len) != OK) fprintf(stderr, "Failed to save your submission!\n"); else - printf(">> Your file was saved with ID %s!\n", lastrun.hash); + printf("> Your file was saved with ID %s!\n", cached.hash); } free(contents); } void -cat_cmd(char *arg) +query_cmd(char *arg) { - if (arg && !strncmp(arg, "flag", 4)) - dump("msgs/cat_flag"); + char *end, *scandir = NULL, *infopath = NULL, *modelpath = NULL; + const char *hash; + struct dirent *de; + int i, which, dirstart; + DIR *d = NULL; + FILE *f = NULL; + size_t size; + + if (cached.valid) + hash = cached.hash; else - printf("meow\n"); -} + hash = mhash(ask("> What is the model name? "), -1); -void -list_cmd(char *arg) -{ - int i; + if (!(d = opendir(resultdir))) return; - printf("Available commands:\n"); - for (i = 0; i < ARRSIZE(commands); i++) - printf("%s%s", i ? " " : "", commands[i].name); - printf("\n"); + dirstart = telldir(d); + for (i = 0; (de = readdir(d));) { + if (de->d_type == DT_DIR && !strpfcmp(hash, de->d_name)) { + printf("%i : %s\n", i, de->d_name); + i++; + } + } + + if (i == 0) { + fprintf(stderr, "Sorry, couldnt find a matching scan result!\n"); + goto cleanup; + } else { + which = strtoul(ask("> Which of these results? "), &end, 10); + if (which >= i || which < 0 || *end) { + fprintf(stderr, "Invalid index!\n"); + goto cleanup; + } + } + + seekdir(d, dirstart); + for (i = 0; (de = readdir(d));) { + if (de->d_type == DT_DIR && !strpfcmp(hash, de->d_name)) { + if (i == which) { + scandir = aprintf("%s/%s", resultdir, de->d_name); + break; + } + i++; + } + } + + if (!scandir) { + fprintf(stderr, "Unexpected error!\n"); + goto cleanup; + } + + infopath = aprintf("%s/%s", scandir, "info"); + if (!(f = fopen(infopath, "r"))) goto cleanup; + if (load_info(&cached, f) != OK) goto cleanup; + fclose(f); + f = NULL; + + print_info(&cached); + + if (strchr(ask("> Download the model? "), 'y')) { + modelpath = aprintf("%s/%s", scandir, "model"); + if (!(f = fopen(modelpath, "r"))) goto cleanup; + fseek(f, 0, SEEK_END); + size = ftell(f); + fseek(f, 0, SEEK_SET); + if (size > MAXFILESIZE) goto cleanup; + printf("> Here you go.. (%liB)\n", size); + while ((i = getc(f)) != EOF) + putc(i, stdout); + fclose(f); + f = NULL; + } + +cleanup: + if (f) fclose(f); + closedir(d); + free(scandir); + free(infopath); + free(modelpath); } int @@ -180,7 +215,11 @@ main() int exit, i; if (!(resultdir = getenv("RESULTDIR"))) - resultdir = "/data"; + resultdir = "scans"; + + setvbuf(stdin, NULL, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); dump("msgs/welcome"); @@ -211,5 +250,5 @@ main() } printf("see you later!\n"); - free_info(&lastrun); + free_info(&cached); } -- cgit v1.2.3-71-gd317