1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/public/cpp/system/file_data_source.h"
6 
7 #include <algorithm>
8 #include <limits>
9 
10 namespace mojo {
11 
12 namespace {
13 
CalculateEndOffset(base::File * file,MojoResult * result)14 uint64_t CalculateEndOffset(base::File* file, MojoResult* result) {
15   if (!file->IsValid())
16     return 0u;
17   int64_t length = file->GetLength();
18   if (length < 0) {
19     *result =
20         FileDataSource::ConvertFileErrorToMojoResult(file->GetLastFileError());
21     return 0u;
22   }
23   return length;
24 }
25 
26 }  // namespace
27 
28 // static
ConvertFileErrorToMojoResult(base::File::Error error)29 MojoResult FileDataSource::ConvertFileErrorToMojoResult(
30     base::File::Error error) {
31   switch (error) {
32     case base::File::FILE_OK:
33       return MOJO_RESULT_OK;
34     case base::File::FILE_ERROR_NOT_FOUND:
35       return MOJO_RESULT_NOT_FOUND;
36     case base::File::FILE_ERROR_SECURITY:
37     case base::File::FILE_ERROR_ACCESS_DENIED:
38       return MOJO_RESULT_PERMISSION_DENIED;
39     case base::File::FILE_ERROR_TOO_MANY_OPENED:
40     case base::File::FILE_ERROR_NO_MEMORY:
41       return MOJO_RESULT_RESOURCE_EXHAUSTED;
42     case base::File::FILE_ERROR_ABORT:
43       return MOJO_RESULT_ABORTED;
44     default:
45       return MOJO_RESULT_UNKNOWN;
46   }
47 }
48 
FileDataSource(base::File file)49 FileDataSource::FileDataSource(base::File file)
50     : file_(std::move(file)),
51       error_(ConvertFileErrorToMojoResult(file_.error_details())),
52       start_offset_(0u),
53       end_offset_(CalculateEndOffset(&file_, &error_)) {}
54 
55 FileDataSource::~FileDataSource() = default;
56 
SetRange(uint64_t start,uint64_t end)57 void FileDataSource::SetRange(uint64_t start, uint64_t end) {
58   if (start > end) {
59     start_offset_ = 0;
60     end_offset_ = 0;
61     if (error_ == MOJO_RESULT_OK)
62       error_ = MOJO_RESULT_INVALID_ARGUMENT;
63   } else {
64     start_offset_ = start;
65     end_offset_ = end;
66   }
67 }
68 
GetLength() const69 uint64_t FileDataSource::GetLength() const {
70   return end_offset_ - start_offset_;
71 }
72 
Read(uint64_t offset,base::span<char> buffer)73 DataPipeProducer::DataSource::ReadResult FileDataSource::Read(
74     uint64_t offset,
75     base::span<char> buffer) {
76   ReadResult result;
77   if (error_ != MOJO_RESULT_OK)
78     result.result = error_;
79   else if (GetLength() < offset)
80     result.result = MOJO_RESULT_INVALID_ARGUMENT;
81 
82   uint64_t readable_size = GetLength() - offset;
83   uint64_t read_size =
84       std::min(static_cast<uint64_t>(std::numeric_limits<int>::max()),
85                std::min(static_cast<uint64_t>(buffer.size()), readable_size));
86   // |read_offset| should not overflow if 'GetLength() < offset' is true.
87   // Otherwise, MOJO_RESULT_INVALID_ARGUMENT should be already set.
88   uint64_t read_offset = start_offset_ + offset;
89   if (read_offset > std::numeric_limits<int64_t>::max())
90     result.result = MOJO_RESULT_INVALID_ARGUMENT;
91 
92   if (result.result != MOJO_RESULT_OK)
93     return result;
94 
95   int bytes_read =
96       file_.Read(static_cast<int64_t>(read_offset), buffer.data(), read_size);
97   if (bytes_read < 0) {
98     result.bytes_read = 0;
99     result.result = ConvertFileErrorToMojoResult(file_.GetLastFileError());
100   } else {
101     result.bytes_read = bytes_read;
102   }
103   return result;
104 }
105 
106 }  // namespace mojo
107