Chromium Code Reviews| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 PlatformFile* file_handle, bool* created) { | 72 PlatformFile* file_handle, bool* created) { |
| 73 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | | 73 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | |
| 74 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | | 74 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 75 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); | 75 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); |
| 76 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 76 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 77 context->src_origin_url(), context->src_type(), true); | 77 context->src_origin_url(), context->src_type(), true); |
| 78 if (!db) | 78 if (!db) |
| 79 return base::PLATFORM_FILE_ERROR_FAILED; | 79 return base::PLATFORM_FILE_ERROR_FAILED; |
| 80 FileId file_id; | 80 FileId file_id; |
| 81 if (!db->GetFileWithPath(virtual_path, &file_id)) { | 81 if (!db->GetFileWithPath(virtual_path, &file_id)) { |
| 82 // The file doesn't exist. | 82 // The file doesn't exist in the database. |
| 83 if (!(file_flags & (base::PLATFORM_FILE_CREATE | | 83 if (!(file_flags & (base::PLATFORM_FILE_CREATE | |
| 84 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) | 84 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) |
| 85 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 85 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 86 FileId parent_id; | 86 FileId parent_id; |
| 87 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | 87 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) |
| 88 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 88 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 89 FileInfo file_info; | 89 FileInfo file_info; |
| 90 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | 90 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); |
| 91 | |
| 92 // There may be a file without database entry out of inconsistency between | |
| 93 // actual filesystem and database. We use PLATFORM_FILE_CREATE_ALWAYS | |
| 94 // to truncate such file. | |
| 95 file_flags &= ~(base::PLATFORM_FILE_OPEN | | |
| 96 base::PLATFORM_FILE_CREATE | | |
| 97 base::PLATFORM_FILE_OPEN_TRUNCATED); | |
| 98 file_flags |= base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 99 base::PLATFORM_FILE_OPEN_ALWAYS; | |
|
ericu
2011/08/10 17:28:01
From platform_file.h:
// PLATFORM_FILE_(OPEN|CRE
tzik
2011/08/16 08:25:33
Yes, the patch went wrong way. It caused tests fai
| |
| 91 PlatformFileError error = CreateFile( | 100 PlatformFileError error = CreateFile( |
| 92 context, context->src_origin_url(), context->src_type(), FilePath(), | 101 context, context->src_origin_url(), context->src_type(), FilePath(), |
| 93 &file_info, file_flags, file_handle); | 102 &file_info, file_flags, file_handle); |
| 103 if (error == base::PLATFORM_FILE_ERROR_EXISTS) | |
| 104 error = base::PLATFORM_FILE_OK; | |
| 94 if (created && base::PLATFORM_FILE_OK == error) | 105 if (created && base::PLATFORM_FILE_OK == error) |
| 95 *created = true; | 106 *created = true; |
| 96 return error; | 107 return error; |
| 97 } | 108 } |
| 98 if (file_flags & base::PLATFORM_FILE_CREATE) | 109 if (file_flags & base::PLATFORM_FILE_CREATE) |
| 99 return base::PLATFORM_FILE_ERROR_EXISTS; | 110 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 100 | 111 |
| 101 FileInfo file_info; | 112 FileInfo file_info; |
| 102 if (!db->GetFileInfo(file_id, &file_info)) { | 113 if (!db->GetFileInfo(file_id, &file_info)) { |
| 103 NOTREACHED(); | 114 NOTREACHED(); |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 788 created = true; | 799 created = true; |
| 789 } else { | 800 } else { |
| 790 if (handle) { | 801 if (handle) { |
| 791 error = underlying_file_util_->CreateOrOpen( | 802 error = underlying_file_util_->CreateOrOpen( |
| 792 context, path, file_flags, handle, &created); | 803 context, path, file_flags, handle, &created); |
| 793 // If this succeeds, we must close handle on any subsequent error. | 804 // If this succeeds, we must close handle on any subsequent error. |
| 794 } else { | 805 } else { |
| 795 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | 806 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. |
| 796 error = underlying_file_util_->EnsureFileExists( | 807 error = underlying_file_util_->EnsureFileExists( |
| 797 context, path, &created); | 808 context, path, &created); |
| 809 | |
| 810 // If the file already exists, we found a stray file out of | |
| 811 // inconsistency between database and filesystem. We should truncate | |
| 812 // it to empty. | |
| 813 if (error == base::PLATFORM_FILE_OK && !created) { | |
| 814 PlatformFile file = base::CreatePlatformFile( | |
| 815 path, base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_CREATE_ALWAYS, | |
| 816 &created, &error); | |
| 817 if (error == base::PLATFORM_FILE_ERROR_EXISTS) | |
| 818 error = base::PLATFORM_FILE_OK; | |
| 819 if (error == base::PLATFORM_FILE_OK) { | |
| 820 if (!base::ClosePlatformFile(file)) | |
| 821 error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 822 } | |
| 823 } | |
| 798 } | 824 } |
| 799 } | 825 } |
| 800 if (error != base::PLATFORM_FILE_OK) | 826 if (error != base::PLATFORM_FILE_OK) |
| 801 return error; | 827 return error; |
| 802 | 828 |
| 803 if (!created) { | 829 if (!created) { |
| 804 NOTREACHED(); | 830 NOTREACHED(); |
| 805 if (handle) { | 831 if (handle) { |
| 806 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | 832 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
| 807 base::ClosePlatformFile(*handle); | 833 base::ClosePlatformFile(*handle); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1025 directories_[key] = database; | 1051 directories_[key] = database; |
| 1026 return database; | 1052 return database; |
| 1027 } | 1053 } |
| 1028 | 1054 |
| 1029 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | 1055 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( |
| 1030 const GURL& origin, bool create) { | 1056 const GURL& origin, bool create) { |
| 1031 if (!InitOriginDatabase(create)) | 1057 if (!InitOriginDatabase(create)) |
| 1032 return FilePath(); | 1058 return FilePath(); |
| 1033 FilePath directory_name; | 1059 FilePath directory_name; |
| 1034 std::string id = GetOriginIdentifierFromURL(origin); | 1060 std::string id = GetOriginIdentifierFromURL(origin); |
| 1035 if (!create && !origin_database_->HasOriginPath(id)) | 1061 |
| 1062 bool exists_in_db = origin_database_->HasOriginPath(id); | |
| 1063 if (!exists_in_db && !create) | |
| 1036 return FilePath(); | 1064 return FilePath(); |
| 1037 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | 1065 if (!origin_database_->GetPathForOrigin(id, &directory_name)) |
| 1038 return FilePath(); | 1066 return FilePath(); |
| 1067 | |
| 1039 FilePath path = file_system_directory_.Append(directory_name); | 1068 FilePath path = file_system_directory_.Append(directory_name); |
| 1040 if (!file_util::DirectoryExists(path) && | 1069 bool exists_in_fs = file_util::DirectoryExists(path); |
| 1041 (!create || !file_util::CreateDirectory(path))) | 1070 if (!exists_in_db && exists_in_fs) { |
| 1042 return FilePath(); | 1071 if (!file_util::Delete(path, true)) |
| 1072 return FilePath(); | |
| 1073 else | |
| 1074 exists_in_fs = false; | |
| 1075 } | |
| 1076 | |
| 1077 if (!exists_in_fs) { | |
| 1078 if (!create || !file_util::CreateDirectory(path)) | |
| 1079 return FilePath(); | |
| 1080 } | |
| 1081 | |
| 1043 return path; | 1082 return path; |
| 1044 } | 1083 } |
| 1045 | 1084 |
| 1046 void ObfuscatedFileSystemFileUtil::MarkUsed() { | 1085 void ObfuscatedFileSystemFileUtil::MarkUsed() { |
| 1047 if (timer_.IsRunning()) | 1086 if (timer_.IsRunning()) |
| 1048 timer_.Reset(); | 1087 timer_.Reset(); |
| 1049 else | 1088 else |
| 1050 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | 1089 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, |
| 1051 &ObfuscatedFileSystemFileUtil::DropDatabases); | 1090 &ObfuscatedFileSystemFileUtil::DropDatabases); |
| 1052 } | 1091 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1093 return false; | 1132 return false; |
| 1094 } | 1133 } |
| 1095 origin_database_.reset( | 1134 origin_database_.reset( |
| 1096 new FileSystemOriginDatabase( | 1135 new FileSystemOriginDatabase( |
| 1097 file_system_directory_.AppendASCII(kOriginDatabaseName))); | 1136 file_system_directory_.AppendASCII(kOriginDatabaseName))); |
| 1098 } | 1137 } |
| 1099 return true; | 1138 return true; |
| 1100 } | 1139 } |
| 1101 | 1140 |
| 1102 } // namespace fileapi | 1141 } // namespace fileapi |
| OLD | NEW |