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> |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 291 |
292 // FileStream ------------------------------------------------------------ | 292 // FileStream ------------------------------------------------------------ |
293 | 293 |
294 FileStream::FileStream() | 294 FileStream::FileStream() |
295 : file_(base::kInvalidPlatformFileValue), | 295 : file_(base::kInvalidPlatformFileValue), |
296 open_flags_(0) { | 296 open_flags_(0) { |
297 DCHECK(!IsOpen()); | 297 DCHECK(!IsOpen()); |
298 } | 298 } |
299 | 299 |
300 FileStream::FileStream(base::PlatformFile file, int flags) | 300 FileStream::FileStream(base::PlatformFile file, int flags) |
301 : file_(file), | 301 : file_(base::kInvalidPlatformFileValue) { |
302 open_flags_(flags) { | 302 Open(file, flags); |
303 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | |
304 // make sure we will perform asynchronous File IO to it. | |
305 if (flags & base::PLATFORM_FILE_ASYNC) { | |
306 async_context_.reset(new AsyncContext()); | |
307 } | |
308 } | 303 } |
309 | 304 |
310 FileStream::~FileStream() { | 305 FileStream::~FileStream() { |
311 Close(); | 306 Close(); |
312 } | 307 } |
313 | 308 |
314 void FileStream::Close() { | 309 void FileStream::Close() { |
315 // Abort any existing asynchronous operations. | 310 // Abort any existing asynchronous operations. |
316 async_context_.reset(); | 311 async_context_.reset(); |
317 | 312 |
318 if (file_ != base::kInvalidPlatformFileValue) { | 313 if (file_ != base::kInvalidPlatformFileValue) { |
319 if (close(file_) != 0) { | 314 if (close(file_) != 0) { |
320 NOTREACHED(); | 315 NOTREACHED(); |
321 } | 316 } |
322 file_ = base::kInvalidPlatformFileValue; | 317 file_ = base::kInvalidPlatformFileValue; |
323 } | 318 } |
324 } | 319 } |
325 | 320 |
| 321 void FileStream::Release() { |
| 322 // Abort any existing asynchronous operations. |
| 323 async_context_.reset(); |
| 324 file_ = base::kInvalidPlatformFileValue; |
| 325 } |
| 326 |
326 int FileStream::Open(const FilePath& path, int open_flags) { | 327 int FileStream::Open(const FilePath& path, int open_flags) { |
327 if (IsOpen()) { | 328 if (IsOpen()) { |
328 DLOG(FATAL) << "File is already open!"; | 329 DLOG(FATAL) << "File is already open!"; |
329 return ERR_UNEXPECTED; | 330 return ERR_UNEXPECTED; |
330 } | 331 } |
331 | 332 |
332 open_flags_ = open_flags; | 333 open_flags_ = open_flags; |
333 file_ = base::CreatePlatformFile(path, open_flags_, NULL); | 334 file_ = base::CreatePlatformFile(path, open_flags_, NULL); |
334 if (file_ == base::kInvalidPlatformFileValue) { | 335 if (file_ == base::kInvalidPlatformFileValue) { |
335 LOG(WARNING) << "Failed to open file: " << errno | 336 LOG(WARNING) << "Failed to open file: " << errno |
336 << " (" << path.ToWStringHack() << ")"; | 337 << " (" << path.ToWStringHack() << ")"; |
337 return MapErrorCode(errno); | 338 return MapErrorCode(errno); |
338 } | 339 } |
339 | 340 |
340 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 341 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
341 async_context_.reset(new AsyncContext()); | 342 async_context_.reset(new AsyncContext()); |
342 } | 343 } |
343 | 344 |
344 return OK; | 345 return OK; |
345 } | 346 } |
346 | 347 |
| 348 int FileStream::Open(base::PlatformFile file, int open_flags) { |
| 349 if (IsOpen()) { |
| 350 DLOG(FATAL) << "File is already open!"; |
| 351 return ERR_UNEXPECTED; |
| 352 } |
| 353 |
| 354 open_flags_ = open_flags; |
| 355 file_ = file; |
| 356 |
| 357 if (open_flags & base::PLATFORM_FILE_ASYNC) |
| 358 async_context_.reset(new AsyncContext()); |
| 359 |
| 360 return OK; |
| 361 } |
| 362 |
347 bool FileStream::IsOpen() const { | 363 bool FileStream::IsOpen() const { |
348 return file_ != base::kInvalidPlatformFileValue; | 364 return file_ != base::kInvalidPlatformFileValue; |
349 } | 365 } |
350 | 366 |
351 int64 FileStream::Seek(Whence whence, int64 offset) { | 367 int64 FileStream::Seek(Whence whence, int64 offset) { |
352 if (!IsOpen()) | 368 if (!IsOpen()) |
353 return ERR_UNEXPECTED; | 369 return ERR_UNEXPECTED; |
354 | 370 |
355 // If we're in async, make sure we don't have a request in flight. | 371 // If we're in async, make sure we don't have a request in flight. |
356 DCHECK(!async_context_.get() || !async_context_->callback()); | 372 DCHECK(!async_context_.get() || !async_context_->callback()); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 return ERR_IO_PENDING; | 454 return ERR_IO_PENDING; |
439 } else { | 455 } else { |
440 return WriteFile(file_, buf, buf_len); | 456 return WriteFile(file_, buf, buf_len); |
441 } | 457 } |
442 } | 458 } |
443 | 459 |
444 int64 FileStream::Truncate(int64 bytes) { | 460 int64 FileStream::Truncate(int64 bytes) { |
445 if (!IsOpen()) | 461 if (!IsOpen()) |
446 return ERR_UNEXPECTED; | 462 return ERR_UNEXPECTED; |
447 | 463 |
448 // We better be open for reading. | 464 // We better be open for writing. |
449 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 465 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
450 | 466 |
451 // Seek to the position to truncate from. | 467 // Seek to the position to truncate from. |
452 int64 seek_position = Seek(FROM_BEGIN, bytes); | 468 int64 seek_position = Seek(FROM_BEGIN, bytes); |
453 if (seek_position != bytes) | 469 if (seek_position != bytes) |
454 return ERR_UNEXPECTED; | 470 return ERR_UNEXPECTED; |
455 | 471 |
456 // And truncate the file. | 472 // And truncate the file. |
457 int result = ftruncate(file_, bytes); | 473 int result = ftruncate(file_, bytes); |
458 return result == 0 ? seek_position : MapErrorCode(errno); | 474 return result == 0 ? seek_position : MapErrorCode(errno); |
459 } | 475 } |
460 | 476 |
461 } // namespace net | 477 } // namespace net |
OLD | NEW |