blob: bfe87bdcc4dc9d20653130006d7065afac536c8f [file] [log] [blame]
rvargas@google.comb1ae3192013-11-28 10:31:31 +09001// 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.org12938d72013-12-04 09:46:32 +09005#include "base/files/file.h"
6
7// TODO(rvargas): remove this (needed for kInvalidPlatformFileValue).
rvargas@google.comb1ae3192013-11-28 10:31:31 +09008#include "base/platform_file.h"
9
10namespace base {
11
rvargas@chromium.org12938d72013-12-04 09:46:32 +090012File::Info::Info()
rvargas@google.comb1ae3192013-11-28 10:31:31 +090013 : size(0),
14 is_directory(false),
15 is_symbolic_link(false) {
16}
17
rvargas@chromium.org12938d72013-12-04 09:46:32 +090018File::Info::~Info() {
19}
20
21File::File()
22 : file_(kInvalidPlatformFileValue),
rvargas@chromium.orgca704f42014-03-26 18:59:31 +090023 error_details_(FILE_ERROR_FAILED),
rvargas@chromium.org12938d72013-12-04 09:46:32 +090024 created_(false),
25 async_(false) {
26}
rvargas@google.comb1ae3192013-11-28 10:31:31 +090027
28#if !defined(OS_NACL)
rvargas@chromium.org12938d72013-12-04 09:46:32 +090029File::File(const FilePath& name, uint32 flags)
30 : file_(kInvalidPlatformFileValue),
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090031 error_details_(FILE_OK),
rvargas@chromium.org12938d72013-12-04 09:46:32 +090032 created_(false),
33 async_(false) {
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090034 Initialize(name, flags);
rvargas@google.comb1ae3192013-11-28 10:31:31 +090035}
36#endif
37
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090038File::File(PlatformFile platform_file)
39 : file_(platform_file),
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090040 error_details_(FILE_OK),
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090041 created_(false),
42 async_(false) {
zmo@chromium.org536b9a92014-03-18 11:39:03 +090043#if defined(OS_POSIX)
44 DCHECK_GE(platform_file, -1);
45#endif
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090046}
47
rvargas@chromium.orgca704f42014-03-26 18:59:31 +090048File::File(Error error_details)
49 : file_(kInvalidPlatformFileValue),
50 error_details_(error_details),
51 created_(false),
52 async_(false) {
53}
54
rvargas@chromium.org12938d72013-12-04 09:46:32 +090055File::File(RValue other)
56 : file_(other.object->TakePlatformFile()),
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090057 error_details_(other.object->error_details()),
rvargas@chromium.org12938d72013-12-04 09:46:32 +090058 created_(other.object->created()),
59 async_(other.object->async_) {
60}
61
62File::~File() {
rvargas@chromium.orge60d9672014-05-06 09:55:35 +090063 // Go through the AssertIOAllowed logic.
64 Close();
rvargas@chromium.org12938d72013-12-04 09:46:32 +090065}
66
67File& File::operator=(RValue other) {
68 if (this != other.object) {
69 Close();
70 SetPlatformFile(other.object->TakePlatformFile());
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090071 error_details_ = other.object->error_details();
rvargas@chromium.org12938d72013-12-04 09:46:32 +090072 created_ = other.object->created();
73 async_ = other.object->async_;
74 }
75 return *this;
76}
77
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090078#if !defined(OS_NACL)
79void File::Initialize(const FilePath& name, uint32 flags) {
80 if (name.ReferencesParent()) {
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090081 error_details_ = FILE_ERROR_ACCESS_DENIED;
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090082 return;
83 }
84 InitializeUnsafe(name, flags);
85}
86#endif
87
mtomasz@chromium.orga5d9be82014-05-30 19:07:30 +090088std::string File::ErrorToString(Error error) {
89 switch (error) {
90 case FILE_OK:
91 return "FILE_OK";
92 case FILE_ERROR_FAILED:
93 return "FILE_ERROR_FAILED";
94 case FILE_ERROR_IN_USE:
95 return "FILE_ERROR_IN_USE";
96 case FILE_ERROR_EXISTS:
97 return "FILE_ERROR_EXISTS";
98 case FILE_ERROR_NOT_FOUND:
99 return "FILE_ERROR_NOT_FOUND";
100 case FILE_ERROR_ACCESS_DENIED:
101 return "FILE_ERROR_ACCESS_DENIED";
102 case FILE_ERROR_TOO_MANY_OPENED:
103 return "FILE_ERROR_TOO_MANY_OPENED";
104 case FILE_ERROR_NO_MEMORY:
105 return "FILE_ERROR_NO_MEMORY";
106 case FILE_ERROR_NO_SPACE:
107 return "FILE_ERROR_NO_SPACE";
108 case FILE_ERROR_NOT_A_DIRECTORY:
109 return "FILE_ERROR_NOT_A_DIRECTORY";
110 case FILE_ERROR_INVALID_OPERATION:
111 return "FILE_ERROR_INVALID_OPERATION";
112 case FILE_ERROR_SECURITY:
113 return "FILE_ERROR_SECURITY";
114 case FILE_ERROR_ABORT:
115 return "FILE_ERROR_ABORT";
116 case FILE_ERROR_NOT_A_FILE:
117 return "FILE_ERROR_NOT_A_FILE";
118 case FILE_ERROR_NOT_EMPTY:
119 return "FILE_ERROR_NOT_EMPTY";
120 case FILE_ERROR_INVALID_URL:
121 return "FILE_ERROR_INVALID_URL";
122 case FILE_ERROR_IO:
123 return "FILE_ERROR_IO";
124 case FILE_ERROR_MAX:
125 break;
126 }
127
128 NOTREACHED();
129 return "";
130}
131
rvargas@google.comb1ae3192013-11-28 10:31:31 +0900132} // namespace base