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
5 #include "base/files/file.h"
6 #include "base/files/file_path.h"
7 #include "util/build_config.h"
8
9 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
10 #include <errno.h>
11 #endif
12
13 namespace base {
14
Info()15 File::Info::Info() : size(0), is_directory(false), is_symbolic_link(false) {}
16
17 File::Info::~Info() = default;
18
File()19 File::File()
20 : error_details_(FILE_ERROR_FAILED), created_(false) {}
21
File(const FilePath & path,uint32_t flags)22 File::File(const FilePath& path, uint32_t flags)
23 : error_details_(FILE_OK), created_(false) {
24 Initialize(path, flags);
25 }
26
File(PlatformFile platform_file)27 File::File(PlatformFile platform_file)
28 : file_(platform_file),
29 error_details_(FILE_OK),
30 created_(false) {
31 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
32 DCHECK_GE(platform_file, -1);
33 #endif
34 }
35
File(Error error_details)36 File::File(Error error_details)
37 : error_details_(error_details), created_(false) {}
38
File(File && other)39 File::File(File&& other)
40 : file_(other.TakePlatformFile()),
41 error_details_(other.error_details()),
42 created_(other.created()) {}
43
~File()44 File::~File() {
45 // Go through the AssertIOAllowed logic.
46 Close();
47 }
48
operator =(File && other)49 File& File::operator=(File&& other) {
50 Close();
51 SetPlatformFile(other.TakePlatformFile());
52 error_details_ = other.error_details();
53 created_ = other.created();
54 return *this;
55 }
56
Initialize(const FilePath & path,uint32_t flags)57 void File::Initialize(const FilePath& path, uint32_t flags) {
58 if (path.ReferencesParent()) {
59 #if defined(OS_WIN)
60 ::SetLastError(ERROR_ACCESS_DENIED);
61 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
62 errno = EACCES;
63 #else
64 #error Unsupported platform
65 #endif
66 error_details_ = FILE_ERROR_ACCESS_DENIED;
67 return;
68 }
69 DoInitialize(path, flags);
70 }
71
ErrorToString(Error error)72 std::string File::ErrorToString(Error error) {
73 switch (error) {
74 case FILE_OK:
75 return "FILE_OK";
76 case FILE_ERROR_FAILED:
77 return "FILE_ERROR_FAILED";
78 case FILE_ERROR_IN_USE:
79 return "FILE_ERROR_IN_USE";
80 case FILE_ERROR_EXISTS:
81 return "FILE_ERROR_EXISTS";
82 case FILE_ERROR_NOT_FOUND:
83 return "FILE_ERROR_NOT_FOUND";
84 case FILE_ERROR_ACCESS_DENIED:
85 return "FILE_ERROR_ACCESS_DENIED";
86 case FILE_ERROR_TOO_MANY_OPENED:
87 return "FILE_ERROR_TOO_MANY_OPENED";
88 case FILE_ERROR_NO_MEMORY:
89 return "FILE_ERROR_NO_MEMORY";
90 case FILE_ERROR_NO_SPACE:
91 return "FILE_ERROR_NO_SPACE";
92 case FILE_ERROR_NOT_A_DIRECTORY:
93 return "FILE_ERROR_NOT_A_DIRECTORY";
94 case FILE_ERROR_INVALID_OPERATION:
95 return "FILE_ERROR_INVALID_OPERATION";
96 case FILE_ERROR_SECURITY:
97 return "FILE_ERROR_SECURITY";
98 case FILE_ERROR_ABORT:
99 return "FILE_ERROR_ABORT";
100 case FILE_ERROR_NOT_A_FILE:
101 return "FILE_ERROR_NOT_A_FILE";
102 case FILE_ERROR_NOT_EMPTY:
103 return "FILE_ERROR_NOT_EMPTY";
104 case FILE_ERROR_INVALID_URL:
105 return "FILE_ERROR_INVALID_URL";
106 case FILE_ERROR_IO:
107 return "FILE_ERROR_IO";
108 case FILE_ERROR_MAX:
109 break;
110 }
111
112 NOTREACHED();
113 return "";
114 }
115
116 } // namespace base
117