OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/common/data_pipe_utils.h" | 5 #include "mojo/common/data_pipe_utils.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 for (;;) { | 22 for (;;) { |
23 const void* buffer = nullptr; | 23 const void* buffer = nullptr; |
24 uint32_t num_bytes = 0; | 24 uint32_t num_bytes = 0; |
25 MojoResult result = BeginReadDataRaw( | 25 MojoResult result = BeginReadDataRaw( |
26 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); | 26 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); |
27 if (result == MOJO_RESULT_OK) { | 27 if (result == MOJO_RESULT_OK) { |
28 size_t bytes_written = write_bytes.Run(buffer, num_bytes); | 28 size_t bytes_written = write_bytes.Run(buffer, num_bytes); |
29 if (bytes_written < num_bytes) { | 29 if (bytes_written < num_bytes) { |
30 LOG(ERROR) << "write_bytes callback wrote fewer bytes (" | 30 LOG(ERROR) << "write_bytes callback wrote fewer bytes (" |
31 << bytes_written << ") written than expected (" << num_bytes | 31 << bytes_written << ") written than expected (" << num_bytes |
32 << ") in BlockingCopyHelper " | 32 << ") in BlockingCopyHelper (pipe closed? out of disk " |
33 << "(pipe closed? out of disk space?)" << std::endl; | 33 "space?)"; |
34 // No need to call EndReadDataRaw(), since |source| will be closed. | |
34 return false; | 35 return false; |
35 } | 36 } |
36 result = EndReadDataRaw(source.get(), num_bytes); | 37 result = EndReadDataRaw(source.get(), num_bytes); |
37 if (result != MOJO_RESULT_OK) { | 38 if (result != MOJO_RESULT_OK) { |
38 LOG(ERROR) << "EndReadDataRaw error (" << result | 39 LOG(ERROR) << "EndReadDataRaw error (" << result |
39 << ") in BlockingCopyHelper" << std::endl; | 40 << ") in BlockingCopyHelper"; |
40 return false; | 41 return false; |
41 } | 42 } |
42 } else if (result == MOJO_RESULT_SHOULD_WAIT) { | 43 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
43 result = Wait(source.get(), | 44 result = Wait(source.get(), |
44 MOJO_HANDLE_SIGNAL_READABLE, | 45 MOJO_HANDLE_SIGNAL_READABLE, |
45 MOJO_DEADLINE_INDEFINITE, | 46 MOJO_DEADLINE_INDEFINITE, |
46 nullptr); | 47 nullptr); |
47 if (result != MOJO_RESULT_OK) { | 48 if (result != MOJO_RESULT_OK) { |
48 // If the producer handle was closed, then treat as EOF. | 49 // If the producer handle was closed, then treat as EOF. |
49 return result == MOJO_RESULT_FAILED_PRECONDITION; | 50 return result == MOJO_RESULT_FAILED_PRECONDITION; |
50 } | 51 } |
51 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { | 52 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
52 // If the producer handle was closed, then treat as EOF. | 53 // If the producer handle was closed, then treat as EOF. |
53 return true; | 54 return true; |
54 } else { | 55 } else { |
55 LOG(ERROR) << "Unhandled error " << result << " in BlockingCopyHelper" | 56 LOG(ERROR) << "Unhandled error " << result << " in BlockingCopyHelper"; |
56 << std::endl; | |
57 // Some other error occurred. | 57 // Some other error occurred. |
58 break; | 58 return false; |
59 } | 59 } |
60 } | 60 } |
61 | |
62 return false; | |
63 } | 61 } |
64 | 62 |
65 size_t CopyToStringHelper( | 63 size_t CopyToStringHelper( |
66 std::string* result, const void* buffer, uint32_t num_bytes) { | 64 std::string* result, const void* buffer, uint32_t num_bytes) { |
67 result->append(static_cast<const char*>(buffer), num_bytes); | 65 result->append(static_cast<const char*>(buffer), num_bytes); |
68 return num_bytes; | 66 return num_bytes; |
69 } | 67 } |
70 | 68 |
71 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { | 69 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { |
72 return fwrite(buffer, 1, num_bytes, fp); | 70 return fwrite(buffer, 1, num_bytes, fp); |
73 } | 71 } |
74 | 72 |
75 bool BlockingCopyFromFile(const base::FilePath& source, | 73 bool BlockingCopyFromFile(const base::FilePath& source, |
76 ScopedDataPipeProducerHandle destination, | 74 ScopedDataPipeProducerHandle destination, |
77 uint32_t skip) { | 75 uint32_t skip) { |
78 base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ); | 76 base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ); |
79 if (!file.IsValid()) | 77 if (!file.IsValid()) |
80 return false; | 78 return false; |
81 if (file.Seek(base::File::FROM_BEGIN, skip) != skip) { | 79 if (file.Seek(base::File::FROM_BEGIN, skip) != skip) |
hansmuller1
2015/03/11 22:02:01
Maybe we should log errors for these failure modes
viettrungluu
2015/03/11 23:58:23
Done.
| |
82 return false; | 80 return false; |
83 } | |
84 for (;;) { | 81 for (;;) { |
85 void* buffer = nullptr; | 82 void* buffer = nullptr; |
86 uint32_t buffer_num_bytes = 0; | 83 uint32_t buffer_num_bytes = 0; |
87 MojoResult result = | 84 MojoResult result = |
88 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, | 85 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, |
89 MOJO_WRITE_DATA_FLAG_NONE); | 86 MOJO_WRITE_DATA_FLAG_NONE); |
90 if (result == MOJO_RESULT_OK) { | 87 if (result == MOJO_RESULT_OK) { |
91 int bytes_read = | 88 int bytes_read = |
92 file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes); | 89 file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes); |
93 if (bytes_read >= 0) { | 90 if (bytes_read >= 0) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 return result == MOJO_RESULT_FAILED_PRECONDITION; | 156 return result == MOJO_RESULT_FAILED_PRECONDITION; |
160 } | 157 } |
161 } | 158 } |
162 } | 159 } |
163 | 160 |
164 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, | 161 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, |
165 const base::FilePath& destination) { | 162 const base::FilePath& destination) { |
166 base::ScopedFILE fp(base::OpenFile(destination, "wb")); | 163 base::ScopedFILE fp(base::OpenFile(destination, "wb")); |
167 if (!fp) { | 164 if (!fp) { |
168 LOG(ERROR) << "OpenFile('" << destination.value() | 165 LOG(ERROR) << "OpenFile('" << destination.value() |
169 << "'failed in BlockingCopyToFile" << std::endl; | 166 << "'failed in BlockingCopyToFile"; |
170 return false; | 167 return false; |
171 } | 168 } |
172 return BlockingCopyHelper( | 169 return BlockingCopyHelper( |
173 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); | 170 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); |
174 } | 171 } |
175 | 172 |
176 void CopyToFile(ScopedDataPipeConsumerHandle source, | 173 void CopyToFile(ScopedDataPipeConsumerHandle source, |
177 const base::FilePath& destination, | 174 const base::FilePath& destination, |
178 base::TaskRunner* task_runner, | 175 base::TaskRunner* task_runner, |
179 const base::Callback<void(bool)>& callback) { | 176 const base::Callback<void(bool)>& callback) { |
(...skipping 10 matching lines...) Expand all Loading... | |
190 base::TaskRunner* task_runner, | 187 base::TaskRunner* task_runner, |
191 const base::Callback<void(bool)>& callback) { | 188 const base::Callback<void(bool)>& callback) { |
192 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, | 189 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, |
193 base::Bind(&BlockingCopyFromFile, source, | 190 base::Bind(&BlockingCopyFromFile, source, |
194 base::Passed(&destination), skip), | 191 base::Passed(&destination), skip), |
195 callback); | 192 callback); |
196 } | 193 } |
197 | 194 |
198 } // namespace common | 195 } // namespace common |
199 } // namespace mojo | 196 } // namespace mojo |
OLD | NEW |