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

@ -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) {