#include "../../include/package.h" #include "../../include/error.h" #include "../../include/pool.h" #include "../../include/util.h" #include "../../include/ctx.h" #include #include 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; } if(NULL == ctx->root){ lm_error_set(LM_ERR_CtxRootNULL); return false; } 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((hashes = fopen(hashes_file, "r")) != NULL){ lm_error_set(LM_ERR_PkgHashesOpenFail); return false; } 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: if(NULL != hashes) fclose(hashes); return ret; }