glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / namestore / plugin_namestore_sqlite.c
index f905787b5e497f5a1b50720128c22dc34ceae80c..e0e8389878c1ae03dba9681597dd6eef47ef6bf0 100644 (file)
@@ -2,20 +2,15 @@
   * This file is part of GNUnet
   * Copyright (C) 2009-2017 GNUnet e.V.
   *
-  * GNUnet is free software; you can redistribute it and/or modify
-  * it under the terms of the GNU General Public License as published
-  * by the Free Software Foundation; either version 3, or (at your
-  * option) any later version.
+  * GNUnet is free software: you can redistribute it and/or modify it
+  * under the terms of the GNU Affero General Public License as published
+  * by the Free Software Foundation, either version 3 of the License,
+  * or (at your option) any later version.
   *
   * GNUnet is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  * General Public License for more details.
-  *
-  * You should have received a copy of the GNU General Public License
-  * along with GNUnet; see the file COPYING.  If not, write to the
-  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-  * Boston, MA 02110-1301, USA.
+  * Affero General Public License for more details.
   */
 
 /**
@@ -105,74 +100,6 @@ struct Plugin
 };
 
 
-/**
- * @brief Prepare a SQL statement
- *
- * @param dbh handle to the database
- * @param zSql SQL statement, UTF-8 encoded
- * @param ppStmt set to the prepared statement
- * @return 0 on success
- */
-static int
-sq_prepare (sqlite3 *dbh,
-            const char *zSql,
-            sqlite3_stmt **ppStmt)
-{
-  char *dummy;
-  int result;
-
-  result =
-      sqlite3_prepare_v2 (dbh,
-                          zSql,
-                          strlen (zSql),
-                          ppStmt,
-                          (const char **) &dummy);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Prepared `%s' / %p: %d\n",
-       zSql,
-       *ppStmt,
-       result);
-  return result;
-}
-
-
-/**
- * Create our database indices.
- *
- * @param dbh handle to the database
- */
-static void
-create_indices (sqlite3 * dbh)
-{
-  /* create indices */
-  if ( (SQLITE_OK !=
-       sqlite3_exec (dbh,
-                      "CREATE INDEX IF NOT EXISTS ir_pkey_reverse ON ns097records (zone_private_key,pkey)",
-                     NULL, NULL, NULL)) ||
-       (SQLITE_OK !=
-       sqlite3_exec (dbh,
-                      "CREATE INDEX IF NOT EXISTS ir_pkey_iter ON ns097records (zone_private_key,rvalue)",
-                     NULL, NULL, NULL)) ||
-       (SQLITE_OK !=
-       sqlite3_exec (dbh,
-                      "CREATE INDEX IF NOT EXISTS it_iter ON ns097records (rvalue)",
-                     NULL, NULL, NULL)) )
-    LOG (GNUNET_ERROR_TYPE_ERROR,
-        "Failed to create indices: %s\n",
-         sqlite3_errmsg (dbh));
-}
-
-
-#if 0
-#define CHECK(a) GNUNET_break(a)
-#define ENULL NULL
-#else
-#define ENULL &e
-#define ENULL_DEFINED 1
-#define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
-#endif
-
-
 /**
  * Initialize the database connections and associated
  * data structures (create tables and indices
@@ -184,17 +111,66 @@ create_indices (sqlite3 * dbh)
 static int
 database_setup (struct Plugin *plugin)
 {
-  sqlite3_stmt *stmt;
-  char *afsdir;
-#if ENULL_DEFINED
-  char *e;
-#endif
+  char *sqlite_filename;
+  struct GNUNET_SQ_ExecuteStatement es[] = {
+    GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
+    GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
+    GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
+    GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
+    GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
+    GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
+    GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
+    GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
+                            " uid INTEGER PRIMARY KEY,"
+                            " zone_private_key BLOB NOT NULL,"
+                            " pkey BLOB,"
+                            " rvalue INT8 NOT NULL,"
+                            " record_count INT NOT NULL,"
+                            " record_data BLOB NOT NULL,"
+                            " label TEXT NOT NULL"
+                            ")"),
+    GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
+                                "ON ns098records (zone_private_key,pkey)"),
+    GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
+                                "ON ns098records (zone_private_key,uid)"),
+    GNUNET_SQ_EXECUTE_STATEMENT_END
+  };
+  struct GNUNET_SQ_PrepareStatement ps[] = {
+    GNUNET_SQ_make_prepare ("INSERT INTO ns098records "
+                            "(zone_private_key,pkey,rvalue,record_count,record_data,label)"
+                            " VALUES (?, ?, ?, ?, ?, ?)",
+                            &plugin->store_records),
+    GNUNET_SQ_make_prepare ("DELETE FROM ns098records "
+                            "WHERE zone_private_key=? AND label=?",
+                            &plugin->delete_records),
+    GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
+                            " FROM ns098records"
+                            " WHERE zone_private_key=? AND pkey=?",
+                            &plugin->zone_to_name),
+    GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
+                            " FROM ns098records"
+                            " WHERE zone_private_key=? AND _rowid_ >= ?"
+                            " ORDER BY _rowid_ ASC"
+                            " LIMIT ?",
+                            &plugin->iterate_zone),
+    GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label,zone_private_key"
+                            " FROM ns098records"
+                            " WHERE _rowid_ >= ?"
+                            " ORDER BY _rowid_ ASC"
+                            " LIMIT ?",
+                            &plugin->iterate_all_zones),
+    GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label,zone_private_key"
+                            " FROM ns098records"
+                            " WHERE zone_private_key=? AND label=?",
+                            &plugin->lookup_label),
+    GNUNET_SQ_PREPARE_END
+  };
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
                                                "namestore-sqlite",
                                                "FILENAME",
-                                               &afsdir))
+                                               &sqlite_filename))
   {
     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "namestore-sqlite",
@@ -202,123 +178,51 @@ database_setup (struct Plugin *plugin)
     return GNUNET_SYSERR;
   }
   if (GNUNET_OK !=
-      GNUNET_DISK_file_test (afsdir))
+      GNUNET_DISK_file_test (sqlite_filename))
   {
     if (GNUNET_OK !=
-        GNUNET_DISK_directory_create_for_file (afsdir))
+        GNUNET_DISK_directory_create_for_file (sqlite_filename))
     {
       GNUNET_break (0);
-      GNUNET_free (afsdir);
+      GNUNET_free (sqlite_filename);
       return GNUNET_SYSERR;
     }
   }
-  /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
-  plugin->fn = afsdir;
+  /* sqlite_filename should be UTF-8-encoded. If it isn't, it's a bug */
+  plugin->fn = sqlite_filename;
 
   /* Open database and precompile statements */
-  if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
+  if (SQLITE_OK !=
+      sqlite3_open (plugin->fn,
+                    &plugin->dbh))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
         _("Unable to initialize SQLite: %s.\n"),
         sqlite3_errmsg (plugin->dbh));
     return GNUNET_SYSERR;
   }
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA temp_store=MEMORY", NULL, NULL,
-                       ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA synchronous=NORMAL", NULL, NULL,
-                       ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA legacy_file_format=OFF", NULL, NULL,
-                       ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA auto_vacuum=INCREMENTAL", NULL,
-                       NULL, ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA encoding=\"UTF-8\"", NULL,
-                       NULL, ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
-                       ENULL));
-  CHECK (SQLITE_OK ==
-         sqlite3_exec (plugin->dbh,
-                       "PRAGMA page_size=4092", NULL, NULL,
-                       ENULL));
-
-  CHECK (SQLITE_OK ==
-         sqlite3_busy_timeout (plugin->dbh,
-                               BUSY_TIMEOUT_MS));
-
-
-  /* Create table */
-  CHECK (SQLITE_OK ==
-         sq_prepare (plugin->dbh,
-                     "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns097records'",
-                     &stmt));
-  if ((sqlite3_step (stmt) == SQLITE_DONE) &&
-      (sqlite3_exec
-       (plugin->dbh,
-        "CREATE TABLE ns097records ("
-        " zone_private_key BLOB NOT NULL,"
-        " pkey BLOB,"
-       " rvalue INT8 NOT NULL,"
-       " record_count INT NOT NULL,"
-        " record_data BLOB NOT NULL,"
-        " label TEXT NOT NULL"
-       ")",
-       NULL, NULL, NULL) != SQLITE_OK))
+  GNUNET_break (SQLITE_OK ==
+                sqlite3_busy_timeout (plugin->dbh,
+                                      BUSY_TIMEOUT_MS));
+  if (GNUNET_OK !=
+      GNUNET_SQ_exec_statements (plugin->dbh,
+                                 es))
   {
-    LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR,
-                "sqlite3_exec");
-    sqlite3_finalize (stmt);
+    GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+        _("Failed to setup database at `%s'\n"),
+        plugin->fn);
     return GNUNET_SYSERR;
   }
