Minor schema / interface tweaks
authorSteve McIntyre <steve@einval.com>
Thu, 26 May 2005 23:25:20 +0000 (23:25 +0000)
committerSteve McIntyre <steve@einval.com>
Thu, 26 May 2005 23:25:20 +0000 (23:25 +0000)
jigdb-sql.c
jigdb.h

index f60a897..3e2b049 100644 (file)
@@ -49,7 +49,6 @@ static int db_create_templates_table(db_state_t *dbp)
             "template_size INTEGER,"
             "image_size INTEGER,"
             "template_mtime INTEGER,"
-            "template_index INTEGER PRIMARY KEY,"
             "template_name VARCHAR(%d),"
             "template_md5 VARCHAR(32),"
             "image_md5 VARCHAR(32));", PATH_MAX);
@@ -76,7 +75,7 @@ static int db_create_blocks_table(db_state_t *dbp)
             "size INTEGER,"
             "uncomp_offset INTEGER,"
             "type INTEGER,"
-            "template_index INTEGER,"
+            "template_id VARCHAR(32),"
             "md5 VARCHAR(32));");
     error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
     if (error)
@@ -96,7 +95,7 @@ static int db_create_blocks_table(db_state_t *dbp)
             sqlite3_free(open_error);
         return error;
     }                                 
-    sprintf(sql_command, "CREATE INDEX blocks_template ON blocks (template_index);");
+    sprintf(sql_command, "CREATE INDEX blocks_template ON blocks (template_id);");
     error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -174,8 +173,10 @@ static int db_create_compressed_table(db_state_t *dbp)
     sprintf(sql_command, "CREATE TABLE compressed ("
             "comp_offset INTEGER,"
             "uncomp_offset INTEGER,"
+            "comp_size INTEGER,"
             "uncomp_size INTEGER,"
-            "template_index INTEGER);");
+            "comp_type INTEGER,"
+            "template_id VARCHAR(32));");
     error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -194,7 +195,7 @@ static int db_create_compressed_table(db_state_t *dbp)
             sqlite3_free(open_error);
         return error;
     }                                 
-    sprintf(sql_command, "CREATE INDEX compressed_template ON compressed (template_index);");
+    sprintf(sql_command, "CREATE INDEX compressed_template ON compressed (template_id);");
     error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -290,27 +291,27 @@ int db_delete_template_cache(JIGDB *dbp, char *template_name)
     int error = 0;
     db_state_t *state = dbp;
     char *open_error;
