From e3fd95c7a599aedb8798ab27813d2457d99be3b3 Mon Sep 17 00:00:00 2001 From: arf20 Date: Tue, 2 Dec 2025 21:36:03 +0100 Subject: generating results --- config.c | 10 +++---- config.h | 3 +- index.c | 9 ++++-- index.htm.tmpl | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- main.c | 52 ++++++++++++++++++++++++++------ search | Bin 47088 -> 47776 bytes search.cfg | 3 ++ 7 files changed, 143 insertions(+), 26 deletions(-) diff --git a/config.c b/config.c index bc9eed6..d99cc2c 100644 --- a/config.c +++ b/config.c @@ -28,7 +28,7 @@ #include unsigned short port = 0; -char *tmpl_path = NULL, *root = NULL; +char *tmpl_path = NULL, *root = NULL, *subdir = NULL; int config_load(const char *conf_path) @@ -39,10 +39,6 @@ config_load(const char *conf_path) return -1; } - //fseek(cfgf , 0, SEEK_END); - //size_t cfgsize = ftell(cfgf); - //rewind(cfgf); - printf("config:\n"); char line[256]; @@ -75,6 +71,10 @@ config_load(const char *conf_path) value[strlen(value) - 1] = '\0'; root = strdup(value); printf("\troot: %s\n", root); + } else if (strcmp(line, "subdir") == 0) { + value[strlen(value) - 1] = '\0'; + subdir = strdup(value); + printf("\tsubdir: %s\n", subdir); } else { fprintf(stderr, "[config] unknown key: %s\n", line); continue; diff --git a/config.h b/config.h index 16e1ca5..b12f226 100644 --- a/config.h +++ b/config.h @@ -31,8 +31,7 @@ /* config */ extern unsigned short port; -extern char *tmpl_path; -extern char *root; +extern char *tmpl_path, *root, *subdir; int config_load(const char *conf_path); diff --git a/index.c b/index.c index 57c05d7..3430178 100644 --- a/index.c +++ b/index.c @@ -171,6 +171,7 @@ index_recurse(size_t size, const char *dir, int examine, size_t rootlen) /* stat it */ node_data_t *data = malloc(sizeof(node_data_t)); + memset(data, 0, sizeof(node_data_t)); data->name = strdup(de->d_name); data->path = strdup(&path[rootlen]); if (stat(path, &data->stat) < 0) { @@ -182,11 +183,13 @@ index_recurse(size_t size, const char *dir, int examine, size_t rootlen) /* examine */ if (examine) { - data->mime = magic_file(magic_cookie, path); - if (!data->mime) + const char *mime = magic_file(magic_cookie, path); + if (!mime) { fprintf(stderr, "[index] error magic_file() %s: %s\n", path, magic_error(magic_cookie)); - } + } else + data->mime = strdup(mime); + } /* recurse */ map_t *child = NULL; diff --git a/index.htm.tmpl b/index.htm.tmpl index fd21578..fdb1b58 100644 --- a/index.htm.tmpl +++ b/index.htm.tmpl @@ -5,10 +5,12 @@ ARFNET @@ -59,12 +95,54 @@

Search

Search all of the ARFNET content fast

-
-
+ +
- - -
+
+
+
+
+ Advanced + + + + + + + + +
+
+ Filtering + +
+ +
+ +
+ +
+
+
+ Sorting +
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
%s
diff --git a/main.c b/main.c index 9aa7e76..f3492aa 100644 --- a/main.c +++ b/main.c @@ -42,21 +42,48 @@ static char *index_format_template = NULL; static index_t g_index = NULL; static const char *result_html_template = - "

%s

%s
\n"; + "
\n" + "%s""%s
\n" + "%s
%s%s

\n" + "
\n"; +static const char * +sizestr(size_t size) +{ + static char buf[32]; + if (size < 1024) + snprintf(buf, 32, "%ld B", size); + else if (size < 1024LL * 1024LL) + snprintf(buf, 32, "%.2f KiB", (float)size/1024.f); + else if (size < 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f MiB", (float)size/(1024.f*1024.f)); + else if (size < 1024LL * 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f GiB", (float)size/(1024.f*1024.f*1024.f)); + else if (size < 1024LL * 1024LL * 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f TiB", (float)size/(1024.f*1024.f*1024.f*1024.f)); + return buf; +} static const char * generate_results_html(results_t *results) { - static char buff[65535]; + static char buff[65535], timebuf[256], urlbuf[4096]; char *pos = buff; for (int i = 0; i < results->size; i++) { + const node_data_t *data = results->results[i]; + struct tm *tm_mtim = gmtime(&data->stat.st_mtim.tv_sec); + strftime(timebuf, 256, "%Y-%m-%d %H:%M:%S", tm_mtim); + + snprintf(urlbuf, 4096, "%s%s", subdir, data->path); + pos += snprintf(pos, 65535 - (pos - buff), result_html_template, - results->results[i]->name, - results->results[i]->path + data->name, + data->mime ? data->mime : "", + urlbuf, data->path, + sizestr(data->stat.st_size), timebuf ); } @@ -103,15 +130,22 @@ enum MHD_Result answer_to_connection( const char *query = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "query"); - results_t *results = index_lookup(g_index, LOOKUP_SUBSTR, query); + results_t *results = NULL; + if (g_index) + results = index_lookup(g_index, LOOKUP_SUBSTR, query); - snprintf(buff, BUFF_SIZE, index_format_template, query, - generate_results_html(results)); + if (results) + snprintf(buff, BUFF_SIZE, index_format_template, query, + generate_results_html(results)); + else + snprintf(buff, BUFF_SIZE, index_format_template, query, + "indexing in progress... try again later"); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); - results_destroy(results); + if (results) + results_destroy(results); printf("%d\n", 200); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); @@ -171,7 +205,7 @@ int main() { printf("[%s] [index] indexeding started...\n", timestr); - g_index = index_new(INIT_MAP_CAPACITY, root, 0); + g_index = index_new(INIT_MAP_CAPACITY, root, 1); time_now = time(NULL); tm_now = gmtime(&time_now); diff --git a/search b/search index e7e9576..8cf64c1 100755 Binary files a/search and b/search differ diff --git a/search.cfg b/search.cfg index b8110dd..b0eaed8 100644 --- a/search.cfg +++ b/search.cfg @@ -9,3 +9,6 @@ template=index.htm.tmpl # root root=/home/arf20/projects/arfminesweeper +# http subdirectory for file links +subdir=/files/ + -- cgit v1.2.3