From bc79fabfef519e743bc345b0d4302d2e3eaf1a30 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 24 Apr 2018 18:26:35 -0700 Subject: [PATCH] modules/media: Add error logging; various cleanup. --- modules/media/download.cc | 15 ++++++++++++++- modules/media/media.cc | 14 ++++++++++---- modules/media/media.h | 7 +++++-- modules/media/thumbnail.cc | 28 +++++++++++++++++++--------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/modules/media/download.cc b/modules/media/download.cc index 6ccf3597f..a8e817b6c 100644 --- a/modules/media/download.cc +++ b/modules/media/download.cc @@ -127,7 +127,20 @@ get__download_local(client &client, sent += write_all(*client.sock, block); }); - assert(sent == file_size); + if(unlikely(read != file_size)) log::error + { + media_log, "File %s/%s [%s] size mismatch: expected %zu got %zu", + server, + file, + string_view{room.room_id}, + file_size, + read + }; + + // Have to kill client here after failing content length expectation. + if(unlikely(read != file_size)) + client.close(net::dc::RST, net::close_ignore); + return {}; } diff --git a/modules/media/media.cc b/modules/media/media.cc index 385e3afe6..b4e0799b0 100644 --- a/modules/media/media.cc +++ b/modules/media/media.cc @@ -16,9 +16,15 @@ IRCD_MODULE "11.7 :Content respository" }; +decltype(media_log) +media_log +{ + "media" +}; + size_t write_file(const m::room &room, - const string_view &content, + const const_buffer &content, const string_view &content_type) { //TODO: TXN @@ -95,7 +101,7 @@ write_file(const m::room &room, size_t read_each_block(const m::room &room, - const std::function &closure) + const std::function &closure) { const auto lpath { @@ -118,7 +124,7 @@ read_each_block(const m::room &room, }; size_t ret{0}; - m::room::messages it{room, 0}; + m::room::messages it{room, 1}; for(; bool(it); ++it) { const m::event &event{*it}; @@ -140,7 +146,7 @@ read_each_block(const m::room &room, pathbuf, pathlen + copy(pathpart, hash) }; - const string_view &block + const const_buffer &block { fs::read(path, buf) }; diff --git a/modules/media/media.h b/modules/media/media.h index faf80b00c..d117aea69 100644 --- a/modules/media/media.h +++ b/modules/media/media.h @@ -10,6 +10,9 @@ using namespace ircd; +extern mapi::header IRCD_MODULE; +extern log::log media_log; + extern "C" m::room::id file_room_id(m::room::id::buf &out, const string_view &server, @@ -19,5 +22,5 @@ m::room::id::buf file_room_id(const string_view &server, const string_view &file); -size_t read_each_block(const m::room &, const std::function &); -size_t write_file(const m::room &room, const string_view &content, const string_view &content_type); +size_t read_each_block(const m::room &, const std::function &); +size_t write_file(const m::room &room, const const_buffer &content, const string_view &content_type); diff --git a/modules/media/thumbnail.cc b/modules/media/thumbnail.cc index dc481edd0..df885534c 100644 --- a/modules/media/thumbnail.cc +++ b/modules/media/thumbnail.cc @@ -221,18 +221,15 @@ try content_type }; - // Send it off to user first - const resource::response response - { - client, remote_request.in.content, content_type - }; - const size_t written { write_file(room, remote_request.in.content, content_type) }; - return response; + return resource::response + { + client, remote_request.in.content, content_type + }; } catch(const ircd::server::unavailable &e) { @@ -286,7 +283,7 @@ get__thumbnail_local(client &client, size_t sent{0}; const auto sink{[&client, &sent] - (const string_view &block) + (const const_buffer &block) { sent += client.write_all(block); }}; @@ -296,7 +293,20 @@ get__thumbnail_local(client &client, read_each_block(room, sink) }; - assert(read == file_size); + if(unlikely(read != 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 + }; + + // Have to kill client here after failing content length expectation. + if(unlikely(read != file_size)) + client.close(net::dc::RST, net::close_ignore); + assert(read == sent); return response; }