OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.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/callback.h" | 17 #include "base/callback.h" |
18 #include "base/eintr_wrapper.h" | 18 #include "base/eintr_wrapper.h" |
19 #include "base/file_path.h" | 19 #include "base/file_path.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
22 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
23 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 24 #include "base/threading/worker_pool.h" |
24 #include "base/waitable_event.h" | 25 #include "base/waitable_event.h" |
25 #include "base/worker_pool.h" | |
26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
27 | 27 |
28 // We cast back and forth, so make sure it's the size we're expecting. | 28 // We cast back and forth, so make sure it's the size we're expecting. |
29 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 29 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
30 | 30 |
31 // Make sure our Whence mappings match the system headers. | 31 // Make sure our Whence mappings match the system headers. |
32 COMPILE_ASSERT(net::FROM_BEGIN == SEEK_SET && | 32 COMPILE_ASSERT(net::FROM_BEGIN == SEEK_SET && |
33 net::FROM_CURRENT == SEEK_CUR && | 33 net::FROM_CURRENT == SEEK_CUR && |
34 net::FROM_END == SEEK_END, whence_matches_system); | 34 net::FROM_END == SEEK_END, whence_matches_system); |
35 | 35 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 243 } |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 void FileStream::AsyncContext::InitiateAsyncRead( | 247 void FileStream::AsyncContext::InitiateAsyncRead( |
248 base::PlatformFile file, char* buf, int buf_len, | 248 base::PlatformFile file, char* buf, int buf_len, |
249 CompletionCallback* callback) { | 249 CompletionCallback* callback) { |
250 DCHECK(!callback_); | 250 DCHECK(!callback_); |
251 callback_ = callback; | 251 callback_ = callback; |
252 | 252 |
253 WorkerPool::PostTask(FROM_HERE, | 253 base::WorkerPool::PostTask(FROM_HERE, |
254 new BackgroundReadTask( | 254 new BackgroundReadTask( |
255 file, buf, buf_len, | 255 file, buf, buf_len, |
256 &background_io_completed_callback_), | 256 &background_io_completed_callback_), |
257 true /* task_is_slow */); | 257 true /* task_is_slow */); |
258 } | 258 } |
259 | 259 |
260 void FileStream::AsyncContext::InitiateAsyncWrite( | 260 void FileStream::AsyncContext::InitiateAsyncWrite( |
261 base::PlatformFile file, const char* buf, int buf_len, | 261 base::PlatformFile file, const char* buf, int buf_len, |
262 CompletionCallback* callback) { | 262 CompletionCallback* callback) { |
263 DCHECK(!callback_); | 263 DCHECK(!callback_); |
264 callback_ = callback; | 264 callback_ = callback; |
265 | 265 |
266 WorkerPool::PostTask(FROM_HERE, | 266 base::WorkerPool::PostTask(FROM_HERE, |
267 new BackgroundWriteTask( | 267 new BackgroundWriteTask( |
268 file, buf, buf_len, | 268 file, buf, buf_len, |
269 &background_io_completed_callback_), | 269 &background_io_completed_callback_), |
270 true /* task_is_slow */); | 270 true /* task_is_slow */); |
271 } | 271 } |
272 | 272 |
273 void FileStream::AsyncContext::OnBackgroundIOCompleted(int result) { | 273 void FileStream::AsyncContext::OnBackgroundIOCompleted(int result) { |
274 result_ = result; | 274 result_ = result; |
275 message_loop_task_ = new CancelableCallbackTask( | 275 message_loop_task_ = new CancelableCallbackTask( |
276 NewCallback(this, &AsyncContext::RunAsynchronousCallback)); | 276 NewCallback(this, &AsyncContext::RunAsynchronousCallback)); |
277 message_loop_->PostTask(FROM_HERE, message_loop_task_); | 277 message_loop_->PostTask(FROM_HERE, message_loop_task_); |
278 background_io_completed_.Signal(); | 278 background_io_completed_.Signal(); |
279 } | 279 } |
280 | 280 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 int64 seek_position = Seek(FROM_BEGIN, bytes); | 472 int64 seek_position = Seek(FROM_BEGIN, bytes); |
473 if (seek_position != bytes) | 473 if (seek_position != bytes) |
474 return ERR_UNEXPECTED; | 474 return ERR_UNEXPECTED; |
475 | 475 |
476 // And truncate the file. | 476 // And truncate the file. |
477 int result = ftruncate(file_, bytes); | 477 int result = ftruncate(file_, bytes); |
478 return result == 0 ? seek_position : MapErrorCode(errno); | 478 return result == 0 ? seek_position : MapErrorCode(errno); |
479 } | 479 } |
480 | 480 |
481 } // namespace net | 481 } // namespace net |
OLD | NEW |