commit 7cc88b34e67b3d35ca10bcbf8b393dbc2713b63e
parent cc1bbb8f1e827863b679932496cf06fa3d5bf81a
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 24 Jun 2021 12:35:56 +0200
added ability to request mulitple files in search without restarting
Diffstat:
M | checker/src/checker.py | | | 45 | +++++++++++++++++++-------------------------- |
M | service/src/main.c | | | 121 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
M | src/main.c | | | 121 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
3 files changed, 157 insertions(+), 130 deletions(-)
diff --git a/checker/src/checker.py b/checker/src/checker.py
@@ -204,6 +204,7 @@ class STLDoctorChecker(BaseChecker):
conn.write(modelname + b"\n")
conn.write("0\n") # first result
conn.write("y\n" if download else "\n")
+ conn.write("q\n") # quit
# Wait for end of info box
resp = conn.recvuntil("================== \n")
@@ -353,39 +354,31 @@ class STLDoctorChecker(BaseChecker):
conn = self.openconn()
resp = self.getfile(conn, name, download=False)
conn.write("search last\n")
- filelist = [l.strip().split(b" : ") for l in conn.recvuntil("?").split(b"\n") if b" : " in l]
+ filelist = [l.strip().split(b" : ")[1] for l in conn.recvuntil("? ").split(b"\n") if b" : " in l]
if len(filelist) == 0:
raise BrokenServiceException("Failed to list files through search")
- index_dict = {fl[1]: fl[0] for fl in filelist}
- targets = [fl[1] for fl in filelist]
# Use it to enumerate other files and grab contents
found = None
- self.debug("Targets:\n" + "\n".join([' - ' + l.decode('latin1') for l in targets]))
- for i,fhash in enumerate(targets):
- if index_dict[fhash] == None:
- self.debug(b"Skipping now missing file " + fhash)
- continue
-
- # Retrieve current file
- self.debug(b"Retrieving file " + fhash + b" at index " + index_dict[fhash])
- conn.write(index_dict[fhash] + b"\ny\n")
- fileinfo = conn.recvuntil(self.prompt)
- #self.debug("File contents:\n" + fileinfo.decode("latin1"))
- found = self.search_flag_bytes(fileinfo)
- if found is not None or i == len(targets) - 1:
+ self.debug("Targets:\n" + "\n".join([' - ' + l.decode('latin1') for l in filelist]))
+ for i, fhash in enumerate(filelist):
+ self.debug(f"Retrieving file {fhash} at index {i}")
+ conn.write(f"{i}\ny\n")
+ resp = conn.recvuntil("==================")
+ resp += conn.recvuntil(b"Here you go.. (")
+ try:
+ size = int(conn.recvuntil(b"B)\n")[:-3])
+ except:
+ raise BrokenServiceException("Download size is not a valid integer")
+ resp += conn.recvn(size)
+ resp += conn.recvuntil("? ")
+ self.debug(resp)
+ found = self.search_flag_bytes(resp)
+ if found is not None or i == len(filelist) - 1:
break
- # Parse evil file again for next iter
- self.getfile(conn, name, download=False)
- conn.write("search last\n")
-
- # Update indicies from new search
- filelist = [l.strip().split(b" : ") for l in conn.recvuntil("?").split(b"\n") if b" : " in l]
- index_dict = {name : None for name in targets}
- for fl in filelist:
- index_dict[fl[1]] = fl[0]
-
+ conn.write("q\n")
+ conn.recvuntil(self.prompt)
self.closeconn(conn)
if found is None:
diff --git a/service/src/main.c b/service/src/main.c
@@ -99,6 +99,57 @@ access_authorized(const char *file)
|| (!loggedin && file[0] != '.');
}
+int
+handle_download(const char *scandir)
+{
+ char *infopath, *modelpath;
+ size_t i, size;
+ FILE *f;
+
+ infopath = aprintf("%s/%s", scandir, "info");
+ if (!(f = fopen(infopath, "r"))) {
+ printf("Selected result is missing!\n");
+ goto cleanup;
+ }
+ free_info(&cached);
+ if (load_info(&cached, f) != OK) {
+ printf("Failed to parse info file!\n");
+ 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"))) {
+ printf("Failed to access file!\n");
+ goto cleanup;
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (size > MAXFILESIZE) {
+ printf("File is too large to send!\n");
+ goto cleanup;
+ }
+ printf("Here you go.. (%liB)\n", size);
+ while ((i = getc(f)) != EOF)
+ putc(i, stdout);
+ fclose(f);
+ f = NULL;
+ }
+
+ return 0;
+
+cleanup:
+ if (f) fclose(f);
+ free(infopath);
+ free(modelpath);
+ return 1;
+}
+
void
cat_cmd(const char *arg)
{
@@ -176,14 +227,11 @@ cleanup:
void
search_cmd(const char *arg)
{
- char *end, *scandir = NULL, *infopath = NULL,
- *modelpath = NULL, **paths = NULL;
- int i, which, dirstart, ishidden, pathc, pathcap = 100;
- const char *hash, *name;
+ char *end, *scandir = NULL, **paths = NULL;
+ int i, which, pathc, pathcap = 100;
+ const char *hash, *name, *resp;
struct dirent *de;
DIR *d = NULL;
- FILE *f = NULL;
- size_t size;
if (arg && !strcmp(arg, "last")) {
if (!cached.valid) {
@@ -201,7 +249,6 @@ search_cmd(const char *arg)
}
paths = checkp(malloc(pathcap * sizeof(char*)));
- dirstart = telldir(d);
for (pathc = 0; (de = readdir(d));) {
if (access_authorized(de->d_name)
&& !strpfcmp(hash, de->d_name + (loggedin ? 1 : 0))) {
@@ -220,55 +267,25 @@ search_cmd(const char *arg)
goto cleanup;
}
- which = strtoul(ask("Which of these results? "), &end, 10);
- if (which >= pathc || which < 0 || *end) {
- printf("Invalid index!\n");
- goto cleanup;
- }
+ while (1) {
+ resp = ask("Which result [q to quit]? ");
+ if (strchr(resp, 'q')) break;
+ which = strtoul(resp, &end, 10);
+ if (which >= pathc || which < 0 || *end) {
+ printf("Invalid index!\n");
+ goto cleanup;
+ }
- scandir = aprintf("%s/%s", resultdir, paths[which]);
+ scandir = aprintf("%s/%s", resultdir, paths[which]);
- infopath = aprintf("%s/%s", scandir, "info");
- if (!(f = fopen(infopath, "r"))) {
- printf("Selected result is missing!\n");
- goto cleanup;
- }
- free_info(&cached);
- if (load_info(&cached, f) != OK) {
- printf("Failed to parse info file!\n");
- goto cleanup;
- }
- fclose(f);
- f = NULL;
+ if (handle_download(scandir)) goto cleanup;
- print_info(&cached);
-
- if (strchr(ask("Download the model? "), 'y')) {
- modelpath = aprintf("%s/%s", scandir, "model");
- if (!(f = fopen(modelpath, "r"))) {
- printf("Failed to access file!\n");
- goto cleanup;
- }
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fseek(f, 0, SEEK_SET);
- if (size > MAXFILESIZE) {
- printf("File is too large to send!\n");
- goto cleanup;
- }
- printf("Here you go.. (%liB)\n", size);
- while ((i = getc(f)) != EOF)
- putc(i, stdout);
- fclose(f);
- f = NULL;
+ free(scandir);
+ scandir = NULL;
}
cleanup:
- if (f) fclose(f);
-
free(scandir);
- free(infopath);
- free(modelpath);
for (i = 0; i < pathc; i++) free(paths[i]);
free(paths);
@@ -295,10 +312,10 @@ list_cmd(const char *arg)
printf(">> %s\n", de->d_name);
path = aprintf("%s/%s/info", resultdir, de->d_name);
if ((f = fopen(path, "r"))) {
- if (load_info(&info, f) != OK)
- printf("Failed to read saved file info!\n");
- else
+ if (load_info(&info, f) == OK)
print_info(&info);
+ else
+ printf("Failed to read saved file info!\n");
fclose(f);
}
free(path);
diff --git a/src/main.c b/src/main.c
@@ -99,6 +99,57 @@ access_authorized(const char *file)
|| (!loggedin && file[0] != '.');
}
+int
+handle_download(const char *scandir)
+{
+ char *infopath, *modelpath;
+ size_t i, size;
+ FILE *f;
+
+ infopath = aprintf("%s/%s", scandir, "info");
+ if (!(f = fopen(infopath, "r"))) {
+ fprintf(stderr, "Selected result is missing!\n");
+ goto cleanup;
+ }
+ free_info(&cached);
+ if (load_info(&cached, f) != OK) {
+ fprintf(stderr, "Failed to parse info file!\n");
+ 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"))) {
+ fprintf(stderr, "Failed to access file!\n");
+ goto cleanup;
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (size > MAXFILESIZE) {
+ fprintf(stderr, "File is too large to send!\n");
+ goto cleanup;
+ }
+ printf("Here you go.. (%liB)\n", size);
+ while ((i = getc(f)) != EOF)
+ putc(i, stdout);
+ fclose(f);
+ f = NULL;
+ }
+
+ return 0;
+
+cleanup:
+ if (f) fclose(f);
+ free(infopath);
+ free(modelpath);
+ return 1;
+}
+
void
cat_cmd(const char *arg)
{
@@ -176,14 +227,11 @@ cleanup:
void
search_cmd(const char *arg)
{
- char *end, *scandir = NULL, *infopath = NULL,
- *modelpath = NULL, **paths = NULL;
- int i, which, dirstart, ishidden, pathc, pathcap = 100;
- const char *hash, *name;
+ char *end, *scandir = NULL, **paths = NULL;
+ int i, which, pathc, pathcap = 100;
+ const char *hash, *name, *resp;
struct dirent *de;
DIR *d = NULL;
- FILE *f = NULL;
- size_t size;
if (arg && !strcmp(arg, "last")) {
if (!cached.valid) {
@@ -201,7 +249,6 @@ search_cmd(const char *arg)
}
paths = checkp(malloc(pathcap * sizeof(char*)));
- dirstart = telldir(d);
for (pathc = 0; (de = readdir(d));) {
if (access_authorized(de->d_name)
&& !strpfcmp(hash, de->d_name + (loggedin ? 1 : 0))) {
@@ -220,55 +267,25 @@ search_cmd(const char *arg)
goto cleanup;
}
- which = strtoul(ask("Which of these results? "), &end, 10);
- if (which >= pathc || which < 0 || *end) {
- fprintf(stderr, "Invalid index!\n");
- goto cleanup;
- }
+ while (1) {
+ resp = ask("Which result [q to quit]? ");
+ if (strchr(resp, 'q')) break;
+ which = strtoul(resp, &end, 10);
+ if (which >= pathc || which < 0 || *end) {
+ fprintf(stderr, "Invalid index!\n");
+ goto cleanup;
+ }
- scandir = aprintf("%s/%s", resultdir, paths[which]);
+ scandir = aprintf("%s/%s", resultdir, paths[which]);
- infopath = aprintf("%s/%s", scandir, "info");
- if (!(f = fopen(infopath, "r"))) {
- fprintf(stderr, "Selected result is missing!\n");
- goto cleanup;
- }
- free_info(&cached);
- if (load_info(&cached, f) != OK) {
- fprintf(stderr, "Failed to parse info file!\n");
- goto cleanup;
- }
- fclose(f);
- f = NULL;
+ if (handle_download(scandir)) goto cleanup;
- print_info(&cached);
-
- if (strchr(ask("Download the model? "), 'y')) {
- modelpath = aprintf("%s/%s", scandir, "model");
- if (!(f = fopen(modelpath, "r"))) {
- fprintf(stderr, "Failed to access file!\n");
- goto cleanup;
- }
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fseek(f, 0, SEEK_SET);
- if (size > MAXFILESIZE) {
- fprintf(stderr, "File is too large to send!\n");
- goto cleanup;
- }
- printf("Here you go.. (%liB)\n", size);
- while ((i = getc(f)) != EOF)
- putc(i, stdout);
- fclose(f);
- f = NULL;
+ free(scandir);
+ scandir = NULL;
}
cleanup:
- if (f) fclose(f);
-
free(scandir);
- free(infopath);
- free(modelpath);
for (i = 0; i < pathc; i++) free(paths[i]);
free(paths);
@@ -295,10 +312,10 @@ list_cmd(const char *arg)
printf(">> %s\n", de->d_name);
path = aprintf("%s/%s/info", resultdir, de->d_name);
if ((f = fopen(path, "r"))) {
- if (load_info(&info, f) != OK)
- fprintf(stderr, "Failed to read saved file info!\n");
- else
+ if (load_info(&info, f) == OK)
print_info(&info);
+ else
+ fprintf(stderr, "Failed to read saved file info!\n");
fclose(f);
}
free(path);