181 lines
5.4 KiB
C
181 lines
5.4 KiB
C
#include "../../include/database.h"
|
|
#include "../../include/package.h"
|
|
#include "../../include/error.h"
|
|
#include "../../include/util.h"
|
|
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
bool lm_database_files_matches(lm_database_t *db, char *path, char *hash){
|
|
char *hashdb = NULL;
|
|
bool ret = false;
|
|
|
|
if(NULL == db || NULL == path || NULL == hash){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
goto end;
|
|
}
|
|
|
|
if(sqlite3_prepare(db->files_db, queries[QUERY_SELECT_FILE_SINGLE], strlen(queries[QUERY_SELECT_FILE_SINGLE]), &db->files_st, NULL) != SQLITE_OK){
|
|
pdebug(__func__, "failed to prepare statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
|
goto end;
|
|
}
|
|
|
|
sqlite3_bind_text(db->files_st, 1, path, strlen(path), SQLITE_STATIC);
|
|
|
|
if(sqlite3_step(db->files_st) != SQLITE_DONE){
|
|
pdebug(__func__, "failed to execute select statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlSelectFail);
|
|
goto end;
|
|
}
|
|
|
|
hashdb = (char*)sqlite3_column_text(db->files_st, 1);
|
|
ret = eq(hashdb, hash);
|
|
|
|
end:
|
|
if(NULL != db->files_st){
|
|
sqlite3_finalize(db->files_st);
|
|
db->files_st = NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool lm_database_files_iskeep(lm_database_t *db, char *path){
|
|
bool ret = false;
|
|
int iskeep = 0;
|
|
|
|
if(NULL == db || NULL == path){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
goto end;
|
|
}
|
|
|
|
if(sqlite3_prepare(db->files_db, queries[QUERY_SELECT_FILE_SINGLE], strlen(queries[QUERY_SELECT_FILE_SINGLE]), &db->files_st, NULL) != SQLITE_OK){
|
|
pdebug(__func__, "failed to prepare statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
|
goto end;
|
|
}
|
|
|
|
sqlite3_bind_text(db->files_st, 1, path, strlen(path), SQLITE_STATIC);
|
|
|
|
if(sqlite3_step(db->files_st) != SQLITE_DONE){
|
|
pdebug(__func__, "failed to execute select statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlSelectFail);
|
|
goto end;
|
|
}
|
|
|
|
iskeep = sqlite3_column_int64(db->files_st, 2);
|
|
ret = iskeep == 1;
|
|
|
|
end:
|
|
if(NULL != db->files_st){
|
|
sqlite3_finalize(db->files_st);
|
|
db->files_st = NULL;
|
|
}
|
|
return ret;
|
|
|
|
}
|
|
|
|
bool lm_database_files_next(lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, bool *keep){
|
|
if(NULL == db || NULL == pkg || NULL == path || NULL == hash || NULL == keep){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
if(NULL == db->files_st &&
|
|
sqlite3_prepare(db->files_db, queries[QUERY_SELECT_FILE_ALL], strlen(queries[QUERY_SELECT_FILE_ALL]), &db->files_st, NULL) != SQLITE_OK){
|
|
pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
|
return false;
|
|
}
|
|
|
|
else if(NULL != db->files_st){
|
|
free(*path);
|
|
free(*hash);
|
|
*path = NULL;
|
|
*hash = NULL;
|
|
}
|
|
|
|
if(sqlite3_step(db->files_st) != SQLITE_ROW){
|
|
sqlite3_finalize(db->files_st);
|
|
db->files_st = NULL;
|
|
return false;
|
|
}
|
|
|
|
*path = strdup((char*)sqlite3_column_text(db->files_st, 0));
|
|
*hash = strdup((char*)sqlite3_column_text(db->files_st, 1));
|
|
*keep = sqlite3_column_int(db->files_st, 2) == 1;
|
|
return true;
|
|
}
|
|
|
|
bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){
|
|
bool ret = false;
|
|
|
|
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
goto end;
|
|
}
|
|
|
|
if(sqlite3_prepare(db->files_db, queries[QUERY_INSERT_FILE_SINGLE], strlen(queries[QUERY_INSERT_FILE_SINGLE]), &db->files_st, NULL) != SQLITE_OK){
|
|
pdebug(__func__, "failed to prepare statement for inserting %s: %s", path, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
|
goto end;
|
|
}
|
|
|
|
sqlite3_bind_text(db->files_st, 1, path, strlen(path), SQLITE_STATIC);
|
|
sqlite3_bind_text(db->files_st, 2, hash, strlen(hash), SQLITE_STATIC);
|
|
if(lm_package_keep_contains(pkg, path))
|
|
sqlite3_bind_int(db->files_st, 3, 1);
|
|
else
|
|
sqlite3_bind_int(db->files_st, 3, 0);
|
|
sqlite3_bind_text(db->files_st, 4, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
|
|
|
if(sqlite3_step(db->files_st) != SQLITE_DONE){
|
|
pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlInsertFail);
|
|
goto end;
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
if(NULL != db->files_st){
|
|
sqlite3_finalize(db->files_st);
|
|
db->files_st = NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg){
|
|
if(NULL == db || NULL == pkg){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
bool ret = false;
|
|
|
|
if(sqlite3_prepare(db->files_db, queries[QUERY_DELETE_FILE_ALL], strlen(queries[QUERY_DELETE_FILE_ALL]), &db->files_st, NULL) != SQLITE_OK){
|
|
pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
|
goto end;
|
|
}
|
|
|
|
sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
|
|
|
if(sqlite3_step(db->files_st) != SQLITE_DONE){
|
|
pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->files_db));
|
|
lm_error_set(LM_ERR_DbSqlDeleteFail);
|
|
goto end;
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
if(NULL != db->files_st){
|
|
sqlite3_finalize(db->files_st);
|
|
db->files_st = NULL;
|
|
}
|
|
return ret;
|
|
}
|