OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // For 64-bit file access (off_t = off64_t, lseek64, etc). | |
6 #define _FILE_OFFSET_BITS 64 | |
7 | |
8 #include "net/base/file_stream.h" | |
9 | |
10 #include <sys/types.h> | |
11 #include <sys/stat.h> | |
12 #include <fcntl.h> | |
13 #include <unistd.h> | |
14 #include <errno.h> | |
15 | |
16 #include "base/basictypes.h" | |
17 #include "base/bind.h" | |
18 #include "base/bind_helpers.h" | |
19 #include "base/callback.h" | |
20 #include "base/eintr_wrapper.h" | |
21 #include "base/file_path.h" | |
22 #include "base/logging.h" | |
23 #include "base/message_loop.h" | |
24 #include "base/metrics/histogram.h" | |
25 #include "base/string_util.h" | |
26 #include "base/threading/thread_restrictions.h" | |
27 #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" | |
31 #include "net/base/io_buffer.h" | |
32 #include "net/base/net_errors.h" | |
33 | |
34 #if defined(OS_ANDROID) | |
35 // Android's bionic libc only supports the LFS transitional API. | |
36 #define off_t off64_t | |
37 #define lseek lseek64 | |
38 #define stat stat64 | |
39 #define fstat fstat64 | |
40 #endif | |
41 | |
42 namespace net { | |
43 | |
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); | |
46 | |
47 // Make sure our Whence mappings match the system headers. | |
48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | |
49 FROM_CURRENT == SEEK_CUR && | |
50 FROM_END == SEEK_END, whence_matches_system); | |
51 | |
52 namespace { | |
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 // Flushes a file using FlushFile() and signals the completion. | |
235 void FlushFileAndSignal(base::PlatformFile file, | |
236 int* result, | |
237 bool record_uma, | |
238 base::WaitableEvent* on_io_complete, | |
239 const net::BoundNetLog& bound_net_log) { | |
240 *result = FlushFile(file, record_uma, bound_net_log); | |
241 on_io_complete->Signal(); | |
242 } | |
243 | |
244 // Called when Read(), Write() or Seek() is completed. | |
245 // |result| contains the result or a network error code. | |
246 template <typename R> | |
247 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream, | |
248 const base::Callback<void(R)>& callback, | |
249 R* result) { | |
250 if (!stream.get()) | |
251 return; | |
252 | |
253 // Reset this before Run() as Run() may issue a new async operation. | |
254 stream->ResetOnIOComplete(); | |
255 callback.Run(*result); | |
256 } | |
257 | |
258 } // namespace | |
259 | |
260 // FileStreamPosix ------------------------------------------------------------ | |
261 | |
262 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) | |
263 : file_(base::kInvalidPlatformFileValue), | |
264 open_flags_(0), | |
265 auto_closed_(true), | |
266 record_uma_(false), | |
267 bound_net_log_(net::BoundNetLog::Make(net_log, | |
268 net::NetLog::SOURCE_FILESTREAM)), | |
269 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
270 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
271 } | |
272 | |
273 FileStreamPosix::FileStreamPosix( | |
274 base::PlatformFile file, int flags, net::NetLog* net_log) | |
275 : file_(file), | |
276 open_flags_(flags), | |
277 auto_closed_(false), | |
278 record_uma_(false), | |
279 bound_net_log_(net::BoundNetLog::Make(net_log, | |
280 net::NetLog::SOURCE_FILESTREAM)), | |
281 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
282 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
283 } | |
284 | |
285 FileStreamPosix::~FileStreamPosix() { | |
286 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
287 // Block until the last open/close/read/write operation is complete. | |
288 // TODO(satorux): Ideally we should not block. crbug.com/115067 | |
289 WaitForIOCompletion(); | |
290 } | |
291 | |
292 if (auto_closed_) { | |
293 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
294 // Close the file in the background. | |
295 if (IsOpen()) { | |
296 const bool posted = base::WorkerPool::PostTask( | |
297 FROM_HERE, | |
298 base::Bind(&CloseFile, file_, bound_net_log_), | |
299 true /* task_is_slow */); | |
300 DCHECK(posted); | |
301 } | |
302 } else { | |
303 CloseSync(); | |
304 } | |
305 } | |
306 | |
307 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | |
308 } | |
309 | |
310 void FileStreamPosix::Close(const CompletionCallback& callback) { | |
311 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
312 | |
313 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
314 DCHECK(!on_io_complete_.get()); | |
315 on_io_complete_.reset(new base::WaitableEvent( | |
316 false /* manual_reset */, false /* initially_signaled */)); | |
317 | |
318 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
319 // destructor ensures that the close operation is complete with | |
320 // WaitForIOCompletion(). See also the destructor. | |
321 const bool posted = base::WorkerPool::PostTaskAndReply( | |
322 FROM_HERE, | |
323 base::Bind(&CloseFileAndSignal, &file_, on_io_complete_.get(), | |
324 bound_net_log_), | |
325 base::Bind(&FileStreamPosix::OnClosed, | |
326 weak_ptr_factory_.GetWeakPtr(), | |
327 callback), | |
328 true /* task_is_slow */); | |
329 | |
330 DCHECK(posted); | |
331 } | |
332 | |
333 void FileStreamPosix::CloseSync() { | |
334 // TODO(satorux): Replace the following async stuff with a | |
335 // DCHECK(open_flags & ASYNC) once once all async clients are migrated to | |
336 // use Close(). crbug.com/114783 | |
337 | |
338 // Abort any existing asynchronous operations. | |
339 weak_ptr_factory_.InvalidateWeakPtrs(); | |
340 // Block until the last open/read/write operation is complete. | |
341 // TODO(satorux): Ideally we should not block. crbug.com/115067 | |
342 WaitForIOCompletion(); | |
343 | |
344 CloseFile(file_, bound_net_log_); | |
345 file_ = base::kInvalidPlatformFileValue; | |
346 } | |
347 | |
348 int FileStreamPosix::Open(const FilePath& path, int open_flags, | |
349 const CompletionCallback& callback) { | |
350 if (IsOpen()) { | |
351 DLOG(FATAL) << "File is already open!"; | |
352 return ERR_UNEXPECTED; | |
353 } | |
354 | |
355 open_flags_ = open_flags; | |
356 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
357 | |
358 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
359 DCHECK(!on_io_complete_.get()); | |
360 on_io_complete_.reset(new base::WaitableEvent( | |
361 false /* manual_reset */, false /* initially_signaled */)); | |
362 | |
363 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
364 // destructor ensures that the open operation is complete with | |
365 // WaitForIOCompletion(). See also the destructor. | |
366 int* result = new int(OK); | |
367 const bool posted = base::WorkerPool::PostTaskAndReply( | |
368 FROM_HERE, | |
369 base::Bind(&OpenFileAndSignal, | |
370 path, open_flags, record_uma_, &file_, result, | |
371 on_io_complete_.get(), bound_net_log_), | |
372 base::Bind(&OnIOComplete<int>, weak_ptr_factory_.GetWeakPtr(), | |
373 callback, base::Owned(result)), | |
374 true /* task_is_slow */); | |
375 DCHECK(posted); | |
376 return ERR_IO_PENDING; | |
377 } | |
378 | |
379 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { | |
380 if (IsOpen()) { | |
381 DLOG(FATAL) << "File is already open!"; | |
382 return ERR_UNEXPECTED; | |
383 } | |
384 | |
385 open_flags_ = open_flags; | |
386 // TODO(satorux): Put a DCHECK once once all async clients are migrated | |
387 // to use Open(). crbug.com/114783 | |
388 // | |
389 // DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
390 | |
391 int result = OK; | |
392 OpenFile(path, open_flags_, record_uma_, &file_, &result, bound_net_log_); | |
393 return result; | |
394 } | |
395 | |
396 bool FileStreamPosix::IsOpen() const { | |
397 return file_ != base::kInvalidPlatformFileValue; | |
398 } | |
399 | |
400 int FileStreamPosix::Seek(Whence whence, int64 offset, | |
401 const Int64CompletionCallback& callback) { | |
402 if (!IsOpen()) | |
403 return ERR_UNEXPECTED; | |
404 | |
405 // Make sure we're async and we have no other in-flight async operations. | |
406 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
407 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
408 DCHECK(!on_io_complete_.get()); | |
409 | |
410 on_io_complete_.reset(new base::WaitableEvent( | |
411 false /* manual_reset */, false /* initially_signaled */)); | |
412 | |
413 int64* result = new int64(-1); | |
414 const bool posted = base::WorkerPool::PostTaskAndReply( | |
415 FROM_HERE, | |
416 base::Bind(&SeekFileAndSignal, file_, whence, offset, result, | |
417 record_uma_, on_io_complete_.get(), bound_net_log_), | |
418 base::Bind(&OnIOComplete<int64>, | |
419 weak_ptr_factory_.GetWeakPtr(), | |
420 callback, base::Owned(result)), | |
421 true /* task is slow */); | |
422 DCHECK(posted); | |
423 return ERR_IO_PENDING; | |
424 } | |
425 | |
426 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) { | |
427 base::ThreadRestrictions::AssertIOAllowed(); | |
428 | |
429 if (!IsOpen()) | |
430 return ERR_UNEXPECTED; | |
431 | |
432 // If we're in async, make sure we don't have a request in flight. | |
433 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC) || | |
434 !on_io_complete_.get()); | |
435 | |
436 off_t result = -1; | |
437 SeekFile(file_, whence, offset, &result, record_uma_, bound_net_log_); | |
438 return result; | |
439 } | |
440 | |
441 int64 FileStreamPosix::Available() { | |
442 base::ThreadRestrictions::AssertIOAllowed(); | |
443 | |
444 if (!IsOpen()) | |
445 return ERR_UNEXPECTED; | |
446 | |
447 int64 cur_pos = SeekSync(FROM_CURRENT, 0); | |
448 if (cur_pos < 0) | |
449 return cur_pos; | |
450 | |
451 struct stat info; | |
452 if (fstat(file_, &info) != 0) { | |
453 return RecordAndMapError(errno, | |
454 FILE_ERROR_SOURCE_GET_SIZE, | |
455 record_uma_, | |
456 bound_net_log_); | |
457 } | |
458 | |
459 int64 size = static_cast<int64>(info.st_size); | |
460 DCHECK_GT(size, cur_pos); | |
461 | |
462 return size - cur_pos; | |
463 } | |
464 | |
465 int FileStreamPosix::Read( | |
466 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | |
467 if (!IsOpen()) | |
468 return ERR_UNEXPECTED; | |
469 | |
470 // read(..., 0) will return 0, which indicates end-of-file. | |
471 DCHECK_GT(buf_len, 0); | |
472 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | |
473 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
474 | |
475 // Make sure we don't have a request in flight. | |
476 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
477 DCHECK(!on_io_complete_.get()); | |
478 | |
479 on_io_complete_.reset(new base::WaitableEvent( | |
480 false /* manual_reset */, false /* initially_signaled */)); | |
481 | |
482 int* result = new int(OK); | |
483 scoped_refptr<IOBuffer> buf = in_buf; | |
484 const bool posted = base::WorkerPool::PostTaskAndReply( | |
485 FROM_HERE, | |
486 base::Bind(&ReadFileAndSignal, file_, buf, buf_len, | |
487 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
488 base::Bind(&OnIOComplete<int>, | |
489 weak_ptr_factory_.GetWeakPtr(), | |
490 callback, base::Owned(result)), | |
491 true /* task is slow */); | |
492 DCHECK(posted); | |
493 return ERR_IO_PENDING; | |
494 } | |
495 | |
496 int FileStreamPosix::ReadSync(char* buf, int buf_len) { | |
497 if (!IsOpen()) | |
498 return ERR_UNEXPECTED; | |
499 | |
500 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
501 // read(..., 0) will return 0, which indicates end-of-file. | |
502 DCHECK_GT(buf_len, 0); | |
503 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | |
504 | |
505 int result = OK; | |
506 ReadFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
507 return result; | |
508 } | |
509 | |
510 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { | |
511 int to_read = buf_len; | |
512 int bytes_total = 0; | |
513 | |
514 do { | |
515 int bytes_read = ReadSync(buf, to_read); | |
516 if (bytes_read <= 0) { | |
517 if (bytes_total == 0) | |
518 return bytes_read; | |
519 | |
520 return bytes_total; | |
521 } | |
522 | |
523 bytes_total += bytes_read; | |
524 buf += bytes_read; | |
525 to_read -= bytes_read; | |
526 } while (bytes_total < buf_len); | |
527 | |
528 return bytes_total; | |
529 } | |
530 | |
531 int FileStreamPosix::Write( | |
532 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { | |
533 if (!IsOpen()) | |
534 return ERR_UNEXPECTED; | |
535 | |
536 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
537 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
538 // write(..., 0) will return 0, which indicates end-of-file. | |
539 DCHECK_GT(buf_len, 0); | |
540 | |
541 // Make sure we don't have a request in flight. | |
542 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
543 DCHECK(!on_io_complete_.get()); | |
544 on_io_complete_.reset(new base::WaitableEvent( | |
545 false /* manual_reset */, false /* initially_signaled */)); | |
546 | |
547 int* result = new int(OK); | |
548 scoped_refptr<IOBuffer> buf = in_buf; | |
549 const bool posted = base::WorkerPool::PostTaskAndReply( | |
550 FROM_HERE, | |
551 base::Bind(&WriteFileAndSignal, file_, buf, buf_len, | |
552 record_uma_, result, on_io_complete_.get(), bound_net_log_), | |
553 base::Bind(&OnIOComplete<int>, | |
554 weak_ptr_factory_.GetWeakPtr(), | |
555 callback, base::Owned(result)), | |
556 true /* task is slow */); | |
557 DCHECK(posted); | |
558 return ERR_IO_PENDING; | |
559 } | |
560 | |
561 int FileStreamPosix::WriteSync( | |
562 const char* buf, int buf_len) { | |
563 if (!IsOpen()) | |
564 return ERR_UNEXPECTED; | |
565 | |
566 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); | |
567 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
568 // write(..., 0) will return 0, which indicates end-of-file. | |
569 DCHECK_GT(buf_len, 0); | |
570 | |
571 int result = OK; | |
572 WriteFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_); | |
573 return result; | |
574 } | |
575 | |
576 int64 FileStreamPosix::Truncate(int64 bytes) { | |
577 base::ThreadRestrictions::AssertIOAllowed(); | |
578 | |
579 if (!IsOpen()) | |
580 return ERR_UNEXPECTED; | |
581 | |
582 // We'd better be open for writing. | |
583 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | |
584 | |
585 // Seek to the position to truncate from. | |
586 int64 seek_position = SeekSync(FROM_BEGIN, bytes); | |
587 if (seek_position != bytes) | |
588 return ERR_UNEXPECTED; | |
589 | |
590 // And truncate the file. | |
591 int result = ftruncate(file_, bytes); | |
592 if (result == 0) | |
593 return seek_position; | |
594 | |
595 return RecordAndMapError(errno, | |
596 FILE_ERROR_SOURCE_SET_EOF, | |
597 record_uma_, | |
598 bound_net_log_); | |
599 } | |
600 | |
601 int FileStreamPosix::Flush(const CompletionCallback& callback) { | |
602 if (!IsOpen()) | |
603 return ERR_UNEXPECTED; | |
604 | |
605 // Make sure we're async and we have no other in-flight async operations. | |
606 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | |
607 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | |
608 DCHECK(!on_io_complete_.get()); | |
609 | |
610 on_io_complete_.reset(new base::WaitableEvent( | |
611 false /* manual_reset */, false /* initially_signaled */)); | |
612 | |
613 int* result = new int(OK); | |
614 const bool posted = base::WorkerPool::PostTaskAndReply( | |
615 FROM_HERE, | |
616 base::Bind(&FlushFileAndSignal, file_, result, | |
617 record_uma_, on_io_complete_.get(), bound_net_log_), | |
618 base::Bind(&OnIOComplete<int>, | |
619 weak_ptr_factory_.GetWeakPtr(), | |
620 callback, base::Owned(result)), | |
621 true /* task is slow */); | |
622 DCHECK(posted); | |
623 return ERR_IO_PENDING; | |
624 } | |
625 | |
626 int FileStreamPosix::FlushSync() { | |
627 if (!IsOpen()) | |
628 return ERR_UNEXPECTED; | |
629 | |
630 return FlushFile(file_, record_uma_, bound_net_log_); | |
631 } | |
632 | |
633 void FileStreamPosix::EnableErrorStatistics() { | |
634 record_uma_ = true; | |
635 } | |
636 | |
637 void FileStreamPosix::SetBoundNetLogSource( | |
638 const net::BoundNetLog& owner_bound_net_log) { | |
639 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | |
640 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | |
641 // Both |BoundNetLog|s are invalid. | |
642 return; | |
643 } | |
644 | |
645 // Should never connect to itself. | |
646 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); | |
647 | |
648 bound_net_log_.AddEvent( | |
649 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | |
650 owner_bound_net_log.source().ToEventParametersCallback()); | |
651 | |
652 owner_bound_net_log.AddEvent( | |
653 net::NetLog::TYPE_FILE_STREAM_SOURCE, | |
654 bound_net_log_.source().ToEventParametersCallback()); | |
655 } | |
656 | |
657 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { | |
658 return file_; | |
659 } | |
660 | |
661 void FileStreamPosix::ResetOnIOComplete() { | |
662 on_io_complete_.reset(); | |
663 weak_ptr_factory_.InvalidateWeakPtrs(); | |
664 } | |
665 | |
666 void FileStreamPosix::OnClosed(const CompletionCallback& callback) { | |
667 file_ = base::kInvalidPlatformFileValue; | |
668 | |
669 // Reset this before Run() as Run() may issue a new async operation. | |
670 ResetOnIOComplete(); | |
671 callback.Run(OK); | |
672 } | |
673 | |
674 void FileStreamPosix::WaitForIOCompletion() { | |
675 // http://crbug.com/115067 | |
676 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
677 if (on_io_complete_.get()) { | |
678 on_io_complete_->Wait(); | |
679 on_io_complete_.reset(); | |
680 } | |
681 } | |
682 | |
683 } // namespace net | |
OLD | NEW |