xref: /haiku/src/system/boot/loader/file_systems/packagefs/PackageSettingsItem.h (revision 4a3268e14fff4dd5a456d824b48ce6503368e4c1)
1 /*
2  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H
6 #define BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H
7 
8 
9 #include <string.h>
10 
11 #include <util/OpenHashTable.h>
12 #include <util/StringHash.h>
13 
14 
15 struct driver_parameter;
16 class Directory;
17 
18 
19 namespace PackageFS {
20 
21 
22 class PackageSettingsItem {
23 public:
24 			class Entry {
25 			public:
26 				Entry(Entry* parent)
27 					:
28 					fParent(parent),
29 					fName(NULL),
30 					fIsBlackListed(false)
31 				{
32 				}
33 
34 				~Entry()
35 				{
36 					free(fName);
37 				}
38 
39 				bool SetName(const char* name, size_t nameLength)
40 				{
41 					fName = (char*)malloc(nameLength + 1);
42 					if (fName == NULL)
43 						return false;
44 
45 					memcpy(fName, name, nameLength);
46 					fName[nameLength] = '\0';
47 					return true;
48 				}
49 
50 				Entry* Parent() const
51 				{
52 					return fParent;
53 				}
54 
55 				const char* Name() const
56 				{
57 					return fName;
58 				}
59 
60 				bool IsBlackListed() const
61 				{
62 					return fIsBlackListed;
63 				}
64 
65 				void SetBlackListed(bool blackListed)
66 				{
67 					fIsBlackListed = blackListed;
68 				}
69 
70 				Entry*& HashNext()
71 				{
72 					return fHashNext;
73 				}
74 
75 			private:
76 				Entry*	fParent;
77 				char*	fName;
78 				bool	fIsBlackListed;
79 				Entry*	fHashNext;
80 			};
81 
82 			class EntryKey {
83 			public:
84 				EntryKey(Entry* parent, const char* name, size_t nameLength)
85 					:
86 					fParent(parent),
87 					fName(name),
88 					fNameLength(nameLength)
89 				{
90 				}
91 
92 				EntryKey(Entry* parent, const char* name)
93 					:
94 					fParent(parent),
95 					fName(name),
96 					fNameLength(strlen(name))
97 				{
98 				}
99 
100 				Entry* Parent() const
101 				{
102 					return fParent;
103 				}
104 
105 				const char* Name() const
106 				{
107 					return fName;
108 				}
109 
110 				size_t NameLength() const
111 				{
112 					return fNameLength;
113 				}
114 
115 				size_t Hash() const
116 				{
117 					return (addr_t)fParent / 8
118 						^ hash_hash_string_part(fName, fNameLength);
119 				}
120 
121 			private:
122 				Entry*		fParent;
123 				const char*	fName;
124 				size_t		fNameLength;
125 			};
126 
127 public:
128 								PackageSettingsItem();
129 								~PackageSettingsItem();
130 
131 	static	PackageSettingsItem* Load(::Directory* systemDirectory,
132 									const char* name);
133 
134 			status_t			Init(const driver_parameter& parameter);
135 
136 			void				AddEntry(Entry* entry);
137 			status_t			AddEntry(const char* path, Entry*& _entry);
138 			Entry*				FindEntry(Entry* parent, const char* name)
139 									const;
140 			Entry*				FindEntry(Entry* parent, const char* name,
141 									size_t nameLength) const;
142 
143 			PackageSettingsItem*& HashNext()
144 									{ return fHashNext; }
145 
146 private:
147 			struct EntryHashDefinition {
148 				typedef EntryKey	KeyType;
149 				typedef	Entry		ValueType;
150 
151 				size_t HashKey(const EntryKey& key) const
152 				{
153 					return key.Hash();
154 				}
155 
156 				size_t Hash(const Entry* value) const
157 				{
158 					return HashKey(EntryKey(value->Parent(), value->Name()));
159 				}
160 
161 				bool Compare(const EntryKey& key, const Entry* value) const
162 				{
163 					const char* name = value->Name();
164 					return key.Parent() == value->Parent()
165 						&& strncmp(key.Name(), name, key.NameLength()) == 0
166 						&& name[key.NameLength()] == '\0';
167 				}
168 
169 				Entry*& GetLink(Entry* value) const
170 				{
171 					return value->HashNext();
172 				}
173 			};
174 
175 			typedef BOpenHashTable<EntryHashDefinition> EntryTable;
176 
177 private:
178 			status_t			_AddBlackListedEntries(
179 									const driver_parameter& parameter);
180 
181 private:
182 			EntryTable			fEntries;
183 			PackageSettingsItem* fHashNext;
184 };
185 
186 
187 }	// namespace PackageFS
188 
189 
190 #endif	// BOOT_LOADER_FILE_SYSTEMS_PACKAGEFS_PACKAGE_SETTINGS_ITEM_H
191