121 lines
2.7 KiB
C
121 lines
2.7 KiB
C
#include "../../include/error.h"
|
|
#include "../../include/pool.h"
|
|
#include "../../include/mptp.h"
|
|
#include "../../include/util.h"
|
|
#include "../../include/pkg.h"
|
|
|
|
#include <dirent.h>
|
|
#include <libgen.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
bool lm_pool_list_load(lm_pool_t *pool, char *file){
|
|
if(NULL == pool || NULL == file){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
if(!can_read(file)){
|
|
lm_error_set(LM_ERR_PoolListCantRead);
|
|
return false;
|
|
}
|
|
|
|
size_t file_len = strlen(file);
|
|
char file_copy[file_len + 1];
|
|
char file_copy2[file_len + 1];
|
|
char *list_dir = NULL, *list_name = NULL;
|
|
|
|
memcpy(file_copy, file, file_len+1);
|
|
memcpy(file_copy2, file, file_len+1);
|
|
|
|
list_dir = dirname(file_copy);
|
|
list_name = basename(file_copy2);
|
|
|
|
char extract_dir[file_len+15];
|
|
snprintf(extract_dir, sizeof(extract_dir), "%s/%s_extracted", list_dir, list_name);
|
|
|
|
pdebug(__func__, "extracting pool to %s", extract_dir);
|
|
|
|
if(!mkdir_ifnot(extract_dir)){
|
|
lm_error_set(LM_ERR_PoolListDirFail);
|
|
return false;
|
|
}
|
|
|
|
struct dirent *ent = NULL;
|
|
size_t ent_len = 0;
|
|
DIR *dirfd = NULL;
|
|
bool ret = false;
|
|
|
|
if(!extract_archive(extract_dir, file))
|
|
goto end;
|
|
|
|
if((dirfd = opendir(extract_dir)) == NULL){
|
|
lm_error_set(LM_ERR_PoolListDirFail);
|
|
goto end;
|
|
}
|
|
|
|
while((ent = readdir(dirfd)) != NULL){
|
|
if(eq(ent->d_name, "..") || eq(ent->d_name, "."))
|
|
continue;
|
|
|
|
ent_len = strlen(ent->d_name);
|
|
char datap[ent_len+sizeof(extract_dir)+10];
|
|
snprintf(datap, sizeof(datap), "%s/%s/DATA", extract_dir, ent->d_name);
|
|
|
|
lm_pkg_t *pkg = lm_pkg_new();
|
|
|
|
if(!lm_pkg_data_load(pkg, datap)){
|
|
pdebug(__func__, "(%s) failed to load new package from %s", pool->name, datap);
|
|
lm_pkg_free(pkg);
|
|
continue;
|
|
}
|
|
|
|
if(!lm_pool_add(pool, pkg)){
|
|
pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->name);
|
|
lm_pkg_free(pkg);
|
|
continue;
|
|
}
|
|
|
|
pdebug(__func__, "(%s) added new package: %s", pool->name, pkg->name);
|
|
}
|
|
|
|
ret = true;
|
|
|
|
end:
|
|
if(NULL != dirfd)
|
|
closedir(dirfd);
|
|
return ret;
|
|
}
|
|
|
|
bool lm_pool_list_get(lm_pool_t *pool, char *file) {
|
|
if(!pool->available)
|
|
return false;
|
|
|
|
if(NULL == pool->url.host)
|
|
return false;
|
|
|
|
if(NULL == pool->url.path)
|
|
return false;
|
|
|
|
int sock = lm_mptp_client_connect(pool->url.host, pool->url.port);
|
|
lm_mptp_t packet;
|
|
bool ret = false;
|
|
|
|
lm_mptp_init(&packet, true, MPTP_C2S_LIST, true);
|
|
lm_mptp_set_host(&packet, pool->url.host);
|
|
lm_mptp_set_data(&packet, pool->url.path, strlen(pool->url.path));
|
|
|
|
if(!lm_mptp_client_send(sock, &packet))
|
|
goto end;
|
|
|
|
if(!lm_mptp_recvfile(sock, file))
|
|
goto end;
|
|
|
|
ret = true;
|
|
end:
|
|
lm_mptp_close(sock);
|
|
if(ret)
|
|
ret = lm_pool_list_load(pool, file);
|
|
return ret;
|
|
}
|