mu: use sha-1 for fallback msgid
We were using a hash based on the path, but it's better to have something that's constant even if the path chnages.
This commit is contained in:
@ -116,6 +116,30 @@ init_file_metadata (MuMsgFile *self, const char* path, const gchar* mdir,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static char*
|
||||
calculate_sha1 (FILE *file)
|
||||
{
|
||||
std::array<uint8_t, 4096> buf{};
|
||||
char *sha1{};
|
||||
GChecksum *checksum{g_checksum_new(G_CHECKSUM_SHA256)};
|
||||
|
||||
while (true) {
|
||||
const auto n = ::fread(buf.data(), 1, buf.size(), file);
|
||||
if (n == 0)
|
||||
break;
|
||||
g_checksum_update (checksum, buf.data(), n);
|
||||
}
|
||||
|
||||
if (::ferror(file))
|
||||
g_warning ("error reading file");
|
||||
else
|
||||
sha1 = g_strdup(g_checksum_get_string(checksum));
|
||||
|
||||
g_checksum_free(checksum);
|
||||
|
||||
return sha1;
|
||||
}
|
||||
|
||||
static GMimeStream*
|
||||
get_mime_stream (MuMsgFile *self, const char *path, GError **err)
|
||||
{
|
||||
@ -140,6 +164,14 @@ get_mime_stream (MuMsgFile *self, const char *path, GError **err)
|
||||
}
|
||||
|
||||
return stream;
|
||||
self->_sha1 = calculate_sha1(file);
|
||||
if (!self->_sha1) {
|
||||
::fclose(file);
|
||||
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_FILE,
|
||||
"failed to get sha-1 for %s", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static gboolean
|
||||
@ -649,16 +681,15 @@ address_type (MuMsgFieldId mfid)
|
||||
static gchar*
|
||||
get_msgid (MuMsgFile *self, gboolean *do_free)
|
||||
{
|
||||
const char *msgid;
|
||||
|
||||
msgid = g_mime_message_get_message_id (self->_mime_msg);
|
||||
const char *msgid{g_mime_message_get_message_id (self->_mime_msg)};
|
||||
if (msgid && strlen(msgid) < MU_STORE_MAX_TERM_LENGTH) {
|
||||
*do_free = FALSE;
|
||||
return (char*)msgid;
|
||||
} else { /* if there is none, fake it */
|
||||
*do_free = TRUE;
|
||||
return g_strdup_printf ("%016" PRIx64 "@fake-msgid",
|
||||
mu_util_get_hash (self->_path));
|
||||
}
|
||||
// if there's no valid message-id, synthesize one;
|
||||
// based on the contents so it stays valid if moved around.
|
||||
*do_free = TRUE;
|
||||
return g_strdup_printf ("%s@mu", self->_sha1);
|
||||
}
|
||||
|
||||
char*
|
||||
|
||||
@ -36,6 +36,7 @@ struct MuMsgFile {
|
||||
size_t _size;
|
||||
char *_path;
|
||||
char *_maildir;
|
||||
char *_sha1;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user