Chromium Code Reviews| 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 #include "base/files/file.h" | 5 #include "base/files/file.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/timer/elapsed_timer.h" | 8 #include "base/timer/elapsed_timer.h" |
| 9 #include "base/trace_event/scoped_file_trace.h" | |
| 10 #include "base/trace_event/trace_event.h" | |
| 9 | 11 |
| 10 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
| 11 #include "base/files/file_posix_hooks_internal.h" | 13 #include "base/files/file_posix_hooks_internal.h" |
| 12 #endif | 14 #endif |
| 13 | 15 |
| 14 namespace base { | 16 namespace base { |
| 15 | 17 |
| 16 File::Info::Info() | 18 File::Info::Info() |
| 17 : size(0), | 19 : size(0), |
| 18 is_directory(false), | 20 is_directory(false), |
| 19 is_symbolic_link(false) { | 21 is_symbolic_link(false) { |
| 20 } | 22 } |
| 21 | 23 |
| 22 File::Info::~Info() { | 24 File::Info::~Info() { |
| 23 } | 25 } |
| 24 | 26 |
| 25 File::File() | 27 File::File() |
| 26 : error_details_(FILE_ERROR_FAILED), | 28 : error_details_(FILE_ERROR_FAILED), |
| 27 created_(false), | 29 created_(false), |
| 28 async_(false) { | 30 async_(false) { |
| 29 } | 31 } |
| 30 | 32 |
| 31 #if !defined(OS_NACL) | 33 #if !defined(OS_NACL) |
| 32 File::File(const FilePath& name, uint32 flags) | 34 File::File(const FilePath& path, uint32 flags) |
| 33 : error_details_(FILE_OK), | 35 : error_details_(FILE_OK), |
| 34 created_(false), | 36 created_(false), |
| 35 async_(false) { | 37 async_(false) { |
| 36 Initialize(name, flags); | 38 Initialize(path, flags); |
| 37 } | 39 } |
| 38 #endif | 40 #endif |
| 39 | 41 |
| 40 File::File(PlatformFile platform_file) | 42 File::File(PlatformFile platform_file) |
| 41 : file_(platform_file), | 43 : file_(platform_file), |
| 42 error_details_(FILE_OK), | 44 error_details_(FILE_OK), |
| 43 created_(false), | 45 created_(false), |
| 44 async_(false) { | 46 async_(false) { |
| 45 #if defined(OS_POSIX) | 47 #if defined(OS_POSIX) |
| 46 DCHECK_GE(platform_file, -1); | 48 DCHECK_GE(platform_file, -1); |
| 47 if (IsValid()) | 49 if (IsValid()) |
| 48 ProtectFileDescriptor(platform_file); | 50 ProtectFileDescriptor(platform_file); |
| 49 #endif | 51 #endif |
| 50 } | 52 } |
| 51 | 53 |
| 52 File::File(Error error_details) | 54 File::File(Error error_details) |
| 53 : error_details_(error_details), | 55 : error_details_(error_details), |
| 54 created_(false), | 56 created_(false), |
| 55 async_(false) { | 57 async_(false) { |
| 56 } | 58 } |
| 57 | 59 |
| 58 File::File(RValue other) | 60 File::File(RValue other) |
| 59 : file_(other.object->TakePlatformFile()), | 61 : file_(other.object->TakePlatformFile()), |
| 62 path_(other.object->path_), | |
| 60 error_details_(other.object->error_details()), | 63 error_details_(other.object->error_details()), |
| 61 created_(other.object->created()), | 64 created_(other.object->created()), |
| 62 async_(other.object->async_) { | 65 async_(other.object->async_) { |
| 63 #if defined(OS_POSIX) | 66 #if defined(OS_POSIX) |
| 64 if (IsValid()) | 67 if (IsValid()) |
| 65 ProtectFileDescriptor(GetPlatformFile()); | 68 ProtectFileDescriptor(GetPlatformFile()); |
| 66 #endif | 69 #endif |
| 67 } | 70 } |
| 68 | 71 |
| 69 File::~File() { | 72 File::~File() { |
| 70 // Go through the AssertIOAllowed logic. | 73 // Go through the AssertIOAllowed logic. |
| 71 Close(); | 74 Close(); |
| 75 TRACE_EVENT_ASYNC_END1(ScopedFileTrace::kGroup, "File", this, | |
| 76 "path", path_.AsUTF8Unsafe()); | |
| 72 } | 77 } |
| 73 | 78 |
| 74 File& File::operator=(RValue other) { | 79 File& File::operator=(RValue other) { |
| 75 if (this != other.object) { | 80 if (this != other.object) { |
| 76 Close(); | 81 Close(); |
| 77 SetPlatformFile(other.object->TakePlatformFile()); | 82 SetPlatformFile(other.object->TakePlatformFile()); |
| 78 error_details_ = other.object->error_details(); | 83 error_details_ = other.object->error_details(); |
| 79 created_ = other.object->created(); | 84 created_ = other.object->created(); |
| 80 async_ = other.object->async_; | 85 async_ = other.object->async_; |
| 81 } | 86 } |
| 82 return *this; | 87 return *this; |
| 83 } | 88 } |
| 84 | 89 |
| 85 #if !defined(OS_NACL) | 90 #if !defined(OS_NACL) |
| 86 void File::Initialize(const FilePath& name, uint32 flags) { | 91 void File::Initialize(const FilePath& path, uint32 flags) { |
| 87 if (name.ReferencesParent()) { | 92 path_ = path; |
| 93 if (path_.ReferencesParent()) { | |
| 88 error_details_ = FILE_ERROR_ACCESS_DENIED; | 94 error_details_ = FILE_ERROR_ACCESS_DENIED; |
| 89 return; | 95 return; |
| 90 } | 96 } |
| 91 DoInitialize(name, flags); | 97 TRACE_EVENT_ASYNC_BEGIN1(ScopedFileTrace::kGroup, "File", this, |
| 98 "path", path_.AsUTF8Unsafe()); | |
|
Dan Beam
2015/04/23 22:32:19
this seemed like the sanest place to start this, b
| |
| 99 DoInitialize(flags); | |
| 92 } | 100 } |
| 93 #endif | 101 #endif |
| 94 | 102 |
| 95 std::string File::ErrorToString(Error error) { | 103 std::string File::ErrorToString(Error error) { |
| 96 switch (error) { | 104 switch (error) { |
| 97 case FILE_OK: | 105 case FILE_OK: |
| 98 return "FILE_OK"; | 106 return "FILE_OK"; |
| 99 case FILE_ERROR_FAILED: | 107 case FILE_ERROR_FAILED: |
| 100 return "FILE_ERROR_FAILED"; | 108 return "FILE_ERROR_FAILED"; |
| 101 case FILE_ERROR_IN_USE: | 109 case FILE_ERROR_IN_USE: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 } | 145 } |
| 138 | 146 |
| 139 bool File::Flush() { | 147 bool File::Flush() { |
| 140 ElapsedTimer timer; | 148 ElapsedTimer timer; |
| 141 bool return_value = DoFlush(); | 149 bool return_value = DoFlush(); |
| 142 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed()); | 150 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed()); |
| 143 return return_value; | 151 return return_value; |
| 144 } | 152 } |
| 145 | 153 |
| 146 } // namespace base | 154 } // namespace base |
| OLD | NEW |