Minor changes.
[egate.git] / kernel / persist.c
index e341536..bea7f4b 100644 (file)
@@ -1,8 +1,83 @@
+/*
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
 /* Gateway kernel - Z39.50 Persistence
  * Europagate, 1995
  *
  * $Log: persist.c,v $
- * Revision 1.1  1995/04/19 07:31:10  adam
+ * Revision 1.10  1995/07/20 08:16:38  adam
+ * Bug fix in persist.c: Double referenced result-sets weren't recovered.
+ *
+ * Revision 1.9  1995/05/19  13:26:00  adam
+ * Bug fixes. Better command line options.
+ *
+ * Revision 1.8  1995/05/16  09:40:43  adam
+ * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
+ *
+ * Revision 1.7  1995/05/03  16:34:19  adam
+ * CCL def command, i.e. user definitions - saved as resource files.
+ *
+ * Revision 1.6  1995/05/03  07:37:44  adam
+ * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
+ * are used when possible.
+ *
+ * Revision 1.5  1995/05/02  15:26:00  adam
+ * Monitor observes death of child (email kernel). The number
+ * of simultanous processes is controlled now. Email requests are
+ * queued if necessary. This scheme should only be forced if no kernels
+ * are idle.
+ *
+ * Revision 1.4  1995/04/20  16:10:46  adam
+ * Modified to work with non-blocking zass-api. Not using non-blocking
+ * facility yet.
+ *
+ * Revision 1.3  1995/04/19  13:19:09  adam
+ * New command: account - for authentication.
+ *
+ * Revision 1.2  1995/04/19  10:46:19  adam
+ * Persistency works much better now. New command: status - history-like
+ *
+ * Revision 1.1  1995/04/19  07:31:10  adam
  * First work on Z39.50 persistence.
  *
  */
 
 #include "kernel.h"
 
+static int fgetsx (char *buf, int size, FILE *inf)
+{
+    char *cp;
+
+    if (!fgets (buf, size, inf))
+        return 0;
+    if ((cp = strchr (buf, '\n')))
+        *cp = '\0';
+    return 1;
+}
+
 static int set_change;
 
 static int obtain_set (ZASS zass, struct gw_user_set *set)
 {
     const struct zass_searchent *p;
-
-    p = zass_search (zass, set->rpn, set->name, set->database);
+    
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s must be obtained", set->name);
+    p = zass_search (zass, set->rpn, set->name, set->database, NULL);
     if (!p)
         return 2;
     if (p->errcode != -1)
@@ -32,6 +119,7 @@ static int obtain_set (ZASS zass, struct gw_user_set *set)
         set_change = 1;
     set->present_flag = 1;
     set->hits = p->num;
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s researched", set->name);
     return 0;
 }
 
@@ -55,10 +143,19 @@ static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn,
     case CCL_RPN_SET:
         set = user_set_search (rpn->u.setname);
        if (!set)
+        {
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s not there at all",
+                    rpn->u.setname);
            return 1;
+        }
        if (set->present_flag)
+        {
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s already there",
+                    rpn->u.setname);
            return 0;
+        }
     }
+    obtain_sets (zass, set->rpn, sets);
     return obtain_set (zass, set);
 }
 
@@ -74,7 +171,7 @@ const struct zass_searchent *zass_p_search (ZASS zass,
     r = obtain_sets (zass, rpn, sets);
     if (r)
         return NULL;
-    return zass_search (zass, rpn, (char*) result_set, (char*) database);
+    return zass_search (zass, rpn, (char*) result_set, (char*) database, NULL);
 }
 
 const struct zass_presentent *zass_p_present (ZASS zass,
@@ -87,67 +184,56 @@ const struct zass_presentent *zass_p_present (ZASS zass,
        return NULL;
     if (!set->present_flag)
     {
-        int r = obtain_set (zass, set);
-       if (r)
-           return NULL;
+        const struct zass_searchent *p;
+
+        p = zass_p_search (zass, set->rpn, result_set, set->database,
+                           info.sets);
+        if (!p)
+            return NULL;
     }
-    return zass_present (zass, (char*) result_set, offset, number);
+    return zass_present (zass, (char*) result_set, offset, number, NULL);
 }
 
 struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
 {
     struct ccl_rpn_node *rpn;
     struct ccl_rpn_attr **attrp;
-    char *cp;
     int type, value, no_read;
+    char *cp;
 
-    if (!fgets (buf, 1024, inf))
+    if (!fgetsx (buf, 1024, inf))
+        return NULL;
+    rpn = malloc (sizeof (*rpn));
+    if (!rpn)
         return NULL;
-    if ((cp = strchr (buf, '\n')))
-        *cp = '\0';
     switch (*buf)
     {
     case 'A':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_AND;
        rpn->u.p[0] = load_rpn (buf, inf);
        rpn->u.p[1] = load_rpn (buf, inf);
         break;
     case 'O':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_OR;
        rpn->u.p[0] = load_rpn (buf, inf);
        rpn->u.p[1] = load_rpn (buf, inf);
         break;
     case 'N':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_NOT;
        rpn->u.p[0] = load_rpn (buf, inf);
        rpn->u.p[1] = load_rpn (buf, inf);
         break;
     case 'P':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_PROX;
        rpn->u.p[0] = load_rpn (buf, inf);
        rpn->u.p[1] = load_rpn (buf, inf);
         break;
     case 'T':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_TERM;
 
        rpn->u.t.term = gw_strdup (buf+2);
        attrp = &rpn->u.t.attr_list;
-        if (!fgets (buf, 1024, inf))
+        if (!fgetsx (buf, 1024, inf))
             return NULL;
        cp = buf;
        while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
@@ -161,11 +247,12 @@ struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
        *attrp = NULL;
        break;
     case 'S':
-        rpn = malloc (sizeof (*rpn));
-       if (!rpn)
-           return NULL;
        rpn->kind = CCL_RPN_SET;
        rpn->u.setname = gw_strdup (buf+2);
+        break;
+    default:
+        free (rpn);
+        return NULL;
     }
     return rpn;
 }
@@ -180,7 +267,7 @@ int load_p_state (int userid)
     int hits;
     struct gw_user_set *set;
 
-    sprintf (fname, "persist.%d", userid);
+    sprintf (fname, "user.%d.p", userid);
 
     inf = fopen (fname, "r");
     if (!inf)
@@ -189,33 +276,56 @@ int load_p_state (int userid)
                "Couldn't open %s", fname);
         return -1;
     }
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, 
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
        "Reading persistence file %s", fname);
