chiark / gitweb /
more thorough kvp.c testing
[disorder] / server / trackdb.c
index 1e643ef7ebbc00f0c0e5c135da29fea3403e3115..bc526caad6acff31d0e952c028df74d0f75f0035 100644 (file)
@@ -36,6 +36,7 @@
 #include <sys/resource.h>
 #include <time.h>
 #include <arpa/inet.h>
+#include <sys/wait.h>
 
 #include "event.h"
 #include "mem.h"
@@ -217,7 +218,8 @@ static pid_t subprogram(ev_source *ev, const char *prog,
   /* If we're in the background then trap subprocess stdout/stderr */
   if(!(pid = xfork())) {
     exitfn = _exit;
-    ev_signal_atfork(ev);
+    if(ev)
+      ev_signal_atfork(ev);
     signal(SIGPIPE, SIG_DFL);
     if(outputfd != -1) {
       xdup2(outputfd, 1);
@@ -305,6 +307,7 @@ static DB *open_db(const char *path,
  */
 void trackdb_open(int flags) {
   int newdb, err;
+  pid_t pid;
 
   /* sanity checks */
   assert(opened == 0);
@@ -337,8 +340,15 @@ void trackdb_open(int flags) {
               oldversion, config->dbversion);
       case TRACKDB_CAN_UPGRADE:
         /* This database needs upgrading */
-        fatal(0, "database needs upgrading from %ld to %ld, which is not yet supported",
-              oldversion, config->dbversion);
+        info("invoking disorder-dbupgrade to upgrade from %ld to %ld",
+             oldversion, config->dbversion);
+        pid = subprogram(0, "disorder-dbupgrade", -1);
+        while(waitpid(pid, &err, 0) == -1 && errno == EINTR)
+          ;
+        if(err)
+          fatal(0, "disorder-dbupgrade %s", wstat(err));
+        info("disorder-dbupgrade succeeded");
+        break;
       case TRACKDB_OPEN_FOR_UPGRADE:
         break;
       default:
@@ -627,6 +637,22 @@ static int tailor_underscore_Word_Break_Other(uint32_t c) {
   }
 }
 
+/** @brief Remove all combining characters in-place
+ * @param s Pointer to start of string
+ * @param ns Length of string
+ * @return New, possiblby reduced, length
+ */
+static size_t remove_combining_chars(uint32_t *s, size_t ns) {
+  uint32_t *start = s, *t = s, *end = s + ns;
+
+  while(s < end) {
+    const uint32_t c = *s++;
+    if(!utf32_combining_class(c))
+      *t++ = c;
+  }
+  return t - start;
+}
+
 /** @brief Normalize and split a string using a given tailoring */
 static void word_split(struct vector *v,
                        const char *s,
@@ -640,6 +666,8 @@ static void word_split(struct vector *v,
   /* Erase case distinctions */
   if(!(t32 = utf32_casefold_compat(t32, nt32, &nt32)))
     return;
+  /* Drop combining characters */
+  nt32 = remove_combining_chars(t32, nt32);
   /* Split into words, treating _ as a space */
   w32 = utf32_word_split(t32, nt32, &nw, pt);
   /* Convert words back to UTF-8 and append to result */
@@ -1806,11 +1834,20 @@ char **trackdb_search(char **wordlist, int nwordlist, int *ntracks) {
   const char *dbname;
 
   *ntracks = 0;                                /* for early returns */
-  /* casefold all the words */
+  /* normalize all the words */
   w = xmalloc(nwordlist * sizeof (char *));
   for(n = 0; n < nwordlist; ++n) {
+    uint32_t *w32;
+    size_t nw32;
+    
     w[n] = utf8_casefold_compat(wordlist[n], strlen(wordlist[n]), 0);
     if(checktag(w[n])) ++ntags;         /* count up tags */
+    /* Strip out combining characters (AFTER checking whether it's a tag) */
+    if(!(w32 = utf8_to_utf32(w[n], strlen(w[n]), &nw32)))
+      return 0;
+    nw32 = remove_combining_chars(w32, nw32);
+    if(!(w[n] = utf32_to_utf8(w32, nw32, 0)))
+      return 0;
   }
   /* find the longest non-stopword */
   for(n = 0; n < nwordlist; ++n)
@@ -1971,9 +2008,9 @@ static int reap_rescan(ev_source attribute((unused)) *ev,
                        void attribute((unused)) *u) {
   if(pid == rescan_pid) rescan_pid = -1;
   if(status)
-    error(0, "disorderd-rescan: %s", wstat(status));
+    error(0, RESCAN": %s", wstat(status));
   else
-    D(("disorderd-rescan terminate: %s", wstat(status)));
+    D((RESCAN" terminated: %s", wstat(status)));
   /* Our cache of file lookups is out of date now */
   cache_clean(&cache_files_type);
   eventlog("rescanned", (char *)0);
@@ -1981,14 +2018,22 @@ static int reap_rescan(ev_source attribute((unused)) *ev,
 }
 
 void trackdb_rescan(ev_source *ev) {
+  int w;
+
   if(rescan_pid != -1) {
     error(0, "rescan already underway");
     return;
   }
   rescan_pid = subprogram(ev, RESCAN, -1);
-  ev_child(ev, rescan_pid, 0, reap_rescan, 0);
-  D(("started rescanner"));
-  
+  if(ev) {
+    ev_child(ev, rescan_pid, 0, reap_rescan, 0);
+    D(("started rescanner"));
+  } else {
+    /* This is the first rescan, we block until it is complete */
+    while(waitpid(rescan_pid, &w, 0) < 0 && errno == EINTR)
+      ;
+    reap_rescan(0, rescan_pid, w, 0, 0);
+  }
 }
 
 int trackdb_rescan_cancel(void) {