update: redesigning ctx functions and structure
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
@ -83,6 +93,9 @@ void lm_ctx_free(lm_ctx_t *ctx) {
|
||||
free(ctx->data);
|
||||
free(ctx->root);
|
||||
free(ctx->temp);
|
||||
|
||||
if(NULL != ctx->db)
|
||||
lm_database_free(ctx->db);
|
||||
|
||||
lm_error_clear();
|
||||
return;
|
||||
|
@ -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){
|
||||
@ -48,11 +51,13 @@ bool lm_ctx_database_foreach(lm_ctx_t *ctx, lm_ctx_database_callback_t callback,
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
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;
|
||||
}
|
||||
|
@ -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);
|
||||
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(!install_resolve(ctx, list, pkg))
|
||||
return false;
|
||||
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;
|
||||
}
|
||||
|
||||
list->resolving = pkglist_del(list->resolving, pkg);
|
||||
end:
|
||||
list->packages = pkglist_add(list->packages, pkg);
|
||||
list->count++;
|
||||
return true;
|
||||
}
|
||||
if(NULL == ctx->root){
|
||||
lm_error_set(LM_ERR_CtxRootNULL);
|
||||
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(mkdir_ifnot(ctx->temp)){
|
||||
lm_error_set(LM_ERR_CtxTempFailMkdir);
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
|
||||
pkg = pkg->next;
|
||||
if((hashes = fopen(hashes_file, "r")) != NULL){
|
||||
lm_error_set(LM_ERR_PkgHashesOpenFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
// actually install packages
|
||||
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
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
pkglist_free(list.packages);
|
||||
pkglist_free(list.resolving);
|
||||
return true;
|
||||
if(NULL != hashes)
|
||||
fclose(hashes);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -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
93
src/ctx/resolve.c
Normal 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
61
src/ctx/update.c
Normal 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
52
src/database/changes.c
Normal 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;
|
||||
}
|
@ -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)){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
if(NULL != hash){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
}
|
||||
ret = true;
|
||||
goto end;
|
||||
}
|
||||
|
196
src/error.c
196
src/error.c
@ -16,100 +16,102 @@ void lm_error_clear() {
|
||||
|
||||
void lm_error_set(lm_error_t code, ...) {
|
||||
lm_error_desc_t errors[] = {
|
||||
{.code = LM_ERR_NoError, .desc = _("no error") },
|
||||
{.code = LM_ERR_URLBadChar, .desc = _("URL contains an invalid character") },
|
||||
{.code = LM_ERR_URLBadProtocol, .desc = _("URL does not have a valid protocol field") },
|
||||
{.code = LM_ERR_URLTooLarge, .desc = _("URL is too large") },
|
||||
{.code = LM_ERR_URLHostLarge, .desc = _("URL hostname is too large") },
|
||||
{.code = LM_ERR_URLPathLarge, .desc = _("URL path is too large") },
|
||||
{.code = LM_ERR_URLBadHost, .desc = _("URL does not have a valid hostname") },
|
||||
{.code = LM_ERR_URLBadPort, .desc = _("URL does not have a valid port number") },
|
||||
{.code = LM_ERR_URLBadPath, .desc = _("URL does not have a valid path") },
|
||||
{.code = LM_ERR_BadPort, .desc = _("hostname does not contain a valid port number") },
|
||||
{.code = LM_ERR_BadHost, .desc = _("hostname is not valid") },
|
||||
{.code = LM_ERR_URLPortUnknown, .desc = _("URL protocol port number is unknown") },
|
||||
{.code = LM_ERR_URLEnd, .desc = _("URL is incomplete") },
|
||||
{.code = LM_ERR_PoolNoSupport, .desc = _("pool does not support the specified protocol") },
|
||||
{.code = LM_ERR_MPTPBadVersion, .desc = _("unsupported MPTP version") },
|
||||
{.code = LM_ERR_MPTPBadCode, .desc = _("invalid MPTP request/response code") },
|
||||
{.code = LM_ERR_MPTPBadUrl, .desc = _("invalid MPTP URL") },
|
||||
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
|
||||
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
|
||||
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
|
||||
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
|
||||
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
||||
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") },
|
||||
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
|
||||
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") },
|
||||
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
|
||||
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
||||
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
|
||||
{.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") },
|
||||
{.code = LM_ERR_NoPort, .desc = _("host port not specified") },
|
||||
{.code = LM_ERR_PoolInfoBad, .desc = _("pool info is badly formatted or is not complete") },
|
||||
{.code = LM_ERR_ArcWBlockFail, .desc = _("failed to write block from archive") },
|
||||
{.code = LM_ERR_ArcRBlockFail, .desc = _("failed to read block from archive") },
|
||||
{.code = LM_ERR_ArcOpenFail, .desc = _("failed to open archive") },
|
||||
{.code = LM_ERR_ArcWHeaderFail, .desc = _("failed to write archive header") },
|
||||
{.code = LM_ERR_ArcWEntryFail, .desc = _("failed to finish writing the archive entry") },
|
||||
{.code = LM_ERR_ArcNewFail, .desc = _("failed to create new archive reader/writer") },
|
||||
{.code = LM_ERR_ArcRealpathFail, .desc = _("failed to resolve full path for archive file") },
|
||||
{.code = LM_ERR_ArcNextHeaderFail, .desc = _("failed to read the next header of the archive") },
|
||||
{.code = LM_ERR_GetCwdFail, .desc = _("failed to obtain current working directory") },
|
||||
{.code = LM_ERR_PoolListDirFail, .desc = _("failed to open extracted pool list directory") },
|
||||
{.code = LM_ERR_PoolListCantRead, .desc = _("failed to read access the pool list file") },
|
||||
{.code = LM_ERR_PoolInfoCantRead, .desc = _("failed to read access the pool info file") },
|
||||
{.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_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") },
|
||||
{.code = LM_ERR_CtxRootFail, .desc = _("specified root path does not exist") },
|
||||
{.code = LM_ERR_CtxRootNotDir, .desc = _("specified root path is not a directory") },
|
||||
{.code = LM_ERR_CtxRootNoWrite, .desc = _("specified root directory does not have write access") },
|
||||
{.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") },
|
||||
{.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") },
|
||||
{.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") },
|
||||
{.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") },
|
||||
{.code = LM_ERR_PkgPathsEmpty, .desc = _("package file and directory paths are empty") },
|
||||
{.code = LM_ERR_SendOpenFail, .desc = _("failed to to open target file for sending") },
|
||||
{.code = LM_ERR_RecvDelFail, .desc = _("failed to to delete target file for receiving") },
|
||||
{.code = LM_ERR_RecvOpenFail, .desc = _("failed to to open target file for receiving") },
|
||||
{.code = LM_ERR_RecvBadCode, .desc = _("got a bad response code for receiving the target file") },
|
||||
{.code = LM_ERR_RecvWriteFail, .desc = _("failed to write to the target file for receiving") },
|
||||
{.code = LM_ERR_PkgNotFound, .desc = _("package not found") },
|
||||
{.code = LM_ERR_DbCantAccess, .desc = _("failed to access to the database file/directory") },
|
||||
{.code = LM_ERR_DbSqlOpenFail, .desc = _("failed to open SQLite database") },
|
||||
{.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_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")},
|
||||
{.code = LM_ERR_PkgGPGArchiveFail, .desc = _("failed to import archive to GPG for package verification") },
|
||||
{.code = LM_ERR_PkgSigNoMatch, .desc = _("package signature verification failed with zero matches") },
|
||||
{.code = LM_ERR_PkgSigNoResult, .desc = _("package signature verification failed with zero results") },
|
||||
{.code = LM_ERR_PoolPathsEmpty, .desc = _("pool file and directory paths are empty") },
|
||||
{.code = LM_ERR_PoolNotAvailable, .desc = _("pool is not avaliable for connection") },
|
||||
{.code = LM_ERR_PoolUrlEmpty, .desc = _("pool URL is empty or invalid") },
|
||||
{.code = LM_ERR_PoolBadDir, .desc = _("pool directory path is not accessible") },
|
||||
{.code = LM_ERR_PoolBadPaths, .desc = _("pool directory sub-paths are not accessible") },
|
||||
{.code = LM_ERR_DbFilesNotFound, .desc = _("package file list not found in the database") },
|
||||
{.code = LM_ERR_DbFilesOpenFail, .desc = _("failed to open package file list in the database") },
|
||||
{.code = LM_ERR_DbFilesDirFail, .desc = _("failed to access package file list database directory") },
|
||||
{.code = LM_ERR_DbFilesUnlinkFail, .desc = _("failed to remove package file list from the database") },
|
||||
{.code = LM_ERR_DbFilesWriteFail, .desc = _("failed to write to the file list in the database") },
|
||||
{.code = LM_ERR_DbKeepsNotFound, .desc = _("package keep list not found in the database") },
|
||||
{.code = LM_ERR_DbKeepsOpenFail, .desc = _("failed to open package keep list in the database") },
|
||||
{.code = LM_ERR_DbKeepsDirFail, .desc = _("failed to access package keep list database directory") },
|
||||
{.code = LM_ERR_DbKeepsUnlinkFail, .desc = _("failed to remove package keep list from the database") },
|
||||
{.code = LM_ERR_DependNotFound, .desc = _("failed to find %s (dependency of %s)") },
|
||||
{.code = LM_ERR_InstallDownloadFail, .desc = _("failed to download %s for installation: %s") },
|
||||
{.code = LM_ERR_PkgNotDownloaded, .desc = _("package is not downloaded") },
|
||||
{.code = LM_ERR_PkgRemoveDownloadFail, .desc = _("failed to remove downloaded package") },
|
||||
{.code = LM_ERR_NoError, .desc = _("no error") },
|
||||
{.code = LM_ERR_URLBadChar, .desc = _("URL contains an invalid character") },
|
||||
{.code = LM_ERR_URLBadProtocol, .desc = _("URL does not have a valid protocol field") },
|
||||
{.code = LM_ERR_URLTooLarge, .desc = _("URL is too large") },
|
||||
{.code = LM_ERR_URLHostLarge, .desc = _("URL hostname is too large") },
|
||||
{.code = LM_ERR_URLPathLarge, .desc = _("URL path is too large") },
|
||||
{.code = LM_ERR_URLBadHost, .desc = _("URL does not have a valid hostname") },
|
||||
{.code = LM_ERR_URLBadPort, .desc = _("URL does not have a valid port number") },
|
||||
{.code = LM_ERR_URLBadPath, .desc = _("URL does not have a valid path") },
|
||||
{.code = LM_ERR_BadPort, .desc = _("hostname does not contain a valid port number") },
|
||||
{.code = LM_ERR_BadHost, .desc = _("hostname is not valid") },
|
||||
{.code = LM_ERR_URLPortUnknown, .desc = _("URL protocol port number is unknown") },
|
||||
{.code = LM_ERR_URLEnd, .desc = _("URL is incomplete") },
|
||||
{.code = LM_ERR_PoolNoSupport, .desc = _("pool does not support the specified protocol") },
|
||||
{.code = LM_ERR_MPTPBadVersion, .desc = _("unsupported MPTP version") },
|
||||
{.code = LM_ERR_MPTPBadCode, .desc = _("invalid MPTP request/response code") },
|
||||
{.code = LM_ERR_MPTPBadUrl, .desc = _("invalid MPTP URL") },
|
||||
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
|
||||
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
|
||||
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
|
||||
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
|
||||
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
|
||||
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
|
||||
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") },
|
||||
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
|
||||
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
|
||||
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") },
|
||||
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
|
||||
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
|
||||
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
|
||||
{.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") },
|
||||
{.code = LM_ERR_NoPort, .desc = _("host port not specified") },
|
||||
{.code = LM_ERR_PoolInfoBad, .desc = _("pool info is badly formatted or is not complete") },
|
||||
{.code = LM_ERR_ArcWBlockFail, .desc = _("failed to write block from archive") },
|
||||
{.code = LM_ERR_ArcRBlockFail, .desc = _("failed to read block from archive") },
|
||||
{.code = LM_ERR_ArcOpenFail, .desc = _("failed to open archive") },
|
||||
{.code = LM_ERR_ArcWHeaderFail, .desc = _("failed to write archive header") },
|
||||
{.code = LM_ERR_ArcWEntryFail, .desc = _("failed to finish writing the archive entry") },
|
||||
{.code = LM_ERR_ArcNewFail, .desc = _("failed to create new archive reader/writer") },
|
||||
{.code = LM_ERR_ArcRealpathFail, .desc = _("failed to resolve full path for archive file") },
|
||||
{.code = LM_ERR_ArcNextHeaderFail, .desc = _("failed to read the next header of the archive") },
|
||||
{.code = LM_ERR_GetCwdFail, .desc = _("failed to obtain current working directory") },
|
||||
{.code = LM_ERR_PoolListDirFail, .desc = _("failed to open extracted pool list directory") },
|
||||
{.code = LM_ERR_PoolListCantRead, .desc = _("failed to read access the pool list file") },
|
||||
{.code = LM_ERR_PoolInfoCantRead, .desc = _("failed to read access the pool info file") },
|
||||
{.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") },
|
||||
{.code = LM_ERR_CtxRootFail, .desc = _("specified root path does not exist") },
|
||||
{.code = LM_ERR_CtxRootNotDir, .desc = _("specified root path is not a directory") },
|
||||
{.code = LM_ERR_CtxRootNoWrite, .desc = _("specified root directory does not have write access") },
|
||||
{.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") },
|
||||
{.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") },
|
||||
{.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") },
|
||||
{.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") },
|
||||
{.code = LM_ERR_PkgPathsEmpty, .desc = _("package file and directory paths are empty") },
|
||||
{.code = LM_ERR_SendOpenFail, .desc = _("failed to to open target file for sending") },
|
||||
{.code = LM_ERR_RecvDelFail, .desc = _("failed to to delete target file for receiving") },
|
||||
{.code = LM_ERR_RecvOpenFail, .desc = _("failed to to open target file for receiving") },
|
||||
{.code = LM_ERR_RecvBadCode, .desc = _("got a bad response code for receiving the target file") },
|
||||
{.code = LM_ERR_RecvWriteFail, .desc = _("failed to write to the target file for receiving") },
|
||||
{.code = LM_ERR_PkgNotFound, .desc = _("package not found") },
|
||||
{.code = LM_ERR_DbCantAccess, .desc = _("failed to access to the database file/directory") },
|
||||
{.code = LM_ERR_DbSqlOpenFail, .desc = _("failed to open SQLite database") },
|
||||
{.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_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")},
|
||||
{.code = LM_ERR_PkgGPGArchiveFail, .desc = _("failed to import archive to GPG for package verification") },
|
||||
{.code = LM_ERR_PkgSigNoMatch, .desc = _("package signature verification failed with zero matches") },
|
||||
{.code = LM_ERR_PkgSigNoResult, .desc = _("package signature verification failed with zero results") },
|
||||
{.code = LM_ERR_PoolPathsEmpty, .desc = _("pool file and directory paths are empty") },
|
||||
{.code = LM_ERR_PoolNotAvailable, .desc = _("pool is not avaliable for connection") },
|
||||
{.code = LM_ERR_PoolUrlEmpty, .desc = _("pool URL is empty or invalid") },
|
||||
{.code = LM_ERR_PoolBadDir, .desc = _("pool directory path is not accessible") },
|
||||
{.code = LM_ERR_PoolBadPaths, .desc = _("pool directory sub-paths are not accessible") },
|
||||
{.code = LM_ERR_DbFilesNotFound, .desc = _("package file list not found in the database") },
|
||||
{.code = LM_ERR_DbFilesOpenFail, .desc = _("failed to open package file list in the database") },
|
||||
{.code = LM_ERR_DbFilesDirFail, .desc = _("failed to access package file list database directory") },
|
||||
{.code = LM_ERR_DbFilesUnlinkFail, .desc = _("failed to remove package file list from the database") },
|
||||
{.code = LM_ERR_DbFilesWriteFail, .desc = _("failed to write to the file list in the database") },
|
||||
{.code = LM_ERR_DbKeepsNotFound, .desc = _("package keep list not found in the database") },
|
||||
{.code = LM_ERR_DbKeepsOpenFail, .desc = _("failed to open package keep list in the database") },
|
||||
{.code = LM_ERR_DbKeepsDirFail, .desc = _("failed to access package keep list database directory") },
|
||||
{.code = LM_ERR_DbKeepsUnlinkFail, .desc = _("failed to remove package keep list from the database") },
|
||||
{.code = LM_ERR_DependNotFound, .desc = _("failed to find %s (dependency of %s)") },
|
||||
{.code = LM_ERR_InstallDownloadFail, .desc = _("failed to download %s for installation: %s") },
|
||||
{.code = LM_ERR_PkgNotDownloaded, .desc = _("package is not downloaded") },
|
||||
{.code = LM_ERR_PkgRemoveDownloadFail, .desc = _("failed to remove downloaded package") },
|
||||
};
|
||||
|
||||
char *fmt = NULL;
|
||||
@ -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);
|
||||
|
@ -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));
|
||||
|
35
src/util.c
35
src/util.c
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user