update: redesigning ctx functions and structure

This commit is contained in:
ngn 2024-07-05 01:44:42 +03:00
parent 38b99eda79
commit 6045f73478
20 changed files with 663 additions and 343 deletions

3
TODO.md Normal file
View File

@ -0,0 +1,3 @@
- Implement callbacks for recvfile and sendfile
- Remove/refactor unused functions
- Add all the ctx methods

View File

@ -28,3 +28,4 @@
#include "error.h"
#include "pool.h"
#include "types.h"
#include "util.h"

View File

@ -1,9 +1,11 @@
#pragma once
#include "database.h"
#include "types.h"
#include <stdbool.h>
typedef struct lm_ctx {
lm_database_t *db; // package database
lm_pool_t *pools; // pool list
char *root; // root path for package installtion
char *temp; // temp path
@ -11,36 +13,85 @@ typedef struct lm_ctx {
const char *version; // libmp version (read-only)
} lm_ctx_t;
typedef bool (*lm_ctx_pool_callback_t)(lm_ctx_t *ctx, lm_pool_t *pool, bool status, void *data);
typedef bool (*lm_ctx_database_callback_t)(lm_ctx_t *ctx, lm_pkg_t *pkg, void *data);
typedef struct lm_ctx_resolve_list {
lm_pkg_t *resolving;
lm_pkg_t *packages;
lm_pkg_t *cur;
size_t count;
} lm_ctx_resolve_list_t;
typedef struct lm_ctx_update_list {
lm_pkg_t **packages;
size_t count;
size_t index;
} lm_ctx_update_list_t;
/* ###################
## ctx callbacks ##
################### */
typedef bool (*lm_ctx_database_callback_t)(lm_ctx_t *ctx, lm_pkg_t *pkg, void *data);
typedef bool (*lm_ctx_download_callback_t)(lm_ctx_t *ctx, lm_pkg_t *pkg, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_install_callback_t)(
lm_ctx_t *ctx, lm_pkg_t *pkg, char *file, size_t current, size_t total, void *data);
typedef bool (*lm_ctx_sync_callback_t)(
lm_ctx_t *ctx, lm_pool_t *pool, bool status, size_t current, size_t total, void *data);
typedef lm_ctx_install_callback_t lm_ctx_remove_callback_t;
typedef lm_ctx_install_callback_t lm_ctx_check_callback_t;
/* ###############
## ctx stuff ##
############### */
void lm_ctx_init(lm_ctx_t *ctx);
bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir);
bool lm_ctx_set_root(lm_ctx_t *ctx, char *dir);
bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir);
void lm_ctx_free(lm_ctx_t *ctx);
lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version);
bool lm_ctx_install(lm_ctx_t *ctx, char *package);
bool lm_ctx_remove(lm_ctx_t *ctx, char *package);
bool lm_ctx_verify(lm_ctx_t *ctx, char *package);
bool lm_ctx_update(lm_ctx_t *ctx);
/* ####################
## main fucntions ##
#################### */
lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version); // find package by name (and version)
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url);
bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name);
void lm_ctx_pool_clear(lm_ctx_t *ctx);
bool lm_ctx_pool_serve(lm_ctx_t *ctx, char *addr, uint8_t threads);
void lm_ctx_pool_test(lm_ctx_t *ctx, lm_ctx_pool_callback_t callback, void *data);
void lm_ctx_pool_get_info(
lm_ctx_t *ctx, bool allow_update, bool force_update, lm_ctx_pool_callback_t callback, void *data);
void lm_ctx_pool_get_list(
lm_ctx_t *ctx, bool allow_update, bool force_update, lm_ctx_pool_callback_t callback, void *data);
lm_ctx_resolve_list_t *lm_ctx_resolve(
lm_ctx_t *ctx, lm_pkg_t *pkg); // resolves a package and returns a list of packages to install
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // returns the next package in the list
void lm_ctx_resolve_free(
lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve
bool lm_ctx_package_install(lm_ctx_t *ctx, lm_pkg_t *pkg);
bool lm_ctx_package_remove(lm_ctx_t *ctx, lm_pkg_t *pkg);
bool lm_ctx_package_verify(lm_ctx_t *ctx, lm_pkg_t *pkg);
lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx); // returns a list of packages to update
lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // returns the next package in the list
void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // frees the update list returned by lm_ctx_update
lm_database_t *lm_ctx_database_new(lm_ctx_t *ctx);
bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_version);
bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name);
bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback, void *data);
bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback,
void *data); // downloads a single package if its not already downloaded
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_remove(
lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callback, void *data); // removes 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
bool lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools
bool lm_ctx_serve(lm_ctx_t *ctx, char *add, uint8_t threads); // serves all the pools
/* ####################
## pool fucntions ##
#################### */
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); // add a pool to the ctx pool list
bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); // remove a pool from the ctx pool list
void lm_ctx_pool_clear(lm_ctx_t *ctx); // clear all the pools in the ctx pool list
bool lm_ctx_pool_serve(
lm_ctx_t *ctx, char *addr, uint8_t threads); // serve all the pools on a MPTP server hosted at the given address
void lm_ctx_pool_test(lm_ctx_t *ctx); // test (ping) all the pool connetions
void lm_ctx_pool_get_info(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback,
void *data); // get pool info of all the pools in the ctx list
void lm_ctx_pool_get_list(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback,
void *data); // get pool list of all the pools in the ctx list
/* ########################
## 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); // find a package by name
bool lm_ctx_database_foreach(
lm_ctx_t *ctx, lm_ctx_database_callback_t callback, void *data); // loop for each package in the database

View File

@ -42,3 +42,7 @@ bool lm_database_keeps_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_kee
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_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);

View File

@ -1,5 +1,8 @@
#pragma once
#define lm_strerror_dup() \
{ strdup(lm_strerror()) }
typedef enum lm_error {
LM_ERR_NoError = 0,
LM_ERR_URLBadChar = 1,

View File

@ -33,3 +33,4 @@ 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_is_same(lm_pkg_t *one, lm_pkg_t *two);

View File

@ -15,6 +15,7 @@ bool is_digit(char c);
bool copy_from_buffer(void *dst, void *buffer, size_t size, ssize_t *total, ssize_t *used);
bool copy_to_buffer(void *buffer, void *src, size_t size, ssize_t *total, ssize_t *used);
bool copy_file(char *dst, char *src);
bool can_write(char *path);
bool can_read(char *path);
bool is_file(char *path);

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-04 03:04+0300\n"
"POT-Creation-Date: 2024-07-04 03:19+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"

View File

@ -2,6 +2,7 @@
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <linux/limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <locale.h>
@ -34,7 +35,10 @@ bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir){
return false;
}
ctx->temp = strdup(dir);
char fullpath[PATH_MAX + 1];
realpath(dir, fullpath);
ctx->temp = strdup(fullpath);
return true;
}
@ -54,7 +58,10 @@ bool lm_ctx_set_root(lm_ctx_t *ctx, char *dir){
return false;
}
ctx->root = strdup(dir);
char fullpath[PATH_MAX + 1];
realpath(dir, fullpath);
ctx->root = strdup(fullpath);
return true;
}
@ -74,7 +81,10 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
return false;
}
ctx->data = strdup(dir);
char fullpath[PATH_MAX + 1];
realpath(dir, fullpath);
ctx->data = strdup(fullpath);
return true;
}
@ -84,6 +94,9 @@ void lm_ctx_free(lm_ctx_t *ctx) {
free(ctx->root);
free(ctx->temp);
if(NULL != ctx->db)
lm_database_free(ctx->db);
lm_error_clear();
return;
}

View File

@ -5,30 +5,32 @@
#include <string.h>
lm_database_t *lm_ctx_database_new(lm_ctx_t *ctx){
bool lm_ctx_database_init(lm_ctx_t *ctx){
if(ctx->db != NULL)
return true;
char dbpath[strlen(ctx->data)+10];
join(dbpath, ctx->data, "db");
return lm_database_new(dbpath);
ctx->db = lm_database_new(dbpath);
return ctx->db != NULL; // error set by function
}
bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_version){
lm_database_t *db = lm_ctx_database_new(ctx);
if(!lm_ctx_database_init(ctx))
return false; // error set by function
lm_pkg_t found;
bool ret = false;
if(NULL == db)
if(NULL == ctx->db)
return false;
if(!lm_database_find(db, &found, pkg->name))
goto end;
if(!lm_database_find(ctx->db, &found, pkg->name))
return false; // error set by function
if(check_version && !eq(found.version, pkg->version))
goto end;
return false;
ret = true;
end:
lm_database_free(db);
return ret;
return true;
}
bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name){
@ -37,10 +39,11 @@ bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name){
return false;
}
lm_database_t *db = lm_ctx_database_new(ctx);
if(!lm_database_find(db, pkg, name))
return false;
return true;
if(!lm_ctx_database_init(ctx))
return false; // error set by function
lm_error_clear();
return lm_database_find(ctx->db, pkg, name);
}
bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback, void *data){
@ -49,10 +52,12 @@ bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback,
return false;
}
lm_database_t *db = lm_ctx_database_new(ctx);
if(!lm_ctx_database_init(ctx))
return false; // error set by function
lm_pkg_t pkg;
while(lm_database_next(db, &pkg)){
while(lm_database_next(ctx->db, &pkg)){
if(!callback(ctx, &pkg, data))
break;
}

View File

@ -7,82 +7,90 @@
#include <stdlib.h>
#include <string.h>
typedef struct install_list {
lm_pkg_t *packages;
lm_pkg_t *resolving;
size_t count;
} install_list_t;
bool install_resolve(lm_ctx_t *ctx, install_list_t *list, lm_pkg_t *pkg){
if(pkglist_contains(list->packages, pkg))
return true;
if(NULL == pkg->depends)
goto end;
list->resolving = pkglist_add(list->resolving, pkg);
for(int i = 0; pkg->depends[i] != NULL; i++){
lm_pkg_t *depend = lm_ctx_find(ctx, pkg->depends[i], NULL);
if(NULL == depend){
lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name);
bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t callback, void *data) {
if(NULL == ctx->temp){
lm_error_set(LM_ERR_CtxTempNULL);
return false;
}
if(lm_ctx_database_is_installed(ctx, depend, true))
continue;
if(pkglist_contains(list->resolving, depend)){
lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name);
if(NULL == ctx->root){
lm_error_set(LM_ERR_CtxRootNULL);
return false;
}
if(!install_resolve(ctx, list, pkg))
if(mkdir_ifnot(ctx->temp)){
lm_error_set(LM_ERR_CtxTempFailMkdir);
return false;
}
list->resolving = pkglist_del(list->resolving, pkg);
end:
list->packages = pkglist_add(list->packages, pkg);
list->count++;
return true;
if(!lm_ctx_database_init(ctx))
return false; // error set by function
if(!extract_archive(ctx->temp, pkg->paths.archive))
return false; // error set by function
size_t bufsize = strlen(ctx->temp) + 25;
char data_file[bufsize], changes_file[bufsize], hashes_file[bufsize];
char install_file[bufsize], files_archive[bufsize];
lm_pkg_t temp;
lm_package_init(&temp);
join(data_file, ctx->temp, "DATA");
join(hashes_file, ctx->temp, "HASHES");
join(changes_file, ctx->temp, "CHANGES");
join(install_file, ctx->temp, "INSTALL");
join(files_archive, ctx->temp, "files.tar.gz");
if(!exists(data_file) || !is_file(data_file) ||
!exists(hashes_file) || !is_file(hashes_file) ||
!exists(changes_file) || !is_file(changes_file) ||
!exists(install_file) || !is_file(install_file) ||
!exists(files_archive) || !is_file(files_archive)){
lm_error_set(LM_ERR_PkgBadArchive);
return false;
}
bool lm_ctx_install(lm_ctx_t *ctx, char *package){
lm_pkg_t *pkg = lm_ctx_find(ctx, package, NULL);
install_list_t list = {
.packages = NULL,
.resolving = NULL,
.count = 0,
};
if(!lm_package_data_load(&temp, data_file))
return false; // error set by function
if(!lm_package_is_same(&temp, pkg)){
lm_error_set(LM_ERR_PkgDataNotMatch);
return false;
}
if(!lm_database_changes_update(ctx->db, pkg, changes_file)){
char *suberr = lm_strerror_dup();
lm_error_set(LM_ERR_PkgChangesUpdateFail, suberr);
return false;
}
char *line = NULL, *hash = NULL, *file = NULL;
size_t line_len = 0;
FILE *hashes = NULL;
bool ret = false;
if(NULL == pkg)
return ret;
if(!install_resolve(ctx, &list, pkg))
goto end;
pkg = list.packages;
while(pkg != NULL){
if(lm_package_downloaded(pkg) && lm_package_verify(pkg))
continue;
if(!lm_pool_package_download(pkg->pool, pkg)){
char *suberror = strdup(lm_strerror());
lm_error_set(LM_ERR_InstallDownloadFail, pkg->name, suberror);
free(suberror);
if((hashes = fopen(hashes_file, "r")) != NULL){
lm_error_set(LM_ERR_PkgHashesOpenFail);
return false;
}
pkg = pkg->next;
while((line_len = getline(&line, 0, hashes)) > 0){
if(HASH_LEN+1 >= line_len)
continue;
line[HASH_LEN] = 0;
hash = line;
file = line+HASH_LEN+1;
if(!lm_database_files_add(ctx->db, pkg, file, hash))
return false; // error set by function
}
// actually install packages
ret = true;
end:
pkglist_free(list.packages);
pkglist_free(list.resolving);
return true;
if(NULL != hashes)
fclose(hashes);
return ret;
}

View File

@ -1,18 +0,0 @@
#include "../../include/pool.h"
#include "../../include/package.h"
#include "../../include/database.h"
#include "../../include/util.h"
#include "../../include/error.h"
#include "../../include/ctx.h"
bool lm_ctx_package_install(lm_ctx_t *ctx, lm_pkg_t *pkg){
return true;
}
bool lm_ctx_package_remove(lm_ctx_t *ctx, lm_pkg_t *pkg){
return true;
}
bool lm_ctx_package_verify(lm_ctx_t *ctx, lm_pkg_t *pkg){
return true;
}

93
src/ctx/resolve.c Normal file
View File

@ -0,0 +1,93 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdlib.h>
#include <string.h>
bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg){
if(pkglist_contains(list->packages, pkg))
return true;
if(NULL == pkg->depends)
goto end;
list->resolving = pkglist_add(list->resolving, pkg);
for(int i = 0; pkg->depends[i] != NULL; i++){
lm_pkg_t *depend = lm_ctx_find(ctx, pkg->depends[i], NULL);
if(NULL == depend){
lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name);
return false;
}
if(lm_ctx_database_is_installed(ctx, depend, true))
continue;
if(pkglist_contains(list->resolving, depend)){
lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name);
return false;
}
if(!__lm_ctx_resolve(ctx, list, pkg))
return false;
}
list->resolving = pkglist_del(list->resolving, pkg);
end:
list->packages = pkglist_add(list->packages, pkg);
list->count++;
return true;
}
lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg){
if(NULL == ctx || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_ctx_resolve_list_t *list = malloc(sizeof(lm_ctx_resolve_list_t));
list->resolving = NULL;
list->packages = NULL;
list->cur = NULL;
list->count = 0;
if(!__lm_ctx_resolve(ctx, list, pkg)){
pkglist_free(list->resolving);
pkglist_free(list->packages);
free(list);
return NULL;
}
pkglist_free(list->resolving);
list->resolving = NULL;
return list;
}
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){
if(NULL == ctx || NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
if(NULL == list->cur){
list->cur = list->packages;
return list->cur;
}
list->cur = list->cur->next;
return list->cur;
}
void lm_ctx_resolve_free(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){
if(NULL == ctx || NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}
pkglist_free(list->packages);
free(list);
return;
}

61
src/ctx/update.c Normal file
View File

@ -0,0 +1,61 @@
#include "../../include/package.h"
#include "../../include/database.h"
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdlib.h>
#include <string.h>
lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx){
lm_ctx_update_list_t *list = malloc(sizeof(lm_ctx_update_list_t));
list->packages = NULL;
list->index = 0;
list->count = 0;
if(!lm_ctx_database_init(ctx))
return NULL;
lm_pkg_t *pkg = malloc(sizeof(lm_pkg_t));
while(lm_database_next(ctx->db, pkg)){
if(NULL == list->packages)
list->packages = malloc(sizeof(lm_pkg_t*));
else
list->packages = realloc(list->packages, (list->count+1)*sizeof(lm_pkg_t*));
list->packages[list->count++] = pkg;
pkg = malloc(sizeof(lm_pkg_t));
}
return list;
}
lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list){
if(NULL == ctx || NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
if(list->index >= list->count-1){
list->index = 0;
return NULL;
}
return list->packages[list->index++];
}
void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list){
if(NULL == ctx || NULL == list){
lm_error_set(LM_ERR_ArgNULL);
return;
}
for(int i = 0; i < list->count; i++){
lm_package_free(list->packages[i]);
free(list->packages[i]);
}
free(list);
}

52
src/database/changes.c Normal file
View File

@ -0,0 +1,52 @@
#include "../../include/database.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){
if(NULL == db || NULL == pkg || NULL == file){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
join_multiple(changes_file, db->dir, pkg->name, "changes");
if(!copy_file(changes_file, file))
return false; // error set by function
return true;
}
bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
join_multiple(changes_file, db->dir, pkg->name, "changes");
if(unlink(changes_file) < 0 && errno != ENOENT){
lm_error_set(LM_ERR_DbChangesUnlinkFail);
return false;
}
return true;
}
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");
if(!exists(changes_file)){
free(changes_file);
return NULL;
}
return changes_file;
}

View File

@ -34,13 +34,11 @@ bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_fil
goto end;
}
while(getline(&line, 0, files) > 0){
line_len = strlen(line);
char path[line_len+1];
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;
@ -102,13 +100,13 @@ end:
}
bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
if(NULL == db || NULL == pkg || NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
// zero out the hash
bzero(hash, HASH_LEN+1);
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");
@ -119,8 +117,8 @@ bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *h
}
FILE *files = fopen(files_list, "r");
char *line = NULL;
size_t line_len = 0;
char *line = NULL;
bool ret = false;
if(NULL == files){
@ -128,18 +126,18 @@ bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *h
goto end;
}
while(getline(&line, 0, files) > 0){
line_len = strlen(line);
char lpath[line_len+1];
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;
}

View File

@ -64,6 +64,8 @@ void lm_error_set(lm_error_t code, ...) {
{.code = LM_ERR_PkgDataBad, .desc = _("failed to parse package data") },
{.code = LM_ERR_PkgBadName, .desc = _("package name is invalid") },
{.code = LM_ERR_CtxDataNULL, .desc = _("data path is not set with in the ctx") },
{.code = LM_ERR_CtxTempNULL, .desc = _("temp path is not set with in the ctx") },
{.code = LM_ERR_CtxRootNULL, .desc = _("root path is not set with in the ctx") },
{.code = LM_ERR_CtxTempFail, .desc = _("specified temp path does not exist") },
{.code = LM_ERR_CtxTempNotDir, .desc = _("specified temp path is not a directory") },
{.code = LM_ERR_CtxTempNoWrite, .desc = _("specified temp directory does not have write access") },
@ -134,9 +136,9 @@ void lm_error_set(lm_error_t code, ...) {
va_start(args, code);
va_copy(argscp, args);
int size = vsnprintf(NULL, 0, fmt, args);
error_str = malloc(size + 1);
vsnprintf(error_str, size, fmt, args);
int size = vsnprintf(NULL, 0, fmt, args) + 2;
error_str = malloc(size);
vsnprintf(error_str, size, fmt, argscp);
va_end(args);
va_end(argscp);

View File

@ -47,6 +47,13 @@ bool lm_package_remove_download(lm_pkg_t *pkg){
return true;
}
bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two){
return eq(one->version, two->version) &&
eq(one->name, two->name) &&
eq(one->desc, two->desc) &&
one->size == two->size;
}
void lm_package_free(lm_pkg_t *pkg){
lm_package_data_free(pkg);
bzero(pkg, sizeof(lm_pkg_t));

View File

@ -389,3 +389,38 @@ void pkglist_free(lm_pkg_t *list) {
free(old);
}
}
bool copy_file(char *dst, char *src) {
FILE *dstp = NULL, *srcp = NULL;
bool ret = false;
if ((dstp = fopen(dst, "w")) == NULL) {
lm_error_set(LM_ERR_DstOpenFail);
goto end;
}
if ((srcp = fopen(src, "r")) == NULL) {
lm_error_set(LM_ERR_SrcOpenFail);
goto end;
}
char buf[255];
size_t bufsize = sizeof(buf), read = 0;
while ((read = fread(buf, 1, bufsize, srcp)) > 0) {
if (fwrite(buf, 1, read, dstp) <= 0) {
lm_error_set(LM_ERR_DstWriteFail);
goto end;
}
}
ret = true;
end:
if (NULL != dstp)
fclose(dstp);
if (NULL != srcp)
fclose(srcp);
return ret;
}