new: custom pool dirs and remove ctx path functions
This commit is contained in:
113
src/ctx/ctx.c
113
src/ctx/ctx.c
@ -9,83 +9,70 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void lm_ctx_init(lm_ctx_t *ctx) {
|
||||
bool __lm_ctx_init_checkdir(char *path){
|
||||
if(!mkdir_ifnot(path)){
|
||||
lm_error_set(LM_ERR_FailMkdir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_dir(path)){
|
||||
lm_error_set(LM_ERR_NotDir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!can_write(path)){
|
||||
lm_error_set(LM_ERR_NoWrite);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_ctx_init(lm_ctx_t *ctx, char *root_dir, char *temp_dir, char *data_dir) {
|
||||
setlocale(LC_ALL, "");
|
||||
textdomain("libmp");
|
||||
|
||||
char *suberr = NULL;
|
||||
bool ret = false;
|
||||
|
||||
bzero(ctx, sizeof(lm_ctx_t));
|
||||
ctx->version = LM_VERSION;
|
||||
|
||||
lm_error_clear();
|
||||
}
|
||||
|
||||
bool lm_ctx_set_temp(lm_ctx_t *ctx, char *dir){
|
||||
if(!mkdir_ifnot(dir)){
|
||||
lm_error_set(LM_ERR_CtxTempFailMkdir);
|
||||
return false;
|
||||
if(root_dir != NULL && !__lm_ctx_init_checkdir(root_dir)){
|
||||
suberr = lm_strerror_dup();
|
||||
pdebug(__func__, "check failed for specified root directory: %s", lm_strerror());
|
||||
lm_error_set(LM_ERR_CtxRootFail, root_dir, suberr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!is_dir(dir)){
|
||||
lm_error_set(LM_ERR_CtxTempNotDir);
|
||||
return false;
|
||||
if(root_dir != NULL)
|
||||
ctx->root = realpath(root_dir, NULL);
|
||||
|
||||
if(temp_dir != NULL && !__lm_ctx_init_checkdir(temp_dir)){
|
||||
suberr = lm_strerror_dup();
|
||||
pdebug(__func__, "check failed for specified temp directory: %s", lm_strerror());
|
||||
lm_error_set(LM_ERR_CtxTempFail, temp_dir, suberr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(!can_write(dir)){
|
||||
lm_error_set(LM_ERR_CtxTempNoWrite);
|
||||
return false;
|
||||
if(temp_dir != NULL)
|
||||
ctx->temp = realpath(temp_dir, NULL);
|
||||
|
||||
if(data_dir != NULL && !__lm_ctx_init_checkdir(data_dir)){
|
||||
suberr = lm_strerror_dup();
|
||||
pdebug(__func__, "check failed for specified data directory: %s", lm_strerror());
|
||||
lm_error_set(LM_ERR_CtxDataFail, data_dir, suberr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
char fullpath[PATH_MAX + 1];
|
||||
realpath(dir, fullpath);
|
||||
if(data_dir != NULL)
|
||||
ctx->data = realpath(data_dir, NULL);
|
||||
|
||||
ctx->temp = strdup(fullpath);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_ctx_set_root(lm_ctx_t *ctx, char *dir){
|
||||
if(!exists(dir)){
|
||||
lm_error_set(LM_ERR_CtxRootFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_dir(dir)){
|
||||
lm_error_set(LM_ERR_CtxRootNotDir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!can_write(dir)){
|
||||
lm_error_set(LM_ERR_CtxRootNoWrite);
|
||||
return false;
|
||||
}
|
||||
|
||||
char fullpath[PATH_MAX + 1];
|
||||
realpath(dir, fullpath);
|
||||
|
||||
ctx->root = strdup(fullpath);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
|
||||
if(!mkdir_ifnot(dir)){
|
||||
lm_error_set(LM_ERR_CtxDataFailMkdir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_dir(dir)){
|
||||
lm_error_set(LM_ERR_CtxDataNotDir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!can_write(dir)){
|
||||
lm_error_set(LM_ERR_CtxDataNoWrite);
|
||||
return false;
|
||||
}
|
||||
|
||||
char fullpath[PATH_MAX + 1];
|
||||
realpath(dir, fullpath);
|
||||
|
||||
ctx->data = strdup(fullpath);
|
||||
return true;
|
||||
ret = true;
|
||||
end:
|
||||
free(suberr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void lm_ctx_free(lm_ctx_t *ctx) {
|
||||
|
@ -28,8 +28,8 @@ lm_pkg_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name, char *version){
|
||||
return found;
|
||||
}
|
||||
|
||||
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) {
|
||||
if(NULL == name){
|
||||
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url, char *dir) {
|
||||
if(NULL == name || NULL == dir){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return NULL;
|
||||
}
|
||||
@ -38,26 +38,15 @@ lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) {
|
||||
if (NULL == pool)
|
||||
return NULL;
|
||||
|
||||
pdebug(__func__, "adding new pool: %s (%s://%s:%d%s)", pool->name, pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
|
||||
if(NULL == url)
|
||||
pdebug(__func__, "adding new pool: %s", pool->name);
|
||||
else
|
||||
pdebug(__func__, "adding new pool: %s (%s://%s:%d%s)", pool->name, pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
|
||||
|
||||
if(NULL != ctx->data){
|
||||
char pools_dir[strlen(ctx->data)+strlen(pool->name)+10];
|
||||
join(pools_dir, ctx->data, "pools");
|
||||
|
||||
if(!mkdir_ifnot(pools_dir)){
|
||||
lm_error_set(LM_ERR_CtxDataFailMkdir);
|
||||
lm_pool_free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bzero(pools_dir, sizeof(pools_dir));
|
||||
join_multiple(pools_dir, ctx->data, "pools", pool->name);
|
||||
|
||||
if(!lm_pool_path_set_dir(pool, pools_dir)){
|
||||
// error is set by set_dir
|
||||
lm_pool_free(pool);
|
||||
return NULL;
|
||||
}
|
||||
if(!lm_pool_path_set_dir(pool, dir)){
|
||||
// error is set by set_dir
|
||||
lm_pool_free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// add to the start
|
||||
|
@ -43,6 +43,7 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
|
||||
};
|
||||
lm_pool_t *cur = ctx->pools;
|
||||
size_t loaded_count = 0;
|
||||
char *tempdir = NULL;
|
||||
bool status = false;
|
||||
|
||||
while(NULL != cur){
|
||||
@ -98,12 +99,14 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
|
||||
goto next_list;
|
||||
}
|
||||
|
||||
if(!do_update && !lm_pool_list_load(cur)){
|
||||
tempdir = lm_ctx_temp_dir(ctx, "list_extracted");
|
||||
|
||||
if(!do_update && !lm_pool_list_load(cur, tempdir)){
|
||||
pdebug(__func__, "(%s) failed to load list: %s", cur->name, lm_strerror());
|
||||
goto next_list;
|
||||
}
|
||||
|
||||
else if(do_update && !lm_pool_list_download(cur, __lm_ctx_sync_callback, &cbdata)) {
|
||||
else if(do_update && !lm_pool_list_download(cur, tempdir, __lm_ctx_sync_callback, &cbdata)) {
|
||||
pdebug(__func__, "(%s) failed to update list: %s", cur->name, lm_strerror());
|
||||
goto next_list;
|
||||
}
|
||||
@ -112,6 +115,9 @@ size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callbac
|
||||
status = true;
|
||||
|
||||
next_list:
|
||||
free(tempdir);
|
||||
tempdir = NULL;
|
||||
|
||||
if(NULL != callback && status)
|
||||
callback(ctx, cur, SYNC_LIST_SUCCESS, 0, 0, data);
|
||||
else if(NULL != callback && !status)
|
||||
|
38
src/ctx/temp.c
Normal file
38
src/ctx/temp.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include "../../include/error.h"
|
||||
#include "../../include/util.h"
|
||||
#include "../../include/ctx.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
char *lm_ctx_temp_dir(lm_ctx_t *ctx, char *dir){
|
||||
if(NULL == dir){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(NULL == ctx->temp){
|
||||
lm_error_set(LM_ERR_CtxTempNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char td[strlen(ctx->temp)+strlen(dir)+10];
|
||||
join(td, ctx->temp, dir);
|
||||
|
||||
if(!mkdir_ifnot(td)){
|
||||
lm_error_set(LM_ERR_CtxTempFailMkdir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return strdup(td);
|
||||
}
|
||||
|
||||
void lm_ctx_temp_clear(lm_ctx_t *ctx) {
|
||||
if(NULL == ctx->temp){
|
||||
lm_error_set(LM_ERR_CtxTempNULL);
|
||||
return;
|
||||
}
|
||||
|
||||
rmrf(ctx->temp);
|
||||
}
|
||||
|
16
src/error.c
16
src/error.c
@ -67,15 +67,9 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.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 is not a directory") },
|
||||
{.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data directory does not have write access") },
|
||||
{.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") },
|
||||
{.code = LM_ERR_CtxTempFail, .desc = _("failed to set the ctx temp director to %s: %s") },
|
||||
{.code = LM_ERR_CtxRootFail, .desc = _("failed to set the ctx root directory to %s: %s") },
|
||||
{.code = LM_ERR_CtxDataFail, .desc = _("failed to set the ctx data directory to %s: %s") },
|
||||
{.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") },
|
||||
@ -161,6 +155,10 @@ void lm_error_set(lm_error_t code, ...) {
|
||||
{.code = LM_ERR_DbChangesNotExists, .desc = _("package changes file not found in the database") },
|
||||
{.code = LM_ERR_DbChangesChmodFail, .desc = _("failed to change mod of the changes file") },
|
||||
{.code = LM_ERR_InstallDirFail, .desc = _("failed to create install script save directory") },
|
||||
{.code = LM_ERR_NoWrite, .desc = _("directory does not have write permissions") },
|
||||
{.code = LM_ERR_NotDir, .desc = _("specified path is not a directory") },
|
||||
{.code = LM_ERR_FailMkdir, .desc = _("failed to create the specified directory") },
|
||||
{.code = LM_ERR_PoolListBadDir, .desc = _("specified list extraction directory is not accessible") },
|
||||
};
|
||||
|
||||
char *fmt = NULL;
|
||||
|
@ -9,8 +9,8 @@
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
|
||||
bool lm_pool_list_load(lm_pool_t *pool){
|
||||
if(NULL == pool){
|
||||
bool lm_pool_list_load(lm_pool_t *pool, char *dir){
|
||||
if(NULL == pool || NULL == dir){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
@ -27,27 +27,27 @@ bool lm_pool_list_load(lm_pool_t *pool){
|
||||
return false;
|
||||
}
|
||||
|
||||
pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->list_dir);
|
||||
pdebug(__func__, "(%s) extracting pool to %s", pool->name, dir);
|
||||
|
||||
if(!mkdir_ifnot(pool->dir)){
|
||||
lm_error_set(LM_ERR_PoolBadDir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!mkdir_ifnot(pool->list_dir)){
|
||||
lm_error_set(LM_ERR_PoolBadPaths);
|
||||
if(!mkdir_ifnot(dir)){
|
||||
lm_error_set(LM_ERR_PoolListBadDir);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t ent_len = 0, list_dir_len = strlen(pool->list_dir);
|
||||
size_t ent_len = 0, list_dir_len = strlen(dir);
|
||||
struct dirent *ent = NULL;
|
||||
DIR *dirfd = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(!extract_archive(pool->list_dir, pool->list_file))
|
||||
if(!extract_archive(dir, pool->list_file))
|
||||
goto end;
|
||||
|
||||
if((dirfd = opendir(pool->list_dir)) == NULL){
|
||||
if((dirfd = opendir(dir)) == NULL){
|
||||
lm_error_set(LM_ERR_PoolListDirFail);
|
||||
goto end;
|
||||
}
|
||||
@ -58,7 +58,7 @@ bool lm_pool_list_load(lm_pool_t *pool){
|
||||
|
||||
ent_len = strlen(ent->d_name);
|
||||
char datap[ent_len+list_dir_len+10];
|
||||
join_multiple(datap, pool->list_dir, ent->d_name, "DATA");
|
||||
join_multiple(datap, dir, ent->d_name, "DATA");
|
||||
|
||||
lm_pkg_t *pkg = lm_package_new();
|
||||
|
||||
@ -85,8 +85,8 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data) {
|
||||
if(NULL == pool){
|
||||
bool lm_pool_list_download(lm_pool_t *pool, char *dir, lm_mptp_transfer_callback_t callback, void *data) {
|
||||
if(NULL == pool || NULL == dir){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
@ -106,11 +106,6 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!mkdir_ifnot(pool->list_dir)){
|
||||
lm_error_set(LM_ERR_PoolBadPaths);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(NULL == pool->url.path || NULL == pool->url.host){
|
||||
lm_error_set(LM_ERR_PoolUrlEmpty);
|
||||
return false;
|
||||
@ -137,7 +132,7 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback
|
||||
end:
|
||||
lm_mptp_close(sock);
|
||||
if(ret)
|
||||
ret = lm_pool_list_load(pool);
|
||||
ret = lm_pool_list_load(pool, dir);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,10 @@
|
||||
|
||||
bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
|
||||
free(pool->dir);
|
||||
free(pool->list_dir);
|
||||
free(pool->info_file);
|
||||
free(pool->list_file);
|
||||
|
||||
pool->dir = NULL;
|
||||
pool->list_dir = NULL;
|
||||
pool->info_file = NULL;
|
||||
pool->list_file = NULL;
|
||||
|
||||
@ -27,7 +25,6 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
|
||||
pool->dir = strdup(dir);
|
||||
pool->info_file = join_alloc(dir, "INFO");
|
||||
pool->list_file = join_alloc(dir, "LIST");
|
||||
pool->list_dir = join_alloc(dir, "LIST_extracted");
|
||||
|
||||
if(exists(pool->info_file) && (!is_file(pool->info_file) || !can_read(pool->info_file) || !can_write(pool->info_file))){
|
||||
lm_error_set(LM_ERR_PoolBadPaths);
|
||||
@ -38,11 +35,6 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
|
||||
lm_error_set(LM_ERR_PoolBadPaths);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(exists(pool->list_dir) && (is_file(pool->list_dir) || !can_read(pool->list_dir) || !can_write(pool->list_dir))){
|
||||
lm_error_set(LM_ERR_PoolBadPaths);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -50,6 +42,5 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
|
||||
bool lm_pool_path_is_empty(lm_pool_t *pool){
|
||||
return NULL == pool->info_file ||
|
||||
NULL == pool->list_file ||
|
||||
NULL == pool->list_dir ||
|
||||
NULL == pool->dir;
|
||||
}
|
||||
|
@ -66,11 +66,12 @@ end:
|
||||
|
||||
void lm_pool_free(lm_pool_t *pool) {
|
||||
free(pool->dir);
|
||||
free(pool->list_dir);
|
||||
free(pool->info_file);
|
||||
free(pool->list_file);
|
||||
|
||||
lm_url_free(&pool->url);
|
||||
lm_pool_info_free(pool);
|
||||
lm_pool_list_free(pool);
|
||||
|
||||
free(pool);
|
||||
}
|
||||
|
57
src/util.c
57
src/util.c
@ -3,6 +3,7 @@
|
||||
|
||||
#include <archive.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/limits.h>
|
||||
@ -498,3 +499,59 @@ end:
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool rmrf(char *p) {
|
||||
DIR *fd = opendir(p);
|
||||
struct dirent *ent = NULL;
|
||||
size_t pl = strlen(p), dl = 0;
|
||||
bool ret = false;
|
||||
|
||||
if (NULL == fd)
|
||||
return false;
|
||||
|
||||
while ((ent = readdir(fd)) != NULL) {
|
||||
if (eq(ent->d_name, ".") || eq(ent->d_name, ".."))
|
||||
continue;
|
||||
|
||||
dl = strlen(ent->d_name);
|
||||
char fp[pl + dl + 10];
|
||||
join(fp, p, ent->d_name);
|
||||
|
||||
switch (ent->d_type) {
|
||||
case DT_DIR:
|
||||
if (!rmrf(fp))
|
||||
goto end;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (unlink(fp) < 0)
|
||||
goto end;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
closedir(fd);
|
||||
rmdir(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool is_dir_empty(char *p) {
|
||||
DIR *fd = opendir(p);
|
||||
struct dirent *ent = NULL;
|
||||
bool empty = true;
|
||||
|
||||
if (NULL == fd)
|
||||
return false;
|
||||
|
||||
while ((ent = readdir(fd)) != NULL) {
|
||||
if (eq(ent->d_name, ".") || eq(ent->d_name, ".."))
|
||||
continue;
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
|
||||
closedir(fd);
|
||||
return empty;
|
||||
}
|
||||
|
Reference in New Issue
Block a user