update: refactoring and small fixes

This commit is contained in:
ngn
2024-07-31 21:16:19 +03:00
parent 3f794e1a90
commit 9cd4eb9905
41 changed files with 779 additions and 752 deletions

View File

@ -27,5 +27,4 @@
#include "ctx.h"
#include "error.h"
#include "pool.h"
#include "types.h"
#include "util.h"

View File

@ -1,7 +1,7 @@
#pragma once
#include "database.h"
#include "mptp.h"
#include "types.h"
#include "pool.h"
#include <stdbool.h>
@ -18,13 +18,12 @@ typedef struct lm_ctx_resolve_list {
lm_pkg_t *resolving;
lm_pkg_t *packages;
lm_pkg_t *cur;
size_t count;
ssize_t count;
} lm_ctx_resolve_list_t;
typedef struct lm_ctx_update_list {
lm_pkg_t *packages;
lm_pkg_t *cur;
size_t count;
lm_entry_t **entries;
ssize_t index, count;
} lm_ctx_update_list_t;
typedef enum lm_ctx_sync_state {
@ -48,8 +47,9 @@ typedef bool (*lm_ctx_sync_callback_t)(
lm_ctx_t *ctx, lm_pool_t *pool, lm_ctx_sync_state_t state, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_serve_callback_t)(lm_pool_t *pool, lm_mptp_t *packet, struct sockaddr *addr, void *data);
typedef bool (*lm_ctx_ping_callback_t)(lm_ctx_t *ctx, lm_pool_t *pool, bool status, void *data);
typedef lm_ctx_install_callback_t lm_ctx_remove_callback_t;
typedef lm_ctx_install_callback_t lm_ctx_check_callback_t;
typedef bool (*lm_ctx_remove_callback_t)(
lm_ctx_t *ctx, lm_entry_t *pkg, char *file, size_t current, size_t total, void *data);
typedef lm_ctx_remove_callback_t lm_ctx_check_callback_t;
/* ###############
## ctx stuff ##
@ -68,21 +68,21 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list); // returns the next package in the list
void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve
lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx); // get a list of packages to update
lm_pkg_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list); // get the next package in the update list
lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx); // get a list of entries to update
lm_entry_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list); // get the next entry in the update list
void lm_ctx_update_list_free(lm_ctx_update_list_t *list); // free the update list
lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg); // returns the updated version of a package
lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_entry_t *pkg); // returns the updated version of a entry
bool lm_ctx_removeable(lm_ctx_t *ctx, lm_pkg_t *pkg); // checks if a package is available for removal
bool lm_ctx_removeable(lm_ctx_t *ctx, lm_entry_t *entry); // checks if a package is available for removal
bool lm_ctx_remove(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callback, void *data); // removes a single package
lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_remove_callback_t callback, void *data); // removes a single package
bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback,
void *data); // downloads a single package
bool lm_ctx_install(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t callback, void *data); // installs/updates a single package
bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, bool run_install, lm_ctx_install_callback_t callback,
void *data); // installs/updates a single package
bool lm_ctx_check(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback, void *data); // checks a single package
lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data); // checks a single package
size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools
void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data); // pings all the pools
bool lm_ctx_serve(
@ -102,9 +102,9 @@ lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find p
## database fucntions ##
######################## */
bool lm_ctx_database_init(lm_ctx_t *ctx); // init ctx database (if not already present)
bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_version); // check if a package is installed
bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name, char *version); // find a package by name
bool lm_ctx_database_next(lm_ctx_t *ctx, lm_pkg_t *pkg); // load the next package into pkg pointer
bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_pkg_t *pkg); // free the used next pointers
bool lm_ctx_database_is_installed(lm_ctx_t *ctx, char *name, char *version); // check if a package is installed
bool lm_ctx_database_find(lm_ctx_t *ctx, lm_entry_t *entry, char *name, char *version); // find a package by name
bool lm_ctx_database_next(lm_ctx_t *ctx, lm_entry_t *entry); // load the next package into pkg pointer
bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_entry_t *entry); // free the used next pointers
char *lm_ctx_database_changes(
lm_ctx_t *ctx, lm_pkg_t *pkg); // get changes file path for a package, FREE THE RETURNED POINTER
lm_ctx_t *ctx, lm_entry_t *entry); // get changes file path for a package, FREE THE RETURNED POINTER

