OLD | NEW |
---|---|
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.h" | 8 #include "net/base/file_stream_posix.h" |
9 | 9 |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <fcntl.h> | 12 #include <fcntl.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #include <errno.h> | 14 #include <errno.h> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
19 #include "base/callback.h" | 19 #include "base/callback.h" |
20 #include "base/eintr_wrapper.h" | 20 #include "base/eintr_wrapper.h" |
21 #include "base/file_path.h" | 21 #include "base/file_path.h" |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/memory/ref_counted.h" | |
23 #include "base/message_loop.h" | 24 #include "base/message_loop.h" |
24 #include "base/metrics/histogram.h" | 25 #include "base/metrics/histogram.h" |
25 #include "base/string_util.h" | 26 #include "base/string_util.h" |
27 #include "base/task_runner_util.h" | |
26 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
27 #include "base/threading/worker_pool.h" | 29 #include "base/threading/worker_pool.h" |
28 #include "base/synchronization/waitable_event.h" | |
29 #include "net/base/file_stream_metrics.h" | |
30 #include "net/base/file_stream_net_log_parameters.h" | 30 #include "net/base/file_stream_net_log_parameters.h" |
31 #include "net/base/io_buffer.h" | 31 #include "net/base/io_buffer.h" |
32 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
33 | 33 |
34 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
35 // Android's bionic libc only supports the LFS transitional API. | 35 // Android's bionic libc only supports the LFS transitional API. |
36 #define off_t off64_t | 36 #define off_t off64_t |
37 #define lseek lseek64 | 37 #define lseek lseek64 |
38 #define stat stat64 | 38 #define stat stat64 |
39 #define fstat fstat64 | 39 #define fstat fstat64 |
40 #endif | 40 #endif |
41 | 41 |
42 namespace net { | 42 namespace net { |
43 | 43 |
44 // We cast back and forth, so make sure it's the size we're expecting. | 44 // We cast back and forth, so make sure it's the size we're expecting. |
45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
46 | 46 |
47 // Make sure our Whence mappings match the system headers. | 47 // Make sure our Whence mappings match the system headers. |
48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | 48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && |
49 FROM_CURRENT == SEEK_CUR && | 49 FROM_CURRENT == SEEK_CUR && |
50 FROM_END == SEEK_END, whence_matches_system); | 50 FROM_END == SEEK_END, whence_matches_system); |
51 | 51 |
52 namespace { | 52 FileStream::AsyncContext::AsyncContext(const BoundNetLog& bound_net_log) |
53 | 53 : file_(base::kInvalidPlatformFileValue), |
54 int RecordAndMapError(int error, | 54 record_uma_(false), |
55 FileErrorSource source, | 55 async_in_progress_(false), |
56 bool record_uma, | 56 destroyed_(false), |
57 const net::BoundNetLog& bound_net_log) { | 57 bound_net_log_(bound_net_log) { |
58 net::Error net_error = MapSystemError(error); | 58 } |
59 | 59 |
60 bound_net_log.AddEvent( | 60 FileStream::AsyncContext::AsyncContext(base::PlatformFile file, |
61 net::NetLog::TYPE_FILE_STREAM_ERROR, | 61 const BoundNetLog& bound_net_log, |
62 base::Bind(&NetLogFileStreamErrorCallback, | 62 int /* open_flags */) |
63 source, error, net_error)); | 63 : file_(file), |
64 | 64 record_uma_(false), |
65 RecordFileError(error, source, record_uma); | 65 async_in_progress_(false), |
66 | 66 destroyed_(false), |
67 return net_error; | 67 bound_net_log_(bound_net_log) { |
68 } | 68 } |
69 | 69 |
70 // Opens a file with some network logging. | 70 void FileStream::AsyncContext::Destroy() { |
willchan no longer on Chromium
2012/08/27 06:59:10
Oops, earlier I neglected to mention how this bit
| |
71 // The opened file and the result code are written to |file| and |result|. | 71 destroyed_ = true; |
72 void OpenFile(const FilePath& path, | 72 if (!async_in_progress_) |
73 int open_flags, | 73 DeleteAbandoned(); |
74 bool record_uma, | 74 } |
75 base::PlatformFile* file, | 75 |
76 int* result, | 76 void FileStream::AsyncContext::OpenAsync( |
77 const net::BoundNetLog& bound_net_log) { | 77 const FilePath& path, |
78 std::string file_name = path.AsUTF8Unsafe(); | 78 int open_flags, |
79 bound_net_log.BeginEvent( | 79 const CompletionCallback& callback) { |
80 net::NetLog::TYPE_FILE_STREAM_OPEN, | 80 DCHECK(!async_in_progress_); |
81 NetLog::StringCallback("file_name", &file_name)); | 81 |
82 | 82 BeginOpenEvent(path); |
83 *result = OK; | 83 |
84 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); | 84 const bool posted = base::PostTaskAndReplyWithResult( |
85 if (*file == base::kInvalidPlatformFileValue) { | 85 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), |
86 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | 86 FROM_HERE, |
87 *result = RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma, | 87 base::Bind(&AsyncContext::OpenFileImpl, |
88 bound_net_log); | 88 base::Unretained(this), path, open_flags), |
89 } | 89 base::Bind(&AsyncContext::OnOpenCompleted, |
90 } | 90 base::Unretained(this), callback)); |
91 | 91 DCHECK(posted); |
92 // Opens a file using OpenFile() and then signals the completion. | 92 |
93 void OpenFileAndSignal(const FilePath& path, | 93 async_in_progress_ = true; |
94 int open_flags, | 94 } |
95 bool record_uma, | 95 |
96 base::PlatformFile* file, | 96 int FileStream::AsyncContext::OpenSync(const FilePath& path, int open_flags) { |
97 int* result, | 97 BeginOpenEvent(path); |
98 base::WaitableEvent* on_io_complete, | 98 int result = OpenFileImpl(path, open_flags); |
99 const net::BoundNetLog& bound_net_log) { | 99 CheckForOpenError(&result); |
100 OpenFile(path, open_flags, record_uma, file, result, bound_net_log); | 100 return result; |
101 on_io_complete->Signal(); | 101 } |
102 } | 102 |
103 | 103 void FileStream::AsyncContext::CloseAsync(const CompletionCallback& callback) { |
104 // Closes a file with some network logging. | 104 DCHECK(!async_in_progress_); |
105 void CloseFile(base::PlatformFile file, | 105 |
106 const net::BoundNetLog& bound_net_log) { | 106 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); |
107 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); | 107 |
108 if (file == base::kInvalidPlatformFileValue) | 108 if (file_ == base::kInvalidPlatformFileValue) { |
109 return; | 109 MessageLoop::current()->PostTask( |
110 | 110 FROM_HERE, |
111 if (!base::ClosePlatformFile(file)) | 111 base::Bind(&AsyncContext::OnAsyncCompleted<int>, |
112 NOTREACHED(); | 112 base::Unretained(this), callback, OK)); |
113 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | 113 } else { |
114 } | 114 const bool posted = base::WorkerPool::PostTaskAndReply( |
115 | 115 FROM_HERE, |
116 // Closes a file with CloseFile() and signals the completion. | 116 base::Bind(&AsyncContext::CloseFileImpl, |
117 void CloseFileAndSignal(base::PlatformFile* file, | 117 base::Unretained(this)), |
118 base::WaitableEvent* on_io_complete, | 118 base::Bind(&AsyncContext::OnCloseCompleted, |
119 const net::BoundNetLog& bound_net_log) { | 119 base::Unretained(this), callback), |
120 CloseFile(*file, bound_net_log); | 120 true /* task_is_slow */); |
121 *file = base::kInvalidPlatformFileValue; | 121 DCHECK(posted); |
122 on_io_complete->Signal(); | 122 |
123 } | 123 async_in_progress_ = true; |
124 | 124 } |
125 // Adjusts the position from where the data is read. | 125 } |
126 void SeekFile(base::PlatformFile file, | 126 |
127 Whence whence, | 127 void FileStream::AsyncContext::CloseSync() { |
128 int64 offset, | 128 DCHECK(!async_in_progress_); |
129 int64* result, | 129 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); |
130 bool record_uma, | 130 if (file_ != base::kInvalidPlatformFileValue) { |
131 const net::BoundNetLog& bound_net_log) { | 131 CloseFileImpl(); |
132 off_t res = lseek(file, static_cast<off_t>(offset), | 132 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
133 static_cast<int>(whence)); | 133 } |
134 if (res == static_cast<off_t>(-1)) { | 134 } |
135 *result = RecordAndMapError(errno, | 135 |
136 FILE_ERROR_SOURCE_SEEK, | 136 void FileStream::AsyncContext::SeekAsync( |
137 record_uma, | 137 Whence whence, |
138 bound_net_log); | 138 int64 offset, |
139 return; | 139 const Int64CompletionCallback& callback) { |
140 } | 140 DCHECK(!async_in_progress_); |
141 *result = res; | 141 |
142 } | 142 const bool posted = base::PostTaskAndReplyWithResult( |
143 | 143 base::WorkerPool::GetTaskRunner(true /* task is slow */), |
144 // Seeks a file by calling SeekSync() and signals the completion. | 144 FROM_HERE, |
145 void SeekFileAndSignal(base::PlatformFile file, | 145 base::Bind(&AsyncContext::SeekFileImpl, |
146 Whence whence, | 146 base::Unretained(this), whence, offset), |
147 int64 offset, | 147 base::Bind(&AsyncContext::OnIOCompleted<int64>, |
148 int64* result, | 148 base::Unretained(this), callback, FILE_ERROR_SOURCE_SEEK)); |
149 bool record_uma, | 149 DCHECK(posted); |
150 base::WaitableEvent* on_io_complete, | 150 |
151 const net::BoundNetLog& bound_net_log) { | 151 async_in_progress_ = true; |
152 SeekFile(file, whence, offset, result, record_uma, bound_net_log); | 152 } |
153 on_io_complete->Signal(); | 153 |
154 } | 154 int64 FileStream::AsyncContext::SeekSync(Whence whence, int64 offset) { |
155 | 155 off_t result = SeekFileImpl(whence, offset); |
156 // ReadFile() is a simple wrapper around read() that handles EINTR signals and | 156 CheckForIOError(&result, FILE_ERROR_SOURCE_SEEK); |
157 // calls MapSystemError() to map errno to net error codes. | 157 return result; |
158 void ReadFile(base::PlatformFile file, | 158 } |
159 char* buf, | 159 |
160 int buf_len, | 160 int64 FileStream::AsyncContext::GetFileSize() { |
161 bool record_uma, | 161 struct stat info; |
162 int* result, | 162 if (fstat(file_, &info) != 0) |
163 const net::BoundNetLog& bound_net_log) { | 163 return RecordAndMapError(errno, FILE_ERROR_SOURCE_GET_SIZE); |
164 base::ThreadRestrictions::AssertIOAllowed(); | 164 |
165 // read(..., 0) returns 0 to indicate end-of-file. | 165 return static_cast<int64>(info.st_size); |
166 | 166 } |
167 // Loop in the case of getting interrupted by a signal. | 167 |
168 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); | 168 int FileStream::AsyncContext::ReadAsync( |
169 if (res == -1) { | 169 IOBuffer* in_buf, |
170 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_READ, | 170 int buf_len, |
171 record_uma, bound_net_log); | 171 const CompletionCallback& callback) { |
172 } | 172 DCHECK(!async_in_progress_); |
173 *result = res; | 173 |
174 } | 174 scoped_refptr<IOBuffer> buf = in_buf; |
175 | 175 const bool posted = base::PostTaskAndReplyWithResult( |
176 // Reads a file using ReadFile() and signals the completion. | 176 base::WorkerPool::GetTaskRunner(true /* task is slow */), |
177 void ReadFileAndSignal(base::PlatformFile file, | 177 FROM_HERE, |
178 scoped_refptr<IOBuffer> buf, | 178 base::Bind(&AsyncContext::ReadFileImpl, |
179 int buf_len, | 179 base::Unretained(this), buf, buf_len), |
180 bool record_uma, | 180 base::Bind(&AsyncContext::OnIOCompleted<int>, |
181 int* result, | 181 base::Unretained(this), callback, FILE_ERROR_SOURCE_READ)); |
182 base::WaitableEvent* on_io_complete, | 182 DCHECK(posted); |
183 const net::BoundNetLog& bound_net_log) { | 183 |
184 ReadFile(file, buf->data(), buf_len, record_uma, result, bound_net_log); | 184 async_in_progress_ = true; |
185 on_io_complete->Signal(); | 185 return ERR_IO_PENDING; |
186 } | 186 } |
187 | 187 |
188 // WriteFile() is a simple wrapper around write() that handles EINTR signals and | 188 int FileStream::AsyncContext::ReadSync(char* in_buf, int buf_len) { |
189 // calls MapSystemError() to map errno to net error codes. It tries to write to | 189 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); |
190 // completion. | 190 int result = ReadFileImpl(buf, buf_len); |
191 void WriteFile(base::PlatformFile file, | 191 CheckForIOError(&result, FILE_ERROR_SOURCE_READ); |
192 const char* buf, | 192 return result; |
193 int buf_len, | 193 } |
194 bool record_uma, | 194 |
195 int* result, | 195 int FileStream::AsyncContext::WriteAsync( |
196 const net::BoundNetLog& bound_net_log) { | 196 IOBuffer* in_buf, |
197 base::ThreadRestrictions::AssertIOAllowed(); | 197 int buf_len, |
198 | 198 const CompletionCallback& callback) { |
199 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); | 199 DCHECK(!async_in_progress_); |
200 if (res == -1) { | 200 |
201 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE, record_uma, | 201 scoped_refptr<IOBuffer> buf = in_buf; |
202 bound_net_log); | 202 const bool posted = base::PostTaskAndReplyWithResult( |
203 } | 203 base::WorkerPool::GetTaskRunner(true /* task is slow */), |
204 *result = res; | 204 FROM_HERE, |
205 } | 205 base::Bind(&AsyncContext::WriteFileImpl, |
206 | 206 base::Unretained(this), buf, buf_len), |
207 // Writes a file using WriteFile() and signals the completion. | 207 base::Bind(&AsyncContext::OnIOCompleted<int>, |
208 void WriteFileAndSignal(base::PlatformFile file, | 208 base::Unretained(this), callback, FILE_ERROR_SOURCE_WRITE)); |
209 scoped_refptr<IOBuffer> buf, | 209 DCHECK(posted); |
210 int buf_len, | 210 |
211 bool record_uma, | 211 async_in_progress_ = true; |
212 int* result, | 212 return ERR_IO_PENDING; |
213 base::WaitableEvent* on_io_complete, | 213 } |
214 const net::BoundNetLog& bound_net_log) { | 214 |
215 WriteFile(file, buf->data(), buf_len, record_uma, result, bound_net_log); | 215 int FileStream::AsyncContext::WriteSync(const char* in_buf, int buf_len) { |
216 on_io_complete->Signal(); | 216 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); |
217 } | 217 int result = WriteFileImpl(buf, buf_len); |
218 | 218 CheckForIOError(&result, FILE_ERROR_SOURCE_WRITE); |
219 // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and | 219 return result; |
220 // calls MapSystemError() to map errno to net error codes. It tries to flush to | 220 } |
221 // completion. | 221 |
222 int FlushFile(base::PlatformFile file, | 222 int FileStream::AsyncContext::Flush() { |
223 bool record_uma, | 223 ssize_t res = HANDLE_EINTR(fsync(file_)); |
224 const net::BoundNetLog& bound_net_log) { | 224 if (res == -1) |
225 base::ThreadRestrictions::AssertIOAllowed(); | 225 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH); |
226 ssize_t res = HANDLE_EINTR(fsync(file)); | |
227 if (res == -1) { | |
228 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma, | |
229 bound_net_log); | |
230 } | |
231 return res; | 226 return res; |
232 } | 227 } |
233 | 228 |
234 // Called when Read(), Write() or Seek() is completed. | 229 int FileStream::AsyncContext::Truncate(int64 bytes) { |
235 // |result| contains the result or a network error code. | |
236 template <typename R> | |
237 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream, | |
238 const base::Callback<void(R)>& callback, | |
239 R* result) { | |
240 if (!stream.get()) | |
241 return; | |
242 | |
243 // Reset this before Run() as Run() may issue a new async operation. | |
244 stream->ResetOnIOComplete(); | |
245 callback.Run(*result); | |
246 } | |
247 | |
248 } // namespace | |
249 | |
250 // FileStreamPosix ------------------------------------------------------------ | |
251 | |
252 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) | |
253 : file_(base::kInvalidPlatformFileValue), | |
254 open_flags_(0), | |
255 auto_closed_(true), | |
256 record_uma_(false), | |
257 bound_net_log_(net::BoundNetLog::Make(net_log, | |
258 net::NetLog::SOURCE_FILESTREAM)), | |
259 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
260 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
261 } | |
262 | |
263 FileStreamPosix::FileStreamPosix( | |
264 base::PlatformFile file, int flags, net::NetLog* net_log) | |
265 : file_(file), | |
266 open_flags_(flags), | |
267 auto_closed_(false), | |
268 record_uma_(false), | |
269 bound_net_log_(net::BoundNetLog::Make(net_log, | |
270 net::NetLog::SOURCE_FILESTREAM)), | |
271 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
272 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
273 } | |
274 | |
275 FileStreamPosix::~FileStreamPosix() { | |
276 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
277 // Block until the last open/close/read/write operation is complete. | |
278 // TODO(satorux): Ideally we should not block. crbug.com/115067 | |
279 WaitForIOCompletion(); | |
280 } | |
281 | |
282 if (auto_closed_) { | |
283 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
284 // Close the file in the background. | |
285 if (IsOpen()) { | |
286 const bool posted = base::WorkerPool::PostTask( | |
287 FROM_HERE, | |
288 base::Bind(&CloseFile, file_, bound_net_log_), | |
289 true /* task_is_slow */); | |
290 DCHECK(posted); | |
291 } | |
292 } else { | |
293 CloseSync(); | |
294 } | |
295 } | |
296 | |
297 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
298 } | |
299 | |
300 void FileStreamPosix::Close(const CompletionCallback& callback) { | |
301 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
302 | |
303 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
304 DCHECK(!on_io_complete_.get()); | |
305 on_io_complete_.reset(new base::WaitableEvent( | |
306 false /* manual_reset */, false /* initially_signaled */)); | |
307 | |
308 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
309 // destructor ensures that the close operation is complete with | |
310 // WaitForIOCompletion(). See also the destructor. | |
311 const bool posted = base::WorkerPool::PostTaskAndReply( | |
312 FROM_HERE, | |
313 base::Bind(&CloseFileAndSignal, &file_, on_io_complete_.get(), | |
314 bound_net_log_), | |
315 base::Bind(&FileStreamPosix::OnClosed, | |
316 weak_ptr_factory_.GetWeakPtr(), | |
317 callback), | |
318 true /* task_is_slow */); | |
319 | |
320 DCHECK(posted); | |
321 } | |
322 | |
323 void FileStreamPosix::CloseSync() { | |
324 // TODO(satorux): Replace the following async stuff with a | |
325 // DCHECK(open_flags & ASYNC) once once all async clients are migrated to | |
326 // use Close(). crbug.com/114783 | |
327 | |
328 // Abort any existing asynchronous operations. | |
329 weak_ptr_factory_.InvalidateWeakPtrs(); | |
330 // Block until the last open/read/write operation is complete. | |
331 // TODO(satorux): Ideally we should not block. crbug.com/115067 | |
332 WaitForIOCompletion(); | |
333 | |
334 CloseFile(file_, bound_net_log_); | |
335 file_ = base::kInvalidPlatformFileValue; | |
336 } | |
337 | |
338 int FileStreamPosix::Open(const FilePath& path, int open_flags, | |
339 const CompletionCallback& callback) { | |
340 if (IsOpen()) { | |
341 DLOG(FATAL) << "File is already open!"; | |
342 return ERR_UNEXPECTED; | |
343 } | |
344 | |
345 open_flags_ = open_flags; | |
346 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
347 | |
348 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
349 DCHECK(!on_io_complete_.get()); | |
350 on_io_complete_.reset(new base::WaitableEvent( | |
351 false /* manual_reset */, false /* initially_signaled */)); | |
352 | |
353 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
354 // destructor ensures that the open operation is complete with | |
355 // WaitForIOCompletion(). See also the destructor. | |
356 int* result = new int(OK); | |
357 const bool posted = base::WorkerPool::PostTaskAndReply( | |
358 FROM_HERE, | |
359 base::Bind(&OpenFileAndSignal, | |
360 path, open_flags, record_uma_, &file_, result, | |
361 on_io_complete_.get(), bound_net_log_), | |
362 base::Bind(&OnIOComplete<int>, weak_ptr_factory_.GetWeakPtr(), | |
363 callback, base::Owned(result)), | |
364 true /* task_is_slow */); | |
365 DCHECK(posted); | |
366 return ERR_IO_PENDING; | |
367 } | |
368 | |
369 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { | |
370 if (IsOpen()) { | |
371 DLOG(FATAL) << "File is already open!"; | |
372 return ERR_UNEXPECTED; | |
373 } | |
374 | |
375 open_flags_ = open_flags; | |
376 // TODO(satorux): Put a DCHECK once once all async clients are migrated | |
377 // to use Open(). crbug.com/114783 | |
378 // | |
379 // DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
380 | |
381 int result = OK; | |
382 OpenFile(path, open_flags_, record_uma_, &file_, &result, bound_net_log_); | |
383 return result; | |
384 } | |
385 | |
386 bool FileStreamPosix::IsOpen() const { | |
387 return file_ != base::kInvalidPlatformFileValue; | |
388 } | |
389 | |
390 int FileStreamPosix::Seek(Whence whence, int64 offset, | |
391 const Int64CompletionCallback& callback) { | |
392 if (!IsOpen()) | |
393 return ERR_UNEXPECTED; | |
394 | |
395 // Make sure we're async and we have no other in-flight async operations. | |
396 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
397 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
398 DCHECK(!on_io_complete_.get()); | |
399 | |
400 on_io_complete_.reset(new base::WaitableEvent( | |
401 false /* manual_reset */, false /* initially_signaled */)); | |
402 | |
403 int64* result = new int64(-1); | |
404 const bool posted = base::WorkerPool::PostTaskAndReply( | |
405 FROM_HERE, | |
406 base::Bind(&SeekFileAndSignal, file_, whence, offset, result, | |
407 record_uma_, on_io_complete_.get(), bound_net_log_), | |
408 base::Bind(&OnIOComplete<int64>, | |
409 weak_ptr_factory_.GetWeakPtr(), | |
410 callback, base::Owned(result)), | |
411 true /* task is slow */); | |
412 DCHECK(posted); | |
413 return ERR_IO_PENDING; | |
414 } | |
415 | |
416 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) { | |
417 base::ThreadRestrictions::AssertIOAllowed(); | |
418 | |
419 if (!IsOpen()) | |
420 return ERR_UNEXPECTED; | |
421 | |
422 // If we're in async, make sure we don't have a request in flight. | |
423 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC) || | |
424 !on_io_complete_.get()); | |
425 | |
426 off_t result = -1; | |
427 SeekFile(file_, whence, offset, &result, record_uma_, bound_net_log_); | |
428 return result; | |
429 } | |
430 | |
431 int64 FileStreamPosix::Available() { | |
432 base::ThreadRestrictions::AssertIOAllowed(); | |
433 | |
434 if (!IsOpen()) | |
435 return ERR_UNEXPECTED; | |
436 | |
437 int64 cur_pos = SeekSync(FROM_CURRENT, 0); | |
438 if (cur_pos < 0) | |
439 return cur_pos; | |
440 | |
441 struct stat info; | |
442 if (fstat(file_, &info) != 0) { | |
443 return RecordAndMapError(errno, | |
444 FILE_ERROR_SOURCE_GET_SIZE, | |
445 record_uma_, | |
446 bound_net_log_); | |
447 } | |
448 | |
449 int64 size = static_cast<int64>(info.st_size); | |
450 DCHECK_GT(size, cur_pos); | |
451 | |
452 return size - cur_pos; | |
453 } | |
454 | |
455 int FileStreamPosix::Read( | |
456 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | |
457 if (!IsOpen()) | |
458 return ERR_UNEXPECTED; | |
459 | |
460 // read(..., 0) will return 0, which indicates end-of-file. | |
461 DCHECK_GT(buf_len, 0); | |
462 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | |
463 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
464 | |
465 // Make sure we don't have a request in flight. | |
466 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
467 DCHECK(!on_io_complete_.get()); | |
468 | |
469 on_io_complete_.reset(new base::WaitableEvent( | |
470 false /* manual_reset */, false /* initially_signaled */)); | |
471 | |
472 int* result = new int(OK); | |
473 scoped_refptr<IOBuffer> buf = in_buf; | |
474 const bool posted = base::WorkerPool::PostTaskAndReply( | |
475 FROM_HERE, | |
476 base::Bind(&ReadFileAndSignal, file_, buf, buf_len, | |
477 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
478 base::Bind(&OnIOComplete<int>, | |
479 weak_ptr_factory_.GetWeakPtr(), | |
480 callback, base::Owned(result)), | |
481 true /* task is slow */); | |
482 DCHECK(posted); | |
483 return ERR_IO_PENDING; | |
484 } | |
485 | |
486 int FileStreamPosix::ReadSync(char* buf, int buf_len) { | |
487 if (!IsOpen()) | |
488 return ERR_UNEXPECTED; | |
489 | |
490 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
491 // read(..., 0) will return 0, which indicates end-of-file. | |
492 DCHECK_GT(buf_len, 0); | |
493 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | |
494 | |
495 int result = OK; | |
496 ReadFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
497 return result; | |
498 } | |
499 | |
500 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { | |
501 int to_read = buf_len; | |
502 int bytes_total = 0; | |
503 | |
504 do { | |
505 int bytes_read = ReadSync(buf, to_read); | |
506 if (bytes_read <= 0) { | |
507 if (bytes_total == 0) | |
508 return bytes_read; | |
509 | |
510 return bytes_total; | |
511 } | |
512 | |
513 bytes_total += bytes_read; | |
514 buf += bytes_read; | |
515 to_read -= bytes_read; | |
516 } while (bytes_total < buf_len); | |
517 | |
518 return bytes_total; | |
519 } | |
520 | |
521 int FileStreamPosix::Write( | |
522 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | |
523 if (!IsOpen()) | |
524 return ERR_UNEXPECTED; | |
525 | |
526 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
527 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
528 // write(..., 0) will return 0, which indicates end-of-file. | |
529 DCHECK_GT(buf_len, 0); | |
530 | |
531 // Make sure we don't have a request in flight. | |
532 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
533 DCHECK(!on_io_complete_.get()); | |
534 on_io_complete_.reset(new base::WaitableEvent( | |
535 false /* manual_reset */, false /* initially_signaled */)); | |
536 | |
537 int* result = new int(OK); | |
538 scoped_refptr<IOBuffer> buf = in_buf; | |
539 const bool posted = base::WorkerPool::PostTaskAndReply( | |
540 FROM_HERE, | |
541 base::Bind(&WriteFileAndSignal, file_, buf, buf_len, | |
542 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
543 base::Bind(&OnIOComplete<int>, | |
544 weak_ptr_factory_.GetWeakPtr(), | |
545 callback, base::Owned(result)), | |
546 true /* task is slow */); | |
547 DCHECK(posted); | |
548 return ERR_IO_PENDING; | |
549 } | |
550 | |
551 int FileStreamPosix::WriteSync( | |
552 const char* buf, int buf_len) { | |
553 if (!IsOpen()) | |
554 return ERR_UNEXPECTED; | |
555 | |
556 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
557 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
558 // write(..., 0) will return 0, which indicates end-of-file. | |
559 DCHECK_GT(buf_len, 0); | |
560 | |
561 int result = OK; | |
562 WriteFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
563 return result; | |
564 } | |
565 | |
566 int64 FileStreamPosix::Truncate(int64 bytes) { | |
567 base::ThreadRestrictions::AssertIOAllowed(); | |
568 | |
569 if (!IsOpen()) | |
570 return ERR_UNEXPECTED; | |
571 | |
572 // We'd better be open for writing. | |
573 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
574 | |
575 // Seek to the position to truncate from. | |
576 int64 seek_position = SeekSync(FROM_BEGIN, bytes); | |
577 if (seek_position != bytes) | |
578 return ERR_UNEXPECTED; | |
579 | |
580 // And truncate the file. | |
581 int result = ftruncate(file_, bytes); | 230 int result = ftruncate(file_, bytes); |
582 if (result == 0) | 231 if (result == 0) |
583 return seek_position; | 232 return bytes; |
584 | 233 |
585 return RecordAndMapError(errno, | 234 return RecordAndMapError(errno, FILE_ERROR_SOURCE_SET_EOF); |
586 FILE_ERROR_SOURCE_SET_EOF, | 235 } |
587 record_uma_, | 236 |
588 bound_net_log_); | 237 int FileStream::AsyncContext::RecordAndMapError(int error, |
589 } | 238 FileErrorSource source) { |
590 | 239 // The following check is against incorrect use or bug. File descriptor |
591 int FileStreamPosix::Flush() { | 240 // shouldn't ever be closed outside of FileStream while it still tries to do |
592 if (!IsOpen()) | 241 // something with it. |
593 return ERR_UNEXPECTED; | 242 DCHECK(error != EBADF); |
594 | 243 net::Error net_error = MapSystemError(error); |
595 return FlushFile(file_, record_uma_, bound_net_log_); | 244 |
596 } | 245 if (!destroyed_) { |
597 | 246 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_ERROR, |
598 void FileStreamPosix::EnableErrorStatistics() { | 247 base::Bind(&NetLogFileStreamErrorCallback, |
599 record_uma_ = true; | 248 source, error, net_error)); |
600 } | 249 } |
601 | 250 RecordFileError(error, source, record_uma_); |
602 void FileStreamPosix::SetBoundNetLogSource( | 251 return net_error; |
603 const net::BoundNetLog& owner_bound_net_log) { | 252 } |
604 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | 253 |
605 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | 254 void FileStream::AsyncContext::BeginOpenEvent(const FilePath& path) { |
606 // Both |BoundNetLog|s are invalid. | 255 std::string file_name = path.AsUTF8Unsafe(); |
607 return; | 256 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, |
608 } | 257 NetLog::StringCallback("file_name", &file_name)); |
609 | 258 } |
610 // Should never connect to itself. | 259 |
611 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); | 260 int FileStream::AsyncContext::OpenFileImpl(const FilePath& path, |
612 | 261 int open_flags) { |
613 bound_net_log_.AddEvent( | 262 if (destroyed_) |
willchan no longer on Chromium
2012/08/27 06:24:06
Remove this. It's arguably a benign race, but as H
| |
614 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | 263 return OK; |
615 owner_bound_net_log.source().ToEventParametersCallback()); | 264 |
616 | 265 file_ = base::CreatePlatformFile(path, open_flags, NULL, NULL); |
617 owner_bound_net_log.AddEvent( | 266 if (file_ == base::kInvalidPlatformFileValue) |
618 net::NetLog::TYPE_FILE_STREAM_SOURCE, | 267 return errno; |
619 bound_net_log_.source().ToEventParametersCallback()); | 268 |
620 } | 269 return OK; |
621 | 270 } |
622 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { | 271 |
623 return file_; | 272 void FileStream::AsyncContext::CheckForOpenError(int* result) { |
624 } | 273 if (file_ == base::kInvalidPlatformFileValue) { |
625 | 274 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
626 void FileStreamPosix::ResetOnIOComplete() { | 275 *result = RecordAndMapError(*result, FILE_ERROR_SOURCE_OPEN); |
627 on_io_complete_.reset(); | 276 } |
628 weak_ptr_factory_.InvalidateWeakPtrs(); | 277 } |
629 } | 278 |
630 | 279 void FileStream::AsyncContext::OnOpenCompleted( |
631 void FileStreamPosix::OnClosed(const CompletionCallback& callback) { | 280 const CompletionCallback& callback, |
281 int result) { | |
282 CheckForOpenError(&result); | |
283 OnAsyncCompleted(callback, result); | |
284 } | |
285 | |
286 void FileStream::AsyncContext::CloseFileImpl() { | |
287 if (!base::ClosePlatformFile(file_)) | |
288 NOTREACHED(); | |
632 file_ = base::kInvalidPlatformFileValue; | 289 file_ = base::kInvalidPlatformFileValue; |
633 | 290 } |
634 // Reset this before Run() as Run() may issue a new async operation. | 291 |
635 ResetOnIOComplete(); | 292 void FileStream::AsyncContext::OnCloseCompleted( |
636 callback.Run(OK); | 293 const CompletionCallback& callback) { |
637 } | 294 if (!destroyed_) |
638 | 295 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
639 void FileStreamPosix::WaitForIOCompletion() { | 296 OnAsyncCompleted(callback, static_cast<int>(OK)); |
640 // http://crbug.com/115067 | 297 } |
641 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 298 |
642 if (on_io_complete_.get()) { | 299 int64 FileStream::AsyncContext::SeekFileImpl(Whence whence, int64 offset) { |
643 on_io_complete_->Wait(); | 300 base::ThreadRestrictions::AssertIOAllowed(); |
644 on_io_complete_.reset(); | 301 |
645 } | 302 // If context has been already destroyed nobody waits for operation results. |
646 } | 303 if (destroyed_) |
647 | 304 return 0; |
305 | |
306 off_t res = lseek(file_, static_cast<off_t>(offset), | |
307 static_cast<int>(whence)); | |
308 if (res == static_cast<off_t>(-1)) | |
309 return errno; | |
310 | |
311 return res; | |
312 } | |
313 | |
314 int FileStream::AsyncContext::ReadFileImpl(scoped_refptr<IOBuffer> buf, | |
315 int buf_len) { | |
316 base::ThreadRestrictions::AssertIOAllowed(); | |
317 | |
318 // If context has been already destroyed nobody waits for operation results. | |
319 if (destroyed_) | |
320 return OK; | |
321 | |
322 // Loop in the case of getting interrupted by a signal. | |
323 ssize_t res = HANDLE_EINTR(read(file_, buf->data(), | |
324 static_cast<size_t>(buf_len))); | |
325 if (res == -1) | |
326 return errno; | |
327 | |
328 return res; | |
329 } | |
330 | |
331 int FileStream::AsyncContext::WriteFileImpl(scoped_refptr<IOBuffer> buf, | |
332 int buf_len) { | |
333 base::ThreadRestrictions::AssertIOAllowed(); | |
334 | |
335 // If context has been already destroyed nobody waits for operation results. | |
336 if (destroyed_) | |
337 return OK; | |
338 | |
339 ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len)); | |
340 if (res == -1) | |
341 return errno; | |
342 | |
343 return res; | |
344 } | |
345 | |
346 template <typename R> | |
347 void FileStream::AsyncContext::CheckForIOError(R* result, | |
348 FileErrorSource source) { | |
349 if (*result < 0) | |
350 *result = RecordAndMapError(static_cast<int>(*result), source); | |
351 } | |
352 | |
353 template <typename R> | |
354 void FileStream::AsyncContext::OnIOCompleted( | |
355 const base::Callback<void(R)>& callback, | |
356 FileErrorSource source, | |
357 R result) { | |
358 CheckForIOError(&result, source); | |
359 OnAsyncCompleted(callback, result); | |
360 } | |
361 | |
362 template <typename R> | |
363 void FileStream::AsyncContext::OnAsyncCompleted( | |
364 const base::Callback<void(R)>& callback, | |
365 R result) { | |
366 if (destroyed_) { | |
willchan no longer on Chromium
2012/08/27 06:24:06
Change this to:
if (orphaned_) {
delete this;
}
| |
367 DeleteAbandoned(); | |
368 } else { | |
369 // Reset this before Run() as Run() may issue a new async operation. | |
370 async_in_progress_ = false; | |
371 callback.Run(result); | |
372 } | |
373 } | |
374 | |
375 void FileStream::AsyncContext::DeleteAbandoned() { | |
willchan no longer on Chromium
2012/08/27 06:24:06
One good thing about removing DeleteAbandoned is t
pivanof
2012/08/27 08:43:31
I don't follow. There are DCHECKs everywhere to en
willchan no longer on Chromium
2012/09/05 00:03:38
You're right, there should not be multiple async o
| |
376 if (file_ != base::kInvalidPlatformFileValue) { | |
377 const bool posted = base::WorkerPool::PostTask( | |
378 FROM_HERE, | |
379 // Context should be deleted after closing, thus Owned(). | |
380 base::Bind(&AsyncContext::CloseFileImpl, base::Owned(this)), | |
381 true /* task_is_slow */); | |
382 DCHECK(posted); | |
383 } else { | |
384 delete this; | |
385 } | |
386 } | |
387 | |
648 } // namespace net | 388 } // namespace net |
OLD | NEW |