1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/files/file.h"
6
7 #include <utility>
8
9 #include "base/check_op.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_tracing.h"
12 #include "base/metrics/histogram.h"
13 #include "base/notreached.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "base/trace_event/base_tracing.h"
17 #include "build/build_config.h"
18
19 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
20 #include <errno.h>
21 #endif
22
23 namespace base {
24
25 File::Info::Info() = default;
26
27 File::Info::~Info() = default;
28
29 File::File() = default;
30
31 #if !BUILDFLAG(IS_NACL)
File(const FilePath & path,uint32_t flags)32 File::File(const FilePath& path, uint32_t flags) : error_details_(FILE_OK) {
33 Initialize(path, flags);
34 }
35 #endif
36
File(ScopedPlatformFile platform_file)37 File::File(ScopedPlatformFile platform_file)
38 : File(std::move(platform_file), false) {}
39
File(PlatformFile platform_file)40 File::File(PlatformFile platform_file) : File(platform_file, false) {}
41
File(ScopedPlatformFile platform_file,bool async)42 File::File(ScopedPlatformFile platform_file, bool async)
43 : file_(std::move(platform_file)), error_details_(FILE_OK), async_(async) {
44 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
45 DCHECK_GE(file_.get(), -1);
46 #endif
47 }
48
File(PlatformFile platform_file,bool async)49 File::File(PlatformFile platform_file, bool async)
50 : file_(platform_file),
51 error_details_(FILE_OK),
52 async_(async) {
53 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
54 DCHECK_GE(platform_file, -1);
55 #endif
56 }
57
File(Error error_details)58 File::File(Error error_details) : error_details_(error_details) {}
59
File(File && other)60 File::File(File&& other)
61 : file_(other.TakePlatformFile()),
62 tracing_path_(other.tracing_path_),
63 error_details_(other.error_details()),
64 created_(other.created()),
65 async_(other.async_) {}
66
~File()67 File::~File() {
68 // Go through the AssertIOAllowed logic.
69 Close();
70 }
71
operator =(File && other)72 File& File::operator=(File&& other) {
73 Close();
74 SetPlatformFile(other.TakePlatformFile());
75 tracing_path_ = other.tracing_path_;
76 error_details_ = other.error_details();
77 created_ = other.created();
78 async_ = other.async_;
79 return *this;
80 }
81
82 #if !BUILDFLAG(IS_NACL)
Initialize(const FilePath & path,uint32_t flags)83 void File::Initialize(const FilePath& path, uint32_t flags) {
84 if (path.ReferencesParent()) {
85 #if BUILDFLAG(IS_WIN)
86 ::SetLastError(ERROR_ACCESS_DENIED);
87 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
88 errno = EACCES;
89 #else
90 #error Unsupported platform
91 #endif
92 error_details_ = FILE_ERROR_ACCESS_DENIED;
93 return;
94 }
95 if (FileTracing::IsCategoryEnabled())
96 tracing_path_ = path;
97 SCOPED_FILE_TRACE("Initialize");
98 DoInitialize(path, flags);
99 }
100 #endif
101
ReadAndCheck(int64_t offset,span<uint8_t> data)102 bool File::ReadAndCheck(int64_t offset, span<uint8_t> data) {
103 int size = checked_cast<int>(data.size());
104 return Read(offset, reinterpret_cast<char*>(data.data()), size) == size;
105 }
106
ReadAtCurrentPosAndCheck(span<uint8_t> data)107 bool File::ReadAtCurrentPosAndCheck(span<uint8_t> data) {
108 int size = checked_cast<int>(data.size());
109 return ReadAtCurrentPos(reinterpret_cast<char*>(data.data()), size) == size;
110 }
111
WriteAndCheck(int64_t offset,span<const uint8_t> data)112 bool File::WriteAndCheck(int64_t offset, span<const uint8_t> data) {
113 int size = checked_cast<int>(data.size());
114 return Write(offset, reinterpret_cast<const char*>(data.data()), size) ==
115 size;
116 }
117
WriteAtCurrentPosAndCheck(span<const uint8_t> data)118 bool File::WriteAtCurrentPosAndCheck(span<const uint8_t> data) {
119 int size = checked_cast<int>(data.size());
120 return WriteAtCurrentPos(reinterpret_cast<const char*>(data.data()), size) ==
121 size;
122 }
123
124 // static
ErrorToString(Error error)125 std::string File::ErrorToString(Error error) {
126 switch (error) {
127 case FILE_OK:
128 return "FILE_OK";
129 case FILE_ERROR_FAILED:
130 return "FILE_ERROR_FAILED";
131 case FILE_ERROR_IN_USE:
132 return "FILE_ERROR_IN_USE";
133 case FILE_ERROR_EXISTS:
134 return "FILE_ERROR_EXISTS";
135 case FILE_ERROR_NOT_FOUND:
136 return "FILE_ERROR_NOT_FOUND";
137 case FILE_ERROR_ACCESS_DENIED:
138 return "FILE_ERROR_ACCESS_DENIED";
139 case FILE_ERROR_TOO_MANY_OPENED:
140 return "FILE_ERROR_TOO_MANY_OPENED";
141 case FILE_ERROR_NO_MEMORY:
142 return "FILE_ERROR_NO_MEMORY";
143 case FILE_ERROR_NO_SPACE:
144 return "FILE_ERROR_NO_SPACE";
145 case FILE_ERROR_NOT_A_DIRECTORY:
146 return "FILE_ERROR_NOT_A_DIRECTORY";
147 case FILE_ERROR_INVALID_OPERATION:
148 return "FILE_ERROR_INVALID_OPERATION";
149 case FILE_ERROR_SECURITY:
150 return "FILE_ERROR_SECURITY";
151 case FILE_ERROR_ABORT:
152 return "FILE_ERROR_ABORT";
153 case FILE_ERROR_NOT_A_FILE:
154 return "FILE_ERROR_NOT_A_FILE";
155 case FILE_ERROR_NOT_EMPTY:
156 return "FILE_ERROR_NOT_EMPTY";
157 case FILE_ERROR_INVALID_URL:
158 return "FILE_ERROR_INVALID_URL";
159 case FILE_ERROR_IO:
160 return "FILE_ERROR_IO";
161 case FILE_ERROR_MAX:
162 break;
163 }
164
165 NOTREACHED();
166 return "";
167 }
168
WriteIntoTrace(perfetto::TracedValue context) const169 void File::WriteIntoTrace(perfetto::TracedValue context) const {
170 auto dict = std::move(context).WriteDictionary();
171 dict.Add("is_valid", IsValid());
172 dict.Add("created", created_);
173 dict.Add("async", async_);
174 dict.Add("error_details", ErrorToString(error_details_));
175 }
176
177 } // namespace base
178