From 93eb7bb8b4a572ac48a543f0f46ec566d42ce370 Mon Sep 17 00:00:00 2001 From: ngn Date: Sat, 6 Jul 2024 04:15:38 +0300 Subject: [PATCH] fix: install getline issues --- locale/tr/LC_MESSAGES/libmp.po | 2 +- src/ctx/install.c | 30 +++++++++++++++++++++++++----- src/database/database.c | 12 +++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 373e486..0d0b1d7 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-06 03:45+0300\n" +"POT-Creation-Date: 2024-07-06 04:13+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/install.c b/src/ctx/install.c index 2406a51..a72f987 100644 --- a/src/ctx/install.c +++ b/src/ctx/install.c @@ -55,40 +55,60 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call return false; // error set by function if(!lm_package_is_same(&temp, pkg)){ + pdebug(__func__, "DATA file does not match with stored %s data", pkg->name); lm_error_set(LM_ERR_PkgDataNotMatch); return false; } if(!lm_database_changes_update(ctx->db, pkg, changes_file)){ char *suberr = lm_strerror_dup(); + pdebug(__func__, "failed to update changes file for %s: %s", pkg->name, suberr); lm_error_set(LM_ERR_PkgChangesUpdateFail, suberr); return false; } char *line = NULL, *hash = NULL, *file = NULL; - size_t line_len = 0; + ssize_t line_len = 0; FILE *hashes = NULL; bool ret = false; if((hashes = fopen(hashes_file, "r")) == NULL){ + pdebug(__func__, "failed to open hash file for %s", pkg->name); lm_error_set(LM_ERR_PkgHashesOpenFail); return false; } - while((line_len = getline(&line, 0, hashes)) > 0){ + while((line_len = getline(&line, (size_t*)&line_len, hashes)) > 0){ + if(NULL == line) + goto next; + if(HASH_LEN+1 >= line_len) - continue; + goto next; line[HASH_LEN] = 0; hash = line; - file = line+HASH_LEN+1; + + file = line+HASH_LEN+2; + file[strlen(file)-1] = 0; + + if(file[0] == '.') + file++; + + pdebug(__func__, "(%lu) %s => %s", line_len, file, hash); if(!lm_database_files_add(ctx->db, pkg, file, hash)) - return false; // error set by function + goto end; + + next: + free(line); + line = NULL; + line_len = 0; } ret = true; end: + free(line); + if(NULL != hashes) fclose(hashes); diff --git a/src/database/database.c b/src/database/database.c index 12599ee..bc164fb 100644 --- a/src/database/database.c +++ b/src/database/database.c @@ -34,7 +34,7 @@ char *queries[] = { " path TEXT PRIMARY KEY NOT NULL," \ " hash TEXT NOT NULL," \ " keep INT NOT NULL," \ - " package TEXT NOT NULL);" + " package TEXT NOT NULL);", // QUERY_INSERT_FILE_SINGLE "INSERT INTO files VALUES (?, ?, ?, ?)", @@ -77,27 +77,29 @@ lm_database_t *lm_database_new(char *path){ join(filesdb, path, "files.db"); if(sqlite3_open(packagesdb, &db->packages_db)){ - pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->packages_db)); + pdebug(__func__, "(%s) failed to open databse: %s", packagesdb, sqlite3_errmsg(db->packages_db)); lm_error_set(LM_ERR_DbSqlOpenFail); return NULL; } if(sqlite3_open(filesdb, &db->files_db)){ - pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->files_db)); + pdebug(__func__, "(%s) failed to open databse: %s", filesdb, sqlite3_errmsg(db->files_db)); lm_error_set(LM_ERR_DbSqlOpenFail); return NULL; } if(sqlite3_exec(db->packages_db, queries[QUERY_CREATE_PACKAGE_TABLE], NULL, 0, &err) != SQLITE_OK){ - pdebug(__func__, "(%s) failed to create packages table: %s", path, err); + pdebug(__func__, "(%s) failed to create packages table: %s", packagesdb, err); lm_error_set(LM_ERR_DbSqlCreateFail); sqlite3_free(err); + db->packages_db = NULL; } if(sqlite3_exec(db->files_db, queries[QUERY_CREATE_FILE_TABLE], NULL, 0, &err) != SQLITE_OK){ - pdebug(__func__, "(%s) failed to create files table: %s", path, err); + pdebug(__func__, "(%s) failed to create files table: %s", filesdb, err); lm_error_set(LM_ERR_DbSqlCreateFail); sqlite3_free(err); + db->files_db = NULL; } db->dir = strdup(path);