1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/files/file.h"
6*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
7*635a8641SAndroid Build Coastguard Worker #include "base/files/file_tracing.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/timer/elapsed_timer.h"
10*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
13*635a8641SAndroid Build Coastguard Worker #include <errno.h>
14*635a8641SAndroid Build Coastguard Worker #endif
15*635a8641SAndroid Build Coastguard Worker
16*635a8641SAndroid Build Coastguard Worker namespace base {
17*635a8641SAndroid Build Coastguard Worker
Info()18*635a8641SAndroid Build Coastguard Worker File::Info::Info()
19*635a8641SAndroid Build Coastguard Worker : size(0),
20*635a8641SAndroid Build Coastguard Worker is_directory(false),
21*635a8641SAndroid Build Coastguard Worker is_symbolic_link(false) {
22*635a8641SAndroid Build Coastguard Worker }
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker File::Info::~Info() = default;
25*635a8641SAndroid Build Coastguard Worker
File()26*635a8641SAndroid Build Coastguard Worker File::File()
27*635a8641SAndroid Build Coastguard Worker : error_details_(FILE_ERROR_FAILED),
28*635a8641SAndroid Build Coastguard Worker created_(false),
29*635a8641SAndroid Build Coastguard Worker async_(false) {
30*635a8641SAndroid Build Coastguard Worker }
31*635a8641SAndroid Build Coastguard Worker
32*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL)
File(const FilePath & path,uint32_t flags)33*635a8641SAndroid Build Coastguard Worker File::File(const FilePath& path, uint32_t flags)
34*635a8641SAndroid Build Coastguard Worker : error_details_(FILE_OK), created_(false), async_(false) {
35*635a8641SAndroid Build Coastguard Worker Initialize(path, flags);
36*635a8641SAndroid Build Coastguard Worker }
37*635a8641SAndroid Build Coastguard Worker #endif
38*635a8641SAndroid Build Coastguard Worker
File(PlatformFile platform_file)39*635a8641SAndroid Build Coastguard Worker File::File(PlatformFile platform_file) : File(platform_file, false) {}
40*635a8641SAndroid Build Coastguard Worker
File(PlatformFile platform_file,bool async)41*635a8641SAndroid Build Coastguard Worker File::File(PlatformFile platform_file, bool async)
42*635a8641SAndroid Build Coastguard Worker : file_(platform_file),
43*635a8641SAndroid Build Coastguard Worker error_details_(FILE_OK),
44*635a8641SAndroid Build Coastguard Worker created_(false),
45*635a8641SAndroid Build Coastguard Worker async_(async) {
46*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
47*635a8641SAndroid Build Coastguard Worker DCHECK_GE(platform_file, -1);
48*635a8641SAndroid Build Coastguard Worker #endif
49*635a8641SAndroid Build Coastguard Worker }
50*635a8641SAndroid Build Coastguard Worker
File(Error error_details)51*635a8641SAndroid Build Coastguard Worker File::File(Error error_details)
52*635a8641SAndroid Build Coastguard Worker : error_details_(error_details),
53*635a8641SAndroid Build Coastguard Worker created_(false),
54*635a8641SAndroid Build Coastguard Worker async_(false) {
55*635a8641SAndroid Build Coastguard Worker }
56*635a8641SAndroid Build Coastguard Worker
File(File && other)57*635a8641SAndroid Build Coastguard Worker File::File(File&& other)
58*635a8641SAndroid Build Coastguard Worker : file_(other.TakePlatformFile()),
59*635a8641SAndroid Build Coastguard Worker tracing_path_(other.tracing_path_),
60*635a8641SAndroid Build Coastguard Worker error_details_(other.error_details()),
61*635a8641SAndroid Build Coastguard Worker created_(other.created()),
62*635a8641SAndroid Build Coastguard Worker async_(other.async_) {}
63*635a8641SAndroid Build Coastguard Worker
~File()64*635a8641SAndroid Build Coastguard Worker File::~File() {
65*635a8641SAndroid Build Coastguard Worker // Go through the AssertIOAllowed logic.
66*635a8641SAndroid Build Coastguard Worker Close();
67*635a8641SAndroid Build Coastguard Worker }
68*635a8641SAndroid Build Coastguard Worker
operator =(File && other)69*635a8641SAndroid Build Coastguard Worker File& File::operator=(File&& other) {
70*635a8641SAndroid Build Coastguard Worker Close();
71*635a8641SAndroid Build Coastguard Worker SetPlatformFile(other.TakePlatformFile());
72*635a8641SAndroid Build Coastguard Worker tracing_path_ = other.tracing_path_;
73*635a8641SAndroid Build Coastguard Worker error_details_ = other.error_details();
74*635a8641SAndroid Build Coastguard Worker created_ = other.created();
75*635a8641SAndroid Build Coastguard Worker async_ = other.async_;
76*635a8641SAndroid Build Coastguard Worker return *this;
77*635a8641SAndroid Build Coastguard Worker }
78*635a8641SAndroid Build Coastguard Worker
79*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL)
Initialize(const FilePath & path,uint32_t flags)80*635a8641SAndroid Build Coastguard Worker void File::Initialize(const FilePath& path, uint32_t flags) {
81*635a8641SAndroid Build Coastguard Worker if (path.ReferencesParent()) {
82*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
83*635a8641SAndroid Build Coastguard Worker ::SetLastError(ERROR_ACCESS_DENIED);
84*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
85*635a8641SAndroid Build Coastguard Worker errno = EACCES;
86*635a8641SAndroid Build Coastguard Worker #else
87*635a8641SAndroid Build Coastguard Worker #error Unsupported platform
88*635a8641SAndroid Build Coastguard Worker #endif
89*635a8641SAndroid Build Coastguard Worker error_details_ = FILE_ERROR_ACCESS_DENIED;
90*635a8641SAndroid Build Coastguard Worker return;
91*635a8641SAndroid Build Coastguard Worker }
92*635a8641SAndroid Build Coastguard Worker if (FileTracing::IsCategoryEnabled())
93*635a8641SAndroid Build Coastguard Worker tracing_path_ = path;
94*635a8641SAndroid Build Coastguard Worker SCOPED_FILE_TRACE("Initialize");
95*635a8641SAndroid Build Coastguard Worker DoInitialize(path, flags);
96*635a8641SAndroid Build Coastguard Worker }
97*635a8641SAndroid Build Coastguard Worker #endif
98*635a8641SAndroid Build Coastguard Worker
ErrorToString(Error error)99*635a8641SAndroid Build Coastguard Worker std::string File::ErrorToString(Error error) {
100*635a8641SAndroid Build Coastguard Worker switch (error) {
101*635a8641SAndroid Build Coastguard Worker case FILE_OK:
102*635a8641SAndroid Build Coastguard Worker return "FILE_OK";
103*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_FAILED:
104*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_FAILED";
105*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_IN_USE:
106*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_IN_USE";
107*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_EXISTS:
108*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_EXISTS";
109*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NOT_FOUND:
110*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NOT_FOUND";
111*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_ACCESS_DENIED:
112*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_ACCESS_DENIED";
113*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_TOO_MANY_OPENED:
114*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_TOO_MANY_OPENED";
115*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NO_MEMORY:
116*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NO_MEMORY";
117*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NO_SPACE:
118*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NO_SPACE";
119*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NOT_A_DIRECTORY:
120*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NOT_A_DIRECTORY";
121*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_INVALID_OPERATION:
122*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_INVALID_OPERATION";
123*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_SECURITY:
124*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_SECURITY";
125*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_ABORT:
126*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_ABORT";
127*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NOT_A_FILE:
128*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NOT_A_FILE";
129*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_NOT_EMPTY:
130*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_NOT_EMPTY";
131*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_INVALID_URL:
132*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_INVALID_URL";
133*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_IO:
134*635a8641SAndroid Build Coastguard Worker return "FILE_ERROR_IO";
135*635a8641SAndroid Build Coastguard Worker case FILE_ERROR_MAX:
136*635a8641SAndroid Build Coastguard Worker break;
137*635a8641SAndroid Build Coastguard Worker }
138*635a8641SAndroid Build Coastguard Worker
139*635a8641SAndroid Build Coastguard Worker NOTREACHED();
140*635a8641SAndroid Build Coastguard Worker return "";
141*635a8641SAndroid Build Coastguard Worker }
142*635a8641SAndroid Build Coastguard Worker
143*635a8641SAndroid Build Coastguard Worker } // namespace base
144