From: Michael Tremer Date: Fri, 19 Aug 2022 13:58:57 +0000 (+0000) Subject: pakfire: check: Extend to verify all files X-Git-Tag: 0.9.28~422 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd261c0108c7263059c5c3f702c30f52b6dcafe1;p=pakfire.git pakfire: check: Extend to verify all files Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 49230294f..c078572e8 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -1172,13 +1172,46 @@ static PyObject* Pakfire_refresh(PakfireObject* self, PyObject* args) { } static PyObject* Pakfire_check(PakfireObject* self) { - int r = pakfire_check(self->pakfire); - if (r) { - PyErr_SetFromErrno(PyExc_OSError); + struct pakfire_filelist* errors = NULL; + int r; + + // Allocate a filelist for errors + r = pakfire_filelist_create(&errors, self->pakfire); + if (r) + goto ERROR; + + // Perform check + r = pakfire_check(self->pakfire, errors); + if (r) + goto ERROR; + + const size_t num_errors = pakfire_filelist_size(errors); + + // Did we find any errors? + if (num_errors) { + PyObject* _errors = PyList_FromFileList(errors); + if (!_errors) + goto ERROR; + + pakfire_filelist_unref(errors); + + // Raise CheckFileVerificationError + PyErr_SetObject(PyExc_CheckFileVerificationError, _errors); + Py_DECREF(_errors); return NULL; } + pakfire_filelist_unref(errors); Py_RETURN_NONE; + +ERROR: + // Cleanup + if (errors) + pakfire_filelist_unref(errors); + + // Raise exception from errno + PyErr_SetFromErrno(PyExc_OSError); + return NULL; } static PyObject* Pakfire_sync(PakfireObject* self, PyObject* args, PyObject* kwargs) { diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index 59c0a26ca..d0f3dcfdd 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -28,9 +28,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -517,3 +519,74 @@ int pakfire_filelist_export(struct pakfire_filelist* list, FILE* f) { return 0; } + +/* + Verifies all files on the filelist +*/ +int pakfire_filelist_verify(struct pakfire_filelist* list, struct pakfire_filelist* errors) { + struct pakfire_progressbar* progressbar = NULL; + struct pakfire_file* file = NULL; + int r; + + DEBUG(list->pakfire, "Verifying filelist (%zu file(s))...\n", list->size); + + // Setup progressbar + r = pakfire_progressbar_create(&progressbar, NULL); + if (r) + goto ERROR; + + // Add status + r = pakfire_progressbar_add_string(progressbar, _("Verifying Files...")); + if (r) + goto ERROR; + + // Add bar + r = pakfire_progressbar_add_bar(progressbar); + if (r) + goto ERROR; + + // Add percentage + r = pakfire_progressbar_add_percentage(progressbar); + if (r) + goto ERROR; + + // Add ETA + r = pakfire_progressbar_add_eta(progressbar); + if (r) + goto ERROR; + + // Start the progressbar + r = pakfire_progressbar_start(progressbar, list->size); + if (r) + goto ERROR; + + // Iterate over the entire list + for (unsigned int i = 0; i < list->size; i++) { + file = list->elements[i]; + + // Verify the file + r = pakfire_file_verify(file); + if (r) { + // Append the file to the error list + r = pakfire_filelist_append(errors, file); + if (r) + goto ERROR; + } + + // Increment progressbar + r = pakfire_progressbar_increment(progressbar, 1); + if (r) + goto ERROR; + } + + // Finish + r = pakfire_progressbar_finish(progressbar); + if (r) + goto ERROR; + +ERROR: + if (progressbar) + pakfire_progressbar_unref(progressbar); + + return r; +} diff --git a/src/libpakfire/include/pakfire/filelist.h b/src/libpakfire/include/pakfire/filelist.h index 335bb5951..08f40c394 100644 --- a/src/libpakfire/include/pakfire/filelist.h +++ b/src/libpakfire/include/pakfire/filelist.h @@ -52,6 +52,8 @@ int pakfire_filelist_scan(struct pakfire_filelist* list, const char* root, int pakfire_filelist_contains(struct pakfire_filelist* list, const char* pattern); int pakfire_filelist_export(struct pakfire_filelist* list, FILE* f); +int pakfire_filelist_verify(struct pakfire_filelist* list, struct pakfire_filelist* errors); + #endif #endif /* PAKFIRE_FILELIST_H */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 4b1d06c51..b08782871 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -34,6 +34,7 @@ enum pakfire_digests { PAKFIRE_DIGEST_SHA512 = 2, }; +#include #include #include #include @@ -113,7 +114,7 @@ int pakfire_update(struct pakfire* pakfire, int transaction_flags, int solver_fl // Check -int pakfire_check(struct pakfire* pakfire); +int pakfire_check(struct pakfire* pakfire, struct pakfire_filelist* errors); // Sync diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 936197bae..4dffb035b 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -1895,7 +1895,27 @@ static int pakfire_verify(struct pakfire* pakfire, int *changed) { 0, changed, NULL, NULL); } -PAKFIRE_EXPORT int pakfire_check(struct pakfire* pakfire) { +static int pakfire_check_files(struct pakfire* pakfire, + struct pakfire_db* db, struct pakfire_filelist* errors) { + struct pakfire_filelist* filelist = NULL; + int r; + + // Fetch the filelist + r = pakfire_db_filelist(db, &filelist); + if (r) + goto ERROR; + + // Verify the filelist + r = pakfire_filelist_verify(filelist, errors); + +ERROR: + if (filelist) + pakfire_filelist_unref(filelist); + + return r; +} + +PAKFIRE_EXPORT int pakfire_check(struct pakfire* pakfire, struct pakfire_filelist* errors) { struct pakfire_db* db = NULL; int r; @@ -1914,6 +1934,11 @@ PAKFIRE_EXPORT int pakfire_check(struct pakfire* pakfire) { if (r) goto ERROR; + // Check files + r = pakfire_check_files(pakfire, db, errors); + if (r) + goto ERROR; + ERROR: if (db) pakfire_db_unref(db);