[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 12/16] fixes


MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

From: arf20 <aruizfernandez05@xxxxxxxxx>

---
 README.md  | 19 +++++++++++++------
 index.c    | 22 ++++++++++++++++++++++
 main.c     | 27 +++++++++++++++++----------
 search.cfg |  2 +-
 4 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
index bc7838c..6430da3 100644
--- a/README.md
+++ b/README.md
@@ -6,19 +6,16 @@ ARFNET Fast file index and search
 
  - C webapp
  - libmicrohttpd
- - POSIX regex
 
 ## Features
 
  - All cached indexed in memory
- - Landing page w roots and searchbox
- - File directory tree browsing
-    - Metadata (mime type, size)
-    - Icons
-    - Sorting
+ - Searchbox
+ - Periodic reindexing and inotify
  - Searching
     - Advanced name substring, exact, regex
     - Sorting
+    - Filtering
 
 ## Building
 
@@ -28,3 +25,13 @@ Depends on libmicrohttpd, libmagic
 make
 ```
 
+## TODO
+
+ - [ ] Regex query
+ - [ ] inotify
+
+## Bugs
+
+ - [ ] Query type not saved on submit
+ - [ ] Long output gets cut after ~300 results
+
diff --git a/index.c b/index.c
index 096fe6e..3840549 100644
--- a/index.c
+++ b/index.c
@@ -262,6 +262,8 @@ index_recurse(size_t size, const char *dir, int examine, size_t rootlen)
         map_insert(map, de->d_name, data, child);
     }
 
+    closedir(dirp);
+
     return map;
 }
 
@@ -291,13 +293,33 @@ void
 index_lookup_substr_caseinsensitive(map_t *index, const char *query,
     results_t *results)
 {
+    for (size_t i = 0; i < index->size; i++) {
+        if (!index->map[i].data)
+            continue;
 
+        for (struct node_s *node = &index->map[i]; node; node = node->next) {
+            if (strcasestr(node->data->name, query))
+                results_insert(results, node->data);
+            if (node->child)
+                index_lookup_substr(node->child, query, results);
+        }
+    }
 }
 
 void
 index_lookup_exact(map_t *index, const char *query, results_t *results)
 {
+    for (size_t i = 0; i < index->size; i++) {
+        if (!index->map[i].data)
+            continue;
 
+        for (struct node_s *node = &index->map[i]; node; node = node->next) {
+            if (strcmp(node->data->name, query) == 0)
+                results_insert(results, node->data);
+            if (node->child)
+                index_lookup_substr(node->child, query, results);
+        }
+    }
 }
 
 void
diff --git a/main.c b/main.c
index d4e0a14..7cdd4fd 100644
--- a/main.c
+++ b/main.c
@@ -116,10 +116,12 @@ sizestr(size_t size)
     return buf;
 }
 
-static const char *
+static char *
 generate_results_html(results_t *results)
 {
-    static char buff[65535], timebuf[256], urlbuf[4096];
+    static char timebuf[256], urlbuf[4096];
+    
+    char *buff = malloc(1024 * results->size); /* alloc 1K per result */
     char *pos = buff;
   
     for (int i = 0; i < results->size; i++) {
@@ -129,7 +131,7 @@ generate_results_html(results_t *results)
 
         snprintf(urlbuf, 4096, "%s%s", subdir, data->path);
 
-        pos += snprintf(pos, 65535 - (pos - buff),
+        pos += snprintf(pos, 1024,
             result_html_template,
             data->name,
             data->mime ? data->mime : "",
@@ -279,7 +281,9 @@ enum MHD_Result answer_to_connection(
         float lookup_time = (finish.tv_sec + (0.000000001 * finish.tv_nsec)) - 
             (start.tv_sec + (0.000000001 * start.tv_nsec));
 
-        if (query && results)
+        char *results_html = NULL;
+        if (query && results) {
+            results_html = generate_results_html(results);
             snprintf(buff, BUFF_SIZE, index_format_template, query,
                 filter_time_low ? filter_time_low : "",
                 filter_time_high ? filter_time_high : "",
@@ -287,7 +291,8 @@ enum MHD_Result answer_to_connection(
                 filter_size_high ? filter_size_high : "",
                 generate_results_header_html(connection, baseurl, sort_type,
                     sort_order, results->size, lookup_time),
-                generate_results_html(results));
+                results_html);
+        }
         else
             snprintf(buff, BUFF_SIZE, index_format_template, "", "",
                 "", "", "", "", "indexing in progress... try again later");
@@ -303,6 +308,7 @@ enum MHD_Result answer_to_connection(
         printf("%d\n", 200);
         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
         MHD_destroy_response(response);
+        free(results_html);
     }
     else {
         response = MHD_create_response_from_buffer(0, (void*)NULL, 0);
@@ -352,8 +358,8 @@ int main() {
 
     /* index loop */
     do {
-        time_t time_now = time(NULL);
-        struct tm *tm_now = gmtime(&time_now);
+        time_t time_start = time(NULL);
+        struct tm *tm_now = gmtime(&time_start);
         static char timestr[256];
         strftime(timestr, 256, "%Y-%m-%d %H:%M:%S", tm_now);
 
@@ -364,11 +370,12 @@ int main() {
 
         g_index = index_new(INIT_MAP_CAPACITY, root, magic_enable);
 
-        time_now = time(NULL);
-        tm_now = gmtime(&time_now);
+        time_t time_stop = time(NULL);
+        tm_now = gmtime(&time_stop);
         strftime(timestr, 256, "%Y-%m-%d %H:%M:%S", tm_now);
 
-        printf("[%s] [index] indexed finished\n", timestr);
+        printf("[%s] [index] indexed finished (%ld s)\n", timestr,
+            time_stop - time_start);
 
         sleep(period);
     } while (1);
diff --git a/search.cfg b/search.cfg
index 9d0baba..83aa8f1 100644
--- a/search.cfg
+++ b/search.cfg
@@ -7,7 +7,7 @@ port=8888
 template=index.htm.tmpl
 
 # root
-root=/home/arf20/projects/arfminesweeper
+root=/home/arf20/projects
 
 # read magic numbers (mime type)
 magic=false
-- 
2.47.3


References:
[arfnet2-search PATCH 00/16] Pre-release deploymentarf20 <arf20@xxxxxxxxx>