OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/fileapi/obfuscated_file_system_file_util.h" | 5 #include "webkit/fileapi/obfuscated_file_system_file_util.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 | 150 |
151 FileInfo file_info; | 151 FileInfo file_info; |
152 if (!db->GetFileInfo(file_id, &file_info)) { | 152 if (!db->GetFileInfo(file_id, &file_info)) { |
153 NOTREACHED(); | 153 NOTREACHED(); |
154 return base::PLATFORM_FILE_ERROR_FAILED; | 154 return base::PLATFORM_FILE_ERROR_FAILED; |
155 } | 155 } |
156 if (file_info.is_directory()) | 156 if (file_info.is_directory()) |
157 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 157 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
158 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 158 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
159 context->src_type(), file_info.data_path); | 159 context->src_type(), file_info.data_path); |
160 return underlying_file_util_->CreateOrOpen( | 160 base::PlatformFileError error = underlying_file_util_->CreateOrOpen( |
161 context, data_path, file_flags, file_handle, created); | 161 context, data_path, file_flags, file_handle, created); |
162 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { | |
163 // TODO(tzik): Also invalidate on-memory usage cache in UsageTracker. | |
164 context->file_system_context()->GetQuotaUtil(context->src_type())-> | |
165 InvalidateUsageCache(context->src_origin_url(), | |
166 context->src_type()); | |
167 LOG(WARNING) << "Lost a backing file."; | |
168 error = base::PLATFORM_FILE_ERROR_FAILED; | |
169 } | |
170 return error; | |
162 } | 171 } |
163 | 172 |
164 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( | 173 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( |
165 FileSystemOperationContext* context, | 174 FileSystemOperationContext* context, |
166 const FilePath& virtual_path, | 175 const FilePath& virtual_path, |
167 bool* created) { | 176 bool* created) { |
168 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 177 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
169 context->src_origin_url(), context->src_type(), true); | 178 context->src_origin_url(), context->src_type(), true); |
170 if (!db) | 179 if (!db) |
171 return base::PLATFORM_FILE_ERROR_FAILED; | 180 return base::PLATFORM_FILE_ERROR_FAILED; |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 * transaction: | 387 * transaction: |
379 * Remove source entry. | 388 * Remove source entry. |
380 * Point target entry to source entry's backing file. | 389 * Point target entry to source entry's backing file. |
381 * Delete target entry's old backing file | 390 * Delete target entry's old backing file |
382 * Move-without-overwrite | 391 * Move-without-overwrite |
383 * Just update metadata | 392 * Just update metadata |
384 */ | 393 */ |
385 if (copy) { | 394 if (copy) { |
386 FilePath src_data_path = DataPathToLocalPath(context->src_origin_url(), | 395 FilePath src_data_path = DataPathToLocalPath(context->src_origin_url(), |
387 context->src_type(), src_file_info.data_path); | 396 context->src_type(), src_file_info.data_path); |
397 if (!underlying_file_util_->PathExists(context, src_data_path)) { | |
398 // TODO(tzik): Also invalidate on-memory usage cache in UsageTracker. | |
399 context->file_system_context()->GetQuotaUtil(context->src_type())-> | |
400 InvalidateUsageCache(context->src_origin_url(), | |
401 context->src_type()); | |
402 LOG(WARNING) << "Lost a backing file."; | |
403 return base::PLATFORM_FILE_ERROR_FAILED; | |
404 } | |
405 | |
388 if (overwrite) { | 406 if (overwrite) { |
389 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), | 407 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), |
390 context->src_type(), dest_file_info.data_path); | 408 context->src_type(), dest_file_info.data_path); |
391 return underlying_file_util_->CopyOrMoveFile(context, | 409 return underlying_file_util_->CopyOrMoveFile(context, |
392 src_data_path, dest_data_path, copy); | 410 src_data_path, dest_data_path, copy); |
393 } else { | 411 } else { |
394 FileId dest_parent_id; | 412 FileId dest_parent_id; |
395 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | 413 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { |
396 NOTREACHED(); // We shouldn't be called in this case. | 414 NOTREACHED(); // We shouldn't be called in this case. |
397 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 415 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
858 if (data_path.empty()) | 876 if (data_path.empty()) |
859 return base::PLATFORM_FILE_ERROR_FAILED; | 877 return base::PLATFORM_FILE_ERROR_FAILED; |
860 bool created = false; | 878 bool created = false; |
861 if (!source_path.empty()) { | 879 if (!source_path.empty()) { |
862 DCHECK(!file_flags); | 880 DCHECK(!file_flags); |
863 DCHECK(!handle); | 881 DCHECK(!handle); |
864 error = underlying_file_util_->CopyOrMoveFile( | 882 error = underlying_file_util_->CopyOrMoveFile( |
865 context, source_path, path, true /* copy */); | 883 context, source_path, path, true /* copy */); |
866 created = true; | 884 created = true; |
867 } else { | 885 } else { |
886 if (file_util::PathExists(path)) { | |
ericu
2011/08/24 03:43:50
This should probably use underlying_file_util_, no
tzik
2011/08/29 06:54:13
We use underlying_file_util_ in the next patch.
It
kinuko
2011/08/29 07:45:37
Maybe we can explicitly call underlying_file_util_
tzik
2011/08/29 08:34:15
Done.
ericu
2011/08/29 22:48:20
Thanks, and Kinuko, that was a good idea.
I've jus
tzik
2011/08/31 02:40:05
Done.
| |
887 if (!file_util::Delete(path, false)) { | |
888 NOTREACHED(); | |
889 return base::PLATFORM_FILE_ERROR_FAILED; | |
890 } | |
891 LOG(WARNING) << "A stray file detected"; | |
892 } | |
893 | |
868 if (handle) { | 894 if (handle) { |
869 error = underlying_file_util_->CreateOrOpen( | 895 error = underlying_file_util_->CreateOrOpen( |
870 context, path, file_flags, handle, &created); | 896 context, path, file_flags, handle, &created); |
871 // If this succeeds, we must close handle on any subsequent error. | 897 // If this succeeds, we must close handle on any subsequent error. |
872 } else { | 898 } else { |
873 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | 899 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. |
874 error = underlying_file_util_->EnsureFileExists( | 900 error = underlying_file_util_->EnsureFileExists( |
875 context, path, &created); | 901 context, path, &created); |
876 } | 902 } |
877 } | 903 } |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1104 directories_[key] = database; | 1130 directories_[key] = database; |
1105 return database; | 1131 return database; |
1106 } | 1132 } |
1107 | 1133 |
1108 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | 1134 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( |
1109 const GURL& origin, bool create) { | 1135 const GURL& origin, bool create) { |
1110 if (!InitOriginDatabase(create)) | 1136 if (!InitOriginDatabase(create)) |
1111 return FilePath(); | 1137 return FilePath(); |
1112 FilePath directory_name; | 1138 FilePath directory_name; |
1113 std::string id = GetOriginIdentifierFromURL(origin); | 1139 std::string id = GetOriginIdentifierFromURL(origin); |
1114 if (!create && !origin_database_->HasOriginPath(id)) | 1140 |
1141 bool exists_in_db = origin_database_->HasOriginPath(id); | |
1142 if (!exists_in_db && !create) | |
1115 return FilePath(); | 1143 return FilePath(); |
1116 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | 1144 if (!origin_database_->GetPathForOrigin(id, &directory_name)) |
1117 return FilePath(); | 1145 return FilePath(); |
1146 | |
1118 FilePath path = file_system_directory_.Append(directory_name); | 1147 FilePath path = file_system_directory_.Append(directory_name); |
1119 if (!file_util::DirectoryExists(path) && | 1148 bool exists_in_fs = file_util::DirectoryExists(path); |
1120 (!create || !file_util::CreateDirectory(path))) | 1149 if (!exists_in_db && exists_in_fs) { |
1121 return FilePath(); | 1150 if (!file_util::Delete(path, true)) |
1151 return FilePath(); | |
1152 exists_in_fs = false; | |
1153 } | |
1154 | |
1155 if (!exists_in_fs) { | |
1156 if (!create || !file_util::CreateDirectory(path)) | |
1157 return FilePath(); | |
1158 } | |
1159 | |
1122 return path; | 1160 return path; |
1123 } | 1161 } |
1124 | 1162 |
1125 void ObfuscatedFileSystemFileUtil::MarkUsed() { | 1163 void ObfuscatedFileSystemFileUtil::MarkUsed() { |
1126 if (timer_.IsRunning()) | 1164 if (timer_.IsRunning()) |
1127 timer_.Reset(); | 1165 timer_.Reset(); |
1128 else | 1166 else |
1129 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | 1167 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, |
1130 &ObfuscatedFileSystemFileUtil::DropDatabases); | 1168 &ObfuscatedFileSystemFileUtil::DropDatabases); |
1131 } | 1169 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1177 return false; | 1215 return false; |
1178 } | 1216 } |
1179 origin_database_.reset( | 1217 origin_database_.reset( |
1180 new FileSystemOriginDatabase( | 1218 new FileSystemOriginDatabase( |
1181 file_system_directory_.AppendASCII(kOriginDatabaseName))); | 1219 file_system_directory_.AppendASCII(kOriginDatabaseName))); |
1182 } | 1220 } |
1183 return true; | 1221 return true; |
1184 } | 1222 } |
1185 | 1223 |
1186 } // namespace fileapi | 1224 } // namespace fileapi |
OLD | NEW |