Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: mojo/common/data_pipe_utils.cc

Issue 992273002: Random drive-by cleanup. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: log seek failures Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | shell/application_manager/data_pipe_peek.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
80 LOG(ERROR) << "Seek of " << skip << " in " << source.value() << " failed";
82 return false; 81 return false;
83 } 82 }
84 for (;;) { 83 for (;;) {
85 void* buffer = nullptr; 84 void* buffer = nullptr;
86 uint32_t buffer_num_bytes = 0; 85 uint32_t buffer_num_bytes = 0;
87 MojoResult result = 86 MojoResult result =
88 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, 87 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes,
89 MOJO_WRITE_DATA_FLAG_NONE); 88 MOJO_WRITE_DATA_FLAG_NONE);
90 if (result == MOJO_RESULT_OK) { 89 if (result == MOJO_RESULT_OK) {
91 int bytes_read = 90 int bytes_read =
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return result == MOJO_RESULT_FAILED_PRECONDITION; 158 return result == MOJO_RESULT_FAILED_PRECONDITION;
160 } 159 }
161 } 160 }
162 } 161 }
163 162
164 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, 163 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source,
165 const base::FilePath& destination) { 164 const base::FilePath& destination) {
166 base::ScopedFILE fp(base::OpenFile(destination, "wb")); 165 base::ScopedFILE fp(base::OpenFile(destination, "wb"));
167 if (!fp) { 166 if (!fp) {
168 LOG(ERROR) << "OpenFile('" << destination.value() 167 LOG(ERROR) << "OpenFile('" << destination.value()
169 << "'failed in BlockingCopyToFile" << std::endl; 168 << "'failed in BlockingCopyToFile";
170 return false; 169 return false;
171 } 170 }
172 return BlockingCopyHelper( 171 return BlockingCopyHelper(
173 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); 172 source.Pass(), base::Bind(&CopyToFileHelper, fp.get()));
174 } 173 }
175 174
176 void CopyToFile(ScopedDataPipeConsumerHandle source, 175 void CopyToFile(ScopedDataPipeConsumerHandle source,
177 const base::FilePath& destination, 176 const base::FilePath& destination,
178 base::TaskRunner* task_runner, 177 base::TaskRunner* task_runner,
179 const base::Callback<void(bool)>& callback) { 178 const base::Callback<void(bool)>& callback) {
(...skipping 10 matching lines...) Expand all
190 base::TaskRunner* task_runner, 189 base::TaskRunner* task_runner,
191 const base::Callback<void(bool)>& callback) { 190 const base::Callback<void(bool)>& callback) {
192 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, 191 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE,
193 base::Bind(&BlockingCopyFromFile, source, 192 base::Bind(&BlockingCopyFromFile, source,
194 base::Passed(&destination), skip), 193 base::Passed(&destination), skip),
195 callback); 194 callback);
196 } 195 }
197 196
198 } // namespace common 197 } // namespace common
199 } // namespace mojo 198 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | shell/application_manager/data_pipe_peek.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698