17 db_file_entry_t entry;
20 struct results *res_head = NULL;
21 struct results *res_current = NULL;
22 struct results *res_tail = NULL;
24 char sql_command[2 * PATH_MAX];
26 JIGDB *db_open(char *db_name)
28 db_state_t *dbp = NULL;
29 int error = 0; /* function return value */
32 /* Allocate state structure */
33 dbp = calloc(1, sizeof(*dbp));
36 error = sqlite3_open(db_name, &dbp->db);
39 fprintf(stderr, "Unable to open sqlite file %s: error %d\n", db_name, error);
44 /* We have a database pointer open. Do we need to init the
45 * "files" table? Try to grab the first row of the table and
46 * see if we get an error. There has to be a better way than
48 error = sqlite3_exec(dbp->db, "SELECT COUNT(*) FROM files;", NULL, NULL, NULL);
49 if (SQLITE_ERROR == error)
51 /* We can't access the table. Delete it and create new */
52 error = sqlite3_exec(dbp->db, "DROP TABLE files;", NULL, NULL, NULL);
53 sprintf(sql_command, "CREATE TABLE files ("
59 "filename VARCHAR(%d),"
60 "extra VARCHAR(%d));", PATH_MAX, PATH_MAX);
61 error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
64 fprintf(stderr, "db_open: got error %d (%s) from create\n", error, open_error);
66 sqlite3_free(open_error);
71 sprintf(sql_command, "CREATE INDEX files_md5 ON files (md5);");
72 error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
75 fprintf(stderr, "db_open: got error %d (%s) from create index\n", error, open_error);
77 sqlite3_free(open_error);
78 sqlite3_close(dbp->db);
82 sprintf(sql_command, "CREATE INDEX files_name ON files (filename);");
83 error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
86 fprintf(stderr, "db_open: got error %d (%s) from create index\n", error, open_error);
88 sqlite3_free(open_error);
89 sqlite3_close(dbp->db);
93 sprintf(sql_command, "CREATE INDEX files_age ON files (age);");
94 error = sqlite3_exec(dbp->db, sql_command, NULL, NULL, &open_error);
97 fprintf(stderr, "db_open: got error %d (%s) from create index\n", error, open_error);
99 sqlite3_free(open_error);
100 sqlite3_close(dbp->db);
110 int db_close(JIGDB *dbp)
112 db_state_t *state = dbp;
113 /* When we're done with the database, close it. */
115 sqlite3_close(state->db);
120 int db_store_file(JIGDB *dbp, db_file_entry_t *entry)
123 db_state_t *state = dbp;
126 sprintf(sql_command, "INSERT INTO files VALUES('%s', %d , %ld , %ld , %lld , '%s', '%s');",
127 entry->md5, entry->type, entry->mtime, entry->age,
128 entry->file_size, entry->filename, entry->extra);
129 error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
132 fprintf(stderr, "db_store_file: Failed to write entry, error %d (%s)\n", error, open_error);
134 sqlite3_free(open_error);
140 void free_results(void)
142 struct results *entry = res_head;
143 struct results *current = res_head;
156 static int results_callback(void *pArg, int argc, char **argv, char **columnNames)
158 struct results *entry = calloc(1, sizeof (*entry));
161 res_tail->next = entry;
165 entry->prev = res_tail;
168 strncpy(entry->entry.md5, argv[0], sizeof(entry->entry.md5));
170 entry->entry.type = strtol(argv[1], NULL, 10);
172 entry->entry.mtime = strtoul(argv[2], NULL, 10);
174 entry->entry.age = strtoul(argv[3], NULL, 10);
176 entry->entry.file_size = strtoull(argv[4], NULL, 10);
178 strncpy(entry->entry.filename, argv[5], sizeof(entry->entry.filename));
180 strncpy(entry->entry.extra, argv[6], sizeof(entry->entry.extra));
185 /* Look up the most recent record that is older than the specified
187 int db_lookup_file_by_age(JIGDB *dbp, time_t age, db_file_entry_t **out)
190 db_state_t *state = dbp;
195 sprintf(sql_command, "SELECT * FROM files WHERE age > %ld", age);
196 error = sqlite3_exec(state->db, sql_command, results_callback, NULL, &open_error);
199 fprintf(stderr, "db_lookup_file_by_age: Failed to lookup, error %d (%s)\n", error, open_error);
203 res_current = res_head;
206 *out = &res_current->entry;
207 res_current = res_current->next;
215 /* Look up the next oldest record */
216 int db_lookup_file_older(JIGDB *dbp, db_file_entry_t **out)
225 *out = &res_current->entry;
226 res_current = res_current->next;
234 int db_lookup_file_by_md5(JIGDB *dbp, char *md5, db_file_entry_t **out)
237 db_state_t *state = dbp;
242 sprintf(sql_command, "SELECT * FROM files WHERE md5 == '%s' ORDER BY filetype ASC;", md5);
243 error = sqlite3_exec(state->db, sql_command, results_callback, NULL, &open_error);
246 fprintf(stderr, "db_lookup_file_by_md5: Failed to lookup, error %d (%s)\n", error, open_error);
250 res_current = res_head;
253 *out = &res_current->entry;
254 res_current = res_current->next;
262 int db_lookup_file_by_name(JIGDB *dbp, char *filename, db_file_entry_t **out)
265 db_state_t *state = dbp;
270 sprintf(sql_command, "SELECT * FROM files WHERE filename == '%s';", filename);
271 error = sqlite3_exec(state->db, sql_command, results_callback, NULL, &open_error);
274 fprintf(stderr, "db_lookup_file_by_name: Failed to lookup, error %d (%s)\n", error, open_error);
278 res_current = res_head;
281 *out = &res_current->entry;
282 res_current = res_current->next;
290 int db_delete_file(JIGDB *dbp, char *md5, enum filetype type, char *filename)
293 db_state_t *state = dbp;
296 sprintf(sql_command, "DELETE FROM files WHERE md5 == '%s' AND type == '%d' AND filename == '%s';", md5, type, filename);
297 error = sqlite3_exec(state->db, sql_command, NULL, NULL, &open_error);
299 fprintf(stderr, "db_delete_file: Failed to delete, error %d (%s)\n", error, open_error);
304 int db_dump(JIGDB *dbp)
307 /* int num_records = 0;
308 db_file_entry_t *entry = NULL;
309 db_state_t *state = dbp; */