blob: a421346b7f9308f423bbbf0bc68db0528c148546 [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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080025 void set_path(const base::FilePath &path) { path_ = path; }
26 const base::FilePath &path() const { return path_; }
Darin Petkov083047b2011-06-23 20:42:48 -070027
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;
Paul Stewart5b9ec982013-01-18 14:12:14 -080053 virtual std::set<std::string> GetGroupsWithProperties(
54 const KeyValueStore &properties) const;
Paul Stewart0756db92012-01-27 08:34:47 -080055 virtual bool ContainsGroup(const std::string &group) const;
Darin Petkov083047b2011-06-23 20:42:48 -070056 virtual bool DeleteKey(const std::string &group, const std::string &key);
57 virtual bool DeleteGroup(const std::string &group);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070058 virtual bool SetHeader(const std::string &header);
Darin Petkov083047b2011-06-23 20:42:48 -070059 virtual bool GetString(const std::string &group,
60 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080061 std::string *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070062 virtual bool SetString(const std::string &group,
63 const std::string &key,
64 const std::string &value);
65 virtual bool GetBool(const std::string &group,
66 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080067 bool *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070068 virtual bool SetBool(const std::string &group,
69 const std::string &key,
70 bool value);
71 virtual bool GetInt(const std::string &group,
72 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080073 int *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070074 virtual bool SetInt(const std::string &group,
75 const std::string &key,
76 int value);
Paul Stewartdab3b5a2012-07-11 18:25:10 -070077 virtual bool GetUint64(const std::string &group,
78 const std::string &key,
79 uint64 *value) const;
80 virtual bool SetUint64(const std::string &group,
81 const std::string &key,
82 uint64 value);
Darin Petkovb2841fd2011-06-30 12:54:12 -070083 virtual bool GetStringList(const std::string &group,
84 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080085 std::vector<std::string> *value) const;
Darin Petkovb2841fd2011-06-30 12:54:12 -070086 virtual bool SetStringList(const std::string &group,
87 const std::string &key,
88 const std::vector<std::string> &value);
Darin Petkov86964e02011-06-29 13:49:28 -070089 virtual bool GetCryptedString(const std::string &group,
90 const std::string &key,
91 std::string *value);
92 virtual bool SetCryptedString(const std::string &group,
93 const std::string &key,
94 const std::string &value);
Darin Petkov083047b2011-06-23 20:42:48 -070095 private:
96 FRIEND_TEST(KeyFileStoreTest, OpenClose);
97 FRIEND_TEST(KeyFileStoreTest, OpenFail);
98
Paul Stewart2ebc16d2012-08-23 10:38:39 -070099 static const char kCorruptSuffix[];
100
Darin Petkov083047b2011-06-23 20:42:48 -0700101 void ReleaseKeyFile();
Paul Stewart5b9ec982013-01-18 14:12:14 -0800102 bool DoesGroupMatchProperties(const std::string &group,
103 const KeyValueStore &properties) const;
Darin Petkov083047b2011-06-23 20:42:48 -0700104
105 GLib *glib_;
Darin Petkov86964e02011-06-29 13:49:28 -0700106 CryptoProvider crypto_;
Darin Petkov083047b2011-06-23 20:42:48 -0700107 GKeyFile *key_file_;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800108 base::FilePath path_;
Darin Petkov86964e02011-06-29 13:49:28 -0700109
110 DISALLOW_COPY_AND_ASSIGN(KeyFileStore);
Darin Petkov083047b2011-06-23 20:42:48 -0700111};
112
113} // namespace shill
114
115#endif // SHILL_KEY_FILE_STORE_