-  sqlite3_finalize (stmt);
-
-  create_indices (plugin->dbh);
-
-  if ( (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label)"
-                    " VALUES (?, ?, ?, ?, ?, ?)",
-                    &plugin->store_records)) ||
-       (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "DELETE FROM ns097records WHERE zone_private_key=? AND label=?",
-                    &plugin->delete_records)) ||
-       (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "SELECT record_count,record_data,label"
-                    " FROM ns097records WHERE zone_private_key=? AND pkey=?",
-                    &plugin->zone_to_name)) ||
-       (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "SELECT record_count,record_data,label"
-                    " FROM ns097records WHERE zone_private_key=?"
-                    " ORDER BY rvalue LIMIT ? OFFSET ?",
-                    &plugin->iterate_zone)) ||
-       (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "SELECT record_count,record_data,label,zone_private_key"
-                    " FROM ns097records ORDER BY rvalue LIMIT ? OFFSET ?",
-                    &plugin->iterate_all_zones))  ||
-       (SQLITE_OK !=
-        sq_prepare (plugin->dbh,
-                    "SELECT record_count,record_data,label,zone_private_key"
-                    " FROM ns097records WHERE zone_private_key=? AND label=?",
-                    &plugin->lookup_label))
-       )
+
+  if (GNUNET_OK !=
+      GNUNET_SQ_prepare (plugin->dbh,
+                         ps))
   {
-    LOG_SQLITE (plugin,
-                GNUNET_ERROR_TYPE_ERROR,
-                "precompiling");
+    GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+        _("Failed to setup database at `%s'\n"),
+        plugin->fn);
     return GNUNET_SYSERR;
   }
   return GNUNET_OK;
