| 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> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "components/filesystem/util.h" | 16 #include "components/filesystem/util.h" |
| 17 #include "mojo/common/common_type_converters.h" |
| 17 #include "mojo/platform_handle/platform_handle_functions.h" | 18 #include "mojo/platform_handle/platform_handle_functions.h" |
| 18 | 19 |
| 19 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); | 20 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); |
| 20 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); | 21 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); |
| 21 | 22 |
| 22 using base::Time; | 23 using base::Time; |
| 23 using mojo::ScopedHandle; | 24 using mojo::ScopedHandle; |
| 24 | 25 |
| 25 namespace filesystem { | 26 namespace filesystem { |
| 26 | 27 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 if (num_bytes_read < 0) { | 87 if (num_bytes_read < 0) { |
| 87 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); | 88 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); |
| 88 return; | 89 return; |
| 89 } | 90 } |
| 90 | 91 |
| 91 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); |
| 92 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | 93 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
| 93 callback.Run(FileError::OK, std::move(bytes_read)); | 94 callback.Run(FileError::OK, std::move(bytes_read)); |
| 94 } | 95 } |
| 95 | 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 |
| 96 // TODO(vtl): Move the implementation to a thread pool. | 119 // TODO(vtl): Move the implementation to a thread pool. |
| 97 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, | 120 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, |
| 98 int64_t offset, | 121 int64_t offset, |
| 99 Whence whence, | 122 Whence whence, |
| 100 const WriteCallback& callback) { | 123 const WriteCallback& callback) { |
| 101 DCHECK(!bytes_to_write.is_null()); | 124 DCHECK(!bytes_to_write.is_null()); |
| 102 if (!file_.IsValid()) { | 125 if (!file_.IsValid()) { |
| 103 callback.Run(GetError(file_), 0); | 126 callback.Run(GetError(file_), 0); |
| 104 return; | 127 return; |
| 105 } | 128 } |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 new_file.TakePlatformFile(), &mojo_handle); | 336 new_file.TakePlatformFile(), &mojo_handle); |
| 314 if (create_result != MOJO_RESULT_OK) { | 337 if (create_result != MOJO_RESULT_OK) { |
| 315 callback.Run(FileError::FAILED, ScopedHandle()); | 338 callback.Run(FileError::FAILED, ScopedHandle()); |
| 316 return; | 339 return; |
| 317 } | 340 } |
| 318 | 341 |
| 319 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); | 342 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); |
| 320 } | 343 } |
| 321 | 344 |
| 322 } // namespace filesystem | 345 } // namespace filesystem |
| OLD | NEW |