| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/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/message_loop.h" | 23 #include "base/message_loop.h" |
| 24 #include "base/metrics/histogram.h" | 24 #include "base/metrics/histogram.h" |
| 25 #include "base/string_util.h" | 25 #include "base/string_util.h" |
| 26 #include "base/threading/thread_restrictions.h" | 26 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
| 28 #include "base/synchronization/waitable_event.h" | 28 #include "base/synchronization/waitable_event.h" |
| 29 #include "net/base/file_stream_metrics.h" | 29 #include "net/base/file_stream_metrics.h" |
| 30 #include "net/base/file_stream_net_log_parameters.h" |
| 30 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 31 | 32 |
| 32 #if defined(OS_ANDROID) | 33 #if defined(OS_ANDROID) |
| 33 // Android's bionic libc only supports the LFS transitional API. | 34 // Android's bionic libc only supports the LFS transitional API. |
| 34 #define off_t off64_t | 35 #define off_t off64_t |
| 35 #define lseek lseek64 | 36 #define lseek lseek64 |
| 36 #define stat stat64 | 37 #define stat stat64 |
| 37 #define fstat fstat64 | 38 #define fstat fstat64 |
| 38 #endif | 39 #endif |
| 39 | 40 |
| 40 namespace net { | 41 namespace net { |
| 41 | 42 |
| 42 // We cast back and forth, so make sure it's the size we're expecting. | 43 // We cast back and forth, so make sure it's the size we're expecting. |
| 43 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | 44 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); |
| 44 | 45 |
| 45 // Make sure our Whence mappings match the system headers. | 46 // Make sure our Whence mappings match the system headers. |
| 46 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | 47 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && |
| 47 FROM_CURRENT == SEEK_CUR && | 48 FROM_CURRENT == SEEK_CUR && |
| 48 FROM_END == SEEK_END, whence_matches_system); | 49 FROM_END == SEEK_END, whence_matches_system); |
| 49 | 50 |
| 50 namespace { | 51 namespace { |
| 51 | 52 |
| 52 int RecordAndMapError(int error, FileErrorSource source, bool record_uma) { | 53 int RecordAndMapError(int error, |
| 54 FileErrorSource source, |
| 55 bool record_uma, |
| 56 const net::BoundNetLog& log) { |
| 57 log.AddEvent(net::NetLog::TYPE_FILE_STREAM_ERROR, |
| 58 make_scoped_refptr( |
| 59 new FileStreamErrorParameters( |
| 60 GetFileErrorSourceName(source), |
| 61 error))); |
| 62 |
| 53 RecordFileError(error, source, record_uma); | 63 RecordFileError(error, source, record_uma); |
| 54 return MapSystemError(error); | 64 return MapSystemError(error); |
| 55 } | 65 } |
| 56 | 66 |
| 57 // ReadFile() is a simple wrapper around read() that handles EINTR signals and | 67 // ReadFile() is a simple wrapper around read() that handles EINTR signals and |
| 58 // calls MapSystemError() to map errno to net error codes. | 68 // calls MapSystemError() to map errno to net error codes. |
| 59 int ReadFile(base::PlatformFile file, char* buf, int buf_len, bool record_uma) { | 69 int ReadFile(base::PlatformFile file, |
| 70 char* buf, |
| 71 int buf_len, |
| 72 bool record_uma, |
| 73 const net::BoundNetLog& log) { |
| 60 base::ThreadRestrictions::AssertIOAllowed(); | 74 base::ThreadRestrictions::AssertIOAllowed(); |
| 61 // read(..., 0) returns 0 to indicate end-of-file. | 75 // read(..., 0) returns 0 to indicate end-of-file. |
| 62 | 76 |
| 63 // Loop in the case of getting interrupted by a signal. | 77 // Loop in the case of getting interrupted by a signal. |
| 64 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); | 78 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len))); |
| 65 if (res == static_cast<ssize_t>(-1)) | 79 if (res == static_cast<ssize_t>(-1)) |
| 66 RecordAndMapError(errno, FILE_ERROR_SOURCE_READ, record_uma); | 80 RecordAndMapError(errno, FILE_ERROR_SOURCE_READ, record_uma, log); |
| 67 return static_cast<int>(res); | 81 return static_cast<int>(res); |
| 68 } | 82 } |
| 69 | 83 |
| 70 void ReadFileTask(base::PlatformFile file, | 84 void ReadFileTask(base::PlatformFile file, |
| 71 char* buf, | 85 char* buf, |
| 72 int buf_len, | 86 int buf_len, |
| 73 bool record_uma, | 87 bool record_uma, |
| 88 const net::BoundNetLog& log, |
| 74 const CompletionCallback& callback) { | 89 const CompletionCallback& callback) { |
| 75 callback.Run(ReadFile(file, buf, buf_len, record_uma)); | 90 callback.Run(ReadFile(file, buf, buf_len, record_uma, log)); |
| 76 } | 91 } |
| 77 | 92 |
| 78 // WriteFile() is a simple wrapper around write() that handles EINTR signals and | 93 // WriteFile() is a simple wrapper around write() that handles EINTR signals and |
| 79 // calls MapSystemError() to map errno to net error codes. It tries to write to | 94 // calls MapSystemError() to map errno to net error codes. It tries to write to |
| 80 // completion. | 95 // completion. |
| 81 int WriteFile(base::PlatformFile file, const char* buf, int buf_len, | 96 int WriteFile(base::PlatformFile file, |
| 82 bool record_uma) { | 97 const char* buf, |
| 98 int buf_len, |
| 99 bool record_uma, |
| 100 const net::BoundNetLog& log) { |
| 83 base::ThreadRestrictions::AssertIOAllowed(); | 101 base::ThreadRestrictions::AssertIOAllowed(); |
| 84 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); | 102 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len)); |
| 85 if (res == -1) | 103 if (res == -1) |
| 86 RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE, record_uma); | 104 RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE, record_uma, log); |
| 87 return res; | 105 return res; |
| 88 } | 106 } |
| 89 | 107 |
| 90 void WriteFileTask(base::PlatformFile file, | 108 void WriteFileTask(base::PlatformFile file, |
| 91 const char* buf, | 109 const char* buf, |
| 92 int buf_len, bool record_uma, | 110 int buf_len, |
| 111 bool record_uma, |
| 112 const net::BoundNetLog& log, |
| 93 const CompletionCallback& callback) { | 113 const CompletionCallback& callback) { |
| 94 callback.Run(WriteFile(file, buf, buf_len, record_uma)); | 114 callback.Run(WriteFile(file, buf, buf_len, record_uma, log)); |
| 95 } | 115 } |
| 96 | 116 |
| 97 // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and | 117 // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and |
| 98 // calls MapSystemError() to map errno to net error codes. It tries to flush to | 118 // calls MapSystemError() to map errno to net error codes. It tries to flush to |
| 99 // completion. | 119 // completion. |
| 100 int FlushFile(base::PlatformFile file, bool record_uma) { | 120 int FlushFile(base::PlatformFile file, |
| 121 bool record_uma, |
| 122 const net::BoundNetLog& log) { |
| 101 base::ThreadRestrictions::AssertIOAllowed(); | 123 base::ThreadRestrictions::AssertIOAllowed(); |
| 102 ssize_t res = HANDLE_EINTR(fsync(file)); | 124 ssize_t res = HANDLE_EINTR(fsync(file)); |
| 103 if (res == -1) | 125 if (res == -1) |
| 104 RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma); | 126 RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma, log); |
| 105 return res; | 127 return res; |
| 106 } | 128 } |
| 107 | 129 |
| 108 } // namespace | 130 } // namespace |
| 109 | 131 |
| 110 // Cancelable wrapper around a Closure. | 132 // Cancelable wrapper around a Closure. |
| 111 class CancelableCallback { | 133 class CancelableCallback { |
| 112 public: | 134 public: |
| 113 explicit CancelableCallback(const base::Closure& callback) | 135 explicit CancelableCallback(const base::Closure& callback) |
| 114 : canceled_(false), | 136 : canceled_(false), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 // FileStream::AsyncContext ---------------------------------------------- | 153 // FileStream::AsyncContext ---------------------------------------------- |
| 132 | 154 |
| 133 class FileStream::AsyncContext { | 155 class FileStream::AsyncContext { |
| 134 public: | 156 public: |
| 135 AsyncContext(); | 157 AsyncContext(); |
| 136 ~AsyncContext(); | 158 ~AsyncContext(); |
| 137 | 159 |
| 138 // These methods post synchronous read() and write() calls to a WorkerThread. | 160 // These methods post synchronous read() and write() calls to a WorkerThread. |
| 139 void InitiateAsyncRead( | 161 void InitiateAsyncRead( |
| 140 base::PlatformFile file, char* buf, int buf_len, | 162 base::PlatformFile file, char* buf, int buf_len, |
| 163 const net::BoundNetLog& log, |
| 141 const CompletionCallback& callback); | 164 const CompletionCallback& callback); |
| 142 void InitiateAsyncWrite( | 165 void InitiateAsyncWrite( |
| 143 base::PlatformFile file, const char* buf, int buf_len, | 166 base::PlatformFile file, const char* buf, int buf_len, |
| 167 const net::BoundNetLog& log, |
| 144 const CompletionCallback& callback); | 168 const CompletionCallback& callback); |
| 145 | 169 |
| 146 const CompletionCallback& callback() const { return callback_; } | 170 const CompletionCallback& callback() const { return callback_; } |
| 147 | 171 |
| 148 // Called by the WorkerPool thread executing the IO after the IO completes. | 172 // Called by the WorkerPool thread executing the IO after the IO completes. |
| 149 // This method queues RunAsynchronousCallback() on the MessageLoop and signals | 173 // This method queues RunAsynchronousCallback() on the MessageLoop and signals |
| 150 // |background_io_completed_callback_|, in case the destructor is waiting. In | 174 // |background_io_completed_callback_|, in case the destructor is waiting. In |
| 151 // that case, the destructor will call RunAsynchronousCallback() instead, and | 175 // that case, the destructor will call RunAsynchronousCallback() instead, and |
| 152 // cancel |message_loop_task_|. | 176 // cancel |message_loop_task_|. |
| 153 // |result| is the result of the Read/Write task. | 177 // |result| is the result of the Read/Write task. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 if (need_to_wait) { | 224 if (need_to_wait) { |
| 201 // We want to see if we block the message loop for too long. | 225 // We want to see if we block the message loop for too long. |
| 202 UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose", | 226 UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose", |
| 203 base::TimeTicks::Now() - start); | 227 base::TimeTicks::Now() - start); |
| 204 } | 228 } |
| 205 } | 229 } |
| 206 } | 230 } |
| 207 | 231 |
| 208 void FileStream::AsyncContext::InitiateAsyncRead( | 232 void FileStream::AsyncContext::InitiateAsyncRead( |
| 209 base::PlatformFile file, char* buf, int buf_len, | 233 base::PlatformFile file, char* buf, int buf_len, |
| 234 const net::BoundNetLog& log, |
| 210 const CompletionCallback& callback) { | 235 const CompletionCallback& callback) { |
| 211 DCHECK(callback_.is_null()); | 236 DCHECK(callback_.is_null()); |
| 212 callback_ = callback; | 237 callback_ = callback; |
| 213 | 238 |
| 214 base::WorkerPool::PostTask( | 239 base::WorkerPool::PostTask( |
| 215 FROM_HERE, | 240 FROM_HERE, |
| 216 base::Bind(&ReadFileTask, file, buf, buf_len, | 241 base::Bind(&ReadFileTask, |
| 242 file, |
| 243 buf, |
| 244 buf_len, |
| 217 record_uma_, | 245 record_uma_, |
| 246 log, |
| 218 base::Bind(&AsyncContext::OnBackgroundIOCompleted, | 247 base::Bind(&AsyncContext::OnBackgroundIOCompleted, |
| 219 base::Unretained(this))), | 248 base::Unretained(this))), |
| 220 true /* task_is_slow */); | 249 true /* task_is_slow */); |
| 221 } | 250 } |
| 222 | 251 |
| 223 void FileStream::AsyncContext::InitiateAsyncWrite( | 252 void FileStream::AsyncContext::InitiateAsyncWrite( |
| 224 base::PlatformFile file, const char* buf, int buf_len, | 253 base::PlatformFile file, const char* buf, int buf_len, |
| 254 const net::BoundNetLog& log, |
| 225 const CompletionCallback& callback) { | 255 const CompletionCallback& callback) { |
| 226 DCHECK(callback_.is_null()); | 256 DCHECK(callback_.is_null()); |
| 227 callback_ = callback; | 257 callback_ = callback; |
| 228 | 258 |
| 229 base::WorkerPool::PostTask( | 259 base::WorkerPool::PostTask( |
| 230 FROM_HERE, | 260 FROM_HERE, |
| 231 base::Bind( | 261 base::Bind(&WriteFileTask, |
| 232 &WriteFileTask, | 262 file, |
| 233 file, buf, buf_len, | 263 buf, |
| 234 record_uma_, | 264 buf_len, |
| 235 base::Bind(&AsyncContext::OnBackgroundIOCompleted, | 265 record_uma_, |
| 236 base::Unretained(this))), | 266 log, |
| 267 base::Bind(&AsyncContext::OnBackgroundIOCompleted, |
| 268 base::Unretained(this))), |
| 237 true /* task_is_slow */); | 269 true /* task_is_slow */); |
| 238 } | 270 } |
| 239 | 271 |
| 240 void FileStream::AsyncContext::OnBackgroundIOCompleted(int result) { | 272 void FileStream::AsyncContext::OnBackgroundIOCompleted(int result) { |
| 241 result_ = result; | 273 result_ = result; |
| 242 message_loop_task_ = new CancelableCallback( | 274 message_loop_task_ = new CancelableCallback( |
| 243 base::Bind(&AsyncContext::RunAsynchronousCallback, | 275 base::Bind(&AsyncContext::RunAsynchronousCallback, |
| 244 base::Unretained(this))); | 276 base::Unretained(this))); |
| 245 message_loop_->PostTask(FROM_HERE, | 277 message_loop_->PostTask(FROM_HERE, |
| 246 base::Bind(&CancelableCallback::Run, | 278 base::Bind(&CancelableCallback::Run, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 274 // FileStream ------------------------------------------------------------ | 306 // FileStream ------------------------------------------------------------ |
| 275 | 307 |
| 276 FileStream::FileStream() | 308 FileStream::FileStream() |
| 277 : file_(base::kInvalidPlatformFileValue), | 309 : file_(base::kInvalidPlatformFileValue), |
| 278 open_flags_(0), | 310 open_flags_(0), |
| 279 auto_closed_(true), | 311 auto_closed_(true), |
| 280 record_uma_(false) { | 312 record_uma_(false) { |
| 281 DCHECK(!IsOpen()); | 313 DCHECK(!IsOpen()); |
| 282 } | 314 } |
| 283 | 315 |
| 316 FileStream::FileStream(net::NetLog* log) |
| 317 : file_(base::kInvalidPlatformFileValue), |
| 318 open_flags_(0), |
| 319 auto_closed_(true), |
| 320 record_uma_(false) { |
| 321 net_log_ = net::BoundNetLog::Make(log, net::NetLog::SOURCE_FILESTREAM); |
| 322 |
| 323 net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 324 } |
| 325 |
| 284 FileStream::FileStream(base::PlatformFile file, int flags) | 326 FileStream::FileStream(base::PlatformFile file, int flags) |
| 285 : file_(file), | 327 : file_(file), |
| 286 open_flags_(flags), | 328 open_flags_(flags), |
| 287 auto_closed_(false), | 329 auto_closed_(false), |
| 288 record_uma_(false) { | 330 record_uma_(false) { |
| 289 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 331 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to |
| 290 // make sure we will perform asynchronous File IO to it. | 332 // make sure we will perform asynchronous File IO to it. |
| 291 if (flags & base::PLATFORM_FILE_ASYNC) { | 333 if (flags & base::PLATFORM_FILE_ASYNC) { |
| 292 async_context_.reset(new AsyncContext()); | 334 async_context_.reset(new AsyncContext()); |
| 293 } | 335 } |
| 294 } | 336 } |
| 295 | 337 |
| 296 FileStream::~FileStream() { | 338 FileStream::~FileStream() { |
| 297 if (auto_closed_) | 339 if (auto_closed_) |
| 298 Close(); | 340 Close(); |
| 341 |
| 342 net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 299 } | 343 } |
| 300 | 344 |
| 301 void FileStream::Close() { | 345 void FileStream::Close() { |
| 346 net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE, NULL); |
| 347 |
| 302 // Abort any existing asynchronous operations. | 348 // Abort any existing asynchronous operations. |
| 303 async_context_.reset(); | 349 async_context_.reset(); |
| 304 | 350 |
| 305 if (file_ != base::kInvalidPlatformFileValue) { | 351 if (file_ != base::kInvalidPlatformFileValue) { |
| 306 if (close(file_) != 0) { | 352 if (close(file_) != 0) { |
| 307 NOTREACHED(); | 353 NOTREACHED(); |
| 308 } | 354 } |
| 309 file_ = base::kInvalidPlatformFileValue; | 355 file_ = base::kInvalidPlatformFileValue; |
| 356 |
| 357 net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); |
| 310 } | 358 } |
| 311 } | 359 } |
| 312 | 360 |
| 313 int FileStream::Open(const FilePath& path, int open_flags) { | 361 int FileStream::Open(const FilePath& path, int open_flags) { |
| 362 net_log_.BeginEvent( |
| 363 net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 364 make_scoped_refptr( |
| 365 new net::NetLogStringParameter("file_name", |
| 366 path.AsUTF8Unsafe()))); |
| 367 |
| 314 if (IsOpen()) { | 368 if (IsOpen()) { |
| 315 DLOG(FATAL) << "File is already open!"; | 369 DLOG(FATAL) << "File is already open!"; |
| 316 return ERR_UNEXPECTED; | 370 return ERR_UNEXPECTED; |
| 317 } | 371 } |
| 318 | 372 |
| 319 open_flags_ = open_flags; | 373 open_flags_ = open_flags; |
| 320 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); | 374 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); |
| 321 if (file_ == base::kInvalidPlatformFileValue) | 375 if (file_ == base::kInvalidPlatformFileValue) { |
| 322 return RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma_); | 376 return RecordAndMapError(errno, |
| 377 FILE_ERROR_SOURCE_OPEN, |
| 378 record_uma_, |
| 379 net_log_); |
| 380 } |
| 323 | 381 |
| 324 if (open_flags_ & base::PLATFORM_FILE_ASYNC) | 382 if (open_flags_ & base::PLATFORM_FILE_ASYNC) |
| 325 async_context_.reset(new AsyncContext()); | 383 async_context_.reset(new AsyncContext()); |
| 326 | 384 |
| 327 return OK; | 385 return OK; |
| 328 } | 386 } |
| 329 | 387 |
| 330 bool FileStream::IsOpen() const { | 388 bool FileStream::IsOpen() const { |
| 331 return file_ != base::kInvalidPlatformFileValue; | 389 return file_ != base::kInvalidPlatformFileValue; |
| 332 } | 390 } |
| 333 | 391 |
| 334 int64 FileStream::Seek(Whence whence, int64 offset) { | 392 int64 FileStream::Seek(Whence whence, int64 offset) { |
| 335 base::ThreadRestrictions::AssertIOAllowed(); | 393 base::ThreadRestrictions::AssertIOAllowed(); |
| 336 | 394 |
| 337 if (!IsOpen()) | 395 if (!IsOpen()) |
| 338 return ERR_UNEXPECTED; | 396 return ERR_UNEXPECTED; |
| 339 | 397 |
| 340 // If we're in async, make sure we don't have a request in flight. | 398 // If we're in async, make sure we don't have a request in flight. |
| 341 DCHECK(!async_context_.get() || async_context_->callback().is_null()); | 399 DCHECK(!async_context_.get() || async_context_->callback().is_null()); |
| 342 | 400 |
| 343 off_t res = lseek(file_, static_cast<off_t>(offset), | 401 off_t res = lseek(file_, static_cast<off_t>(offset), |
| 344 static_cast<int>(whence)); | 402 static_cast<int>(whence)); |
| 345 if (res == static_cast<off_t>(-1)) | 403 if (res == static_cast<off_t>(-1)) { |
| 346 return RecordAndMapError(errno, FILE_ERROR_SOURCE_SEEK, record_uma_); | 404 return RecordAndMapError(errno, |
| 405 FILE_ERROR_SOURCE_SEEK, |
| 406 record_uma_, |
| 407 net_log_); |
| 408 } |
| 347 | 409 |
| 348 return res; | 410 return res; |
| 349 } | 411 } |
| 350 | 412 |
| 351 int64 FileStream::Available() { | 413 int64 FileStream::Available() { |
| 352 base::ThreadRestrictions::AssertIOAllowed(); | 414 base::ThreadRestrictions::AssertIOAllowed(); |
| 353 | 415 |
| 354 if (!IsOpen()) | 416 if (!IsOpen()) |
| 355 return ERR_UNEXPECTED; | 417 return ERR_UNEXPECTED; |
| 356 | 418 |
| 357 int64 cur_pos = Seek(FROM_CURRENT, 0); | 419 int64 cur_pos = Seek(FROM_CURRENT, 0); |
| 358 if (cur_pos < 0) | 420 if (cur_pos < 0) |
| 359 return cur_pos; | 421 return cur_pos; |
| 360 | 422 |
| 361 struct stat info; | 423 struct stat info; |
| 362 if (fstat(file_, &info) != 0) | 424 if (fstat(file_, &info) != 0) { |
| 363 return RecordAndMapError(errno, FILE_ERROR_SOURCE_GET_SIZE, record_uma_); | 425 return RecordAndMapError(errno, |
| 426 FILE_ERROR_SOURCE_GET_SIZE, |
| 427 record_uma_, |
| 428 net_log_); |
| 429 } |
| 364 | 430 |
| 365 int64 size = static_cast<int64>(info.st_size); | 431 int64 size = static_cast<int64>(info.st_size); |
| 366 DCHECK_GT(size, cur_pos); | 432 DCHECK_GT(size, cur_pos); |
| 367 | 433 |
| 368 return size - cur_pos; | 434 return size - cur_pos; |
| 369 } | 435 } |
| 370 | 436 |
| 371 int FileStream::Read( | 437 int FileStream::Read( |
| 372 char* buf, int buf_len, const CompletionCallback& callback) { | 438 char* buf, int buf_len, const CompletionCallback& callback) { |
| 373 if (!IsOpen()) | 439 if (!IsOpen()) |
| 374 return ERR_UNEXPECTED; | 440 return ERR_UNEXPECTED; |
| 375 | 441 |
| 376 // read(..., 0) will return 0, which indicates end-of-file. | 442 // read(..., 0) will return 0, which indicates end-of-file. |
| 377 DCHECK_GT(buf_len, 0); | 443 DCHECK_GT(buf_len, 0); |
| 378 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 444 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| 379 | 445 |
| 380 if (async_context_.get()) { | 446 if (async_context_.get()) { |
| 381 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 447 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| 382 // If we're in async, make sure we don't have a request in flight. | 448 // If we're in async, make sure we don't have a request in flight. |
| 383 DCHECK(async_context_->callback().is_null()); | 449 DCHECK(async_context_->callback().is_null()); |
| 384 if (record_uma_) | 450 if (record_uma_) |
| 385 async_context_->EnableErrorStatistics(); | 451 async_context_->EnableErrorStatistics(); |
| 386 async_context_->InitiateAsyncRead(file_, buf, buf_len, callback); | 452 async_context_->InitiateAsyncRead(file_, buf, buf_len, net_log_, callback); |
| 387 return ERR_IO_PENDING; | 453 return ERR_IO_PENDING; |
| 388 } else { | 454 } else { |
| 389 return ReadFile(file_, buf, buf_len, record_uma_); | 455 return ReadFile(file_, buf, buf_len, record_uma_, net_log_); |
| 390 } | 456 } |
| 391 } | 457 } |
| 392 | 458 |
| 393 int FileStream::ReadUntilComplete(char *buf, int buf_len) { | 459 int FileStream::ReadUntilComplete(char *buf, int buf_len) { |
| 394 int to_read = buf_len; | 460 int to_read = buf_len; |
| 395 int bytes_total = 0; | 461 int bytes_total = 0; |
| 396 | 462 |
| 397 do { | 463 do { |
| 398 int bytes_read = Read(buf, to_read, CompletionCallback()); | 464 int bytes_read = Read(buf, to_read, CompletionCallback()); |
| 399 if (bytes_read <= 0) { | 465 if (bytes_read <= 0) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 418 | 484 |
| 419 if (!IsOpen()) | 485 if (!IsOpen()) |
| 420 return ERR_UNEXPECTED; | 486 return ERR_UNEXPECTED; |
| 421 | 487 |
| 422 if (async_context_.get()) { | 488 if (async_context_.get()) { |
| 423 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 489 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| 424 // If we're in async, make sure we don't have a request in flight. | 490 // If we're in async, make sure we don't have a request in flight. |
| 425 DCHECK(async_context_->callback().is_null()); | 491 DCHECK(async_context_->callback().is_null()); |
| 426 if (record_uma_) | 492 if (record_uma_) |
| 427 async_context_->EnableErrorStatistics(); | 493 async_context_->EnableErrorStatistics(); |
| 428 async_context_->InitiateAsyncWrite(file_, buf, buf_len, callback); | 494 async_context_->InitiateAsyncWrite(file_, buf, buf_len, net_log_, callback); |
| 429 return ERR_IO_PENDING; | 495 return ERR_IO_PENDING; |
| 430 } else { | 496 } else { |
| 431 return WriteFile(file_, buf, buf_len, record_uma_); | 497 return WriteFile(file_, buf, buf_len, record_uma_, net_log_); |
| 432 } | 498 } |
| 433 } | 499 } |
| 434 | 500 |
| 435 int64 FileStream::Truncate(int64 bytes) { | 501 int64 FileStream::Truncate(int64 bytes) { |
| 436 base::ThreadRestrictions::AssertIOAllowed(); | 502 base::ThreadRestrictions::AssertIOAllowed(); |
| 437 | 503 |
| 438 if (!IsOpen()) | 504 if (!IsOpen()) |
| 439 return ERR_UNEXPECTED; | 505 return ERR_UNEXPECTED; |
| 440 | 506 |
| 441 // We better be open for reading. | 507 // We'd better be open for reading. |
| 442 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 508 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 443 | 509 |
| 444 // Seek to the position to truncate from. | 510 // Seek to the position to truncate from. |
| 445 int64 seek_position = Seek(FROM_BEGIN, bytes); | 511 int64 seek_position = Seek(FROM_BEGIN, bytes); |
| 446 if (seek_position != bytes) | 512 if (seek_position != bytes) |
| 447 return ERR_UNEXPECTED; | 513 return ERR_UNEXPECTED; |
| 448 | 514 |
| 449 // And truncate the file. | 515 // And truncate the file. |
| 450 int result = ftruncate(file_, bytes); | 516 int result = ftruncate(file_, bytes); |
| 451 if (result == 0) | 517 if (result == 0) |
| 452 return seek_position; | 518 return seek_position; |
| 453 | 519 |
| 454 return RecordAndMapError(errno, FILE_ERROR_SOURCE_SET_EOF, record_uma_); | 520 return RecordAndMapError(errno, |
| 521 FILE_ERROR_SOURCE_SET_EOF, |
| 522 record_uma_, |
| 523 net_log_); |
| 455 } | 524 } |
| 456 | 525 |
| 457 int FileStream::Flush() { | 526 int FileStream::Flush() { |
| 458 if (!IsOpen()) | 527 if (!IsOpen()) |
| 459 return ERR_UNEXPECTED; | 528 return ERR_UNEXPECTED; |
| 460 | 529 |
| 461 return FlushFile(file_, record_uma_); | 530 return FlushFile(file_, record_uma_, net_log_); |
| 462 } | 531 } |
| 463 | 532 |
| 464 void FileStream::EnableErrorStatistics() { | 533 void FileStream::EnableErrorStatistics() { |
| 465 record_uma_ = true; | 534 record_uma_ = true; |
| 466 } | 535 } |
| 467 | 536 |
| 537 void FileStream::SetBoundNetLogSource(const net::BoundNetLog& log) { |
| 538 if (log.source().id == net::NetLog::Source::kInvalidId) |
| 539 return; |
| 540 |
| 541 if (log.source().id == net_log_.source().id) |
| 542 return; |
| 543 |
| 544 net_log_.AddEvent( |
| 545 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, |
| 546 make_scoped_refptr( |
| 547 new net::NetLogIntegerParameter("source_dependency", |
| 548 log.source().id))); |
| 549 |
| 550 log.AddEvent( |
| 551 net::NetLog::TYPE_FILE_STREAM_SOURCE, |
| 552 make_scoped_refptr( |
| 553 new net::NetLogIntegerParameter("source_dependency", |
| 554 net_log_.source().id))); |
| 555 } |
| 556 |
| 468 } // namespace net | 557 } // namespace net |
| OLD | NEW |