From: Michael Tremer Date: Thu, 23 Sep 2021 10:15:36 +0000 (+0000) Subject: file: Store digests in the same way than packages X-Git-Tag: 0.9.28~933 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e8dfbeb6c3c7fe192698267acc3000171ee1921;p=pakfire.git file: Store digests in the same way than packages Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 1a489963c..70cf0b76f 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -297,7 +297,7 @@ static int pakfire_db_create_schema(struct pakfire_db* db) { "mode INTEGER, " "user TEXT, " "'group' TEXT, " - "hash1 TEXT, " + "digest TEXT, " "ctime INTEGER, " "mtime INTEGER, " "capabilities TEXT, " @@ -671,6 +671,7 @@ static const struct pakfire_digest { } pakfire_digests[] = { { PAKFIRE_DIGEST_SHA512, "sha512:" }, { PAKFIRE_DIGEST_SHA256, "sha256:" }, + { PAKFIRE_DIGEST_SHA1, "sha1:" }, { PAKFIRE_DIGEST_NONE, NULL }, }; @@ -797,6 +798,7 @@ END: static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct pakfire_archive* archive) { sqlite3_stmt* stmt = NULL; + char* digest = NULL; int r = 1; // Get the filelist from the archive @@ -813,7 +815,7 @@ static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct } const char* sql = "INSERT INTO files(pkg, path, size, config, datafile, mode, " - "user, 'group', hash1, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + "user, 'group', digest, ctime, mtime, capabilities) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // Prepare the statement r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL); @@ -900,14 +902,31 @@ static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct goto END; } - // Bind hash1 - const char* chksum = pakfire_file_get_chksum(file); + enum pakfire_digests digest_type = PAKFIRE_DIGEST_NONE; - r = sqlite3_bind_text(stmt, 9, chksum, -1, NULL); - if (r) { - ERROR(db->pakfire, "Could not bind hash1: %s\n", sqlite3_errmsg(db->handle)); - pakfire_file_unref(file); - goto END; + // Bind digest + const char* hexdigest = pakfire_file_get_hexdigest(file, &digest_type); + if (hexdigest) { + digest = pakfire_db_pack_digest(digest_type, hexdigest); + if (!digest) { + ERROR(db->pakfire, "Could not pack digest: %m\n"); + pakfire_file_unref(file); + goto END; + } + + r = sqlite3_bind_text(stmt, 9, digest, -1, NULL); + if (r) { + ERROR(db->pakfire, "Could not bind digest: %s\n", sqlite3_errmsg(db->handle)); + pakfire_file_unref(file); + goto END; + } + } else { + r = sqlite3_bind_null(stmt, 9); + if (r) { + ERROR(db->pakfire, "Could not bind digest: %s\n", sqlite3_errmsg(db->handle)); + pakfire_file_unref(file); + goto END; + } } // Bind ctime @@ -955,6 +974,12 @@ static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct // Reset bound values sqlite3_reset(stmt); + + // Free digest + if (digest) { + free(digest); + digest = NULL; + } } // All okay @@ -963,6 +988,8 @@ static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, struct END: if (stmt) sqlite3_finalize(stmt); + if (digest) + free(digest); pakfire_filelist_unref(filelist); diff --git a/src/libpakfire/downloader.c b/src/libpakfire/downloader.c index 6f98c52c2..71972ae5e 100644 --- a/src/libpakfire/downloader.c +++ b/src/libpakfire/downloader.c @@ -359,6 +359,10 @@ static struct pakfire_transfer* pakfire_downloader_create_transfer( transfer->md = EVP_sha256(); break; + case PAKFIRE_DIGEST_SHA1: + transfer->md = EVP_sha1(); + break; + case PAKFIRE_DIGEST_NONE: break; } diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 76430db33..6cdc22595 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -51,7 +52,10 @@ struct pakfire_file { time_t ctime; time_t mtime; - char* chksum; + // Digest + enum pakfire_digests digest_type; + unsigned char digest[EVP_MAX_MD_SIZE]; + char* hexdigest; #warning TODO capabilities, config, data // capabilities @@ -118,6 +122,8 @@ struct archive_entry* pakfire_file_archive_entry(struct pakfire_file* file) { archive_entry_set_ctime(entry, pakfire_file_get_ctime(file), 0); archive_entry_set_mtime(entry, pakfire_file_get_mtime(file), 0); + // XXX copy digest + return entry; } @@ -144,15 +150,16 @@ int pakfire_file_copy_archive_entry(struct pakfire_file* file, struct archive_en pakfire_file_set_ctime(file, archive_entry_ctime(entry)); pakfire_file_set_mtime(file, archive_entry_mtime(entry)); + // XXX copy digest + return 0; } static void pakfire_file_free(struct pakfire_file* file) { - pakfire_unref(file->pakfire); - - if (file->chksum) - free(file->chksum); + if (file->hexdigest) + free(file->hexdigest); + pakfire_unref(file->pakfire); free(file); } @@ -245,18 +252,71 @@ PAKFIRE_EXPORT void pakfire_file_set_mtime(struct pakfire_file* file, time_t tim file->mtime = time; } -PAKFIRE_EXPORT const char* pakfire_file_get_chksum(struct pakfire_file* file) { - return file->chksum; +PAKFIRE_EXPORT const unsigned char* pakfire_file_get_digest( + struct pakfire_file* file, enum pakfire_digests* type) { + *type = file->digest_type; + + if (file->digest_type) + return file->digest; + + return NULL; +} + +PAKFIRE_EXPORT const char* pakfire_file_get_hexdigest( + struct pakfire_file* file, enum pakfire_digests* type) { + *type = file->digest_type; + + if (!file->hexdigest) { + const size_t digest_length = pakfire_digest_length(file->digest_type); + if (!digest_length) + return NULL; + + file->hexdigest = __pakfire_hexlify(file->digest, digest_length); + } + + return file->hexdigest; +} + +PAKFIRE_EXPORT int pakfire_file_set_digest(struct pakfire_file* file, + enum pakfire_digests type, const unsigned char* digest) { + if (!digest) { + errno = EINVAL; + return 1; + } + + // How long would the digest be? + const size_t digest_length = pakfire_digest_length(type); + if (!digest_length) { + errno = EINVAL; + return 1; + } + + // Store type + file->digest_type = type; + + // Store digest + memcpy(file->digest, digest, digest_length); + + return 0; } -PAKFIRE_EXPORT void pakfire_file_set_chksum(struct pakfire_file* file, const char* chksum) { - if (file->chksum) { - free(file->chksum); - file->chksum = NULL; +PAKFIRE_EXPORT int pakfire_file_set_hexdigest(struct pakfire_file* file, + enum pakfire_digests type, const char* hexdigest) { + const size_t digest_length = pakfire_digest_length(type); + if (!digest_length) { + errno = EINVAL; + return 1; } - if (chksum) - file->chksum = strdup(chksum); + // Allocate a buffer for the binary representation of the digest + unsigned char* digest = alloca(digest_length); + if (!digest) + return 1; + + // Convert from hex to binary + __pakfire_unhexlify(digest, digest_length, hexdigest); + + return pakfire_file_set_digest(file, type, digest); } static int pakfire_file_levels(struct pakfire_file* file) { diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index a8c042e46..ad06b3d44 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -201,7 +201,10 @@ static int pakfire_filelist_parse_line(struct pakfire_file** file, struct pakfir // checksum case 6: - pakfire_file_set_chksum(*file, word); + if (strcmp(word, "-") == 0) + break; + + pakfire_file_set_hexdigest(*file, PAKFIRE_DIGEST_SHA1, word); break; // path @@ -252,7 +255,10 @@ static int pakfire_filelist_parse_line(struct pakfire_file** file, struct pakfir // checksum case 7: - pakfire_file_set_chksum(*file, word); + if (strcmp(word, "-") == 0) + break; + + pakfire_file_set_hexdigest(*file, PAKFIRE_DIGEST_SHA1, word); break; } } diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index ee2e5caad..38fec8a6b 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -58,8 +58,14 @@ void pakfire_file_set_ctime(struct pakfire_file* file, time_t time); time_t pakfire_file_get_mtime(struct pakfire_file* file); void pakfire_file_set_mtime(struct pakfire_file* file, time_t time); -const char* pakfire_file_get_chksum(struct pakfire_file* file); -void pakfire_file_set_chksum(struct pakfire_file* file, const char* chksum); +const unsigned char* pakfire_file_get_digest( + struct pakfire_file* file, enum pakfire_digests* type); +const char* pakfire_file_get_hexdigest( + struct pakfire_file* file, enum pakfire_digests* type); +int pakfire_file_set_digest(struct pakfire_file* file, + enum pakfire_digests type, const unsigned char* digest); +int pakfire_file_set_hexdigest(struct pakfire_file* file, + enum pakfire_digests type, const char* hexdigest); struct pakfire_file* pakfire_file_parse_from_file(const char* list, unsigned int format); diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index ee0a60f04..9a170682f 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -31,8 +31,9 @@ struct pakfire; enum pakfire_digests { PAKFIRE_DIGEST_NONE = 0, - PAKFIRE_DIGEST_SHA256 = 1 << 0, - PAKFIRE_DIGEST_SHA512 = 1 << 1, + PAKFIRE_DIGEST_SHA1 = 1 << 0, + PAKFIRE_DIGEST_SHA256 = 1 << 1, + PAKFIRE_DIGEST_SHA512 = 1 << 2, }; #include diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index aadbb9fb2..6f7f21535 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -80,12 +80,16 @@ global: # file pakfire_file_cmp; pakfire_file_create; + pakfire_file_get_digest; pakfire_file_get_group; + pakfire_file_get_hexdigest; pakfire_file_get_mode; pakfire_file_get_path; pakfire_file_get_time; pakfire_file_get_user; + pakfire_file_set_digest; pakfire_file_set_group; + pakfire_file_set_hexdigest; pakfire_file_set_mode; pakfire_file_set_path; pakfire_file_set_time; diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 7b5c19f28..05f461dd6 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -437,6 +437,10 @@ PAKFIRE_EXPORT int pakfire_package_set_digest(struct pakfire_package* pkg, id = REPOKEY_TYPE_SHA512; break; + case PAKFIRE_DIGEST_SHA1: + id = REPOKEY_TYPE_SHA1; + break; + default: errno = ENOTSUP; return 1; @@ -1016,6 +1020,10 @@ PAKFIRE_EXPORT char* pakfire_package_dump(struct pakfire_package* pkg, int flags pakfire_package_dump_add_line(&string, _("SHA256 Digest"), hexdigest); break; + case PAKFIRE_DIGEST_SHA1: + pakfire_package_dump_add_line(&string, _("SHA1 Digest"), hexdigest); + break; + case PAKFIRE_DIGEST_NONE: break; } diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index b815d5eb0..8051d4c0f 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -787,6 +787,9 @@ size_t pakfire_digest_length(enum pakfire_digests digest) { case PAKFIRE_DIGEST_SHA256: return 32; + case PAKFIRE_DIGEST_SHA1: + return 20; + case PAKFIRE_DIGEST_NONE: return 0; }