#include "shared/log.h"
#include "shared/util.h"
#include <tzplatform_config.h>
+#include <openssl/sha.h>
#define BID_SNAME ".note.gnu.build-id"
char *rpm_info;
};
+static bool sha1sum(const char *filename, char *output)
+{
+ assert(filename);
+ assert(output);
+
+ bool result = false;
+
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ _E("Can not open %s to compute the hash sum", filename);
+ return false;
+ }
+
+ SHA_CTX context;
+ if (SHA1_Init(&context) == 0) {
+ _E("Failed to initialize SHA1 context for: %s", filename);
+ goto exit;
+ }
+
+ ssize_t bytes;
+ do {
+ char buff[4096];
+ bytes = read(fd, buff, sizeof(buff));
+ if (bytes < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+
+ _E("Error while reading file %s for checksum: %m", filename);
+ goto exit;
+ }
+
+ if (SHA1_Update(&context, buff, bytes) == 0) {
+ _E("Failed to update SHA1 hash sum for: %s", filename);
+ goto exit;
+ }
+ } while (bytes > 0);
+
+ unsigned char hash[SHA_DIGEST_LENGTH];
+ if (SHA1_Final(hash, &context) == 0) {
+ _E("Failed to compute the SHA1 hash sum for: %s", filename);
+ goto exit;
+ }
+
+ for (size_t i = 0; i < sizeof(hash); i++)
+ snprintf(&output[i*2], 2 + 1, "%02x", hash[i]);
+
+ result = true;
+exit:
+ close(fd);
+ return result;
+}
+
void rpm_info_free(struct rpm_info* ri)
{
free(ri->file_path);
ri->rpm_info = NULL;
pkgs_not_found = g_slist_append(pkgs_not_found, ri);
} else {
- int fret = fprintf(f, "%s %s %s\n", file_path, build_id, rpm_info);
+ char hash_sum[2*SHA_DIGEST_LENGTH + 1];
+
+ if (!sha1sum(file_path, hash_sum))
+ snprintf(hash_sum, sizeof(hash_sum), "hash-error");
+
+ int fret = fprintf(f, "%s %s %s %s\n", file_path, build_id, rpm_info, hash_sum);
free(rpm_info);
free(build_id);