OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/filesystem/directory_impl.h" | 5 #include "components/filesystem/directory_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 mojo::InterfaceRequest<File> file, | 61 mojo::InterfaceRequest<File> file, |
62 uint32_t open_flags, | 62 uint32_t open_flags, |
63 const OpenFileCallback& callback) { | 63 const OpenFileCallback& callback) { |
64 base::FilePath path; | 64 base::FilePath path; |
65 FileError error = ValidatePath(raw_path, directory_path_, &path); | 65 FileError error = ValidatePath(raw_path, directory_path_, &path); |
66 if (error != FileError::OK) { | 66 if (error != FileError::OK) { |
67 callback.Run(error); | 67 callback.Run(error); |
68 return; | 68 return; |
69 } | 69 } |
70 | 70 |
71 #if defined(OS_WIN) | 71 if (base::DirectoryExists(path)) { |
72 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. | |
73 if (base::DirectoryExists(path)) | |
74 open_flags |= base::File::FLAG_BACKUP_SEMANTICS; | |
75 #endif // OS_WIN | |
76 | |
77 base::File base_file(path, open_flags); | |
78 if (!base_file.IsValid()) { | |
79 callback.Run(GetError(base_file)); | |
80 return; | |
81 } | |
82 | |
83 base::File::Info info; | |
84 if (!base_file.GetInfo(&info)) { | |
85 callback.Run(FileError::FAILED); | |
86 return; | |
87 } | |
88 | |
89 if (info.is_directory) { | |
90 // We must not return directories as files. In the file abstraction, we can | 72 // We must not return directories as files. In the file abstraction, we can |
91 // fetch raw file descriptors over mojo pipes, and passing a file | 73 // fetch raw file descriptors over mojo pipes, and passing a file |
92 // descriptor to a directory is a sandbox escape on Windows. | 74 // descriptor to a directory is a sandbox escape on Windows. |
93 callback.Run(FileError::NOT_A_FILE); | 75 callback.Run(FileError::NOT_A_FILE); |
94 return; | 76 return; |
95 } | 77 } |
96 | 78 |
| 79 base::File base_file(path, open_flags); |
| 80 if (!base_file.IsValid()) { |
| 81 callback.Run(GetError(base_file)); |
| 82 return; |
| 83 } |
| 84 |
97 if (file.is_pending()) { | 85 if (file.is_pending()) { |
98 new FileImpl(std::move(file), path, std::move(base_file), lock_table_); | 86 new FileImpl(std::move(file), path, std::move(base_file), lock_table_); |
99 } | 87 } |
100 callback.Run(FileError::OK); | 88 callback.Run(FileError::OK); |
101 } | 89 } |
102 | 90 |
103 void DirectoryImpl::OpenFileHandle(const mojo::String& raw_path, | 91 void DirectoryImpl::OpenFileHandle(const mojo::String& raw_path, |
104 uint32_t open_flags, | 92 uint32_t open_flags, |
105 const OpenFileHandleCallback& callback) { | 93 const OpenFileHandleCallback& callback) { |
106 base::FilePath path; | 94 base::FilePath path; |
107 FileError error = ValidatePath(raw_path, directory_path_, &path); | 95 FileError error = ValidatePath(raw_path, directory_path_, &path); |
108 if (error != FileError::OK) { | 96 if (error != FileError::OK) { |
109 callback.Run(error, ScopedHandle()); | 97 callback.Run(error, ScopedHandle()); |
110 return; | 98 return; |
111 } | 99 } |
112 | 100 |
113 #if defined(OS_WIN) | 101 if (base::DirectoryExists(path)) { |
114 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. | |
115 if (base::DirectoryExists(path)) | |
116 open_flags |= base::File::FLAG_BACKUP_SEMANTICS; | |
117 #endif // OS_WIN | |
118 | |
119 base::File base_file(path, open_flags); | |
120 if (!base_file.IsValid()) { | |
121 callback.Run(GetError(base_file), ScopedHandle()); | |
122 return; | |
123 } | |
124 | |
125 base::File::Info info; | |
126 if (!base_file.GetInfo(&info)) { | |
127 callback.Run(FileError::FAILED, ScopedHandle()); | |
128 return; | |
129 } | |
130 | |
131 if (info.is_directory) { | |
132 // We must not return directories as files. In the file abstraction, we can | 102 // We must not return directories as files. In the file abstraction, we can |
133 // fetch raw file descriptors over mojo pipes, and passing a file | 103 // fetch raw file descriptors over mojo pipes, and passing a file |
134 // descriptor to a directory is a sandbox escape on Windows. | 104 // descriptor to a directory is a sandbox escape on Windows. |
135 callback.Run(FileError::NOT_A_FILE, ScopedHandle()); | 105 callback.Run(FileError::NOT_A_FILE, ScopedHandle()); |
136 return; | 106 return; |
137 } | 107 } |
138 | 108 |
| 109 base::File base_file(path, open_flags); |
| 110 if (!base_file.IsValid()) { |
| 111 callback.Run(GetError(base_file), ScopedHandle()); |
| 112 return; |
| 113 } |
| 114 |
139 MojoHandle mojo_handle; | 115 MojoHandle mojo_handle; |
140 MojoResult create_result = MojoCreatePlatformHandleWrapper( | 116 MojoResult create_result = MojoCreatePlatformHandleWrapper( |
141 base_file.TakePlatformFile(), &mojo_handle); | 117 base_file.TakePlatformFile(), &mojo_handle); |
142 if (create_result != MOJO_RESULT_OK) { | 118 if (create_result != MOJO_RESULT_OK) { |
143 callback.Run(FileError::FAILED, ScopedHandle()); | 119 callback.Run(FileError::FAILED, ScopedHandle()); |
144 return; | 120 return; |
145 } | 121 } |
146 | 122 |
147 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); | 123 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); |
148 } | 124 } |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 data_size) == -1) { | 327 data_size) == -1) { |
352 callback.Run(GetError(base_file)); | 328 callback.Run(GetError(base_file)); |
353 return; | 329 return; |
354 } | 330 } |
355 } | 331 } |
356 | 332 |
357 callback.Run(FileError::OK); | 333 callback.Run(FileError::OK); |
358 } | 334 } |
359 | 335 |
360 } // namespace filesystem | 336 } // namespace filesystem |
OLD | NEW |