rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 5 | #include "base/files/file.h" |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 7 | #include "base/files/file_tracing.h" |
tnagel | 7eaf477 | 2015-04-03 19:11:46 +0900 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
| 9 | #include "base/timer/elapsed_timer.h" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 13 | File::Info::Info() |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 14 | : size(0), |
| 15 | is_directory(false), |
| 16 | is_symbolic_link(false) { |
| 17 | } |
| 18 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 19 | File::Info::~Info() { |
| 20 | } |
| 21 | |
| 22 | File::File() |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame] | 23 | : error_details_(FILE_ERROR_FAILED), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 24 | created_(false), |
| 25 | async_(false) { |
| 26 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 27 | |
| 28 | #if !defined(OS_NACL) |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 29 | File::File(const FilePath& path, uint32 flags) |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame] | 30 | : error_details_(FILE_OK), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 31 | created_(false), |
| 32 | async_(false) { |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 33 | Initialize(path, flags); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 34 | } |
| 35 | #endif |
| 36 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 37 | File::File(PlatformFile platform_file) |
| 38 | : file_(platform_file), |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 39 | error_details_(FILE_OK), |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 40 | created_(false), |
| 41 | async_(false) { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 42 | #if defined(OS_POSIX) |
| 43 | DCHECK_GE(platform_file, -1); |
| 44 | #endif |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 45 | } |
| 46 | |
rvargas@chromium.org | ca704f4 | 2014-03-26 18:59:31 +0900 | [diff] [blame] | 47 | File::File(Error error_details) |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame] | 48 | : error_details_(error_details), |
rvargas@chromium.org | ca704f4 | 2014-03-26 18:59:31 +0900 | [diff] [blame] | 49 | created_(false), |
| 50 | async_(false) { |
| 51 | } |
| 52 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 53 | File::File(RValue other) |
| 54 | : file_(other.object->TakePlatformFile()), |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 55 | tracing_path_(other.object->tracing_path_), |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 56 | error_details_(other.object->error_details()), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 57 | created_(other.object->created()), |
| 58 | async_(other.object->async_) { |
| 59 | } |
| 60 | |
| 61 | File::~File() { |
rvargas@chromium.org | e60d967 | 2014-05-06 09:55:35 +0900 | [diff] [blame] | 62 | // Go through the AssertIOAllowed logic. |
| 63 | Close(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 64 | } |
| 65 | |
reillyg | 9b86e31 | 2015-06-16 09:48:48 +0900 | [diff] [blame] | 66 | // static |
| 67 | File File::CreateForAsyncHandle(PlatformFile platform_file) { |
| 68 | File file(platform_file); |
| 69 | // It would be nice if we could validate that |platform_file| was opened with |
| 70 | // FILE_FLAG_OVERLAPPED on Windows but this doesn't appear to be possible. |
| 71 | file.async_ = true; |
| 72 | return file.Pass(); |
| 73 | } |
| 74 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 75 | File& File::operator=(RValue other) { |
| 76 | if (this != other.object) { |
| 77 | Close(); |
| 78 | SetPlatformFile(other.object->TakePlatformFile()); |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 79 | tracing_path_ = other.object->tracing_path_; |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 80 | error_details_ = other.object->error_details(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 81 | created_ = other.object->created(); |
| 82 | async_ = other.object->async_; |
| 83 | } |
| 84 | return *this; |
| 85 | } |
| 86 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 87 | #if !defined(OS_NACL) |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 88 | void File::Initialize(const FilePath& path, uint32 flags) { |
| 89 | if (path.ReferencesParent()) { |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 90 | error_details_ = FILE_ERROR_ACCESS_DENIED; |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 91 | return; |
| 92 | } |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 93 | if (FileTracing::IsCategoryEnabled()) |
| 94 | tracing_path_ = path; |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 95 | SCOPED_FILE_TRACE("Initialize"); |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 96 | DoInitialize(path, flags); |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 97 | } |
| 98 | #endif |
| 99 | |
mtomasz@chromium.org | a5d9be8 | 2014-05-30 19:07:30 +0900 | [diff] [blame] | 100 | std::string File::ErrorToString(Error error) { |
| 101 | switch (error) { |
| 102 | case FILE_OK: |
| 103 | return "FILE_OK"; |
| 104 | case FILE_ERROR_FAILED: |
| 105 | return "FILE_ERROR_FAILED"; |
| 106 | case FILE_ERROR_IN_USE: |
| 107 | return "FILE_ERROR_IN_USE"; |
| 108 | case FILE_ERROR_EXISTS: |
| 109 | return "FILE_ERROR_EXISTS"; |
| 110 | case FILE_ERROR_NOT_FOUND: |
| 111 | return "FILE_ERROR_NOT_FOUND"; |
| 112 | case FILE_ERROR_ACCESS_DENIED: |
| 113 | return "FILE_ERROR_ACCESS_DENIED"; |
| 114 | case FILE_ERROR_TOO_MANY_OPENED: |
| 115 | return "FILE_ERROR_TOO_MANY_OPENED"; |
| 116 | case FILE_ERROR_NO_MEMORY: |
| 117 | return "FILE_ERROR_NO_MEMORY"; |
| 118 | case FILE_ERROR_NO_SPACE: |
| 119 | return "FILE_ERROR_NO_SPACE"; |
| 120 | case FILE_ERROR_NOT_A_DIRECTORY: |
| 121 | return "FILE_ERROR_NOT_A_DIRECTORY"; |
| 122 | case FILE_ERROR_INVALID_OPERATION: |
| 123 | return "FILE_ERROR_INVALID_OPERATION"; |
| 124 | case FILE_ERROR_SECURITY: |
| 125 | return "FILE_ERROR_SECURITY"; |
| 126 | case FILE_ERROR_ABORT: |
| 127 | return "FILE_ERROR_ABORT"; |
| 128 | case FILE_ERROR_NOT_A_FILE: |
| 129 | return "FILE_ERROR_NOT_A_FILE"; |
| 130 | case FILE_ERROR_NOT_EMPTY: |
| 131 | return "FILE_ERROR_NOT_EMPTY"; |
| 132 | case FILE_ERROR_INVALID_URL: |
| 133 | return "FILE_ERROR_INVALID_URL"; |
| 134 | case FILE_ERROR_IO: |
| 135 | return "FILE_ERROR_IO"; |
| 136 | case FILE_ERROR_MAX: |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | NOTREACHED(); |
| 141 | return ""; |
| 142 | } |
| 143 | |
tnagel | 7eaf477 | 2015-04-03 19:11:46 +0900 | [diff] [blame] | 144 | bool File::Flush() { |
| 145 | ElapsedTimer timer; |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 146 | SCOPED_FILE_TRACE("Flush"); |
tnagel | 7eaf477 | 2015-04-03 19:11:46 +0900 | [diff] [blame] | 147 | bool return_value = DoFlush(); |
| 148 | UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed()); |
| 149 | return return_value; |
| 150 | } |
| 151 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 152 | } // namespace base |