From 4d0fed097aa1bd23a00488897982363a6aff64e3 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 27 Apr 2019 17:14:45 +0300 Subject: [PATCH] Allow zero count in readFilePart. GitOrigin-RevId: 97e6f7defe69d1993542c356798961333b061e39 --- td/generate/scheme/td_api.tl | 4 +++- td/telegram/files/FileManager.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 13f7f79c..66f771a3 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3261,7 +3261,9 @@ setFileGenerationProgress generation_id:int64 expected_size:int32 local_prefix_s finishFileGeneration generation_id:int64 error:error = Ok; //@description Reads a part of a file from the TDLib file cache and returns read bytes. This method is intended to be used only if the client has no direct access to TDLib's file system, because it is usually slower than a direct read from the file -//@file_id Identifier of the file. The file must be located in the TDLib file cache @offset The offset from which to read the file @count Number of bytes to read. An error will be returned if there are not enough bytes available in the file from the specified position +//@file_id Identifier of the file. The file must be located in the TDLib file cache +//@offset The offset from which to read the file +//@count Number of bytes to read. An error will be returned if there are not enough bytes available in the file from the specified position. Pass 0 to read all available data from the specified position readFilePart file_id:int32 offset:int32 count:int32 = FilePart; //@description Deletes a file from the TDLib file cache @file_id Identifier of the file to delete diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index de693bf4..86d9f697 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -1831,14 +1831,19 @@ void FileManager::read_file_part(FileId file_id, int32 offset, int32 count, int if (offset < 0) { return promise.set_error(Status::Error(400, "Parameter offset must be non-negative")); } - if (count <= 0) { - return promise.set_error(Status::Error(400, "Parameter count must be positive")); + if (count < 0) { + return promise.set_error(Status::Error(400, "Parameter count must be non-negative")); } auto file_view = FileView(node); - // TODO this check is safer to do in another thread - if (file_view.downloaded_prefix(offset) < static_cast(count)) { + if (count == 0) { + count = narrow_cast(file_view.downloaded_prefix(offset)); + if (count == 0) { + return promise.set_value(td_api::make_object()); + } + } else if (file_view.downloaded_prefix(offset) < static_cast(count)) { + // TODO this check is safer to do in another thread return promise.set_error(Status::Error(400, "There is not enough downloaded bytes in the file to read")); }