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

Side by Side Diff: net/base/file_stream_posix.cc

Issue 21363: Adds a new method ReadUntilComplete to FileStream which ensure that a... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « net/base/file_stream.h ('k') | net/base/file_stream_win.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 (c) 2008 The Chromium Authors. All rights reserved. Use of this 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the 2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file. 3 // LICENSE file.
4 4
5 // For 64-bit file access (off_t = off64_t, lseek64, etc). 5 // For 64-bit file access (off_t = off64_t, lseek64, etc).
6 #define _FILE_OFFSET_BITS 64 6 #define _FILE_OFFSET_BITS 64
7 7
8 #include "net/base/file_stream.h" 8 #include "net/base/file_stream.h"
9 9
10 #include <sys/types.h> 10 #include <sys/types.h>
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 ssize_t res = read(file_, buf, static_cast<size_t>(buf_len)); 147 ssize_t res = read(file_, buf, static_cast<size_t>(buf_len));
148 if (res == static_cast<ssize_t>(-1)) { 148 if (res == static_cast<ssize_t>(-1)) {
149 if (errno == EINTR) 149 if (errno == EINTR)
150 continue; 150 continue;
151 return MapErrorCode(errno); 151 return MapErrorCode(errno);
152 } 152 }
153 return static_cast<int>(res); 153 return static_cast<int>(res);
154 } 154 }
155 } 155 }
156 156
157 int FileStream::ReadUntilComplete(char *buf, int buf_len) {
158 int to_read = buf_len;
159 int bytes_total = 0;
160
161 do {
162 int bytes_read = Read(buf, to_read, NULL);
163 if (bytes_read <= 0) {
164 if (bytes_total == 0)
165 return bytes_read;
166
167 return bytes_total;
168 }
169
170 bytes_total += bytes_read;
171 buf += bytes_read;
172 to_read -= bytes_read;
173 } while (bytes_total < buf_len);
174
175 return bytes_total;
176 }
177
157 // TODO(deanm): async. 178 // TODO(deanm): async.
158 int FileStream::Write( 179 int FileStream::Write(
159 const char* buf, int buf_len, CompletionCallback* callback) { 180 const char* buf, int buf_len, CompletionCallback* callback) {
160 181
161 // read(..., 0) will return 0, which indicates end-of-file. 182 // read(..., 0) will return 0, which indicates end-of-file.
162 DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX); 183 DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX);
163 184
164 if (!IsOpen()) 185 if (!IsOpen())
165 return ERR_UNEXPECTED; 186 return ERR_UNEXPECTED;
166 187
167 int total_bytes_written = 0; 188 int total_bytes_written = 0;
168 size_t len = static_cast<size_t>(buf_len); 189 size_t len = static_cast<size_t>(buf_len);
169 while (total_bytes_written < buf_len) { 190 while (total_bytes_written < buf_len) {
170 ssize_t res = write(file_, buf, len); 191 ssize_t res = write(file_, buf, len);
171 if (res == static_cast<ssize_t>(-1)) { 192 if (res == static_cast<ssize_t>(-1)) {
172 if (errno == EINTR) 193 if (errno == EINTR)
173 continue; 194 continue;
174 return MapErrorCode(errno); 195 return MapErrorCode(errno);
175 } 196 }
176 total_bytes_written += res; 197 total_bytes_written += res;
177 buf += res; 198 buf += res;
178 len -= res; 199 len -= res;
179 } 200 }
180 return total_bytes_written; 201 return total_bytes_written;
181 } 202 }
182 203
183 } // namespace net 204 } // namespace net
OLDNEW
« no previous file with comments | « net/base/file_stream.h ('k') | net/base/file_stream_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698