OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/filesystem/file_impl.h" | 5 #include "components/filesystem/file_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (num_bytes_read < 0) { | 87 if (num_bytes_read < 0) { |
88 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); | 88 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); | 92 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
93 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | 93 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
94 callback.Run(FileError::OK, std::move(bytes_read)); | 94 callback.Run(FileError::OK, std::move(bytes_read)); |
95 } | 95 } |
96 | 96 |
| 97 void FileImpl::ReadEntireFile(const ReadEntireFileCallback& callback) { |
| 98 if (!file_.IsValid()) { |
| 99 callback.Run(GetError(file_), mojo::Array<uint8_t>()); |
| 100 return; |
| 101 } |
| 102 |
| 103 // Seek to the front of the file. |
| 104 if (file_.Seek(base::File::FROM_BEGIN, 0) == -1) { |
| 105 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); |
| 106 return; |
| 107 } |
| 108 |
| 109 std::string contents; |
| 110 const int kBufferSize = 1 << 16; |
| 111 scoped_ptr<char[]> buf(new char[kBufferSize]); |
| 112 size_t len; |
| 113 while ((len = file_.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0) |
| 114 contents.append(buf.get(), len); |
| 115 |
| 116 callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents)); |
| 117 } |
| 118 |
97 // TODO(vtl): Move the implementation to a thread pool. | 119 // TODO(vtl): Move the implementation to a thread pool. |
98 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, | 120 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, |
99 int64_t offset, | 121 int64_t offset, |
100 Whence whence, | 122 Whence whence, |
101 const WriteCallback& callback) { | 123 const WriteCallback& callback) { |
102 DCHECK(!bytes_to_write.is_null()); | 124 DCHECK(!bytes_to_write.is_null()); |
103 if (!file_.IsValid()) { | 125 if (!file_.IsValid()) { |
104 callback.Run(GetError(file_), 0); | 126 callback.Run(GetError(file_), 0); |
105 return; | 127 return; |
106 } | 128 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 new_file.TakePlatformFile(), &mojo_handle); | 336 new_file.TakePlatformFile(), &mojo_handle); |
315 if (create_result != MOJO_RESULT_OK) { | 337 if (create_result != MOJO_RESULT_OK) { |
316 callback.Run(FileError::FAILED, ScopedHandle()); | 338 callback.Run(FileError::FAILED, ScopedHandle()); |
317 return; | 339 return; |
318 } | 340 } |
319 | 341 |
320 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); | 342 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); |
321 } | 343 } |
322 | 344 |
323 } // namespace filesystem | 345 } // namespace filesystem |
OLD | NEW |