blob: 47b9f88f1888d1d3cf4b70070b460bd8e8880289 [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"
rvargas@chromium.org4826afb2014-06-24 14:40:09 +09006#include "base/files/file_path.h"
dbeamfe14ece2015-05-11 16:53:47 +09007#include "base/files/file_tracing.h"
tnagel7eaf4772015-04-03 19:11:46 +09008#include "base/metrics/histogram.h"
9#include "base/timer/elapsed_timer.h"
rvargas@google.comb1ae3192013-11-28 10:31:31 +090010
11namespace base {
12
rvargas@chromium.org12938d72013-12-04 09:46:32 +090013File::Info::Info()
rvargas@google.comb1ae3192013-11-28 10:31:31 +090014 : size(0),
15 is_directory(false),
16 is_symbolic_link(false) {
17}
18
rvargas@chromium.org12938d72013-12-04 09:46:32 +090019File::Info::~Info() {
20}
21
22File::File()
rvargas@chromium.org4826afb2014-06-24 14:40:09 +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)
dbeamfe14ece2015-05-11 16:53:47 +090029File::File(const FilePath& path, uint32 flags)
rvargas@chromium.org4826afb2014-06-24 14:40:09 +090030 : error_details_(FILE_OK),
rvargas@chromium.org12938d72013-12-04 09:46:32 +090031 created_(false),
32 async_(false) {
dbeamfe14ece2015-05-11 16:53:47 +090033 Initialize(path, flags);
rvargas@google.comb1ae3192013-11-28 10:31:31 +090034}
35#endif
36
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090037File::File(PlatformFile platform_file)
38 : file_(platform_file),
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090039 error_details_(FILE_OK),
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090040 created_(false),
41 async_(false) {
zmo@chromium.org536b9a92014-03-18 11:39:03 +090042#if defined(OS_POSIX)
43 DCHECK_GE(platform_file, -1);
44#endif
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090045}
46
rvargas@chromium.orgca704f42014-03-26 18:59:31 +090047File::File(Error error_details)
rvargas@chromium.org4826afb2014-06-24 14:40:09 +090048 : error_details_(error_details),
rvargas@chromium.orgca704f42014-03-26 18:59:31 +090049 created_(false),
50 async_(false) {
51}
52
rvargas@chromium.org12938d72013-12-04 09:46:32 +090053File::File(RValue other)
54 : file_(other.object->TakePlatformFile()),
dbeam8054d4e2015-07-02 12:33:08 +090055 tracing_path_(other.object->tracing_path_),
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090056 error_details_(other.object->error_details()),
rvargas@chromium.org12938d72013-12-04 09:46:32 +090057 created_(other.object->created()),
58 async_(other.object->async_) {
59}
60
61File::~File() {
rvargas@chromium.orge60d9672014-05-06 09:55:35 +090062 // Go through the AssertIOAllowed logic.
63 Close();
rvargas@chromium.org12938d72013-12-04 09:46:32 +090064}
65
reillyg9b86e312015-06-16 09:48:48 +090066// static
67File 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.org12938d72013-12-04 09:46:32 +090075File& File::operator=(RValue other) {
76 if (this != other.object) {
77 Close();
78 SetPlatformFile(other.object->TakePlatformFile());
dbeam8054d4e2015-07-02 12:33:08 +090079 tracing_path_ = other.object->tracing_path_;
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090080 error_details_ = other.object->error_details();
rvargas@chromium.org12938d72013-12-04 09:46:32 +090081 created_ = other.object->created();
82 async_ = other.object->async_;
83 }
84 return *this;
85}
86
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090087#if !defined(OS_NACL)
dbeamfe14ece2015-05-11 16:53:47 +090088void File::Initialize(const FilePath& path, uint32 flags) {
89 if (path.ReferencesParent()) {
rvargas@chromium.org9cce0322014-01-09 07:30:21 +090090 error_details_ = FILE_ERROR_ACCESS_DENIED;
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090091 return;
92 }
dbeam8054d4e2015-07-02 12:33:08 +090093 if (FileTracing::IsCategoryEnabled())
94 tracing_path_ = path;
dbeamfe14ece2015-05-11 16:53:47 +090095 SCOPED_FILE_TRACE("Initialize");
dbeam8054d4e2015-07-02 12:33:08 +090096 DoInitialize(path, flags);
rvargas@chromium.orge207eae2014-01-04 07:14:15 +090097}
98#endif
99
mtomasz@chromium.orga5d9be82014-05-30 19:07:30 +0900100std::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
tnagel7eaf4772015-04-03 19:11:46 +0900144bool File::Flush() {
145 ElapsedTimer timer;
dbeamfe14ece2015-05-11 16:53:47 +0900146 SCOPED_FILE_TRACE("Flush");
tnagel7eaf4772015-04-03 19:11:46 +0900147 bool return_value = DoFlush();
148 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed());
149 return return_value;
150}
151
rvargas@google.comb1ae3192013-11-28 10:31:31 +0900152} // namespace base