QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / base / files / file.cc
blob47b9f88f1888d1d3cf4b70070b460bd8e8880289
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.
5 #include "base/files/file.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_tracing.h"
8 #include "base/metrics/histogram.h"
9 #include "base/timer/elapsed_timer.h"
11 namespace base {
13 File::Info::Info()
14 : size(0),
15 is_directory(false),
16 is_symbolic_link(false) {
19 File::Info::~Info() {
22 File::File()
23 : error_details_(FILE_ERROR_FAILED),
24 created_(false),
25 async_(false) {
28 #if !defined(OS_NACL)
29 File::File(const FilePath& path, uint32 flags)
30 : error_details_(FILE_OK),
31 created_(false),
32 async_(false) {
33 Initialize(path, flags);
35 #endif
37 File::File(PlatformFile platform_file)
38 : file_(platform_file),
39 error_details_(FILE_OK),
40 created_(false),
41 async_(false) {
42 #if defined(OS_POSIX)
43 DCHECK_GE(platform_file, -1);
44 #endif
47 File::File(Error error_details)
48 : error_details_(error_details),
49 created_(false),
50 async_(false) {
53 File::File(RValue other)
54 : file_(other.object->TakePlatformFile()),
55 tracing_path_(other.object->tracing_path_),
56 error_details_(other.object->error_details()),
57 created_(other.object->created()),
58 async_(other.object->async_) {
61 File::~File() {
62 // Go through the AssertIOAllowed logic.
63 Close();
66 // static
67 File 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();
75 File& File::operator=(RValue other) {
76 if (this != other.object) {
77 Close();
78 SetPlatformFile(other.object->TakePlatformFile());
79 tracing_path_ = other.object->tracing_path_;
80 error_details_ = other.object->error_details();
81 created_ = other.object->created();
82 async_ = other.object->async_;
84 return *this;
87 #if !defined(OS_NACL)
88 void File::Initialize(const FilePath& path, uint32 flags) {
89 if (path.ReferencesParent()) {
90 error_details_ = FILE_ERROR_ACCESS_DENIED;
91 return;
93 if (FileTracing::IsCategoryEnabled())
94 tracing_path_ = path;
95 SCOPED_FILE_TRACE("Initialize");
96 DoInitialize(path, flags);
98 #endif
100 std::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;
140 NOTREACHED();
141 return "";
144 bool File::Flush() {
145 ElapsedTimer timer;
146 SCOPED_FILE_TRACE("Flush");
147 bool return_value = DoFlush();
148 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed());
149 return return_value;
152 } // namespace base