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"
16 is_symbolic_link(false) {
23 : error_details_(FILE_ERROR_FAILED
),
29 File::File(const FilePath
& path
, uint32 flags
)
30 : error_details_(FILE_OK
),
33 Initialize(path
, flags
);
37 File::File(PlatformFile platform_file
)
38 : file_(platform_file
),
39 error_details_(FILE_OK
),
43 DCHECK_GE(platform_file
, -1);
47 File::File(Error error_details
)
48 : error_details_(error_details
),
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_
) {
62 // Go through the AssertIOAllowed logic.
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.
75 File
& File::operator=(RValue other
) {
76 if (this != other
.object
) {
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_
;
88 void File::Initialize(const FilePath
& path
, uint32 flags
) {
89 if (path
.ReferencesParent()) {
90 error_details_
= FILE_ERROR_ACCESS_DENIED
;
93 if (FileTracing::IsCategoryEnabled())
95 SCOPED_FILE_TRACE("Initialize");
96 DoInitialize(path
, flags
);
100 std::string
File::ErrorToString(Error error
) {
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";
135 return "FILE_ERROR_IO";
146 SCOPED_FILE_TRACE("Flush");
147 bool return_value
= DoFlush();
148 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer
.Elapsed());