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