new: better way of handling files db
This commit is contained in:
parent
98e5c861df
commit
3a6f1c06c0
22
Makefile
22
Makefile
@ -5,26 +5,28 @@ PO_SRCS = $(wildcard locale/*/*/*.po)
|
||||
PO_OUTS = $(patsubst locale/%.po,locale/%.mo,$(PO_SRCS))
|
||||
PO_DIRS = $(wildcard locale/*/*)
|
||||
|
||||
SRCS = $(wildcard src/*.c) $(wildcard src/*/*.c)
|
||||
OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS))
|
||||
HDRS = $(wildcard include/*.h)
|
||||
CFLAGS = -O3 -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
|
||||
LIBS = -lpthread -larchive -linih -lgpgme -lsqlite3
|
||||
SRCS = $(wildcard src/*.c) $(wildcard src/*/*.c)
|
||||
OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS))
|
||||
HDRS = $(wildcard include/*.h)
|
||||
CFLAGS = -O3 -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
|
||||
LIBS = -lpthread -larchive -linih -lgpgme -lsqlite3
|
||||
|
||||
DEBUG = 0
|
||||
VERSION = 24.00
|
||||
|
||||
all: dist/libmp.so $(PO_OUTS)
|
||||
all: dist dist/libmp.so $(PO_OUTS)
|
||||
|
||||
dist/libmp.so: $(OBJS)
|
||||
$(CC) -shared -o $@ $^ $(LIBS) $(CFLAGS)
|
||||
|
||||
dist/%.o: src/%.c
|
||||
dist:
|
||||
mkdir -p dist/ctx
|
||||
mkdir -p dist/mptp
|
||||
mkdir -p dist/pool
|
||||
mkdir -p dist/package
|
||||
mkdir -p dist/database
|
||||
|
||||
dist/libmp.so: $(OBJS)
|
||||
$(CC) -shared -o $@ $^ $(LIBS) $(CFLAGS)
|
||||
|
||||
dist/%.o: src/%.c
|
||||
$(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) -DLM_VERSION=\"${VERSION}\" -DLM_DEBUG=${DEBUG}
|
||||
|
||||
locale/%.mo: locale/%.po
|
||||
|
@ -2,8 +2,11 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DATA_DIR "/tmp/data"
|
||||
#define TEMP_DIR "/tmp/temp"
|
||||
#define ROOT_DIR "/tmp/root"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int ret = EXIT_FAILURE;
|
||||
@ -15,6 +18,11 @@ int main(int argc, char *argv[]) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!mkdir_ifnot(ROOT_DIR)) {
|
||||
printf("failed to create root dir: %s", ROOT_DIR);
|
||||
return ret;
|
||||
}
|
||||
|
||||
lm_ctx_init(&ctx);
|
||||
|
||||
if (!lm_ctx_set_data(&ctx, DATA_DIR)) {
|
||||
@ -22,6 +30,16 @@ int main(int argc, char *argv[]) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!lm_ctx_set_temp(&ctx, TEMP_DIR)) {
|
||||
printf("failed to set temp dir: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!lm_ctx_set_root(&ctx, ROOT_DIR)) {
|
||||
printf("failed to set root dir: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (lm_ctx_pool_add(&ctx, "base", argv[1]) == NULL) {
|
||||
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
@ -31,12 +49,12 @@ int main(int argc, char *argv[]) {
|
||||
lm_ctx_sync(&ctx, true, NULL, NULL);
|
||||
|
||||
if ((pkg = lm_ctx_find(&ctx, "which", NULL)) == NULL) {
|
||||
printf("failed to find package: %s (%d)", lm_strerror(), lm_error());
|
||||
printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!lm_ctx_download(&ctx, pkg, NULL, NULL)) {
|
||||
printf("failed to download package: %s (%d)", lm_strerror(), lm_error());
|
||||
printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error());
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
@ -5,43 +5,50 @@
|
||||
#define HASH_LEN 32
|
||||
|
||||
typedef enum lm_query_index {
|
||||
QUERY_CREATE_TABLE = 0,
|
||||
QUERY_INSERT_PACKAGE = 1,
|
||||
QUERY_SELECT_PACKAGE = 2,
|
||||
QUERY_DELETE_PACKAGE = 3,
|
||||
QUERY_ALL_PACKAGE = 4,
|
||||
QUERY_CREATE_PACKAGE_TABLE = 0,
|
||||
QUERY_INSERT_PACKAGE_SINGLE = 1,
|
||||
QUERY_SELECT_PACKAGE_SINGLE = 2,
|
||||
QUERY_DELETE_PACKAGE_SINGLE = 3,
|
||||
QUERY_SELECT_PACKAGE_ALL = 4,
|
||||
QUERY_CREATE_FILE_TABLE = 5,
|
||||
QUERY_INSERT_FILE_SINGLE = 6,
|
||||
QUERY_DELETE_FILE_ALL = 7,
|
||||
QUERY_SELECT_FILE_SINGLE = 8,
|
||||
QUERY_SELECT_FILE_ALL = 9,
|
||||
QUERY_UPDATE_FILE_1 = 10,
|
||||
QUERY_UPDATE_FILE_2 = 11,
|
||||
} lm_query_index_t;
|
||||
|
||||
extern char *queries[];
|
||||
|
||||
typedef struct lm_database {
|
||||
sqlite3_stmt *st;
|
||||
sqlite3 *sql;
|
||||
char *dir;
|
||||
lm_pkg_t *pkg;
|
||||
} lm_database_t;
|
||||
sqlite3 *packages_db;
|
||||
sqlite3_stmt *packages_st;
|
||||
|
||||
typedef bool (*lm_database_files_eachfunc_t)(lm_pkg_t *pkg, char *path, char *hash, void *data);
|
||||
typedef bool (*lm_database_keeps_eachfunc_t)(lm_pkg_t *pkg, char *path, void *data);
|
||||
sqlite3 *files_db;
|
||||
sqlite3_stmt *files_st;
|
||||
|
||||
lm_pkg_t *pkg;
|
||||
char *dir;
|
||||
} lm_database_t;
|
||||
|
||||
lm_database_t *lm_database_new(char *path);
|
||||
void lm_database_free(lm_database_t *db);
|
||||
|
||||
bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg, char *name);
|
||||
bool lm_database_next(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_add(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_del(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_package_find(
|
||||
lm_database_t *db, lm_pkg_t *pkg, char *name); // finds a package by its name, package stored in *pkg
|
||||
bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg); // gets the next package in the database
|
||||
bool lm_database_packege_add(lm_database_t *db, lm_pkg_t *pkg); // adds a package to the database
|
||||
bool lm_database_package_del(lm_database_t *db, lm_pkg_t *pkg); // delete a package from the database
|
||||
|
||||
bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_files_eachfunc_t func, void *data);
|
||||
bool lm_database_files_matches(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash);
|
||||
bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash);
|
||||
bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash);
|
||||
bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg);
|
||||
|
||||
bool lm_database_keeps_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_keeps_eachfunc_t func, void *data);
|
||||
bool lm_database_keeps_load(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_keeps_save(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_keeps_del(lm_database_t *db, lm_pkg_t *pkg);
|
||||
bool lm_database_files_matches(
|
||||
lm_database_t *db, char *path, char *hash); // checks if the given file matches with the given hash
|
||||
bool lm_database_files_iskeep(lm_database_t *db, char *path); // checks if the given file is marked as keep
|
||||
bool lm_database_files_next(
|
||||
lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, bool *keep); // gets the next file of the package
|
||||
bool lm_database_files_add(
|
||||
lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash); // adds a file to the files database
|
||||
bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg); // dels all files of belonging to a package
|
||||
|
||||
bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file);
|
||||
char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg);
|
||||
|
@ -115,6 +115,9 @@ typedef enum lm_error {
|
||||
LM_ERR_SendReadFail = 107,
|
||||
LM_ERR_RecvBadSize = 108,
|
||||
LM_ERR_RecvNotCompleted = 109,
|
||||
LM_ERR_DbSqlSelectFail = 110,
|
||||
LM_ERR_DbSqlDeleteFail = 111,
|
||||
|
||||
} lm_error_t;
|
||||
|
||||
typedef struct lm_error_desc {
|
||||
|
@ -8,7 +8,7 @@ msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2024-07-05 16:20+0300\n"
|
||||
"POT-Creation-Date: 2024-07-06 03:45+0300\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
@ -314,161 +314,169 @@ msgid "failed to insert to the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:92
|
||||
msgid "failed to find entry in SQLite database"
|
||||
msgid "failed to select from the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:93
|
||||
msgid "failed to init GPG for package verification"
|
||||
msgid "failed to delete from the table in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:94
|
||||
msgid "failed to import signature to GPG for package verification"
|
||||
msgid "failed to find entry in SQLite database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:95
|
||||
msgid "failed to import archive to GPG for package verification"
|
||||
msgid "failed to init GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:96
|
||||
msgid "package signature verification failed with zero matches"
|
||||
msgid "failed to import signature to GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:97
|
||||
msgid "package signature verification failed with zero results"
|
||||
msgid "failed to import archive to GPG for package verification"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:98
|
||||
msgid "pool file and directory paths are empty"
|
||||
msgid "package signature verification failed with zero matches"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:99
|
||||
msgid "pool is not avaliable for connection"
|
||||
msgid "package signature verification failed with zero results"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:100
|
||||
msgid "pool URL is empty or invalid"
|
||||
msgid "pool file and directory paths are empty"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:101
|
||||
msgid "pool directory path is not accessible"
|
||||
msgid "pool is not avaliable for connection"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:102
|
||||
msgid "pool directory sub-paths are not accessible"
|
||||
msgid "pool URL is empty or invalid"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:103
|
||||
msgid "package file list not found in the database"
|
||||
msgid "pool directory path is not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:104
|
||||
msgid "failed to open package file list in the database"
|
||||
msgid "pool directory sub-paths are not accessible"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:105
|
||||
msgid "failed to access package file list database directory"
|
||||
msgid "package file list not found in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:106
|
||||
msgid "failed to remove package file list from the database"
|
||||
msgid "failed to open package file list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:107
|
||||
msgid "failed to write to the file list in the database"
|
||||
msgid "failed to access package file list database directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:108
|
||||
msgid "package keep list not found in the database"
|
||||
msgid "failed to remove package file list from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:109
|
||||
msgid "failed to open package keep list in the database"
|
||||
msgid "failed to write to the file list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:110
|
||||
msgid "failed to access package keep list database directory"
|
||||
msgid "package keep list not found in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:111
|
||||
msgid "failed to remove package keep list from the database"
|
||||
msgid "failed to open package keep list in the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:112
|
||||
msgid "failed to access package keep list database directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:113
|
||||
msgid "failed to remove package keep list from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:114
|
||||
#, c-format
|
||||
msgid "failed to find %s (dependency of %s)"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:113
|
||||
#: src/error.c:115
|
||||
#, c-format
|
||||
msgid "failed to download %s for installation: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:114
|
||||
#: src/error.c:116
|
||||
#, fuzzy
|
||||
msgid "package is not downloaded"
|
||||
msgstr "URL hostname is too large"
|
||||
|
||||
#: src/error.c:115 src/error.c:116
|
||||
#: src/error.c:117 src/error.c:118
|
||||
msgid "failed to remove downloaded package"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:117
|
||||
#: src/error.c:119
|
||||
msgid "failed to open the destination file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:118
|
||||
#: src/error.c:120
|
||||
msgid "failed to open the source file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:119 src/error.c:120
|
||||
#: src/error.c:121 src/error.c:122
|
||||
msgid "failed to write to the destination file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:121
|
||||
#: src/error.c:123
|
||||
msgid "package does not have associated pool"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:122
|
||||
#: src/error.c:124
|
||||
msgid "failed to create specified temp directory"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:123
|
||||
#: src/error.c:125
|
||||
msgid "package archive does not contain required files"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:124
|
||||
#: src/error.c:126
|
||||
msgid "package data does not match with target package"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:125
|
||||
#: src/error.c:127
|
||||
#, c-format
|
||||
msgid "failed to update changes file for package: %s"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:126
|
||||
#: src/error.c:128
|
||||
msgid "failed to access package hashes file"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:127
|
||||
#: src/error.c:129
|
||||
msgid "failed to remove package changes file from the database"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:128
|
||||
#: src/error.c:130
|
||||
msgid "failed to stat target file for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:129
|
||||
#: src/error.c:131
|
||||
msgid "failed to format target file size for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:130
|
||||
#: src/error.c:132
|
||||
msgid "failed to read target file size for sending"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:131
|
||||
#: src/error.c:133
|
||||
msgid "failed to parse target file size for receiving"
|
||||
msgstr ""
|
||||
|
||||
#: src/error.c:132
|
||||
#: src/error.c:134
|
||||
msgid "target file is not fully received"
|
||||
msgstr ""
|
||||
|
@ -20,8 +20,8 @@ void lm_ctx_init(lm_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir){
|
||||
if(!exists(dir)){
|
||||
lm_error_set(LM_ERR_CtxTempFail);
|
||||
if(!mkdir_ifnot(dir)){
|
||||
lm_error_set(LM_ERR_CtxTempFailMkdir);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_versi
|
||||
if(NULL == ctx->db)
|
||||
return false;
|
||||
|
||||
if(!lm_database_find(ctx->db, &found, pkg->name))
|
||||
if(!lm_database_package_find(ctx->db, &found, pkg->name))
|
||||
return false; // error set by function
|
||||
|
||||
if(check_version && !eq(found.version, pkg->version))
|
||||
@ -43,7 +43,7 @@ bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name){
|
||||
return false; // error set by function
|
||||
|
||||
lm_error_clear();
|
||||
return lm_database_find(ctx->db, pkg, name);
|
||||
return lm_database_package_find(ctx->db, pkg, name);
|
||||
}
|
||||
|
||||
bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback, void *data){
|
||||
@ -57,7 +57,7 @@ bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback,
|
||||
|
||||
lm_pkg_t pkg;
|
||||
|
||||
while(lm_database_next(ctx->db, &pkg)){
|
||||
while(lm_database_package_next(ctx->db, &pkg)){
|
||||
if(!callback(ctx, &pkg, data))
|
||||
break;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call
|
||||
return false;
|
||||
}
|
||||
|
||||
if(mkdir_ifnot(ctx->temp)){
|
||||
if(!mkdir_ifnot(ctx->temp)){
|
||||
lm_error_set(LM_ERR_CtxTempFailMkdir);
|
||||
return false;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx){
|
||||
|
||||
lm_pkg_t *pkg = malloc(sizeof(lm_pkg_t));
|
||||
|
||||
while(lm_database_next(ctx->db, pkg)){
|
||||
while(lm_database_package_next(ctx->db, pkg)){
|
||||
if(NULL == list->packages)
|
||||
list->packages = malloc(sizeof(lm_pkg_t*));
|
||||
else
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@ -13,10 +14,13 @@ bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){
|
||||
return false;
|
||||
}
|
||||
|
||||
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
char changes_file[strlen(pkg->name)+20];
|
||||
sprintf(changes_file, "%s_changes", pkg->name);
|
||||
|
||||
if(!copy_file(changes_file, file))
|
||||
char changes_path[strlen(db->dir)+sizeof(changes_file)];
|
||||
join(changes_path, db->dir, changes_file);
|
||||
|
||||
if(!copy_file(changes_path, file))
|
||||
return false; // error set by function
|
||||
|
||||
return true;
|
||||
@ -28,10 +32,13 @@ bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
return false;
|
||||
}
|
||||
|
||||
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
char changes_file[strlen(pkg->name)+20];
|
||||
sprintf(changes_file, "%s_changes", pkg->name);
|
||||
|
||||
if(unlink(changes_file) < 0 && errno != ENOENT){
|
||||
char changes_path[strlen(db->dir)+sizeof(changes_file)];
|
||||
join(changes_path, db->dir, changes_file);
|
||||
|
||||
if(unlink(changes_path) < 0 && errno != ENOENT){
|
||||
lm_error_set(LM_ERR_DbChangesUnlinkFail);
|
||||
return false;
|
||||
}
|
||||
@ -40,13 +47,16 @@ bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
}
|
||||
|
||||
char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg){
|
||||
char *changes_file = malloc(strlen(db->dir)+strlen(pkg->name)+20);
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
char changes_file[strlen(pkg->name)+20];
|
||||
sprintf(changes_file, "%s_changes", pkg->name);
|
||||
|
||||
if(!exists(changes_file)){
|
||||
free(changes_file);
|
||||
char *changes_path = malloc(strlen(db->dir)+sizeof(changes_file));
|
||||
join(changes_path, db->dir, changes_file);
|
||||
|
||||
if(!exists(changes_path)){
|
||||
free(changes_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return changes_file;
|
||||
return changes_path;
|
||||
}
|
||||
|
@ -9,16 +9,50 @@
|
||||
#include <strings.h>
|
||||
|
||||
char *queries[] = {
|
||||
// QUERY_CREATE_PACKAGE_TABLE
|
||||
"CREATE TABLE IF NOT EXISTS packages (" \
|
||||
" name TEXT PRIMARY KEY NOT NULL," \
|
||||
" desc TEXT NOT NULL," \
|
||||
" version TEXT NOT NULL," \
|
||||
" size INT NOT NULL," \
|
||||
" depends TEXT NOT NULL);",
|
||||
|
||||
// QUERY_INSERT_PACKAGE_SINGLE
|
||||
"INSERT INTO packages VALUES (?, ?, ?, ?, ?)",
|
||||
|
||||
// QUERY_SELECT_PACKAGE_SINGLE
|
||||
"SELECT * FROM packages WHERE name = '?'",
|
||||
"DELETE from packages WHERE name = '?'",
|
||||
|
||||
// QUERY_DELETE_PACKAGE_SINGLE
|
||||
"DELETE FROM packages WHERE name = '?'",
|
||||
|
||||
// QUERY_SELECT_PACKAGE_ALL
|
||||
"SELECT * FROM packages",
|
||||
|
||||
// QUERY_CREATE_FILE_TABLE
|
||||
"CREATE TABLE IF NOT EXISTS files (" \
|
||||
" path TEXT PRIMARY KEY NOT NULL," \
|
||||
" hash TEXT NOT NULL," \
|
||||
" keep INT NOT NULL," \
|
||||
" package TEXT NOT NULL);"
|
||||
|
||||
// QUERY_INSERT_FILE_SINGLE
|
||||
"INSERT INTO files VALUES (?, ?, ?, ?)",
|
||||
|
||||
// QUERY_DELETE_FILE_ALL
|
||||
"DELETE FROM files WHERE package = '?'",
|
||||
|
||||
// QUERY_SELECT_FILE_SINGLE
|
||||
"SELECT * FROM files WHERE path = '?'",
|
||||
|
||||
// QUERY_SELECT_FILE_ALL
|
||||
"SELECT * FROM files WHERE package = '?'",
|
||||
|
||||
// QUERY_UPDATE_FILE_1
|
||||
"UPDATE files SET keep = 1 WHERE path = '?'",
|
||||
|
||||
// QUERY_UPDATE_FILE_2
|
||||
"UPDATE files SET keep = 0 WHERE path = '?'",
|
||||
};
|
||||
|
||||
lm_database_t *lm_database_new(char *path){
|
||||
@ -31,30 +65,54 @@ lm_database_t *lm_database_new(char *path){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char dbfile[strlen(path)+20];
|
||||
join(dbfile, path, "packages.db");
|
||||
if(!exists(path) && !mkdir_ifnot(path)){
|
||||
lm_error_set(LM_ERR_DbCantAccess);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t pathlen = strlen(path);
|
||||
char packagesdb[pathlen+20], filesdb[pathlen+20];
|
||||
|
||||
join(packagesdb, path, "packages.db");
|
||||
join(filesdb, path, "files.db");
|
||||
|
||||
if(sqlite3_open(dbfile, &db->sql)){
|
||||
pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->sql));
|
||||
if(sqlite3_open(packagesdb, &db->packages_db)){
|
||||
pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlOpenFail);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(sqlite3_open(filesdb, &db->files_db)){
|
||||
pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->files_db));
|
||||
lm_error_set(LM_ERR_DbSqlOpenFail);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(sqlite3_exec(db->sql, queries[QUERY_CREATE_TABLE], NULL, 0, &err) != SQLITE_OK){
|
||||
if(sqlite3_exec(db->packages_db, queries[QUERY_CREATE_PACKAGE_TABLE], NULL, 0, &err) != SQLITE_OK){
|
||||
pdebug(__func__, "(%s) failed to create packages table: %s", path, err);
|
||||
lm_error_set(LM_ERR_DbSqlCreateFail);
|
||||
sqlite3_free(err);
|
||||
}
|
||||
|
||||
if(sqlite3_exec(db->files_db, queries[QUERY_CREATE_FILE_TABLE], NULL, 0, &err) != SQLITE_OK){
|
||||
pdebug(__func__, "(%s) failed to create files table: %s", path, err);
|
||||
lm_error_set(LM_ERR_DbSqlCreateFail);
|
||||
sqlite3_free(err);
|
||||
}
|
||||
|
||||
db->dir = strdup(path);
|
||||
return db;
|
||||
}
|
||||
|
||||
void lm_database_free(lm_database_t *db){
|
||||
if(NULL != db->st)
|
||||
sqlite3_finalize(db->st);
|
||||
if(NULL != db->packages_st)
|
||||
sqlite3_finalize(db->packages_st);
|
||||
|
||||
sqlite3_close(db->sql);
|
||||
if(NULL != db->files_st)
|
||||
sqlite3_finalize(db->files_st);
|
||||
|
||||
sqlite3_close(db->packages_db);
|
||||
sqlite3_close(db->files_db);
|
||||
free(db->dir);
|
||||
|
||||
lm_pkg_t *cur = db->pkg, *prev = NULL;
|
||||
|
@ -10,166 +10,171 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_files_eachfunc_t func, void *data){
|
||||
if(NULL == db || NULL == pkg || NULL == func){
|
||||
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;
|
||||
}
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
|
||||
if(!exists(files_list)){
|
||||
lm_error_set(LM_ERR_DbFilesNotFound);
|
||||
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;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "r");
|
||||
char *line = NULL, hash[HASH_LEN+1];
|
||||
size_t line_len = 0;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
goto end;
|
||||
else if(NULL != db->files_st){
|
||||
free(*path);
|
||||
free(*hash);
|
||||
*path = NULL;
|
||||
*hash = NULL;
|
||||
}
|
||||
|
||||
while((line_len = getline(&line, 0, files)) > 0){
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
char path[line_len+1];
|
||||
memcpy(path, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
|
||||
if(!func(pkg, path, hash, data))
|
||||
goto end;
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
if(sqlite3_step(db->files_st) != SQLITE_ROW){
|
||||
sqlite3_finalize(db->files_st);
|
||||
db->files_st = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != files)
|
||||
fclose(files);
|
||||
|
||||
return ret;
|
||||
*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){
|
||||
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char pkg_dir[strlen(db->dir)+strlen(pkg->name)+5];
|
||||
char files_list[sizeof(pkg_dir)+15];
|
||||
bool ret = false;
|
||||
|
||||
join(pkg_dir, db->dir, pkg->name);
|
||||
join(files_list, pkg_dir, "files");
|
||||
|
||||
if(!mkdir_ifnot(pkg_dir)){
|
||||
lm_error_set(LM_ERR_DbFilesDirFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "a");
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(fprintf(files, "%s:%s\n", hash, path) < 0){
|
||||
pdebug(__func__, "failed to append file to files list: %s", files_list);
|
||||
lm_error_set(LM_ERR_DbFilesWriteFail);
|
||||
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 != files)
|
||||
fclose(files);
|
||||
|
||||
if(NULL != db->files_st){
|
||||
sqlite3_finalize(db->files_st);
|
||||
db->files_st = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){
|
||||
if(NULL == db || NULL == pkg || NULL == path){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(NULL != hash)
|
||||
bzero(hash, HASH_LEN+1); // zero out the hash
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+10];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
|
||||
if(!exists(files_list)){
|
||||
lm_error_set(LM_ERR_DbFilesNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "r");
|
||||
size_t line_len = 0;
|
||||
char *line = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while((line_len = getline(&line, 0, files)) > 0){
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
char lpath[line_len+1];
|
||||
memcpy(lpath, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
|
||||
if(eq(lpath, path)){
|
||||
if(NULL != hash){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
}
|
||||
ret = true;
|
||||
goto end;
|
||||
}
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != files)
|
||||
fclose(files);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+10];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
bool ret = false;
|
||||
|
||||
if(unlink(files_list) < 0 && errno != ENOENT){
|
||||
pdebug(__func__, "failed to delete file list for %s: %s", pkg->name, files_list);
|
||||
lm_error_set(LM_ERR_DbFilesUnlinkFail);
|
||||
return 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;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -1,153 +0,0 @@
|
||||
#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_keeps_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_keeps_eachfunc_t func, void *data){
|
||||
if(NULL == db || NULL == pkg || NULL == func){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
||||
|
||||
if(!exists(keeps_list)){
|
||||
lm_error_set(LM_ERR_DbKeepsNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *keeps = fopen(keeps_list, "r");
|
||||
char *line = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == keeps){
|
||||
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, keeps) > 0){
|
||||
if(line[0] == 0)
|
||||
continue;
|
||||
|
||||
if(!func(pkg, line, data))
|
||||
goto end;
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != keeps)
|
||||
fclose(keeps);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_keeps_load(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
||||
|
||||
if(!exists(keeps_list)){
|
||||
lm_error_set(LM_ERR_DbKeepsNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *keeps = fopen(keeps_list, "r");
|
||||
char *line = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == keeps){
|
||||
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, keeps) > 0){
|
||||
if(line[0] == 0)
|
||||
continue;
|
||||
|
||||
if(!lm_package_keep_add(pkg, line))
|
||||
goto end;
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != keeps)
|
||||
fclose(keeps);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_keeps_save(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char pkg_dir[strlen(db->dir)+strlen(pkg->name)+5];
|
||||
char keeps_list[sizeof(pkg_dir)+15];
|
||||
bool ret = false;
|
||||
|
||||
join(pkg_dir, db->dir, pkg->name);
|
||||
join(keeps_list, pkg_dir, "keeps");
|
||||
|
||||
if(!mkdir_ifnot(pkg_dir)){
|
||||
lm_error_set(LM_ERR_DbKeepsDirFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *keeps = fopen(keeps_list, "a");
|
||||
|
||||
if(NULL == keeps){
|
||||
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for(int i = 0; NULL != pkg->keeps[i]; i++)
|
||||
fprintf(keeps, "%s\n", pkg->keeps[i]);
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != keeps)
|
||||
fclose(keeps);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_keeps_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
||||
|
||||
if(unlink(keeps_list) < 0 && errno != ENOENT){
|
||||
pdebug(__func__, "failed to delete keep list for %s: %s", pkg->name, keeps_list);
|
||||
lm_error_set(LM_ERR_DbKeepsUnlinkFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
bool lm_database_add(lm_database_t *db, lm_pkg_t *pkg){
|
||||
bool lm_database_packege_add(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -17,39 +17,39 @@ bool lm_database_add(lm_database_t *db, lm_pkg_t *pkg){
|
||||
char depends[lm_package_depend_strlen(pkg)];
|
||||
bool ret = false;
|
||||
|
||||
if(sqlite3_prepare(db->sql, queries[QUERY_INSERT_PACKAGE], strlen(queries[QUERY_INSERT_PACKAGE]), &db->st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->sql));
|
||||
if(sqlite3_prepare(db->packages_db, queries[QUERY_INSERT_PACKAGE_SINGLE], strlen(queries[QUERY_INSERT_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(db->st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->st, 2, pkg->desc, strlen(pkg->desc), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->st, 3, pkg->version, strlen(pkg->version), SQLITE_STATIC);
|
||||
sqlite3_bind_int64(db->st, 4, pkg->size);
|
||||
sqlite3_bind_text(db->packages_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->packages_st, 2, pkg->desc, strlen(pkg->desc), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->packages_st, 3, pkg->version, strlen(pkg->version), SQLITE_STATIC);
|
||||
sqlite3_bind_int64(db->packages_st, 4, pkg->size);
|
||||
|
||||
if(!lm_package_depend_tostr(pkg, depends)){
|
||||
pdebug(__func__, "failed to convert depends to string for inserting %s: %s", pkg->name, lm_strerror());
|
||||
goto end;
|
||||
}
|
||||
sqlite3_bind_text(db->st, 5, depends, strlen(depends), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->packages_st, 5, depends, strlen(depends), SQLITE_STATIC);
|
||||
|
||||
if(sqlite3_step(db->st) != SQLITE_DONE){
|
||||
pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->sql));
|
||||
if(sqlite3_step(db->packages_st) != SQLITE_DONE){
|
||||
pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlInsertFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != db->st){
|
||||
sqlite3_finalize(db->st);
|
||||
db->st = NULL;
|
||||
if(NULL != db->packages_st){
|
||||
sqlite3_finalize(db->packages_st);
|
||||
db->packages_st = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg, char *name){
|
||||
bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg, char *name){
|
||||
if(NULL == db || NULL == name){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -57,15 +57,15 @@ bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg, char *name){
|
||||
|
||||
bool ret = false;
|
||||
|
||||
if(sqlite3_prepare(db->sql, queries[QUERY_SELECT_PACKAGE], strlen(queries[QUERY_SELECT_PACKAGE]), &db->st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for finding %s: %s", name, sqlite3_errmsg(db->sql));
|
||||
if(sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_SINGLE], strlen(queries[QUERY_SELECT_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for finding %s: %s", name, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(db->st, 1, name, strlen(name), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->packages_st, 1, name, strlen(name), SQLITE_STATIC);
|
||||
|
||||
if(sqlite3_step(db->st) != SQLITE_ROW){
|
||||
if(sqlite3_step(db->packages_st) != SQLITE_ROW){
|
||||
pdebug(__func__, "got no rows for %s", name);
|
||||
lm_error_set(LM_ERR_DbSqlNotFound);
|
||||
goto end;
|
||||
@ -80,12 +80,12 @@ bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg, char *name){
|
||||
// we are initing it just in case the caller didn't
|
||||
lm_package_init(pkg);
|
||||
|
||||
pkg->name = strdup((char*)sqlite3_column_text(db->st, 0));
|
||||
pkg->desc = strdup((char*)sqlite3_column_text(db->st, 1));
|
||||
pkg->version = strdup((char*)sqlite3_column_text(db->st, 2));
|
||||
pkg->size = sqlite3_column_int64(db->st, 3);
|
||||
pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, 0));
|
||||
pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, 1));
|
||||
pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, 2));
|
||||
pkg->size = sqlite3_column_int64(db->packages_st, 3);
|
||||
|
||||
char *depends = (char*)sqlite3_column_text(db->st, 3);
|
||||
char *depends = (char*)sqlite3_column_text(db->packages_st, 3);
|
||||
if(!lm_package_depend_fromstr(pkg, depends)){
|
||||
pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror());
|
||||
// error is set by the function
|
||||
@ -94,14 +94,14 @@ bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg, char *name){
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != db->st){
|
||||
sqlite3_finalize(db->st);
|
||||
db->st = NULL;
|
||||
if(NULL != db->packages_st){
|
||||
sqlite3_finalize(db->packages_st);
|
||||
db->packages_st = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
bool lm_database_package_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
@ -109,63 +109,62 @@ bool lm_database_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
|
||||
bool ret = false;
|
||||
|
||||
if(sqlite3_prepare(db->sql, queries[QUERY_DELETE_PACKAGE], strlen(queries[QUERY_DELETE_PACKAGE]), &db->st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->sql));
|
||||
if(sqlite3_prepare(db->packages_db, queries[QUERY_DELETE_PACKAGE_SINGLE], strlen(queries[QUERY_DELETE_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(db->st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
||||
sqlite3_bind_text(db->packages_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
|
||||
|
||||
if(sqlite3_step(db->st) != SQLITE_DONE){
|
||||
pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->sql));
|
||||
lm_error_set(LM_ERR_DbSqlInsertFail);
|
||||
if(sqlite3_step(db->packages_st) != SQLITE_DONE){
|
||||
pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlDeleteFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != db->st){
|
||||
sqlite3_finalize(db->st);
|
||||
db->st = NULL;
|
||||
if(NULL != db->packages_st){
|
||||
sqlite3_finalize(db->packages_st);
|
||||
db->packages_st = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_next(lm_database_t *db, lm_pkg_t *pkg){
|
||||
bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(NULL == db->st){
|
||||
if(sqlite3_prepare(db->sql, queries[QUERY_ALL_PACKAGE], strlen(queries[QUERY_ALL_PACKAGE]), &db->st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->sql));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
return false;
|
||||
}
|
||||
if(NULL == db->packages_st &&
|
||||
sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_ALL], strlen(queries[QUERY_SELECT_PACKAGE_ALL]), &db->packages_st, NULL) != SQLITE_OK){
|
||||
pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->packages_db));
|
||||
lm_error_set(LM_ERR_DbSqlPrepareFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
else
|
||||
else if(NULL != db->packages_st)
|
||||
lm_package_free(pkg);
|
||||
|
||||
if(sqlite3_step(db->st) != SQLITE_ROW){
|
||||
sqlite3_finalize(db->st);
|
||||
db->st = NULL;
|
||||
if(sqlite3_step(db->packages_st) != SQLITE_ROW){
|
||||
sqlite3_finalize(db->packages_st);
|
||||
db->packages_st = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
lm_package_init(pkg);
|
||||
|
||||
pkg->name = strdup((char*)sqlite3_column_text(db->st, 0));
|
||||
pkg->desc = strdup((char*)sqlite3_column_text(db->st, 1));
|
||||
pkg->version = strdup((char*)sqlite3_column_text(db->st, 2));
|
||||
pkg->size = sqlite3_column_int64(db->st, 3);
|
||||
pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, 0));
|
||||
pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, 1));
|
||||
pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, 2));
|
||||
pkg->size = sqlite3_column_int64(db->packages_st, 3);
|
||||
|
||||
char *depends = (char*)sqlite3_column_text(db->st, 3);
|
||||
char *depends = (char*)sqlite3_column_text(db->packages_st, 3);
|
||||
if(!lm_package_depend_fromstr(pkg, depends)){
|
||||
pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror());
|
||||
sqlite3_finalize(db->st);
|
||||
sqlite3_finalize(db->packages_st);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -89,6 +89,8 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_DbSqlCreateFail, .desc = _("failed to create table in SQLite database") },
|
||||
{.code = LM_ERR_DbSqlPrepareFail, .desc = _("failed to prepare statement for SQLite database") },
|
||||
{.code = LM_ERR_DbSqlInsertFail, .desc = _("failed to insert to the table in SQLite database") },
|
||||
{.code = LM_ERR_DbSqlSelectFail, .desc = _("failed to select from the table in SQLite database") },
|
||||
{.code = LM_ERR_DbSqlDeleteFail, .desc = _("failed to delete from the table in SQLite database") },
|
||||
{.code = LM_ERR_DbSqlNotFound, .desc = _("failed to find entry in SQLite database") },
|
||||
{.code = LM_ERR_PkgGPGFail, .desc = _("failed to init GPG for package verification") },
|
||||
{.code = LM_ERR_PkgGPGSigFail, .desc = _("failed to import signature to GPG for package verification")},
|
||||
|
Loading…
Reference in New Issue
Block a user