OLD | NEW |
| (Empty) |
1 // Copyright 2006-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Defines class FileStore | |
17 | |
18 #include <vector> | |
19 #include "omaha/base/debug.h" | |
20 #include "omaha/base/file.h" | |
21 #include "omaha/base/file_store.h" | |
22 #include "omaha/base/string.h" | |
23 #include "omaha/base/utils.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 // Open the store | |
28 bool FileStore::Open(const TCHAR* file_path) { | |
29 file_path_ = String_MakeEndWith(file_path, _T("\\"), false); | |
30 return true; | |
31 } | |
32 | |
33 // Close the store | |
34 bool FileStore::Close() { | |
35 file_path_.Empty(); | |
36 return true; | |
37 } | |
38 | |
39 // Clear the store | |
40 bool FileStore::Clear() { | |
41 if (File::Exists(file_path_) && File::IsDirectory(file_path_)) { | |
42 return SUCCEEDED(DeleteDirectoryFiles(file_path_)); | |
43 } else { | |
44 return true; | |
45 } | |
46 } | |
47 | |
48 // Read a value from the store | |
49 bool FileStore::Read(const TCHAR* name, std::vector<byte>* data) const { | |
50 ASSERT1(name); | |
51 ASSERT1(data); | |
52 | |
53 return Exists(name) && SUCCEEDED(ReadEntireFile(file_path_ + name, 0, data)); | |
54 } | |
55 | |
56 // Write a value to the store | |
57 bool FileStore::Write(const TCHAR* name, byte* data, int data_size) { | |
58 ASSERT1(name); | |
59 ASSERT1(data); | |
60 ASSERT1(data_size); | |
61 | |
62 std::vector<byte> buffer(data_size); | |
63 memcpy(&buffer.front(), data, data_size); | |
64 | |
65 return SUCCEEDED(WriteEntireFile(file_path_ + name, buffer)); | |
66 } | |
67 | |
68 // Check to see a named value exists in the store | |
69 bool FileStore::Exists(const TCHAR* name) const { | |
70 ASSERT1(name); | |
71 | |
72 return File::Exists(file_path_ + name); | |
73 } | |
74 | |
75 // Remove a value from the store | |
76 bool FileStore::Remove(const TCHAR* name) { | |
77 ASSERT1(name); | |
78 | |
79 return SUCCEEDED(File::Remove(file_path_ + name)); | |
80 } | |
81 | |
82 // Get the number of values for this store | |
83 bool FileStore::GetValueCount(uint32* value_count) { | |
84 ASSERT1(value_count); | |
85 | |
86 std::vector<CString> matching_paths; | |
87 | |
88 if (FAILED(File::GetWildcards(file_path_, _T("*"), &matching_paths))) { | |
89 return false; | |
90 } | |
91 | |
92 *value_count = matching_paths.size(); | |
93 | |
94 return true; | |
95 } | |
96 | |
97 // Get the value name for the given value name index | |
98 bool FileStore::GetValueNameAt(uint32 index, CString* value_name) { | |
99 ASSERT1(value_name); | |
100 | |
101 std::vector<CString> matching_paths; | |
102 | |
103 if (FAILED(File::GetWildcards(file_path_, _T("*"), &matching_paths))) { | |
104 return false; | |
105 } | |
106 if (index >= matching_paths.size()) { | |
107 return false; | |
108 } | |
109 | |
110 *value_name = matching_paths[index].Mid(file_path_.GetLength()); | |
111 | |
112 return true; | |
113 } | |
114 | |
115 } // namespace omaha | |
116 | |
OLD | NEW |