View File

@ -1,78 +1,86 @@
#pragma once
#include "database.h"
#include "package.h"
#include <sqlite3.h>
#include <stdbool.h>
#include <sys/types.h>
#define HASH_LEN 32
enum lm_query_index {
QUERY_CREATE_PACKAGE_TABLE = 0,
QUERY_INSERT_PACKAGE_SINGLE = 1,
QUERY_SELECT_PACKAGE_SINGLE_1 = 2,
QUERY_SELECT_PACKAGE_SINGLE_2 = 3,
QUERY_DELETE_PACKAGE_SINGLE = 4,
QUERY_SELECT_PACKAGE_ALL = 5,
QUERY_CREATE_FILE_TABLE = 6,
QUERY_INSERT_FILE_SINGLE = 7,
QUERY_DELETE_FILE_ALL = 8,
QUERY_SELECT_FILE_SINGLE = 9,
QUERY_SELECT_FILE_ALL = 10,
QUERY_UPDATE_FILE_1 = 11,
QUERY_UPDATE_FILE_2 = 12,
QUERY_CREATE_ENTRY_TABLE = 0,
QUERY_INSERT_ENTRY_SINGLE = 1,
QUERY_SELECT_ENTRY_SINGLE_1 = 2,
QUERY_SELECT_ENTRY_SINGLE_2 = 3,
QUERY_DELETE_ENTRY_SINGLE = 4,
QUERY_SELECT_ENTRY_ALL = 5,
QUERY_CREATE_FILE_TABLE = 6,
QUERY_INSERT_FILE_SINGLE = 7,
QUERY_DELETE_FILE_ALL = 8,
QUERY_SELECT_FILE_SINGLE = 9,
QUERY_SELECT_FILE_ALL = 10,
QUERY_UPDATE_FILE_1 = 11,
QUERY_UPDATE_FILE_2 = 12,
};
enum lm_columns {
FILES_COLUMN_PATH = 1,
FILES_COLUMN_HASH = 2,
FILES_COLUMN_KEEP = 3,
FILES_COLUMN_PACKAGE = 4,
FILES_COLUMN_PATH = 1,
FILES_COLUMN_HASH = 2,
FILES_COLUMN_KEEP = 3,
FILES_COLUMN_ENTRY = 4,
PACKAGES_COLUMN_NAME = 1,
PACKAGES_COLUMN_VERSION = 2,
PACKAGES_COLUMN_DESC = 3,
PACKAGES_COLUMN_SIZE = 4,
PACKAGES_COLUMN_DEPENDS = 5,
ENTRIES_COLUMN_NAME = 1,
ENTRIES_COLUMN_VERSION = 2,
ENTRIES_COLUMN_DESC = 3,
ENTRIES_COLUMN_SIZE = 4,
ENTRIES_COLUMN_DEPENDS = 5,
};
extern char *queries[];
typedef lm_pkg_data_t lm_entry_t;
typedef struct lm_database {
sqlite3 *packages_db;
sqlite3_stmt *packages_st;
sqlite3 *entries_db;
sqlite3_stmt *entries_st;
sqlite3 *files_db;
sqlite3_stmt *files_st;
lm_pkg_t *pkg;
char *dir;
char *dir;
} lm_database_t;
void lm_entry_init(lm_entry_t *entry);
void lm_entry_free(lm_entry_t *entry);
lm_database_t *lm_database_new(char *path);
void lm_database_free(lm_database_t *db);
bool lm_database_step_all(sqlite3_stmt *st);
bool lm_database_package_find(
lm_database_t *db, lm_pkg_t *pkg, char *name, char *version); // 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_package_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
void lm_database_package_next_free(
lm_database_t *db, lm_pkg_t *pkg); // frees resources used for lm_database_package_next
bool lm_database_entry_find(
lm_database_t *db, lm_entry_t *out, char *name, char *version); // finds a entry by its name, entry stored in *pkg
bool lm_database_entry_next(lm_database_t *db, lm_entry_t *entry); // gets the next entry in the database
bool lm_database_entry_add(lm_database_t *db, lm_entry_t *entry); // adds a entry to the database
bool lm_database_entry_del(lm_database_t *db, lm_entry_t *entry); // delete a entry from the database
void lm_database_entry_next_free(
lm_database_t *db, lm_entry_t *entry); // frees resources used for lm_database_entry_next
size_t lm_database_files_count(
lm_database_t *db, lm_pkg_t *pkg); // returns the count of files associated with a package
ssize_t lm_database_files_count(
lm_database_t *db, lm_entry_t *entry); // returns the count of files associated with a entry
bool lm_database_files_contains(
lm_database_t *db, lm_pkg_t *pkg, char *path); // check if a package contains the given file
lm_database_t *db, lm_entry_t *entry, char *path); // check if a entry contains the given file
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
lm_database_t *db, lm_entry_t *entry, char **path, char **hash, bool *keep); // gets the next file of the entry
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
void lm_database_files_next_free(lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash,
lm_database_t *db, lm_entry_t *entry, char *path, char *hash); // adds a file to the files database
bool lm_database_files_del(lm_database_t *db, lm_entry_t *entry); // dels all files of belonging to a entry
void lm_database_files_next_free(lm_database_t *db, lm_entry_t *entry, char **path, char **hash,
bool *keep); // frees resources used for lm_database_files_next
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);
bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_changes_update(lm_database_t *db, lm_entry_t *entry, char *file);
char *lm_database_changes_get(lm_database_t *db, lm_entry_t *entry);
bool lm_database_changes_del(lm_database_t *db, lm_entry_t *entry);

