Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: webkit/fileapi/file_system_usage_cache.cc

Issue 7608018: Handle inconsistency between DB and Files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed regression Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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 char FileSystemUsageCache::kUsageFileName[] = ".usage"; 13 const char FileSystemUsageCache::kUsageFileName[] = ".usage";
14 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU3"; 14 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU4";
15 const int FileSystemUsageCache::kUsageFileHeaderSize = 4; 15 const int FileSystemUsageCache::kUsageFileHeaderSize = 4;
16
17 /* Pickle::{Read,Write}Bool treat bool as int */
16 const int FileSystemUsageCache::kUsageFileSize = 18 const int FileSystemUsageCache::kUsageFileSize =
17 sizeof(Pickle::Header) + 19 sizeof(Pickle::Header) +
18 FileSystemUsageCache::kUsageFileHeaderSize + 20 FileSystemUsageCache::kUsageFileHeaderSize +
19 sizeof(int32) + sizeof(int64); 21 sizeof(int) + sizeof(int32) + sizeof(int64);
20 22
21 // static 23 // static
22 int64 FileSystemUsageCache::GetUsage(const FilePath& usage_file_path) { 24 int64 FileSystemUsageCache::GetUsage(const FilePath& usage_file_path) {
25 bool is_valid = true;
23 uint32 dirty = 0; 26 uint32 dirty = 0;
24 int64 fs_usage; 27 int64 fs_usage;
25 fs_usage = Read(usage_file_path, &dirty); 28 fs_usage = Read(usage_file_path, &is_valid, &dirty);
26 29
27 if (fs_usage < 0) 30 if (fs_usage < 0)
28 return -1; 31 return -1;
29 32
30 return fs_usage; 33 return fs_usage;
31 } 34 }
32 35
33 // static 36 // static
34 int32 FileSystemUsageCache::GetDirty(const FilePath& usage_file_path) { 37 int32 FileSystemUsageCache::GetDirty(const FilePath& usage_file_path) {
38 bool is_valid = true;
35 uint32 dirty = 0; 39 uint32 dirty = 0;
36 int64 fs_usage; 40 int64 fs_usage;
37 fs_usage = Read(usage_file_path, &dirty); 41 fs_usage = Read(usage_file_path, &is_valid, &dirty);
38 42
39 if (fs_usage < 0) 43 if (fs_usage < 0)
40 return -1; 44 return -1;
41 45
42 return static_cast<int32>(dirty); 46 return static_cast<int32>(dirty);
43 } 47 }
44 48
45 // static 49 // static
46 bool FileSystemUsageCache::IncrementDirty(const FilePath& usage_file_path) { 50 bool FileSystemUsageCache::IncrementDirty(const FilePath& usage_file_path) {
51 bool is_valid = true;
47 uint32 dirty = 0; 52 uint32 dirty = 0;
48 int64 fs_usage; 53 int64 fs_usage;
49 fs_usage = Read(usage_file_path, &dirty); 54 fs_usage = Read(usage_file_path, &is_valid, &dirty);
50 55
51 if (fs_usage < 0) 56 if (fs_usage < 0)
52 return false; 57 return false;
53 58
54 return Write(usage_file_path, dirty + 1, fs_usage) >= 0; 59 return Write(usage_file_path, is_valid, dirty + 1, fs_usage) >= 0;
55 } 60 }
56 61
57 // static 62 // static
58 bool FileSystemUsageCache::DecrementDirty(const FilePath& usage_file_path) { 63 bool FileSystemUsageCache::DecrementDirty(const FilePath& usage_file_path) {
64 bool is_valid = true;
59 uint32 dirty = 0; 65 uint32 dirty = 0;
60 int64 fs_usage; 66 int64 fs_usage;
61 fs_usage = Read(usage_file_path, &dirty); 67 fs_usage = Read(usage_file_path, &is_valid, &dirty);
62 68
63 if (fs_usage < 0 || dirty <= 0) 69 if (fs_usage < 0 || dirty <= 0)
64 return false; 70 return false;
65 71
66 return Write(usage_file_path, dirty - 1, fs_usage) >= 0; 72 return Write(usage_file_path, is_valid, dirty - 1, fs_usage) >= 0;
73 }
74
75 // static
76 bool FileSystemUsageCache::Invalidate(const FilePath& usage_file_path) {
77 bool is_valid = true;
78 uint32 dirty = 0;
79 int64 fs_usage;
80 fs_usage = Read(usage_file_path, &is_valid, &dirty);
81
82 return fs_usage >= 0 && Write(usage_file_path, false, dirty, fs_usage);
83 }
84
85 bool FileSystemUsageCache::IsValid(const FilePath& usage_file_path) {
86 bool is_valid = true;
87 uint32 dirty = 0;
88 int64 fs_usage;
89 fs_usage = Read(usage_file_path, &is_valid, &dirty);
90 return is_valid;
67 } 91 }
68 92
69 // static 93 // static
70 int FileSystemUsageCache::AtomicUpdateUsageByDelta( 94 int FileSystemUsageCache::AtomicUpdateUsageByDelta(
71 const FilePath& usage_file_path, int64 delta) { 95 const FilePath& usage_file_path, int64 delta) {
96 bool is_valid = true;
72 uint32 dirty = 0; 97 uint32 dirty = 0;
73 int64 fs_usage; 98 int64 fs_usage;
74 // TODO(dmikurube): Make sure that usage_file_path is available. 99 // TODO(dmikurube): Make sure that usage_file_path is available.
75 fs_usage = Read(usage_file_path, &dirty); 100 fs_usage = Read(usage_file_path, &is_valid, &dirty);
76 101
77 if (fs_usage < 0) 102 if (fs_usage < 0)
78 return -1; 103 return -1;
79 104
80 return Write(usage_file_path, dirty, fs_usage + delta); 105 return Write(usage_file_path, is_valid, dirty, fs_usage + delta);
81 } 106 }
82 107
83 // static 108 // static
84 int FileSystemUsageCache::UpdateUsage(const FilePath& usage_file_path, 109 int FileSystemUsageCache::UpdateUsage(const FilePath& usage_file_path,
85 int64 fs_usage) { 110 int64 fs_usage) {
86 return Write(usage_file_path, 0, fs_usage); 111 return Write(usage_file_path, true, 0, fs_usage);
87 } 112 }
88 113
89 // static 114 // static
90 bool FileSystemUsageCache::Exists(const FilePath& usage_file_path) { 115 bool FileSystemUsageCache::Exists(const FilePath& usage_file_path) {
91 return file_util::PathExists(usage_file_path); 116 return file_util::PathExists(usage_file_path);
92 } 117 }
93 118
94 // static 119 // static
95 bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) { 120 bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) {
96 return file_util::Delete(usage_file_path, true); 121 return file_util::Delete(usage_file_path, true);
97 } 122 }
98 123
99 // static 124 // static
100 int64 FileSystemUsageCache::Read(const FilePath& usage_file_path, 125 int64 FileSystemUsageCache::Read(const FilePath& usage_file_path,
126 bool* is_valid,
101 uint32* dirty) { 127 uint32* dirty) {
102 char buffer[kUsageFileSize]; 128 char buffer[kUsageFileSize];
103 const char *header; 129 const char *header;
104 DCHECK(!usage_file_path.empty()); 130 DCHECK(!usage_file_path.empty());
105 if (kUsageFileSize != 131 if (kUsageFileSize !=
106 file_util::ReadFile(usage_file_path, buffer, kUsageFileSize)) 132 file_util::ReadFile(usage_file_path, buffer, kUsageFileSize))
107 return -1; 133 return -1;
108 Pickle read_pickle(buffer, kUsageFileSize); 134 Pickle read_pickle(buffer, kUsageFileSize);
109 void* iter = NULL; 135 void* iter = NULL;
110 int64 fs_usage; 136 int64 fs_usage;
111 137
112 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) || 138 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) ||
139 !read_pickle.ReadBool(&iter, is_valid) ||
113 !read_pickle.ReadUInt32(&iter, dirty) || 140 !read_pickle.ReadUInt32(&iter, dirty) ||
114 !read_pickle.ReadInt64(&iter, &fs_usage)) 141 !read_pickle.ReadInt64(&iter, &fs_usage))
115 return -1; 142 return -1;
116 143
117 if (header[0] != kUsageFileHeader[0] || 144 if (header[0] != kUsageFileHeader[0] ||
118 header[1] != kUsageFileHeader[1] || 145 header[1] != kUsageFileHeader[1] ||
119 header[2] != kUsageFileHeader[2] || 146 header[2] != kUsageFileHeader[2] ||
120 header[3] != kUsageFileHeader[3]) 147 header[3] != kUsageFileHeader[3])
121 return -1; 148 return -1;
122 149
123 return fs_usage; 150 return fs_usage;
124 } 151 }
125 152
126 // static 153 // static
127 int FileSystemUsageCache::Write(const FilePath& usage_file_path, 154 int FileSystemUsageCache::Write(const FilePath& usage_file_path,
128 uint32 dirty, int64 fs_usage) { 155 bool is_valid,
156 uint32 dirty,
157 int64 fs_usage) {
129 Pickle write_pickle; 158 Pickle write_pickle;
130 write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize); 159 write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize);
160 write_pickle.WriteBool(is_valid);
131 write_pickle.WriteUInt32(dirty); 161 write_pickle.WriteUInt32(dirty);
132 write_pickle.WriteInt64(fs_usage); 162 write_pickle.WriteInt64(fs_usage);
133 163
134 DCHECK(!usage_file_path.empty()); 164 DCHECK(!usage_file_path.empty());
135 FilePath temporary_usage_file_path; 165 FilePath temporary_usage_file_path;
136 file_util::CreateTemporaryFileInDir(usage_file_path.DirName(), 166 file_util::CreateTemporaryFileInDir(usage_file_path.DirName(),
137 &temporary_usage_file_path); 167 &temporary_usage_file_path);
138 int bytes_written = file_util::WriteFile(temporary_usage_file_path, 168 int bytes_written = file_util::WriteFile(temporary_usage_file_path,
139 (const char *)write_pickle.data(), 169 (const char *)write_pickle.data(),
140 write_pickle.size()); 170 write_pickle.size());
141 if (bytes_written != kUsageFileSize) 171 if (bytes_written != kUsageFileSize)
142 return -1; 172 return -1;
143 173
144 if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path)) 174 if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path))
145 return bytes_written; 175 return bytes_written;
146 else 176 else
147 return -1; 177 return -1;
148 } 178 }
149 179
150 } // namespace fileapi 180 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_usage_cache.h ('k') | webkit/fileapi/obfuscated_file_system_file_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698