From b8d2d69752ca0d0687b4cd1ecb63fb4e4bf54898 Mon Sep 17 00:00:00 2001 From: ngn Date: Fri, 19 Jul 2024 19:23:48 +0300 Subject: [PATCH] update: make the package changes file read-only --- include/error.h | 2 ++ include/package.h | 2 +- locale/tr/LC_MESSAGES/libmp.po | 10 +++++++++- src/ctx/database.c | 4 ++-- src/database/changes.c | 11 ++++++++++- src/database/package.c | 8 ++++---- src/error.c | 2 ++ src/package/depend.c | 16 +++++++++++----- 8 files changed, 41 insertions(+), 14 deletions(-) diff --git a/include/error.h b/include/error.h index 3f486c3..3be5c57 100644 --- a/include/error.h +++ b/include/error.h @@ -140,6 +140,8 @@ typedef enum lm_error { LM_ERR_FileHashNoMatch = 132, LM_ERR_InfoNotLoaded = 133, LM_ERR_NoPools = 134, + LM_ERR_DbChangesNotExists = 135, + LM_ERR_DbChangesChmodFail = 136, } lm_error_t; typedef struct lm_error_desc { diff --git a/include/package.h b/include/package.h index 3bac9f1..0ddc1b1 100644 --- a/include/package.h +++ b/include/package.h @@ -21,7 +21,7 @@ bool lm_package_downloaded(lm_pkg_t *pkg); bool lm_package_depend_add(lm_pkg_t *pkg, char *depend); size_t lm_package_depend_count(lm_pkg_t *pkg); size_t lm_package_depend_strlen(lm_pkg_t *pkg); -bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer); +char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer); bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer); void lm_package_depend_free(lm_pkg_t *pkg); bool lm_package_depends(lm_pkg_t *pkg, char *dep); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index a6af3d6..0dbe71f 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-18 23:48+0300\n" +"POT-Creation-Date: 2024-07-19 19:22+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -591,3 +591,11 @@ msgstr "URL hostname is too large" #: src/error.c:159 msgid "pool list is empty" msgstr "" + +#: src/error.c:160 +msgid "package changes file not found in the database" +msgstr "" + +#: src/error.c:161 +msgid "failed to change mod of the changes file" +msgstr "" diff --git a/src/ctx/database.c b/src/ctx/database.c index a2f63a8..dcf78dd 100644 --- a/src/ctx/database.c +++ b/src/ctx/database.c @@ -72,11 +72,11 @@ bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_pkg_t *pkg){ char *lm_ctx_database_changes(lm_ctx_t *ctx, lm_pkg_t *pkg){ if(NULL == ctx || NULL == pkg){ lm_error_set(LM_ERR_ArgNULL); - return false; + return NULL; } if(!lm_ctx_database_init(ctx)) - return false; // error set by function + return NULL; // error set by function return lm_database_changes_get(ctx->db, pkg); } diff --git a/src/database/changes.c b/src/database/changes.c index 65138ce..286e0f6 100644 --- a/src/database/changes.c +++ b/src/database/changes.c @@ -20,7 +20,15 @@ bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){ char changes_path[strlen(db->dir)+sizeof(changes_file)]; join(changes_path, db->dir, changes_file); - return copy_file(changes_path, file); + if(!copy_file(changes_path, file)) + return false; + + if(chmod(changes_path, 0444) < 0){ + lm_error_set(LM_ERR_DbChangesChmodFail); + return false; + } + + return true; } bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){ @@ -51,6 +59,7 @@ char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg){ join(changes_path, db->dir, changes_file); if(!exists(changes_path)){ + lm_error_set(LM_ERR_DbChangesNotExists); free(changes_path); return NULL; } diff --git a/src/database/package.c b/src/database/package.c index 5e98f67..1a6bf87 100644 --- a/src/database/package.c +++ b/src/database/package.c @@ -101,7 +101,7 @@ bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg, char *name, char // error is set by the function goto end; } - + ret = true; end: if(NULL != db->packages_st){ @@ -151,13 +151,13 @@ bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){ if(NULL != db->packages_st) lm_package_free(pkg); - else if(NULL == db->packages_st && + else if(NULL == db->packages_st && sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_ALL], strlen(queries[QUERY_SELECT_PACKAGE_ALL]), &db->packages_st, NULL) != SQLITE_OK){ pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->packages_db)); lm_error_set(LM_ERR_DbSqlPrepareFail); return false; } - + lm_package_init(pkg); if(sqlite3_step(db->packages_st) != SQLITE_ROW){ @@ -191,7 +191,7 @@ void lm_database_package_next_free(lm_database_t *db, lm_pkg_t *pkg){ sqlite3_finalize(db->packages_st); db->packages_st = NULL; } - + if(NULL != pkg) lm_package_free(pkg); } diff --git a/src/error.c b/src/error.c index 34932e1..fabc990 100644 --- a/src/error.c +++ b/src/error.c @@ -157,6 +157,8 @@ void lm_error_set(lm_error_t code, ...) { {.code = LM_ERR_FileHashNoMatch, .desc = _("file hash does not match for %s") }, {.code = LM_ERR_InfoNotLoaded, .desc = _("pool info is not loaded") }, {.code = LM_ERR_NoPools, .desc = _("pool list is empty") }, + {.code = LM_ERR_DbChangesNotExists, .desc = _("package changes file not found in the database") }, + {.code = LM_ERR_DbChangesChmodFail, .desc = _("failed to change mod of the changes file") }, }; char *fmt = NULL; diff --git a/src/package/depend.c b/src/package/depend.c index d7ef925..aeec8dc 100644 --- a/src/package/depend.c +++ b/src/package/depend.c @@ -69,15 +69,21 @@ size_t lm_package_depend_strlen(lm_pkg_t *pkg){ return len; } -bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ - if(NULL == buffer || NULL == pkg){ +char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ + if(NULL == pkg){ lm_error_set(LM_ERR_ArgNULL); - return false; + return NULL; + } + + if(NULL == buffer){ + size_t buflen = lm_package_depend_strlen(pkg)+1; + buffer = malloc(buflen); + bzero(buffer, buflen); } if(NULL == pkg->depends){ buffer[0] = 0; - return true; + return buffer; } size_t bufsz = 0, depsz = 0; @@ -97,7 +103,7 @@ bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ } buffer[bufsz] = 0; - return true; + return buffer; } bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){