View File

@ -131,7 +131,7 @@ typedef enum lm_error {
LM_ERR_InstallSpawnFail = 123,
LM_ERR_InstallBackChdirFail = 124,
LM_ERR_InstallStatusFail = 125,
LM_ERR_InstallScriptFail = 126,
LM_ERR_InstallRunFail = 126,
LM_ERR_PkgBreaks = 127,
LM_ERR_PkgUpToDate = 128,
LM_ERR_HashOpenFail = 129,
@ -142,6 +142,8 @@ typedef enum lm_error {
LM_ERR_NoPools = 134,
LM_ERR_DbChangesNotExists = 135,
LM_ERR_DbChangesChmodFail = 136,
LM_ERR_InstallDirFail = 137,
LM_ERR_InstallSaveFail = 138,
} lm_error_t;
typedef struct lm_error_desc {

View File

@ -1,8 +1,9 @@
#pragma once
#include "types.h"
#include <netdb.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
// clang-format off

View File

@ -1,6 +1,6 @@
#pragma once
#include "types.h"
#include <stdbool.h>
#include <sys/types.h>
#define PKG_DATA_SIZE "size"
#define PKG_DATA_DESC "desc"
@ -8,32 +8,46 @@
#define PKG_DATA_DEPENDS "depends"
#define PKG_DATA_KEEPS "keeps"
typedef struct lm_pkg_data {
char *name;
char *desc;
char **depends;
char **keeps;
char *version;
ssize_t size;
} lm_pkg_data_t;
typedef struct lm_pkg {
struct lm_pkg_data data;
struct lm_pool *pool;
struct lm_pkg *next;
char *signature;
char *archive;
} lm_pkg_t;
lm_pkg_t *lm_package_new();
void lm_package_free(lm_pkg_t *pkg);
bool lm_package_verify(lm_pkg_t *pkg);
void lm_package_init(lm_pkg_t *pkg);
bool lm_package_copy(lm_pkg_t *dst, lm_pkg_t *src);
bool lm_package_downloaded(lm_pkg_t *pkg);
bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two);
bool lm_package_data_load(lm_pkg_t *pkg, char *file);
void lm_package_data_free(lm_pkg_t *pkg);
bool lm_package_data_load(lm_pkg_data_t *data, char *file);
void lm_package_data_free(lm_pkg_data_t *data);
bool lm_package_downloaded(lm_pkg_t *pkg);
bool lm_package_depend_add(lm_pkg_t *pkg, char *depend);
size_t lm_package_depend_count(lm_pkg_t *pkg);
size_t lm_package_depend_strlen(lm_pkg_t *pkg);
char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer);
bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer);
void lm_package_depend_free(lm_pkg_t *pkg);
bool lm_package_depends(lm_pkg_t *pkg, char *dep);
bool lm_package_data_depend_add(lm_pkg_data_t *data, char *depend);
ssize_t lm_package_data_depend_count(lm_pkg_data_t *data);
ssize_t lm_package_data_depend_strlen(lm_pkg_data_t *data);
char *lm_package_data_depend_tostr(lm_pkg_data_t *data, char *buffer);
bool lm_package_data_depend_fromstr(lm_pkg_data_t *data, char *buffer);
void lm_package_data_depend_free(lm_pkg_data_t *data);
bool lm_package_data_depends(lm_pkg_data_t *data, char *dep);
size_t lm_package_keep_count(lm_pkg_t *pkg);
bool lm_package_keep_add(lm_pkg_t *pkg, char *path);
bool lm_package_keep_contains(lm_pkg_t *pkg, char *path);
void lm_package_keep_free(lm_pkg_t *pkg);
size_t lm_package_data_keep_count(lm_pkg_data_t *data);
bool lm_package_data_keep_add(lm_pkg_data_t *data, char *path);
bool lm_package_data_keep_contains(lm_pkg_data_t *data, char *path);
void lm_package_data_keep_free(lm_pkg_data_t *data);
bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path);
bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path);
bool lm_package_path_is_empty(lm_pkg_t *pkg);
void lm_package_path_free(lm_pkg_t *pkg);
bool lm_package_path_copy(lm_pkg_t *dst, lm_pkg_t *src);
bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two);