@@ -404,11 +308,12 @@ namestore_sqlite_store_records (void *cls,
   int n;
   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
   uint64_t rvalue;
-  size_t data_size;
-  unsigned int i;
+  ssize_t data_size;
 
-  memset (&pkey, 0, sizeof (pkey));
-  for (i=0;i<rd_count;i++)
+  memset (&pkey,
+         0,
+         sizeof (pkey));
+  for (unsigned int i=0;i<rd_count;i++)
     if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
     {
       GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) ==
@@ -422,6 +327,11 @@ namestore_sqlite_store_records (void *cls,
                                      UINT64_MAX);
   data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
                                                  rd);
+  if (data_size < 0)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
   if (data_size > 64 * 65536)
   {
     GNUNET_break (0);
@@ -442,7 +352,7 @@ namestore_sqlite_store_records (void *cls,
                                              data_size,
                                              data);
     if ( (ret < 0) ||
-        (data_size != (size_t) ret) )
+        (data_size != ret) )
     {
       GNUNET_break (0);
       return GNUNET_SYSERR;
@@ -539,28 +449,50 @@ get_records_and_call_iterator (struct Plugin *plugin,
                                GNUNET_NAMESTORE_RecordIterator iter,
                                void *iter_cls)
 {
-  uint32_t record_count;
-  size_t data_size;
-  void *data;
-  char *label;
-  struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
   int ret;
   int sret;
 
   ret = GNUNET_OK;
-  for (uint64_t i = 0;i<limit ; i++)
+  for (uint64_t i = 0;i<limit;i++)
   {
-    if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
+    sret = sqlite3_step (stmt);
+
+    if (SQLITE_DONE == sret)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Iteration done (no results)\n");
+      ret = GNUNET_NO;
+      break;
+    }
+    if (SQLITE_ROW != sret)
     {
+      LOG_SQLITE (plugin,
+                 GNUNET_ERROR_TYPE_ERROR,
+                 "sqlite_step");
+      ret = GNUNET_SYSERR;
+      break;
+    }
+
+    {
+      uint64_t seq;
+      uint32_t record_count;
+      size_t data_size;
+      void *data;
+      char *label;
+      struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
       struct GNUNET_SQ_ResultSpec rs[] = {
+        GNUNET_SQ_result_spec_uint64 (&seq),
         GNUNET_SQ_result_spec_uint32 (&record_count),
-        GNUNET_SQ_result_spec_variable_size (&data, &data_size),
+        GNUNET_SQ_result_spec_variable_size (&data,
+                                            &data_size),
         GNUNET_SQ_result_spec_string (&label),
         GNUNET_SQ_result_spec_end
       };
       struct GNUNET_SQ_ResultSpec rsx[] = {
+        GNUNET_SQ_result_spec_uint64 (&seq),
         GNUNET_SQ_result_spec_uint32 (&record_count),
-        GNUNET_SQ_result_spec_variable_size (&data, &data_size),
+        GNUNET_SQ_result_spec_variable_size (&data,
+                                            &data_size),
         GNUNET_SQ_result_spec_string (&label),
         GNUNET_SQ_result_spec_auto_from_type (&zk),
         GNUNET_SQ_result_spec_end
@@ -604,6 +536,7 @@ get_records_and_call_iterator (struct Plugin *plugin,
         {
           if (NULL != iter)
             iter (iter_cls,
+                 seq + 1,
                   zone_key,
                   label,
                   record_count,
@@ -612,21 +545,6 @@ get_records_and_call_iterator (struct Plugin *plugin,
       }
       GNUNET_SQ_cleanup_result (rs);
     }
-    else
-    {
-      if (SQLITE_DONE != sret)
-      {
-        LOG_SQLITE (plugin,
-                    GNUNET_ERROR_TYPE_ERROR,
-                    "sqlite_step");
-        ret = GNUNET_SYSERR;
-      }
-      else
-      {
-        ret = GNUNET_NO;
-      }
-      break;
-    }
   }
   GNUNET_SQ_reset (plugin->dbh,
                    stmt);
@@ -642,7 +560,7 @@ get_records_and_call_iterator (struct Plugin *plugin,
  * @param label name of the record in the zone
  * @param iter function to call with the result
  * @param iter_cls closure for @a iter
- * @return #GNUNET_OK on success, else #GNUNET_SYSERR
+ * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
  */
 static int
 namestore_sqlite_lookup_records (void *cls,
@@ -659,7 +577,10 @@ namestore_sqlite_lookup_records (void *cls,
   };
 
   if (NULL == zone)
+  {
+    GNUNET_break (0);
     return GNUNET_SYSERR;
+  }
   if (GNUNET_OK !=
       GNUNET_SQ_bind (plugin->lookup_label,
                       params))
@@ -685,7 +606,7 @@ namestore_sqlite_lookup_records (void *cls,
  *
  * @param cls closure (internal context for the plugin)
  * @param zone hash of public key of the zone, NULL to iterate over all zones
- * @param offset offset in the list of all matching records
+ * @param serial serial number to exclude in the list of all matching records
  * @param limit maximum number of results to return
  * @param iter function to call with the result
  * @param iter_cls closure for @a iter
@@ -694,7 +615,7 @@ namestore_sqlite_lookup_records (void *cls,
 static int
 namestore_sqlite_iterate_records (void *cls,
                                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
-                                 uint64_t offset,
+                                 uint64_t serial,
                                   uint64_t limit,
                                  GNUNET_NAMESTORE_RecordIterator iter,
                                   void *iter_cls)
@@ -706,8 +627,8 @@ namestore_sqlite_iterate_records (void *cls,
   if (NULL == zone)
   {
     struct GNUNET_SQ_QueryParam params[] = {
+      GNUNET_SQ_query_param_uint64 (&serial),
       GNUNET_SQ_query_param_uint64 (&limit),
-      GNUNET_SQ_query_param_uint64 (&offset),
       GNUNET_SQ_query_param_end
     };
 
@@ -719,8 +640,8 @@ namestore_sqlite_iterate_records (void *cls,
   {
     struct GNUNET_SQ_QueryParam params[] = {
       GNUNET_SQ_query_param_auto_from_type (zone),
+      GNUNET_SQ_query_param_uint64 (&serial),
       GNUNET_SQ_query_param_uint64 (&limit),
-      GNUNET_SQ_query_param_uint64 (&offset),
       GNUNET_SQ_query_param_end
     };
 
@@ -761,7 +682,8 @@ static int
 namestore_sqlite_zone_to_name (void *cls,
                               const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
                               const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
-                              GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
+                              GNUNET_NAMESTORE_RecordIterator iter,
+                              void *iter_cls)
 {
   struct Plugin *plugin = cls;
   struct GNUNET_SQ_QueryParam params[] = {