blob: 4d962b5a8bd3cb30983b478ed30a52293ae51c8b [file] [log] [blame]
Paul Stewart2ebc16d2012-08-23 10:38:39 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov083047b2011-06-23 20:42:48 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_KEY_FILE_STORE_
6#define SHILL_KEY_FILE_STORE_
7
8#include <base/file_path.h>
9#include <gtest/gtest_prod.h> // for FRIEND_TEST
10
Darin Petkov86964e02011-06-29 13:49:28 -070011#include "shill/crypto_provider.h"
Darin Petkov083047b2011-06-23 20:42:48 -070012#include "shill/glib.h"
13#include "shill/store_interface.h"
14
15namespace shill {
16
17// A key file store implementation of the store interface. See
18// http://www.gtk.org/api/2.6/glib/glib-Key-value-file-parser.html for details
19// of the key file format.
20class KeyFileStore : public StoreInterface {
21 public:
Darin Petkovb2841fd2011-06-30 12:54:12 -070022 explicit KeyFileStore(GLib *glib);
Darin Petkov083047b2011-06-23 20:42:48 -070023 virtual ~KeyFileStore();
24
25 void set_path(const FilePath &path) { path_ = path; }
26 const FilePath &path() const { return path_; }
27
Paul Stewart5dc40aa2011-10-28 19:43:43 -070028 // Returns true if the store exists and is non-empty.
Paul Stewart0756db92012-01-27 08:34:47 -080029 bool IsNonEmpty() const;
Paul Stewart5dc40aa2011-10-28 19:43:43 -070030
Chris Masone9d779932011-08-25 16:33:41 -070031 // Opens the store. Returns true on success. This method must be
32 // invoked before using any of the getters or setters.
33 // This method does not complete gracefully if invoked on a store
34 // that has been opened already but not closed yet.
35 bool Open();
36
37 // Closes the store and flushes it to persistent storage. Returns true on
38 // success. Note that the store is considered closed even if Close returns
39 // false.
40 // This method does not complete gracefully if invoked on a store
41 // that has not been opened successfully or has been closed already.
42 bool Close();
43
Paul Stewart2ebc16d2012-08-23 10:38:39 -070044 // Mark the underlying file store as corrupted, moving the data file
45 // to a new filename. This will prevent the file from being re-opened
46 // the next time Open() is called.
47 bool MarkAsCorrupted();
48
Darin Petkov083047b2011-06-23 20:42:48 -070049 // Inherited from StoreInterface.
Chris Masoneb9c00592011-10-06 13:10:39 -070050 virtual bool Flush();
Paul Stewart0756db92012-01-27 08:34:47 -080051 virtual std::set<std::string> GetGroups() const;
52 virtual std::set<std::string> GetGroupsWithKey(const std::string &key) const;
53 virtual bool ContainsGroup(const std::string &group) const;
Darin Petkov083047b2011-06-23 20:42:48 -070054 virtual bool DeleteKey(const std::string &group, const std::string &key);
55 virtual bool DeleteGroup(const std::string &group);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070056 virtual bool SetHeader(const std::string &header);
Darin Petkov083047b2011-06-23 20:42:48 -070057 virtual bool GetString(const std::string &group,
58 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080059 std::string *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070060 virtual bool SetString(const std::string &group,
61 const std::string &key,
62 const std::string &value);
63 virtual bool GetBool(const std::string &group,
64 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080065 bool *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070066 virtual bool SetBool(const std::string &group,
67 const std::string &key,
68 bool value);
69 virtual bool GetInt(const std::string &group,
70 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080071 int *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070072 virtual bool SetInt(const std::string &group,
73 const std::string &key,
74 int value);
Paul Stewartdab3b5a2012-07-11 18:25:10 -070075 virtual bool GetUint64(const std::string &group,
76 const std::string &key,
77 uint64 *value) const;
78 virtual bool SetUint64(const std::string &group,
79 const std::string &key,
80 uint64 value);
Darin Petkovb2841fd2011-06-30 12:54:12 -070081 virtual bool GetStringList(const std::string &group,
82 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080083 std::vector<std::string> *value) const;
Darin Petkovb2841fd2011-06-30 12:54:12 -070084 virtual bool SetStringList(const std::string &group,
85 const std::string &key,
86 const std::vector<std::string> &value);
Darin Petkov86964e02011-06-29 13:49:28 -070087 virtual bool GetCryptedString(const std::string &group,
88 const std::string &key,
89 std::string *value);
90 virtual bool SetCryptedString(const std::string &group,
91 const std::string &key,
92 const std::string &value);
Darin Petkov083047b2011-06-23 20:42:48 -070093 private:
94 FRIEND_TEST(KeyFileStoreTest, OpenClose);
95 FRIEND_TEST(KeyFileStoreTest, OpenFail);
96
Paul Stewart2ebc16d2012-08-23 10:38:39 -070097 static const char kCorruptSuffix[];
98
Darin Petkov083047b2011-06-23 20:42:48 -070099 void ReleaseKeyFile();
100
101 GLib *glib_;
Darin Petkov86964e02011-06-29 13:49:28 -0700102 CryptoProvider crypto_;
Darin Petkov083047b2011-06-23 20:42:48 -0700103 GKeyFile *key_file_;
104 FilePath path_;
Darin Petkov86964e02011-06-29 13:49:28 -0700105
106 DISALLOW_COPY_AND_ASSIGN(KeyFileStore);
Darin Petkov083047b2011-06-23 20:42:48 -0700107};
108
109} // namespace shill
110
111#endif // SHILL_KEY_FILE_STORE_