View File

@ -1,10 +1,30 @@
#pragma once
#include "mptp.h"
#include "types.h"
#include "package.h"
#include "url.h"
#include <stdbool.h>
#include <sys/socket.h>
typedef struct lm_pool_info {
char *maintainer;
char *pubkey;
ssize_t size;
} lm_pool_info_t;
typedef struct lm_pool {
struct lm_pool *next;
lm_pool_info_t info;
lm_url_t url;
lm_pkg_t *pkg;
char *name;
char *info_file;
char *list_file;
char *list_dir;
char *dir;
bool available, loaded;
} lm_pool_t;
#define POOL_INFO_SIZE "size"
#define POOL_INFO_PUBKEY "pubkey"
#define POOL_INFO_MAINTAINER "maintainer"
@ -18,7 +38,6 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg);
bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir);
bool lm_pool_path_is_empty(lm_pool_t *pool);
void lm_pool_path_free(lm_pool_t *pool);
bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data);
bool lm_pool_info_load(lm_pool_t *pool);

View File

@ -1,51 +0,0 @@
#pragma once
#include "url.h"
#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>
typedef struct lm_pkg_file {
char *path;
char *hash;
} lm_pkg_file_t;
typedef struct lm_pkg_path {
char *archive;
char *signature;
} lm_pkg_path_t;
typedef struct lm_pkg {
struct lm_pool *pool;
struct lm_pkg *next;
lm_pkg_path_t paths;
char *name;
char *desc;
char **depends;
char **keeps;
char *version;
size_t size;
} lm_pkg_t;
typedef struct lm_pool_info {
char *maintainer;
char *pubkey;
ssize_t size;
} lm_pool_info_t;
typedef struct lm_pool_path {
char *info_file;
char *list_file;
char *list_dir;
char *dir;
} lm_pool_path_t;
typedef struct lm_pool {
struct lm_pool *next;
lm_pool_info_t info;
lm_pool_path_t paths;
lm_url_t url;
lm_pkg_t *pkg;
char *name;
bool available, loaded;
} lm_pool_t;

View File

@ -1,5 +1,6 @@
#pragma once
#include "types.h"
#include "pool.h"
#include <archive.h>
#include <libintl.h>
#include <netinet/in.h>
@ -23,6 +24,7 @@ bool can_read(char *path);
bool is_file(char *path);
bool is_dir(char *path);
bool exists(char *path);
bool is_empty(char *p);
bool package_parse(char *package, char *name, char *version);
bool package_version_valid(char *name);