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 in the database. | 82 // The file doesn't exist. |
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_ALWAYS | | |
98 base::PLATFORM_FILE_OPEN_TRUNCATED); | |
99 file_flags |= base::PLATFORM_FILE_CREATE_ALWAYS; | |
100 PlatformFileError error = CreateFile( | 91 PlatformFileError error = CreateFile( |
101 context, context->src_origin_url(), context->src_type(), FilePath(), | 92 context, context->src_origin_url(), context->src_type(), FilePath(), |
102 &file_info, file_flags, file_handle); | 93 &file_info, file_flags, file_handle); |
103 if (error == base::PLATFORM_FILE_ERROR_EXISTS) | |
104 error = base::PLATFORM_FILE_OK; | |
105 if (created && base::PLATFORM_FILE_OK == error) | 94 if (created && base::PLATFORM_FILE_OK == error) |
106 *created = true; | 95 *created = true; |
107 return error; | 96 return error; |
108 } | 97 } |
109 if (file_flags & base::PLATFORM_FILE_CREATE) | 98 if (file_flags & base::PLATFORM_FILE_CREATE) |
110 return base::PLATFORM_FILE_ERROR_EXISTS; | 99 return base::PLATFORM_FILE_ERROR_EXISTS; |
111 | 100 |
112 FileInfo file_info; | 101 FileInfo file_info; |
113 if (!db->GetFileInfo(file_id, &file_info)) { | 102 if (!db->GetFileInfo(file_id, &file_info)) { |
114 NOTREACHED(); | 103 NOTREACHED(); |
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 created = true; | 788 created = true; |
800 } else { | 789 } else { |
801 if (handle) { | 790 if (handle) { |
802 error = underlying_file_util_->CreateOrOpen( | 791 error = underlying_file_util_->CreateOrOpen( |
803 context, path, file_flags, handle, &created); | 792 context, path, file_flags, handle, &created); |
804 // If this succeeds, we must close handle on any subsequent error. | 793 // If this succeeds, we must close handle on any subsequent error. |
805 } else { | 794 } else { |
806 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | 795 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. |
807 error = underlying_file_util_->EnsureFileExists( | 796 error = underlying_file_util_->EnsureFileExists( |
808 context, path, &created); | 797 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 } | |
824 } | 798 } |
825 } | 799 } |
826 if (error != base::PLATFORM_FILE_OK) | 800 if (error != base::PLATFORM_FILE_OK) |
827 return error; | 801 return error; |
828 | 802 |
829 if (!created) { | 803 if (!created) { |
830 NOTREACHED(); | 804 NOTREACHED(); |
831 if (handle) { | 805 if (handle) { |
832 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | 806 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
833 base::ClosePlatformFile(*handle); | 807 base::ClosePlatformFile(*handle); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1051 directories_[key] = database; | 1025 directories_[key] = database; |
1052 return database; | 1026 return database; |
1053 } | 1027 } |
1054 | 1028 |
1055 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | 1029 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( |
1056 const GURL& origin, bool create) { | 1030 const GURL& origin, bool create) { |
1057 if (!InitOriginDatabase(create)) | 1031 if (!InitOriginDatabase(create)) |
1058 return FilePath(); | 1032 return FilePath(); |
1059 FilePath directory_name; | 1033 FilePath directory_name; |
1060 std::string id = GetOriginIdentifierFromURL(origin); | 1034 std::string id = GetOriginIdentifierFromURL(origin); |
1061 | 1035 if (!create && !origin_database_->HasOriginPath(id)) |
1062 bool exists_in_db = origin_database_->HasOriginPath(id); | |
1063 if (!exists_in_db && !create) | |
1064 return FilePath(); | 1036 return FilePath(); |
1065 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | 1037 if (!origin_database_->GetPathForOrigin(id, &directory_name)) |
1066 return FilePath(); | 1038 return FilePath(); |
1067 | |
1068 FilePath path = file_system_directory_.Append(directory_name); | 1039 FilePath path = file_system_directory_.Append(directory_name); |
1069 bool exists_in_fs = file_util::DirectoryExists(path); | 1040 if (!file_util::DirectoryExists(path) && |
1070 if (!exists_in_db && exists_in_fs) { | 1041 (!create || !file_util::CreateDirectory(path))) |
1071 if (!file_util::Delete(path, true)) | 1042 return FilePath(); |
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 | |
1082 return path; | 1043 return path; |
1083 } | 1044 } |
1084 | 1045 |
1085 void ObfuscatedFileSystemFileUtil::MarkUsed() { | 1046 void ObfuscatedFileSystemFileUtil::MarkUsed() { |
1086 if (timer_.IsRunning()) | 1047 if (timer_.IsRunning()) |
1087 timer_.Reset(); | 1048 timer_.Reset(); |
1088 else | 1049 else |
1089 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | 1050 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, |
1090 &ObfuscatedFileSystemFileUtil::DropDatabases); | 1051 &ObfuscatedFileSystemFileUtil::DropDatabases); |
1091 } | 1052 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1132 return false; | 1093 return false; |
1133 } | 1094 } |
1134 origin_database_.reset( | 1095 origin_database_.reset( |
1135 new FileSystemOriginDatabase( | 1096 new FileSystemOriginDatabase( |
1136 file_system_directory_.AppendASCII(kOriginDatabaseName))); | 1097 file_system_directory_.AppendASCII(kOriginDatabaseName))); |
1137 } | 1098 } |
1138 return true; | 1099 return true; |
1139 } | 1100 } |
1140 | 1101 |
1141 } // namespace fileapi | 1102 } // namespace fileapi |
OLD | NEW |