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

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

Issue 12320003: Fix net::FileStream to handle POSIX errors correctly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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_context.cc ('k') | net/base/file_stream_context_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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // 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_context.h" 8 #include "net/base/file_stream_context.h"
9 9
10 #include <sys/types.h> 10 #include <sys/types.h>
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 async_in_progress_(false), 61 async_in_progress_(false),
62 orphaned_(false), 62 orphaned_(false),
63 bound_net_log_(bound_net_log) { 63 bound_net_log_(bound_net_log) {
64 } 64 }
65 65
66 FileStream::Context::~Context() { 66 FileStream::Context::~Context() {
67 } 67 }
68 68
69 int64 FileStream::Context::GetFileSize() const { 69 int64 FileStream::Context::GetFileSize() const {
70 struct stat info; 70 struct stat info;
71 if (fstat(file_, &info) != 0) 71 if (fstat(file_, &info) != 0) {
72 return RecordAndMapError(errno, FILE_ERROR_SOURCE_GET_SIZE); 72 IOResult result = IOResult::FromOSError(errno);
73 RecordError(result, FILE_ERROR_SOURCE_GET_SIZE);
74 return result.result;
75 }
73 76
74 return static_cast<int64>(info.st_size); 77 return static_cast<int64>(info.st_size);
75 } 78 }
76 79
77 int FileStream::Context::ReadAsync(IOBuffer* in_buf, 80 int FileStream::Context::ReadAsync(IOBuffer* in_buf,
78 int buf_len, 81 int buf_len,
79 const CompletionCallback& callback) { 82 const CompletionCallback& callback) {
80 DCHECK(!async_in_progress_); 83 DCHECK(!async_in_progress_);
81 84
82 scoped_refptr<IOBuffer> buf = in_buf; 85 scoped_refptr<IOBuffer> buf = in_buf;
83 const bool posted = base::PostTaskAndReplyWithResult( 86 const bool posted = base::PostTaskAndReplyWithResult(
84 base::WorkerPool::GetTaskRunner(true /* task is slow */), 87 base::WorkerPool::GetTaskRunner(true /* task is slow */),
85 FROM_HERE, 88 FROM_HERE,
86 base::Bind(&Context::ReadFileImpl, 89 base::Bind(&Context::ReadFileImpl,
87 base::Unretained(this), buf, buf_len), 90 base::Unretained(this), buf, buf_len),
88 base::Bind(&Context::ProcessAsyncResult, 91 base::Bind(&Context::ProcessAsyncResult,
89 base::Unretained(this), IntToInt64(callback), 92 base::Unretained(this), IntToInt64(callback),
90 FILE_ERROR_SOURCE_READ)); 93 FILE_ERROR_SOURCE_READ));
91 DCHECK(posted); 94 DCHECK(posted);
92 95
93 async_in_progress_ = true; 96 async_in_progress_ = true;
94 return ERR_IO_PENDING; 97 return ERR_IO_PENDING;
95 } 98 }
96 99
97 int FileStream::Context::ReadSync(char* in_buf, int buf_len) { 100 int FileStream::Context::ReadSync(char* in_buf, int buf_len) {
98 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); 101 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf);
99 int64 result = ReadFileImpl(buf, buf_len); 102 IOResult result = ReadFileImpl(buf, buf_len);
100 CheckForIOError(&result, FILE_ERROR_SOURCE_READ); 103 RecordError(result, FILE_ERROR_SOURCE_READ);
101 return result; 104 return result.result;
102 } 105 }
103 106
104 int FileStream::Context::WriteAsync(IOBuffer* in_buf, 107 int FileStream::Context::WriteAsync(IOBuffer* in_buf,
105 int buf_len, 108 int buf_len,
106 const CompletionCallback& callback) { 109 const CompletionCallback& callback) {
107 DCHECK(!async_in_progress_); 110 DCHECK(!async_in_progress_);
108 111
109 scoped_refptr<IOBuffer> buf = in_buf; 112 scoped_refptr<IOBuffer> buf = in_buf;
110 const bool posted = base::PostTaskAndReplyWithResult( 113 const bool posted = base::PostTaskAndReplyWithResult(
111 base::WorkerPool::GetTaskRunner(true /* task is slow */), 114 base::WorkerPool::GetTaskRunner(true /* task is slow */),
112 FROM_HERE, 115 FROM_HERE,
113 base::Bind(&Context::WriteFileImpl, 116 base::Bind(&Context::WriteFileImpl,
114 base::Unretained(this), buf, buf_len), 117 base::Unretained(this), buf, buf_len),
115 base::Bind(&Context::ProcessAsyncResult, 118 base::Bind(&Context::ProcessAsyncResult,
116 base::Unretained(this), IntToInt64(callback), 119 base::Unretained(this), IntToInt64(callback),
117 FILE_ERROR_SOURCE_WRITE)); 120 FILE_ERROR_SOURCE_WRITE));
118 DCHECK(posted); 121 DCHECK(posted);
119 122
120 async_in_progress_ = true; 123 async_in_progress_ = true;
121 return ERR_IO_PENDING; 124 return ERR_IO_PENDING;
122 } 125 }
123 126
124 int FileStream::Context::WriteSync(const char* in_buf, int buf_len) { 127 int FileStream::Context::WriteSync(const char* in_buf, int buf_len) {
125 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); 128 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf);
126 int64 result = WriteFileImpl(buf, buf_len); 129 IOResult result = WriteFileImpl(buf, buf_len);
127 CheckForIOError(&result, FILE_ERROR_SOURCE_WRITE); 130 RecordError(result, FILE_ERROR_SOURCE_WRITE);
128 return result; 131 return result.result;
129 } 132 }
130 133
131 int FileStream::Context::Truncate(int64 bytes) { 134 int FileStream::Context::Truncate(int64 bytes) {
132 int result = ftruncate(file_, bytes); 135 if (ftruncate(file_, bytes) != 0) {
133 if (result == 0) 136 IOResult result = IOResult::FromOSError(errno);
134 return bytes; 137 RecordError(result, FILE_ERROR_SOURCE_SET_EOF);
138 return result.result;
139 }
135 140
136 return RecordAndMapError(errno, FILE_ERROR_SOURCE_SET_EOF); 141 return bytes;
137 } 142 }
138 143
139 int64 FileStream::Context::SeekFileImpl(Whence whence, int64 offset) { 144 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
145 int64 offset) {
140 off_t res = lseek(file_, static_cast<off_t>(offset), 146 off_t res = lseek(file_, static_cast<off_t>(offset),
141 static_cast<int>(whence)); 147 static_cast<int>(whence));
142 if (res == static_cast<off_t>(-1)) 148 if (res == static_cast<off_t>(-1))
143 return errno; 149 return IOResult::FromOSError(errno);
144 150
145 return res; 151 return IOResult(res, 0);
146 } 152 }
147 153
148 int64 FileStream::Context::FlushFileImpl() { 154 FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
149 ssize_t res = HANDLE_EINTR(fsync(file_)); 155 ssize_t res = HANDLE_EINTR(fsync(file_));
150 if (res == -1) 156 if (res == -1)
151 return errno; 157 return IOResult::FromOSError(errno);
152 158
153 return res; 159 return IOResult(res, 0);
154 } 160 }
155 161
156 int64 FileStream::Context::ReadFileImpl(scoped_refptr<IOBuffer> buf, 162 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
157 int buf_len) { 163 scoped_refptr<IOBuffer> buf,
164 int buf_len) {
158 // Loop in the case of getting interrupted by a signal. 165 // Loop in the case of getting interrupted by a signal.
159 ssize_t res = HANDLE_EINTR(read(file_, buf->data(), 166 ssize_t res = HANDLE_EINTR(read(file_, buf->data(),
160 static_cast<size_t>(buf_len))); 167 static_cast<size_t>(buf_len)));
161 if (res == -1) 168 if (res == -1)
162 return errno; 169 return IOResult::FromOSError(errno);
163 170
164 return res; 171 return IOResult(res, 0);
165 } 172 }
166 173
167 int64 FileStream::Context::WriteFileImpl(scoped_refptr<IOBuffer> buf, 174 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
168 int buf_len) { 175 scoped_refptr<IOBuffer> buf,
176 int buf_len) {
169 ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len)); 177 ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len));
170 if (res == -1) 178 if (res == -1)
171 return errno; 179 return IOResult::FromOSError(errno);
172 180
173 return res; 181 return IOResult(res, 0);
174 } 182 }
175 183
176 } // namespace net 184 } // namespace net
OLDNEW
« no previous file with comments | « net/base/file_stream_context.cc ('k') | net/base/file_stream_context_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698