OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_util_proxy.h" | 5 #include "base/file_util_proxy.h" |
6 | 6 |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 | 8 |
9 // TODO(jianli): Move the code from anonymous namespace to base namespace so | 9 // TODO(jianli): Move the code from anonymous namespace to base namespace so |
10 // that all of the base:: prefixes would be unnecessary. | 10 // that all of the base:: prefixes would be unnecessary. |
11 namespace { | 11 namespace { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Performs common checks for move and copy. | 15 // Performs common checks for move and copy. |
16 // This also removes the destination directory if it's non-empty and all other | 16 // This also removes the destination directory if it's non-empty and all other |
17 // checks are passed (so that the copy/move correctly overwrites the destination
). | 17 // checks are passed (so that the copy/move correctly overwrites the |
| 18 // destination). |
18 static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( | 19 static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( |
19 const FilePath& src_file_path, | 20 const FilePath& src_file_path, |
20 const FilePath& dest_file_path) { | 21 const FilePath& dest_file_path) { |
21 // Exits earlier if the source path does not exist. | 22 // Exits earlier if the source path does not exist. |
22 if (!file_util::PathExists(src_file_path)) | 23 if (!file_util::PathExists(src_file_path)) |
23 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 24 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
24 | 25 |
25 // The parent of the |dest_file_path| does not exist. | 26 // The parent of the |dest_file_path| does not exist. |
26 if (!file_util::DirectoryExists(dest_file_path.DirName())) | 27 if (!file_util::DirectoryExists(dest_file_path.DirName())) |
27 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 28 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | 113 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; |
113 base::PlatformFileError error_code_; | 114 base::PlatformFileError error_code_; |
114 }; | 115 }; |
115 | 116 |
116 class RelayCreateOrOpen : public MessageLoopRelay { | 117 class RelayCreateOrOpen : public MessageLoopRelay { |
117 public: | 118 public: |
118 RelayCreateOrOpen( | 119 RelayCreateOrOpen( |
119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 120 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
120 const FilePath& file_path, | 121 const FilePath& file_path, |
121 int file_flags, | 122 int file_flags, |
122 bool return_no_handle, | |
123 base::FileUtilProxy::CreateOrOpenCallback* callback) | 123 base::FileUtilProxy::CreateOrOpenCallback* callback) |
124 : message_loop_proxy_(message_loop_proxy), | 124 : message_loop_proxy_(message_loop_proxy), |
125 file_path_(file_path), | 125 file_path_(file_path), |
126 file_flags_(file_flags), | 126 file_flags_(file_flags), |
127 callback_(callback), | 127 callback_(callback), |
128 file_handle_(base::kInvalidPlatformFileValue), | 128 file_handle_(base::kInvalidPlatformFileValue), |
129 return_no_handle_(return_no_handle), | |
130 created_(false) { | 129 created_(false) { |
131 DCHECK(callback); | 130 DCHECK(callback); |
132 } | 131 } |
133 | 132 |
134 protected: | 133 protected: |
135 virtual ~RelayCreateOrOpen() { | 134 virtual ~RelayCreateOrOpen() { |
136 if (file_handle_ != base::kInvalidPlatformFileValue) | 135 if (file_handle_ != base::kInvalidPlatformFileValue) |
137 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); | 136 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
138 } | 137 } |
139 | 138 |
140 virtual void RunWork() { | 139 virtual void RunWork() { |
141 if (!file_util::DirectoryExists(file_path_.DirName())) { | 140 if (!file_util::DirectoryExists(file_path_.DirName())) { |
142 // If its parent does not exist, should return NOT_FOUND error. | 141 // If its parent does not exist, should return NOT_FOUND error. |
143 set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); | 142 set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
144 return; | 143 return; |
145 } | 144 } |
146 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 145 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
147 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, | 146 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, |
148 &created_, &error_code); | 147 &created_, &error_code); |
149 // If the return_no_handle is true the caller is not interested | |
150 // in the file_handle_. Close it right now. | |
151 if (return_no_handle_ && file_handle_ != base::kInvalidPlatformFileValue) { | |
152 // We don't check the return value here. | |
153 base::ClosePlatformFile(file_handle_); | |
154 file_handle_ = base::kInvalidPlatformFileValue; | |
155 } | |
156 set_error_code(error_code); | 148 set_error_code(error_code); |
157 } | 149 } |
158 | 150 |
159 virtual void RunCallback() { | 151 virtual void RunCallback() { |
160 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 152 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), |
161 created_); | 153 created_); |
162 delete callback_; | 154 delete callback_; |
163 } | 155 } |
164 | 156 |
165 private: | 157 private: |
166 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 158 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
167 FilePath file_path_; | 159 FilePath file_path_; |
168 int file_flags_; | 160 int file_flags_; |
169 base::FileUtilProxy::CreateOrOpenCallback* callback_; | 161 base::FileUtilProxy::CreateOrOpenCallback* callback_; |
170 base::PlatformFile file_handle_; | 162 base::PlatformFile file_handle_; |
171 bool return_no_handle_; | |
172 bool created_; | 163 bool created_; |
173 }; | 164 }; |
174 | 165 |
175 class RelayCreateTemporary : public MessageLoopRelay { | 166 class RelayCreateTemporary : public MessageLoopRelay { |
176 public: | 167 public: |
177 RelayCreateTemporary( | 168 RelayCreateTemporary( |
178 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 169 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
179 base::FileUtilProxy::CreateTemporaryCallback* callback) | 170 base::FileUtilProxy::CreateTemporaryCallback* callback) |
180 : message_loop_proxy_(message_loop_proxy), | 171 : message_loop_proxy_(message_loop_proxy), |
181 callback_(callback), | 172 callback_(callback), |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 protected: | 243 protected: |
253 virtual void RunWork() { | 244 virtual void RunWork() { |
254 if (!base::ClosePlatformFile(file_handle_)) | 245 if (!base::ClosePlatformFile(file_handle_)) |
255 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); | 246 set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
256 } | 247 } |
257 | 248 |
258 private: | 249 private: |
259 base::PlatformFile file_handle_; | 250 base::PlatformFile file_handle_; |
260 }; | 251 }; |
261 | 252 |
| 253 class RelayEnsureFileExists : public MessageLoopRelay { |
| 254 public: |
| 255 RelayEnsureFileExists( |
| 256 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 257 const FilePath& file_path, |
| 258 base::FileUtilProxy::EnsureFileExistsCallback* callback) |
| 259 : message_loop_proxy_(message_loop_proxy), |
| 260 file_path_(file_path), |
| 261 callback_(callback), |
| 262 created_(false) { |
| 263 DCHECK(callback); |
| 264 } |
| 265 |
| 266 protected: |
| 267 virtual void RunWork() { |
| 268 if (!file_util::DirectoryExists(file_path_.DirName())) { |
| 269 // If its parent does not exist, should return NOT_FOUND error. |
| 270 set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 271 return; |
| 272 } |
| 273 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 274 // Tries to create the |file_path_| exclusively. This should fail |
| 275 // with PLATFORM_FILE_ERROR_EXISTS if the path already exists. |
| 276 base::PlatformFile handle = base::CreatePlatformFile( |
| 277 file_path_, |
| 278 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 279 &created_, &error_code); |
| 280 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { |
| 281 // Make sure created_ is false. |
| 282 created_ = false; |
| 283 error_code = base::PLATFORM_FILE_OK; |
| 284 } |
| 285 if (handle != base::kInvalidPlatformFileValue) |
| 286 base::ClosePlatformFile(handle); |
| 287 set_error_code(error_code); |
| 288 } |
| 289 |
| 290 virtual void RunCallback() { |
| 291 callback_->Run(error_code(), created_); |
| 292 delete callback_; |
| 293 } |
| 294 |
| 295 private: |
| 296 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 297 FilePath file_path_; |
| 298 base::FileUtilProxy::EnsureFileExistsCallback* callback_; |
| 299 bool created_; |
| 300 }; |
| 301 |
262 class RelayDelete : public RelayWithStatusCallback { | 302 class RelayDelete : public RelayWithStatusCallback { |
263 public: | 303 public: |
264 RelayDelete(const FilePath& file_path, | 304 RelayDelete(const FilePath& file_path, |
265 bool recursive, | 305 bool recursive, |
266 base::FileUtilProxy::StatusCallback* callback) | 306 base::FileUtilProxy::StatusCallback* callback) |
267 : RelayWithStatusCallback(callback), | 307 : RelayWithStatusCallback(callback), |
268 file_path_(file_path), | 308 file_path_(file_path), |
269 recursive_(recursive) { | 309 recursive_(recursive) { |
270 } | 310 } |
271 | 311 |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 } // namespace | 728 } // namespace |
689 | 729 |
690 namespace base { | 730 namespace base { |
691 | 731 |
692 // static | 732 // static |
693 bool FileUtilProxy::CreateOrOpen( | 733 bool FileUtilProxy::CreateOrOpen( |
694 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 734 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
695 const FilePath& file_path, int file_flags, | 735 const FilePath& file_path, int file_flags, |
696 CreateOrOpenCallback* callback) { | 736 CreateOrOpenCallback* callback) { |
697 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 737 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
698 message_loop_proxy, file_path, file_flags, false /* return_no_handle */, | 738 message_loop_proxy, file_path, file_flags, callback)); |
699 callback)); | |
700 } | 739 } |
701 | 740 |
702 // static | 741 // static |
703 bool FileUtilProxy::Create( | |
704 scoped_refptr<MessageLoopProxy> message_loop_proxy, | |
705 const FilePath& file_path, int file_flags, | |
706 CreateOrOpenCallback* callback) { | |
707 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | |
708 message_loop_proxy, file_path, file_flags, true /* return_no_handle */, | |
709 callback)); | |
710 } | |
711 | |
712 // static | |
713 bool FileUtilProxy::CreateTemporary( | 742 bool FileUtilProxy::CreateTemporary( |
714 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 743 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
715 CreateTemporaryCallback* callback) { | 744 CreateTemporaryCallback* callback) { |
716 return Start(FROM_HERE, message_loop_proxy, | 745 return Start(FROM_HERE, message_loop_proxy, |
717 new RelayCreateTemporary(message_loop_proxy, callback)); | 746 new RelayCreateTemporary(message_loop_proxy, callback)); |
718 } | 747 } |
719 | 748 |
720 // static | 749 // static |
721 bool FileUtilProxy::CreateDirectory( | 750 bool FileUtilProxy::CreateDirectory( |
722 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 751 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
723 const FilePath& file_path, | 752 const FilePath& file_path, |
724 bool exclusive, | 753 bool exclusive, |
725 bool recursive, | 754 bool recursive, |
726 StatusCallback* callback) { | 755 StatusCallback* callback) { |
727 return Start(FROM_HERE, message_loop_proxy, new RelayCreateDirectory( | 756 return Start(FROM_HERE, message_loop_proxy, new RelayCreateDirectory( |
728 file_path, exclusive, recursive, callback)); | 757 file_path, exclusive, recursive, callback)); |
729 } | 758 } |
730 | 759 |
731 // static | 760 // static |
732 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 761 bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
733 base::PlatformFile file_handle, | 762 base::PlatformFile file_handle, |
734 StatusCallback* callback) { | 763 StatusCallback* callback) { |
735 return Start(FROM_HERE, message_loop_proxy, | 764 return Start(FROM_HERE, message_loop_proxy, |
736 new RelayClose(file_handle, callback)); | 765 new RelayClose(file_handle, callback)); |
737 } | 766 } |
738 | 767 |
739 // static | 768 // static |
| 769 bool FileUtilProxy::EnsureFileExists( |
| 770 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 771 const FilePath& file_path, |
| 772 EnsureFileExistsCallback* callback) { |
| 773 return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( |
| 774 message_loop_proxy, file_path, callback)); |
| 775 } |
| 776 |
| 777 // static |
740 bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 778 bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
741 const FilePath& file_path, | 779 const FilePath& file_path, |
742 bool recursive, | 780 bool recursive, |
743 StatusCallback* callback) { | 781 StatusCallback* callback) { |
744 return Start(FROM_HERE, message_loop_proxy, | 782 return Start(FROM_HERE, message_loop_proxy, |
745 new RelayDelete(file_path, recursive, callback)); | 783 new RelayDelete(file_path, recursive, callback)); |
746 } | 784 } |
747 | 785 |
748 // static | 786 // static |
749 bool FileUtilProxy::Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 787 bool FileUtilProxy::Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 | 908 |
871 // static | 909 // static |
872 bool FileUtilProxy::Flush( | 910 bool FileUtilProxy::Flush( |
873 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 911 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
874 PlatformFile file, | 912 PlatformFile file, |
875 StatusCallback* callback) { | 913 StatusCallback* callback) { |
876 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 914 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
877 } | 915 } |
878 | 916 |
879 } // namespace base | 917 } // namespace base |
OLD | NEW |