diff options
Diffstat (limited to 'service/src')
| -rw-r--r-- | service/src/main.c | 93 |
1 files changed, 60 insertions, 33 deletions
diff --git a/service/src/main.c b/service/src/main.c index 5f3282d..cc92cf5 100644 --- a/service/src/main.c +++ b/service/src/main.c @@ -2,7 +2,6 @@ #include <string.h> #include <stdarg.h> #include <unistd.h> -#include <dirent.h> #include <fcntl.h> #include <time.h> #include <errno.h> @@ -56,7 +55,6 @@ save_submission(struct parseinfo *info, char *stldata, int stlsize) *indexpath = NULL, *modelpath = NULL; FILE *f = NULL; int status = OK; - DIR *d; modeldir = aprintf("%s%s-%i", loggedin ? "." : "", info->hash, time(NULL)); @@ -296,22 +294,28 @@ search_cmd(const char *arg) while ((c = fgetc(f)) > 0) { if (c == '\n') { matchlen = 0; + continue; } else if (matchlen == -1) { continue; + } else if (!matchlen && c == '.') { + if (!loggedin) matchlen = -1; + continue; } else if (c == hash[matchlen]) { matchlen += 1; - if (matchlen == strlen(hash)) { - fseek(f, -matchlen, SEEK_CUR); - putchar(' '); - while ((c = fgetc(f)) > 0 && c != '\n') - putchar(c); - putchar('\n'); - matchlen = 0; - reslen += 1; - } } else { matchlen = -1; } + + if (matchlen == strlen(hash)) { + fseek(f, -matchlen, SEEK_CUR); + putchar(' '); + if (loggedin) putchar('.'); + while ((c = fgetc(f)) > 0 && c != '\n') + putchar(c); + putchar('\n'); + matchlen = 0; + reslen += 1; + } } flock(fileno(f), LOCK_UN); @@ -326,7 +330,7 @@ search_cmd(const char *arg) resp = ask("> Enter %s [q to quit]: ", resp ? "another" : "hash"); if (strchr(resp, 'q')) break; - if (checkalph(resp, "abcdef0123456789-") != OK) { + if (checkalph(resp, ".abcdef0123456789-") != OK) { ERR("Invalid model id specified\n"); goto exit; } @@ -345,43 +349,50 @@ exit: void list_cmd(const char *arg) { - struct dirent *de; struct parseinfo info; - char *path; - FILE *f; - DIR *d; + char buf[256], *path; + FILE *f, *fn; if (!loggedin) { ERR("Not logged in!\n"); return; } - if (!(d = opendir(resultdir))) return; - - while ((de = readdir(d))) { - if (*de->d_name == '.' && !strchr(".", de->d_name[1])) { - printf(">> %s\n", de->d_name); - path = aprintf("%s/%s/info", resultdir, de->d_name); - if ((f = fopen(path, "r")) && load_info(&info, f) == OK) { - print_info(&info); - free_info(&info); - } else { - ERR("Failed to read file info!\n"); - } - if (f) fclose(f); - free(path); - } + path = aprintf("%s/.index", resultdir); + if (!(f = fopen(path, "r"))) { + ERR("Failed to get files index\n"); + free(path); + return; } + free(path); - closedir(d); + 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(f); + free(path); + } + flock(fileno(f), LOCK_UN); + fclose(f); } void auth_cmd(const char *arg) { const char *hash; - char *ndir; + char *ndir, *indexpath; int ret; + FILE *f; if (loggedin) { ERR("Already logged in!\n"); @@ -400,6 +411,22 @@ auth_cmd(const char *arg) return; } + if (errno != EEXIST) { + 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; |
