| 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 |
| 11 namespace fileapi { | 11 namespace fileapi { |
| 12 | 12 |
| 13 const FilePath::CharType FileSystemUsageCache::kUsageFileName[] = | 13 const base::FilePath::CharType FileSystemUsageCache::kUsageFileName[] = |
| 14 FILE_PATH_LITERAL(".usage"); | 14 FILE_PATH_LITERAL(".usage"); |
| 15 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU5"; | 15 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU5"; |
| 16 const int FileSystemUsageCache::kUsageFileHeaderSize = 4; | 16 const int FileSystemUsageCache::kUsageFileHeaderSize = 4; |
| 17 | 17 |
| 18 /* Pickle::{Read,Write}Bool treat bool as int */ | 18 /* Pickle::{Read,Write}Bool treat bool as int */ |
| 19 const int FileSystemUsageCache::kUsageFileSize = | 19 const int FileSystemUsageCache::kUsageFileSize = |
| 20 sizeof(Pickle::Header) + | 20 sizeof(Pickle::Header) + |
| 21 FileSystemUsageCache::kUsageFileHeaderSize + | 21 FileSystemUsageCache::kUsageFileHeaderSize + |
| 22 sizeof(int) + sizeof(int32) + sizeof(int64); | 22 sizeof(int) + sizeof(int32) + sizeof(int64); |
| 23 | 23 |
| 24 // static | 24 // static |
| 25 int64 FileSystemUsageCache::GetUsage(const FilePath& usage_file_path) { | 25 int64 FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path) { |
| 26 bool is_valid = true; | 26 bool is_valid = true; |
| 27 uint32 dirty = 0; | 27 uint32 dirty = 0; |
| 28 int64 fs_usage; | 28 int64 fs_usage; |
| 29 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 29 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 30 | 30 |
| 31 if (fs_usage < 0) | 31 if (fs_usage < 0) |
| 32 return -1; | 32 return -1; |
| 33 | 33 |
| 34 return fs_usage; | 34 return fs_usage; |
| 35 } | 35 } |
| 36 | 36 |
| 37 // static | 37 // static |
| 38 int32 FileSystemUsageCache::GetDirty(const FilePath& usage_file_path) { | 38 int32 FileSystemUsageCache::GetDirty(const base::FilePath& usage_file_path) { |
| 39 bool is_valid = true; | 39 bool is_valid = true; |
| 40 uint32 dirty = 0; | 40 uint32 dirty = 0; |
| 41 int64 fs_usage; | 41 int64 fs_usage; |
| 42 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 42 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 43 | 43 |
| 44 if (fs_usage < 0) | 44 if (fs_usage < 0) |
| 45 return -1; | 45 return -1; |
| 46 | 46 |
| 47 return static_cast<int32>(dirty); | 47 return static_cast<int32>(dirty); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // static | 50 // static |
| 51 bool FileSystemUsageCache::IncrementDirty(const FilePath& usage_file_path) { | 51 bool FileSystemUsageCache::IncrementDirty(const base::FilePath& usage_file_path)
{ |
| 52 bool is_valid = true; | 52 bool is_valid = true; |
| 53 uint32 dirty = 0; | 53 uint32 dirty = 0; |
| 54 int64 fs_usage; | 54 int64 fs_usage; |
| 55 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 55 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 56 | 56 |
| 57 if (fs_usage < 0) | 57 if (fs_usage < 0) |
| 58 return false; | 58 return false; |
| 59 | 59 |
| 60 return Write(usage_file_path, is_valid, dirty + 1, fs_usage) >= 0; | 60 return Write(usage_file_path, is_valid, dirty + 1, fs_usage) >= 0; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // static | 63 // static |
| 64 bool FileSystemUsageCache::DecrementDirty(const FilePath& usage_file_path) { | 64 bool FileSystemUsageCache::DecrementDirty(const base::FilePath& usage_file_path)
{ |
| 65 bool is_valid = true; | 65 bool is_valid = true; |
| 66 uint32 dirty = 0; | 66 uint32 dirty = 0; |
| 67 int64 fs_usage; | 67 int64 fs_usage; |
| 68 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 68 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 69 | 69 |
| 70 if (fs_usage < 0 || dirty <= 0) | 70 if (fs_usage < 0 || dirty <= 0) |
| 71 return false; | 71 return false; |
| 72 | 72 |
| 73 return Write(usage_file_path, is_valid, dirty - 1, fs_usage) >= 0; | 73 return Write(usage_file_path, is_valid, dirty - 1, fs_usage) >= 0; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // static | 76 // static |
| 77 bool FileSystemUsageCache::Invalidate(const FilePath& usage_file_path) { | 77 bool FileSystemUsageCache::Invalidate(const base::FilePath& usage_file_path) { |
| 78 bool is_valid = true; | 78 bool is_valid = true; |
| 79 uint32 dirty = 0; | 79 uint32 dirty = 0; |
| 80 int64 fs_usage; | 80 int64 fs_usage; |
| 81 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 81 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 82 | 82 |
| 83 return fs_usage >= 0 && Write(usage_file_path, false, dirty, fs_usage); | 83 return fs_usage >= 0 && Write(usage_file_path, false, dirty, fs_usage); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool FileSystemUsageCache::IsValid(const FilePath& usage_file_path) { | 86 bool FileSystemUsageCache::IsValid(const base::FilePath& usage_file_path) { |
| 87 bool is_valid = true; | 87 bool is_valid = true; |
| 88 uint32 dirty = 0; | 88 uint32 dirty = 0; |
| 89 int64 result = Read(usage_file_path, &is_valid, &dirty); | 89 int64 result = Read(usage_file_path, &is_valid, &dirty); |
| 90 if (result < 0) | 90 if (result < 0) |
| 91 return false; | 91 return false; |
| 92 | 92 |
| 93 return is_valid; | 93 return is_valid; |
| 94 } | 94 } |
| 95 | 95 |
| 96 // static | 96 // static |
| 97 int FileSystemUsageCache::AtomicUpdateUsageByDelta( | 97 int FileSystemUsageCache::AtomicUpdateUsageByDelta( |
| 98 const FilePath& usage_file_path, int64 delta) { | 98 const base::FilePath& usage_file_path, int64 delta) { |
| 99 bool is_valid = true; | 99 bool is_valid = true; |
| 100 uint32 dirty = 0; | 100 uint32 dirty = 0; |
| 101 int64 fs_usage; | 101 int64 fs_usage; |
| 102 // TODO(dmikurube): Make sure that usage_file_path is available. | 102 // TODO(dmikurube): Make sure that usage_file_path is available. |
| 103 fs_usage = Read(usage_file_path, &is_valid, &dirty); | 103 fs_usage = Read(usage_file_path, &is_valid, &dirty); |
| 104 | 104 |
| 105 if (fs_usage < 0) | 105 if (fs_usage < 0) |
| 106 return -1; | 106 return -1; |
| 107 | 107 |
| 108 return Write(usage_file_path, is_valid, dirty, fs_usage + delta); | 108 return Write(usage_file_path, is_valid, dirty, fs_usage + delta); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // static | 111 // static |
| 112 int FileSystemUsageCache::UpdateUsage(const FilePath& usage_file_path, | 112 int FileSystemUsageCache::UpdateUsage(const base::FilePath& usage_file_path, |
| 113 int64 fs_usage) { | 113 int64 fs_usage) { |
| 114 return Write(usage_file_path, true, 0, fs_usage); | 114 return Write(usage_file_path, true, 0, fs_usage); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // static | 117 // static |
| 118 bool FileSystemUsageCache::Exists(const FilePath& usage_file_path) { | 118 bool FileSystemUsageCache::Exists(const base::FilePath& usage_file_path) { |
| 119 return file_util::PathExists(usage_file_path); | 119 return file_util::PathExists(usage_file_path); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // static | 122 // static |
| 123 bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) { | 123 bool FileSystemUsageCache::Delete(const base::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 base::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 if (usage_file_path.empty() || | 133 if (usage_file_path.empty() || |
| 134 kUsageFileSize != file_util::ReadFile(usage_file_path, | 134 kUsageFileSize != file_util::ReadFile(usage_file_path, |
| 135 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; |
| 146 | 146 |
| 147 if (header[0] != kUsageFileHeader[0] || | 147 if (header[0] != kUsageFileHeader[0] || |
| 148 header[1] != kUsageFileHeader[1] || | 148 header[1] != kUsageFileHeader[1] || |
| 149 header[2] != kUsageFileHeader[2] || | 149 header[2] != kUsageFileHeader[2] || |
| 150 header[3] != kUsageFileHeader[3]) | 150 header[3] != kUsageFileHeader[3]) |
| 151 return -1; | 151 return -1; |
| 152 | 152 |
| 153 return fs_usage; | 153 return fs_usage; |
| 154 } | 154 } |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 int FileSystemUsageCache::Write(const FilePath& usage_file_path, | 157 int FileSystemUsageCache::Write(const base::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 FilePath temporary_usage_file_path; | 167 base::FilePath temporary_usage_file_path; |
| 168 if (usage_file_path.empty() || | 168 if (usage_file_path.empty() || |
| 169 !file_util::CreateTemporaryFileInDir(usage_file_path.DirName(), | 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 |