From bffff30ac3531001eeb9c015c5af4d8cad062c84 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 25 Apr 2018 18:44:02 -0700 Subject: [PATCH] modules/media: Additional checks / cleanup. --- modules/media/media.cc | 16 ++++++++++++---- modules/media/thumbnail.cc | 16 ++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/modules/media/media.cc b/modules/media/media.cc index b4e0799b0..34db62ea8 100644 --- a/modules/media/media.cc +++ b/modules/media/media.cc @@ -88,14 +88,12 @@ write_file(const m::room &room, pathbuf, pathlen + copy(pathpart, hash) }; - if(!fs::exists(path)) - wrote += size(fs::write(path, block)); - + wrote += size(fs::overwrite(path, block)); off += blksz; } assert(off == size(content)); - assert(wrote <= off); + assert(wrote == off); return wrote; } @@ -151,6 +149,16 @@ read_each_block(const m::room &room, fs::read(path, buf) }; + if(unlikely(size(block) != blksz)) throw error + { + "File [%s] block [%s] (%s) blksz %zu != %zu", + string_view{room.room_id}, + string_view{at<"event_id"_>(event)}, + path, + blksz, + size(block) + }; + assert(size(block) == blksz); ret += size(block); closure(block); diff --git a/modules/media/thumbnail.cc b/modules/media/thumbnail.cc index df885534c..c3888971c 100644 --- a/modules/media/thumbnail.cc +++ b/modules/media/thumbnail.cc @@ -281,32 +281,32 @@ get__thumbnail_local(client &client, client, http::OK, content_type, file_size }; - size_t sent{0}; - const auto sink{[&client, &sent] + size_t sent_size{0}; + const auto sink{[&client, &sent_size] (const const_buffer &block) { - sent += client.write_all(block); + sent_size += client.write_all(block); }}; - const size_t read + const size_t read_size { read_each_block(room, sink) }; - if(unlikely(read != file_size)) log::error + if(unlikely(read_size != file_size)) log::error { media_log, "File %s/%s [%s] size mismatch: expected %zu got %zu", hostname, mediaid, string_view{room.room_id}, file_size, - read + read_size }; // Have to kill client here after failing content length expectation. - if(unlikely(read != file_size)) + if(unlikely(read_size != file_size)) client.close(net::dc::RST, net::close_ignore); - assert(read == sent); + assert(read_size == sent_size); return response; }