1/*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef PACKAGE_SETTINGS_H
6#define PACKAGE_SETTINGS_H
7
8
9#include <packagefs.h>
10#include <util/OpenHashTable.h>
11
12#include "StringKey.h"
13
14
15struct driver_parameter;
16
17
18class PackageSettingsItem {
19public:
20			class Entry {
21			public:
22				Entry(Entry* parent, const String& name)
23					:
24					fParent(parent),
25					fName(name),
26					fIsBlocked(false)
27				{
28				}
29
30				Entry* Parent() const
31				{
32					return fParent;
33				}
34
35				const String& Name() const
36				{
37					return fName;
38				}
39
40				bool IsBlocked() const
41				{
42					return fIsBlocked;
43				}
44
45				void SetBlocked(bool blocked)
46				{
47					fIsBlocked = blocked;
48				}
49
50				Entry*& HashNext()
51				{
52					return fHashNext;
53				}
54
55			private:
56				Entry*	fParent;
57				String	fName;
58				bool	fIsBlocked;
59				Entry*	fHashNext;
60			};
61
62			class EntryKey {
63			public:
64				EntryKey(Entry* parent, const char* name)
65					:
66					fParent(parent),
67					fName(name),
68					fHash((addr_t)parent / 8 ^ hash_hash_string(name))
69				{
70				}
71
72				EntryKey(Entry* parent, const String& name)
73					:
74					fParent(parent),
75					fName(name),
76					fHash((addr_t)parent / 8 ^ name.Hash())
77				{
78				}
79
80				Entry* Parent() const
81				{
82					return fParent;
83				}
84
85				const char* Name() const
86				{
87					return fName;
88				}
89
90				size_t Hash() const
91				{
92					return fHash;
93				}
94
95			private:
96				Entry*		fParent;
97				const char*	fName;
98				size_t		fHash;
99			};
100
101public:
102								PackageSettingsItem();
103								~PackageSettingsItem();
104
105			status_t			Init(const char* name);
106			status_t			ApplySettings(
107									const driver_parameter* parameters,
108									int parameterCount);
109
110			const String&		Name() const
111									{ return fName; }
112
113			void				AddEntry(Entry* entry);
114			status_t			AddEntry(const char* path, Entry*& _entry);
115			Entry*				FindEntry(Entry* parent, const String& name)
116									const;
117			Entry*				FindEntry(Entry* parent, const char* name)
118									const;
119
120			PackageSettingsItem*& HashNext()
121									{ return fHashNext; }
122
123private:
124			struct EntryHashDefinition {
125				typedef EntryKey	KeyType;
126				typedef	Entry		ValueType;
127
128				size_t HashKey(const EntryKey& key) const
129				{
130					return key.Hash();
131				}
132
133				size_t Hash(const Entry* value) const
134				{
135					return HashKey(EntryKey(value->Parent(), value->Name()));
136				}
137
138				bool Compare(const EntryKey& key, const Entry* value) const
139				{
140					return key.Parent() == value->Parent()
141						&& strcmp(key.Name(), value->Name()) == 0;
142				}
143
144				Entry*& GetLink(Entry* value) const
145				{
146					return value->HashNext();
147				}
148			};
149
150			typedef BOpenHashTable<EntryHashDefinition> EntryTable;
151
152private:
153			status_t			_AddBlockedEntries(
154									const driver_parameter& parameter);
155
156private:
157			String				fName;
158			EntryTable			fEntries;
159			PackageSettingsItem* fHashNext;
160};
161
162
163struct PackageSettingsItemHashDefinition {
164	typedef StringKey			KeyType;
165	typedef	PackageSettingsItem	ValueType;
166
167	size_t HashKey(const StringKey& key) const
168	{
169		return key.Hash();
170	}
171
172	size_t Hash(const PackageSettingsItem* value) const
173	{
174		return HashKey(value->Name());
175	}
176
177	bool Compare(const StringKey& key, const PackageSettingsItem* value) const
178	{
179		return key == value->Name();
180	}
181
182	PackageSettingsItem*& GetLink(PackageSettingsItem* value) const
183	{
184		return value->HashNext();
185	}
186};
187
188
189class PackageSettings {
190public:
191								PackageSettings();
192								~PackageSettings();
193
194			status_t			Load(dev_t mountPointDeviceID,
195									ino_t mountPointNodeID,
196									PackageFSMountType mountType);
197
198			const PackageSettingsItem* PackageItemFor(const String& name) const;
199
200private:
201			typedef BOpenHashTable<PackageSettingsItemHashDefinition>
202				PackageItemTable;
203
204private:
205			status_t			_AddPackageSettingsItem(const char* name,
206									const driver_parameter* parameters,
207									int parameterCount);
208
209private:
210			PackageItemTable	fPackageItems;
211};
212
213
214#endif	// PACKAGE_SETTINGS_H
215