-    if (!fgets (fline, 1024, inf))
+
+    if (!fgetsx (fline, 1024, inf))
         return -1;
     if (sscanf (fline, "%s", info.target) != 1)
         *info.target = '\0';
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, 
-           "Reading persistence file %s (2)", fname);
     read_kernel_res ();
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target start");
-    reopen_target ();
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target end");
-    while (fgets (fline, 1024, inf))
+
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    if (sscanf (fline, "%s", info.account) != 1)
+        *info.account = '\0';
+   
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    free (info.database);
+    info.database = gw_strdup (fline);
+
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    if (sscanf (fline, "%d", &info.setno) != 1)
+        return -1;
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    if (sscanf (fline, "%d", &info.next_position) != 1)
+        return -1;
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
+           "Reading persistence file %s (2)", fname);
+    while (fgetsx (fline, 1024, inf))
     {
-        gw_log (GW_LOG_FATAL, KERNEL_LOG, 
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
               "Reading persistence file %s (3)", fname);
         if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
            return -1;
-       gw_log (GW_LOG_FATAL, KERNEL_LOG,
+        if (!fgetsx (fline, 1024, inf))        /* search string */
+            return -1;
+       gw_log (GW_LOG_DEBUG, KERNEL_LOG,
                "Adding %s, %d hits, database %s",
                resultname, hits, database);
-       set = user_set_add (resultname, hits, database, NULL, 0);
+       gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Search string %s", fline);
+       set = user_set_add (resultname, hits, database, NULL, 0, fline);
        set->rpn = load_rpn (fline, inf);
+#if 0
        ccl_pr_tree (set->rpn, stderr);
-       fgets (fline, 1024, inf);
+#endif
+       fgetsx (fline, 1024, inf);
     }
     fclose (inf);
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Finished reading %s", fname);
     return 0;
 }
 
@@ -256,13 +366,23 @@ static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
     }
 }
 
+static void save_sets (FILE *of, struct gw_user_set *sp)
+{
+    if (!sp)
+        return;
+    save_sets (of, sp->prev);
+    fprintf (of, "%s %d %s\n%s\n", sp->name, sp->hits, sp->database,
+             sp->search_str);
+    save_rpn (sp->rpn, of);
+    fprintf (of, "X\n");
+}
+
 int save_p_state (int userid)
 {
     FILE *of;
     char fname[128];
-    struct gw_user_set *sp;
 
-    sprintf (fname, "persist.%d", userid);
+    sprintf (fname, "user.%d.p", userid);
 
     of = fopen (fname, "w");
     if (!of)
@@ -272,14 +392,18 @@ int save_p_state (int userid)
         return -1;
     }
     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
-    fprintf (of, "%s\n", info.target);
-    for (sp = info.sets; sp; sp = sp->prev)
-    {
-        fprintf (of, "%s %d %s\n", sp->name, sp->hits, sp->database);
-       save_rpn (sp->rpn, of);
-       fprintf (of, "X\n");
-    }
+    fprintf (of, "%s\n%s\n%s\n%d\n%d\n", info.target, info.account,
+             info.database, info.setno, info.next_position);
+    save_sets (of, info.sets);
     fclose (of);
     return 0;
 }
 
+void del_p_state (int userid)
+{
+    char fname[128];
+
+    sprintf (fname, "user.%d.p", userid);
+    unlink (fname);
+}
+