OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/file_system_usage_cache.h" | 5 #include "webkit/fileapi/file_system_usage_cache.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 | 10 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) { | 123 bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) { |
124 return file_util::Delete(usage_file_path, true); | 124 return file_util::Delete(usage_file_path, true); |
125 } | 125 } |
126 | 126 |
127 // static | 127 // static |
128 int64 FileSystemUsageCache::Read(const FilePath& usage_file_path, | 128 int64 FileSystemUsageCache::Read(const FilePath& usage_file_path, |
129 bool* is_valid, | 129 bool* is_valid, |
130 uint32* dirty) { | 130 uint32* dirty) { |
131 char buffer[kUsageFileSize]; | 131 char buffer[kUsageFileSize]; |
132 const char *header; | 132 const char *header; |
133 DCHECK(!usage_file_path.empty()); | 133 if (usage_file_path.empty() || |
134 if (kUsageFileSize != | 134 kUsageFileSize != file_util::ReadFile(usage_file_path, |
135 file_util::ReadFile(usage_file_path, buffer, kUsageFileSize)) | 135 buffer, kUsageFileSize)) |
136 return -1; | 136 return -1; |
137 Pickle read_pickle(buffer, kUsageFileSize); | 137 Pickle read_pickle(buffer, kUsageFileSize); |
138 PickleIterator iter(read_pickle); | 138 PickleIterator iter(read_pickle); |
139 int64 fs_usage; | 139 int64 fs_usage; |
140 | 140 |
141 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) || | 141 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) || |
142 !read_pickle.ReadBool(&iter, is_valid) || | 142 !read_pickle.ReadBool(&iter, is_valid) || |
143 !read_pickle.ReadUInt32(&iter, dirty) || | 143 !read_pickle.ReadUInt32(&iter, dirty) || |
144 !read_pickle.ReadInt64(&iter, &fs_usage)) | 144 !read_pickle.ReadInt64(&iter, &fs_usage)) |
145 return -1; | 145 return -1; |
(...skipping 11 matching lines...) Expand all Loading... |
157 int FileSystemUsageCache::Write(const FilePath& usage_file_path, | 157 int FileSystemUsageCache::Write(const FilePath& usage_file_path, |
158 bool is_valid, | 158 bool is_valid, |
159 uint32 dirty, | 159 uint32 dirty, |
160 int64 fs_usage) { | 160 int64 fs_usage) { |
161 Pickle write_pickle; | 161 Pickle write_pickle; |
162 write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize); | 162 write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize); |
163 write_pickle.WriteBool(is_valid); | 163 write_pickle.WriteBool(is_valid); |
164 write_pickle.WriteUInt32(dirty); | 164 write_pickle.WriteUInt32(dirty); |
165 write_pickle.WriteInt64(fs_usage); | 165 write_pickle.WriteInt64(fs_usage); |
166 | 166 |
167 DCHECK(!usage_file_path.empty()); | |
168 FilePath temporary_usage_file_path; | 167 FilePath temporary_usage_file_path; |
169 if (!file_util::CreateTemporaryFileInDir(usage_file_path.DirName(), | 168 if (usage_file_path.empty() || |
| 169 !file_util::CreateTemporaryFileInDir(usage_file_path.DirName(), |
170 &temporary_usage_file_path)) { | 170 &temporary_usage_file_path)) { |
171 return -1; | 171 return -1; |
172 } | 172 } |
173 | 173 |
174 int bytes_written = file_util::WriteFile(temporary_usage_file_path, | 174 int bytes_written = file_util::WriteFile(temporary_usage_file_path, |
175 (const char *)write_pickle.data(), | 175 (const char *)write_pickle.data(), |
176 write_pickle.size()); | 176 write_pickle.size()); |
177 if (bytes_written != kUsageFileSize) | 177 if (bytes_written != kUsageFileSize) |
178 return -1; | 178 return -1; |
179 | 179 |
180 if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path)) | 180 if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path)) |
181 return bytes_written; | 181 return bytes_written; |
182 else | 182 else |
183 return -1; | 183 return -1; |
184 } | 184 } |
185 | 185 |
186 } // namespace fileapi | 186 } // namespace fileapi |
OLD | NEW |