Skip to content

Commit

Permalink
Allow zero count in readFilePart.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 97e6f7defe69d1993542c356798961333b061e39
  • Loading branch information
levlam committed Apr 27, 2019
1 parent be685c7 commit 4d0fed0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion td/generate/scheme/td_api.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions td/telegram/files/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64>(count)) {
if (count == 0) {
count = narrow_cast<int32>(file_view.downloaded_prefix(offset));
if (count == 0) {
return promise.set_value(td_api::make_object<td_api::filePart>());
}
} else if (file_view.downloaded_prefix(offset) < static_cast<int64>(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"));
}

Expand Down

0 comments on commit 4d0fed0

Please sign in to comment.