-    unsigned long template_index = 0;
+    unsigned char *template_id = NULL;
     db_template_entry_t *template = NULL;
     
     error = db_lookup_template_by_path(dbp, template_name, &template);
     if (!error)
     {
-        template_index = template->template_index;
+        template_id = template->template_md5;
 
-        sprintf(sql_command, "DELETE FROM blocks WHERE template_index == %ld;", template_index);
+        sprintf(sql_command, "DELETE FROM blocks WHERE template_id == '%s';", template_id);
         error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
         if (error)
             fprintf(stderr, "db_delete_template_cache: Failed to delete block entries, error %d (%s)\n", error, open_error);
         else
         {
-            sprintf(sql_command, "DELETE FROM compressed WHERE template_index == %ld;", template_index);
+            sprintf(sql_command, "DELETE FROM compressed WHERE template_id == '%s';", template_id);
             error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
             if (error)
                 fprintf(stderr, "db_delete_template_cache: Failed to delete compressed entries, error %d (%s)\n", error, open_error);
             else
             {
-                sprintf(sql_command, "DELETE FROM templates WHERE template_index == %ld;", template_index);
+                sprintf(sql_command, "DELETE FROM templates WHERE template_id == '%s';", template_id);
                 error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
                 if (error)
                     fprintf(stderr, "db_delete_template_cache: Failed to delete template entry, error %d (%s)\n", error, open_error);
@@ -370,15 +371,13 @@ static int results_callback(void *pArg, int argc, char **argv, char **columnName
             if (argv[2])
                 entry->data.template.template_mtime = strtoul(argv[2], NULL, 10);
             if (argv[3])
-                entry->data.template.template_index = strtoul(argv[3], NULL, 10);
-            if (argv[4])
-                strncpy(entry->data.template.template_name, argv[4],
+                strncpy(entry->data.template.template_name, argv[3],
                         sizeof(entry->data.template.template_name));
-            if (argv[5])
-                strncpy(entry->data.template.template_md5, argv[5],
+            if (argv[4])
+                strncpy(entry->data.template.template_md5, argv[4],
                         sizeof(entry->data.template.template_md5));
-            if (argv[6])
-                strncpy(entry->data.template.image_md5, argv[6],
+            if (argv[5])
+                strncpy(entry->data.template.image_md5, argv[5],
                         sizeof(entry->data.template.image_md5));
             break;
         case RES_BLOCK:
@@ -391,7 +390,7 @@ static int results_callback(void *pArg, int argc, char **argv, char **columnName
             if (argv[3])
                 entry->data.block.type = strtoul(argv[3], NULL, 10);
             if (argv[4])
-                entry->data.block.template_index = strtoul(argv[4], NULL, 10);
+                strncpy(entry->data.block.template_id, argv[4], sizeof(entry->data.block.template_id));
             if (argv[5])
                 strncpy(entry->data.block.md5, argv[5], sizeof(entry->data.block.md5));
             break;
@@ -417,9 +416,11 @@ static int results_callback(void *pArg, int argc, char **argv, char **columnName
             if (argv[1])
                 entry->data.compressed.uncomp_offset = strtoull(argv[1], NULL, 10);
             if (argv[2])
-                entry->data.compressed.uncomp_size = strtoull(argv[2], NULL, 10);
+                entry->data.compressed.comp_size = strtoull(argv[2], NULL, 10);
             if (argv[3])
-                entry->data.compressed.template_index = strtoul(argv[3], NULL, 10);
+                entry->data.compressed.uncomp_size = strtoull(argv[3], NULL, 10);
+            if (argv[4])
+                strncpy(entry->data.compressed.template_id, argv[4], sizeof(entry->data.compressed.template_id));
             break;
     }
     
@@ -432,9 +433,9 @@ int db_store_template(JIGDB *dbp, db_template_entry_t *entry)
     db_state_t *state = dbp;
     char *open_error;
 
-    sprintf(sql_command, "INSERT INTO templates VALUES(%lld,%lld,%ld,%ld,'%s','%s','%s');",
+    sprintf(sql_command, "INSERT INTO templates VALUES(%lld,%lld,%ld,'%s','%s','%s');",
             entry->template_size, entry->image_size, entry->template_mtime,
-            entry->template_index, entry->template_name, entry->template_md5, entry->image_md5);
+            entry->template_name, entry->template_md5, entry->image_md5);
     error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -483,9 +484,9 @@ int db_store_block(JIGDB *dbp, db_block_entry_t *entry)
     db_state_t *state = dbp;
     char *open_error;
 
-    sprintf(sql_command, "INSERT INTO blocks VALUES(%lld,%lld,%lld,%d,%ld,'%s');",
+    sprintf(sql_command, "INSERT INTO blocks VALUES(%lld,%lld,%lld,%d,'%s','%s');",
             entry->image_offset, entry->size, entry->uncomp_offset, entry->type,
-            entry->template_index, entry->md5);
+            entry->template_id, entry->md5);
     error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -498,7 +499,7 @@ int db_store_block(JIGDB *dbp, db_block_entry_t *entry)
 }
     
 int db_lookup_block_by_offset(JIGDB *dbp, unsigned long long image_offset,
-                              unsigned long template_index, db_block_entry_t **out)
+                              unsigned char *template_id, db_block_entry_t **out)
 {
     int error = 0;
     db_state_t *state = dbp;
@@ -508,9 +509,9 @@ int db_lookup_block_by_offset(JIGDB *dbp, unsigned long long image_offset,
     free_results();
 
     sprintf(sql_command,
-            "SELECT * FROM blocks WHERE template_index == %ld "
+            "SELECT * FROM blocks WHERE template_id == '%s' "
             "AND image_offset <= %lld "
-            "AND (image_offset + size) > %lld;", template_index, image_offset, image_offset);
+            "AND (image_offset + size) > %lld;", template_id, image_offset, image_offset);
     error = sqlite3_exec(state->db, sql_command, results_callback, &result_type, &open_error);
     if (error)
     {
@@ -648,8 +649,9 @@ int db_store_compressed(JIGDB *dbp, db_compressed_entry_t *entry)
     db_state_t *state = dbp;
     char *open_error;
 
-    sprintf(sql_command, "INSERT INTO compressed VALUES(%ld,%ld,%ld,%ld);",
-            entry->comp_offset, entry->uncomp_offset, entry->uncomp_size, entry->template_index);
+    sprintf(sql_command, "INSERT INTO compressed VALUES(%lld,%lld,%lld,%lld,%d,'%s');",
+            entry->comp_offset, entry->uncomp_offset, entry->comp_size,
+            entry->uncomp_size, entry->comp_type, entry->template_id);
     error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
     if (error)
     {
@@ -662,7 +664,7 @@ int db_store_compressed(JIGDB *dbp, db_compressed_entry_t *entry)
 }
 
 int db_lookup_compressed_by_offset(JIGDB *dbp, unsigned long uncomp_offset,
-                                   unsigned long template_index, db_compressed_entry_t **out)
+                                   unsigned char *template_id, db_compressed_entry_t **out)
 {
     int error = 0;
     db_state_t *state = dbp;
@@ -672,9 +674,9 @@ int db_lookup_compressed_by_offset(JIGDB *dbp, unsigned long uncomp_offset,
     free_results();
 
     sprintf(sql_command,
-            "SELECT * FROM compressed WHERE template_index == %ld "
+            "SELECT * FROM compressed WHERE template_id == '%s' "
             "AND uncomp_offset <= %ld "
-            "AND (uncomp_offset + size) > %ld;", template_index, uncomp_offset, uncomp_offset);
+            "AND (uncomp_offset + size) > %ld;", template_id, uncomp_offset, uncomp_offset);
     error = sqlite3_exec(state->db, sql_command, results_callback, &result_type, &open_error);
     if (error)
     {
diff --git a/jigdb.h b/jigdb.h
index 34b47f7..b348e5e 100644 (file)
--- a/jigdb.h
+++ b/jigdb.h
@@ -4,6 +4,13 @@
 #include <time.h>
 #include <limits.h>
 
+typedef enum
+{
+    CT_GZIP,
+    CT_BZIP2
+} ctype_e;
+
+
 #define JIGDB void
 
 /* Top-level details about a template file */
@@ -12,10 +19,9 @@ typedef struct
     unsigned long long template_size;           /* The size of the template file */
     unsigned long long image_size;              /* The size of the output image */
     time_t             template_mtime;          /* The mtime of the template file */
-    unsigned long      template_index;          /* record number in the template table */
     unsigned char      template_name[PATH_MAX]; /* The template file name */
-    unsigned char      template_md5[32];        /* MD5 of the template file */
-    unsigned char      image_md5[32];           /* MD5 of the image file */
+    unsigned char      template_md5[33];        /* MD5 of the template file */
+    unsigned char      image_md5[33];           /* MD5 of the image file */
 } db_template_entry_t;
 
 /* Details about the blocks in the template file; each points to a
@@ -33,9 +39,9 @@ typedef struct
     unsigned long long size;                    /* Size of the lump */
     unsigned long long uncomp_offset;           /* Offset within the compressed template
                                                    data iff type is BT_BLOCK */
-    unsigned long      template_index;          /* record number in the template table */
+    unsigned char      template_id[33];         /* MD5 of the template */
     enum blocktype     type;
-    unsigned char      md5[32];                 /* MD5 iff type is BT_FILE */
+    unsigned char      md5[33];                 /* MD5 iff type is BT_FILE */
 } db_block_entry_t;
 
 /* Details about files we know about: local mirror, files in a local
@@ -54,7 +60,7 @@ typedef struct
     time_t             mtime;              /* mtime of the file when we saw it */
     time_t             time_added;         /* time when this record was added */
     enum filetype      type;
-    unsigned char      md5[32];            /* md5sum of the file */
+    unsigned char      md5[33];            /* md5sum of the file */
     char               filename[PATH_MAX]; /* path to the file */
     char               extra[PATH_MAX];    /* empty for local files;
                                             * path to ISO for local ISO loopbacks;
@@ -70,12 +76,16 @@ typedef struct
  * right compressed block. */
 typedef struct
 {
-    unsigned long      comp_offset;             /* Start offset of this compressed block,
+    unsigned long long comp_offset;             /* Start offset of this compressed block,
                                                    measured from the beginning of the
-                                                   template file */
-    unsigned long      uncomp_offset;           /* Start offset when uncompressed */
-    unsigned long      uncomp_size;             /* Size of uncompressed block */
-    unsigned long      template_index;          /* record number in the template table */
+                                                   template file. NOTE: This is NOT the
+                                                   start of the compressed data itself,
+                                                   but the start of the header! */
+    unsigned long long uncomp_offset;           /* Start offset when uncompressed */
+    unsigned long long comp_size;               /* Size of the compressed block */
+    unsigned long long uncomp_size;             /* Size of uncompressed block */
+    ctype_e            comp_type;               /* CT_GZIP or CT_BZIP2 */
+    unsigned char      template_id[33];         /* MD5 of the template */
 } db_compressed_entry_t;
 
 /*******************
@@ -121,7 +131,7 @@ int db_store_block(JIGDB *dbp, db_block_entry_t *entry);
 /* Lookup a block by output offset. The specified offset will be
  * within the range covered by the returned entry, or ENOENT. */
 int db_lookup_block_by_offset(JIGDB *dbp, unsigned long long image_offset,
-                              unsigned long template_index, db_block_entry_t **out);
+                              unsigned char *template_id, db_block_entry_t **out);
 
 /****************
  *
@@ -158,6 +168,6 @@ int db_store_compressed(JIGDB *dbp, db_compressed_entry_t *entry);
 
 /* Lookup a block by its UNCOMPRESSED offset */
 int db_lookup_compressed_by_offset(JIGDB *dbp, unsigned long uncomp_offset,
-                                   unsigned long template_index, db_compressed_entry_t **out);
+                                   unsigned char *template_id, db_compressed_entry_t **out);
 
 #endif /* JIGDB_H */