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" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 7 | |
| 8 | namespace base { |
| 9 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 10 | File::Info::Info() |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 11 | : size(0), |
| 12 | is_directory(false), |
| 13 | is_symbolic_link(false) { |
| 14 | } |
| 15 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 16 | File::Info::~Info() { |
| 17 | } |
| 18 | |
| 19 | File::File() |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame^] | 20 | : error_details_(FILE_ERROR_FAILED), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 21 | created_(false), |
| 22 | async_(false) { |
| 23 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 24 | |
| 25 | #if !defined(OS_NACL) |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 26 | File::File(const FilePath& name, uint32 flags) |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame^] | 27 | : error_details_(FILE_OK), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 28 | created_(false), |
| 29 | async_(false) { |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 30 | Initialize(name, flags); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 31 | } |
| 32 | #endif |
| 33 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 34 | File::File(PlatformFile platform_file) |
| 35 | : file_(platform_file), |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 36 | error_details_(FILE_OK), |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 37 | created_(false), |
| 38 | async_(false) { |
zmo@chromium.org | 536b9a9 | 2014-03-18 11:39:03 +0900 | [diff] [blame] | 39 | #if defined(OS_POSIX) |
| 40 | DCHECK_GE(platform_file, -1); |
| 41 | #endif |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 42 | } |
| 43 | |
rvargas@chromium.org | ca704f4 | 2014-03-26 18:59:31 +0900 | [diff] [blame] | 44 | File::File(Error error_details) |
rvargas@chromium.org | 4826afb | 2014-06-24 14:40:09 +0900 | [diff] [blame^] | 45 | : error_details_(error_details), |
rvargas@chromium.org | ca704f4 | 2014-03-26 18:59:31 +0900 | [diff] [blame] | 46 | created_(false), |
| 47 | async_(false) { |
| 48 | } |
| 49 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 50 | File::File(RValue other) |
| 51 | : file_(other.object->TakePlatformFile()), |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 52 | error_details_(other.object->error_details()), |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 53 | created_(other.object->created()), |
| 54 | async_(other.object->async_) { |
| 55 | } |
| 56 | |
| 57 | File::~File() { |
rvargas@chromium.org | e60d967 | 2014-05-06 09:55:35 +0900 | [diff] [blame] | 58 | // Go through the AssertIOAllowed logic. |
| 59 | Close(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | File& File::operator=(RValue other) { |
| 63 | if (this != other.object) { |
| 64 | Close(); |
| 65 | SetPlatformFile(other.object->TakePlatformFile()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 66 | error_details_ = other.object->error_details(); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 67 | created_ = other.object->created(); |
| 68 | async_ = other.object->async_; |
| 69 | } |
| 70 | return *this; |
| 71 | } |
| 72 | |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 73 | #if !defined(OS_NACL) |
| 74 | void File::Initialize(const FilePath& name, uint32 flags) { |
| 75 | if (name.ReferencesParent()) { |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 76 | error_details_ = FILE_ERROR_ACCESS_DENIED; |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | InitializeUnsafe(name, flags); |
| 80 | } |
| 81 | #endif |
| 82 | |
mtomasz@chromium.org | a5d9be8 | 2014-05-30 19:07:30 +0900 | [diff] [blame] | 83 | std::string File::ErrorToString(Error error) { |
| 84 | switch (error) { |
| 85 | case FILE_OK: |
| 86 | return "FILE_OK"; |
| 87 | case FILE_ERROR_FAILED: |
| 88 | return "FILE_ERROR_FAILED"; |
| 89 | case FILE_ERROR_IN_USE: |
| 90 | return "FILE_ERROR_IN_USE"; |
| 91 | case FILE_ERROR_EXISTS: |
| 92 | return "FILE_ERROR_EXISTS"; |
| 93 | case FILE_ERROR_NOT_FOUND: |
| 94 | return "FILE_ERROR_NOT_FOUND"; |
| 95 | case FILE_ERROR_ACCESS_DENIED: |
| 96 | return "FILE_ERROR_ACCESS_DENIED"; |
| 97 | case FILE_ERROR_TOO_MANY_OPENED: |
| 98 | return "FILE_ERROR_TOO_MANY_OPENED"; |
| 99 | case FILE_ERROR_NO_MEMORY: |
| 100 | return "FILE_ERROR_NO_MEMORY"; |
| 101 | case FILE_ERROR_NO_SPACE: |
| 102 | return "FILE_ERROR_NO_SPACE"; |
| 103 | case FILE_ERROR_NOT_A_DIRECTORY: |
| 104 | return "FILE_ERROR_NOT_A_DIRECTORY"; |
| 105 | case FILE_ERROR_INVALID_OPERATION: |
| 106 | return "FILE_ERROR_INVALID_OPERATION"; |
| 107 | case FILE_ERROR_SECURITY: |
| 108 | return "FILE_ERROR_SECURITY"; |
| 109 | case FILE_ERROR_ABORT: |
| 110 | return "FILE_ERROR_ABORT"; |
| 111 | case FILE_ERROR_NOT_A_FILE: |
| 112 | return "FILE_ERROR_NOT_A_FILE"; |
| 113 | case FILE_ERROR_NOT_EMPTY: |
| 114 | return "FILE_ERROR_NOT_EMPTY"; |
| 115 | case FILE_ERROR_INVALID_URL: |
| 116 | return "FILE_ERROR_INVALID_URL"; |
| 117 | case FILE_ERROR_IO: |
| 118 | return "FILE_ERROR_IO"; |
| 119 | case FILE_ERROR_MAX: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | NOTREACHED(); |
| 124 | return ""; |
| 125 | } |
| 126 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 127 | } // namespace base |