fix: list archive extraction and package creation errors

This commit is contained in:
ngn 2024-06-28 21:48:04 +03:00
parent 3b19e2840b
commit ed52710355
17 changed files with 192 additions and 140 deletions

View File

@ -1,8 +1,8 @@
CC = gcc
all: ../dist/example_pool ../dist/example_server
all: ../dist/example_client ../dist/example_server
../dist/example_pool: pool/*.c ../dist/libmp.so
../dist/example_client: client/*.c ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@

View File

@ -20,11 +20,16 @@ int main(int argc, char *argv[]) {
goto end;
}
if (!lm_pool_info_load(pool, "./examples/tests/pool/INFO")) {
if (!lm_pool_info_load(pool, "./examples/tests/INFO")) {
printf("failed to load pool info: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_pool_list_load(pool, "./examples/tests/LIST")) {
printf("failed to load pool list: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_pools_serve(&ctx, argv[1], 10)) {
printf("failed to serve the pools: %s (%d)\n", lm_strerror(), lm_error());
goto end;

1
examples/tests/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
LIST_extracted

View File

@ -1,4 +1,4 @@
[base]
size = 314073
size = 314121
maintainer = ngn
pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,61 +1,63 @@
#pragma once
typedef enum lm_error {
LM_ERR_NoError = 0,
LM_ERR_URLBadChar = 1,
LM_ERR_URLBadProtocol = 2,
LM_ERR_URLTooLarge = 3,
LM_ERR_URLHostLarge = 4,
LM_ERR_URLPathLarge = 5,
LM_ERR_URLBadHost = 6,
LM_ERR_URLBadPort = 7,
LM_ERR_URLBadPath = 8,
LM_ERR_URLPortUnknown = 9,
LM_ERR_BadPort = 10,
LM_ERR_BadHost = 11,
LM_ERR_PoolNoSupport = 12,
LM_ERR_URLEnd = 13,
LM_ERR_MPTPBadVersion = 14,
LM_ERR_MPTPBadCode = 15,
LM_ERR_MPTPBadUrl = 16,
LM_ERR_MPTPHostFail = 17,
LM_ERR_MPTPSocketFail = 18,
LM_ERR_MPTPConnectFail = 19,
LM_ERR_MPTPRecvFail = 20,
LM_ERR_MPTPSendFail = 21,
LM_ERR_MPTPBadData = 22,
LM_ERR_MPTPBadHost = 23,
LM_ERR_MPTPSetsockopt = 24,
LM_ERR_MPTPTimeout = 25,
LM_ERR_MPTPBindFail = 26,
LM_ERR_ArgNULL = 27,
LM_ERR_MPTPNotResponse = 28,
LM_ERR_MPTPNotRequest = 29,
LM_ERR_MPTPNotLast = 30,
LM_ERR_NoPort = 31,
LM_ERR_PoolInfoBad = 32,
LM_ERR_ArcWBlockFail = 33,
LM_ERR_ArcRBlockFail = 34,
LM_ERR_ArcOpenFail = 35,
LM_ERR_ArcWHeaderFail = 36,
LM_ERR_ArcWEntryFail = 37,
LM_ERR_GetCwdFail = 38,
LM_ERR_PoolListDirFail = 39,
LM_ERR_PoolListCantRead = 40,
LM_ERR_PoolInfoCantRead = 41,
LM_ERR_PkgBadName = 42,
LM_ERR_PkgDataBad = 43,
LM_ERR_CtxDataNULL = 44,
LM_ERR_CtxTempFail = 45,
LM_ERR_CtxTempNotDir = 46,
LM_ERR_CtxTempNoWrite = 47,
LM_ERR_CtxRootFail = 48,
LM_ERR_CtxRootNotDir = 49,
LM_ERR_CtxRootNoWrite = 50,
LM_ERR_CtxDataNotDir = 51,
LM_ERR_CtxDataNoWrite = 52,
LM_ERR_CtxDataFailMkdir = 53,
LM_ERR_NoError = 0,
LM_ERR_URLBadChar = 1,
LM_ERR_URLBadProtocol = 2,
LM_ERR_URLTooLarge = 3,
LM_ERR_URLHostLarge = 4,
LM_ERR_URLPathLarge = 5,
LM_ERR_URLBadHost = 6,
LM_ERR_URLBadPort = 7,
LM_ERR_URLBadPath = 8,
LM_ERR_URLPortUnknown = 9,
LM_ERR_BadPort = 10,
LM_ERR_BadHost = 11,
LM_ERR_PoolNoSupport = 12,
LM_ERR_URLEnd = 13,
LM_ERR_MPTPBadVersion = 14,
LM_ERR_MPTPBadCode = 15,
LM_ERR_MPTPBadUrl = 16,
LM_ERR_MPTPHostFail = 17,
LM_ERR_MPTPSocketFail = 18,
LM_ERR_MPTPConnectFail = 19,
LM_ERR_MPTPRecvFail = 20,
LM_ERR_MPTPSendFail = 21,
LM_ERR_MPTPBadData = 22,
LM_ERR_MPTPBadHost = 23,
LM_ERR_MPTPSetsockopt = 24,
LM_ERR_MPTPTimeout = 25,
LM_ERR_MPTPBindFail = 26,
LM_ERR_ArgNULL = 27,
LM_ERR_MPTPNotResponse = 28,
LM_ERR_MPTPNotRequest = 29,
LM_ERR_MPTPNotLast = 30,
LM_ERR_NoPort = 31,
LM_ERR_PoolInfoBad = 32,
LM_ERR_ArcWBlockFail = 33,
LM_ERR_ArcRBlockFail = 34,
LM_ERR_ArcOpenFail = 35,
LM_ERR_ArcWHeaderFail = 36,
LM_ERR_ArcWEntryFail = 37,
LM_ERR_ArcNewFail = 38,
LM_ERR_ArcNextHeaderFail = 39,
LM_ERR_GetCwdFail = 39,
LM_ERR_PoolListDirFail = 40,
LM_ERR_PoolListCantRead = 41,
LM_ERR_PoolInfoCantRead = 42,
LM_ERR_PkgBadName = 43,
LM_ERR_PkgDataBad = 44,
LM_ERR_CtxDataNULL = 45,
LM_ERR_CtxTempFail = 46,
LM_ERR_CtxTempNotDir = 47,
LM_ERR_CtxTempNoWrite = 48,
LM_ERR_CtxRootFail = 49,
LM_ERR_CtxRootNotDir = 50,
LM_ERR_CtxRootNoWrite = 51,
LM_ERR_CtxDataNotDir = 52,
LM_ERR_CtxDataNoWrite = 53,
LM_ERR_CtxDataFailMkdir = 54,
LM_ERR_ArcRealpathFail = 55,
} lm_error_t;
typedef struct lm_error_desc {

View File

@ -4,12 +4,12 @@
#include <stddef.h>
typedef struct lm_pkg {
size_t size;
struct lm_pkg *next;
char *name;
char *desc;
char **depends;
char *version;
size_t size;
} lm_pkg_t;
typedef struct lm_pool_info {

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-28 20:24+0300\n"
"POT-Creation-Date: 2024-06-28 21:45+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"
@ -176,66 +176,78 @@ msgid "failed to finish writing the archive entry"
msgstr ""
#: src/error.c:51
msgid "failed to obtain current working directory"
msgid "failed to create new archive reader/writer"
msgstr ""
#: src/error.c:52
msgid "failed to open extracted pool list directory"
msgid "failed to resolve full path for archive file"
msgstr ""
#: src/error.c:53
msgid "failed to read access the pool list file"
msgid "failed to read the next header of the archive"
msgstr ""
#: src/error.c:54
msgid "failed to read access the pool info file"
msgid "failed to obtain current working directory"
msgstr ""
#: src/error.c:55
msgid "failed to parse package data"
msgid "failed to open extracted pool list directory"
msgstr ""
#: src/error.c:56
msgid "failed to read access the pool list file"
msgstr ""
#: src/error.c:57
msgid "failed to read access the pool info file"
msgstr ""
#: src/error.c:58
msgid "failed to parse package data"
msgstr ""
#: src/error.c:59
#, fuzzy
msgid "package name is invalid"
msgstr "URL hostname is too large"
#: src/error.c:57
#: src/error.c:60
msgid "data path is not set with in the ctx"
msgstr ""
#: src/error.c:58
#: src/error.c:61
msgid "specified temp path does not exist"
msgstr ""
#: src/error.c:59
#: src/error.c:62
msgid "specified temp path is not a directory"
msgstr ""
#: src/error.c:60
#: src/error.c:63
msgid "specified temp directory does not have write access"
msgstr ""
#: src/error.c:61
#: src/error.c:64
msgid "specified root path does not exist"
msgstr ""
#: src/error.c:62
#: src/error.c:65
msgid "specified root path is not a directory"
msgstr ""
#: src/error.c:63
#: src/error.c:66
msgid "specified root directory does not have write access"
msgstr ""
#: src/error.c:64
#: src/error.c:67
msgid "specified data path does not exist"
msgstr ""
#: src/error.c:65
#: src/error.c:68
msgid "specified data path is not a directory"
msgstr ""
#: src/error.c:66
#: src/error.c:69
msgid "failed to create specified data directory"
msgstr ""

View File

@ -10,60 +10,63 @@ void lm_error_set(lm_error_t code) {
char *lm_strerror() {
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 = _("failed to parse pool info") },
{.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_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_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 = _("failed to parse pool info") },
{.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") },
};
for (int i = 0; i < sizeof(errors) / sizeof(lm_error_desc_t); i++) {

View File

@ -25,7 +25,7 @@ bool lm_pkg_data_depend_add(lm_pkg_t *pkg, char *depend){
return false;
}
if(is_pkg_name_valid(depend)){
if(!is_pkg_name_valid(depend)){
lm_error_set(LM_ERR_PkgBadName);
return false;
}
@ -89,6 +89,9 @@ void lm_pkg_data_free(lm_pkg_t *pkg){
free(pkg->desc);
free(pkg->name);
free(pkg->version);
for(int i = 0; pkg->depends[i] != NULL; i++)
free(pkg->depends[i]);
if(NULL != pkg->depends){
for(int i = 0; pkg->depends[i] != NULL; i++)
free(pkg->depends[i]);
}
}

View File

@ -29,11 +29,13 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
memcpy(file_copy2, file, file_len+1);
list_dir = dirname(file_copy);
list_name = dirname(file_copy2);
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;
@ -53,24 +55,32 @@ bool lm_pool_list_load(lm_pool_t *pool, char *file){
}
while((ent = readdir(dirfd)) != NULL){
ent_len = strlen(ent->d_name);
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);

View File

@ -131,6 +131,12 @@ bool extract_archive(char *dst, char *src) {
int flags = 0, rc = 0;
char *oldpwd = NULL;
bool ret = false;
char srcfull[PATH_MAX + 1];
if (NULL == realpath(src, srcfull)) {
lm_error_set(LM_ERR_ArcRealpathFail);
goto end;
}
oldpwd = getcwd(NULL, 0);
if (NULL == oldpwd) {
@ -144,7 +150,12 @@ bool extract_archive(char *dst, char *src) {
flags |= ARCHIVE_EXTRACT_UNLINK;
reader = archive_read_new();
writer = archive_write_new();
writer = archive_write_disk_new();
if (NULL == reader || NULL == writer) {
lm_error_set(LM_ERR_ArcNewFail);
goto end;
}
archive_write_disk_set_options(writer, flags);
archive_write_disk_set_standard_lookup(writer);
@ -152,7 +163,7 @@ bool extract_archive(char *dst, char *src) {
archive_read_support_format_tar(reader);
archive_read_support_filter_gzip(reader);
if (archive_read_open_filename(reader, src, 1024 * 10) != ARCHIVE_OK) {
if (archive_read_open_filename(reader, srcfull, 1024 * 10) != ARCHIVE_OK) {
lm_error_set(LM_ERR_ArcOpenFail);
goto end;
}
@ -174,6 +185,11 @@ bool extract_archive(char *dst, char *src) {
}
}
if (rc != ARCHIVE_EOF) {
lm_error_set(LM_ERR_ArcNextHeaderFail);
goto end;
}
ret = true;
end:
@ -183,8 +199,8 @@ end:
}
if (NULL != writer) {
archive_write_close(reader);
archive_write_free(reader);
archive_write_close(writer);
archive_write_free(writer);
}
if (NULL != oldpwd) {