2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "PackageSettingsItem.h"
9 #include <driver_settings.h>
11 #include <AutoDeleter.h>
13 #include <system/directories.h>
19 // #pragma mark - PackageSettingsItem
22 PackageSettingsItem::PackageSettingsItem()
30 PackageSettingsItem::~PackageSettingsItem()
32 Entry
* entry
= fEntries
.Clear(true);
33 while (entry
!= NULL
) {
34 Entry
* next
= entry
->HashNext();
41 /*static*/ PackageSettingsItem
*
42 PackageSettingsItem::Load(::Directory
* systemDirectory
, const char* name
)
44 // open the driver settings file
45 const char* settingsFilePath
46 = kSystemSettingsDirectory
"/packages" + strlen(kSystemDirectory
) + 1;
48 int fd
= open_from(systemDirectory
, settingsFilePath
, B_READ_ONLY
, 0);
51 FileDescriptorCloser
fdCloser(fd
);
53 // load the driver settings
54 void* settingsHandle
= load_driver_settings_file(fd
);
55 if (settingsHandle
== NULL
)
57 CObjectDeleter
<void, status_t
> settingsDeleter(settingsHandle
,
58 &unload_driver_settings
);
60 const driver_settings
* settings
= get_driver_settings(settingsHandle
);
61 for (int i
= 0; i
< settings
->parameter_count
; i
++) {
62 const driver_parameter
& parameter
= settings
->parameters
[i
];
63 if (strcmp(parameter
.name
, "Package") != 0
64 || parameter
.value_count
< 1
65 || strcmp(parameter
.values
[0], name
) != 0) {
69 PackageSettingsItem
* settingsItem
70 = new(std::nothrow
) PackageSettingsItem
;
71 if (settingsItem
== NULL
|| settingsItem
->Init(parameter
) != B_OK
) {
84 PackageSettingsItem::Init(const driver_parameter
& parameter
)
86 if (fEntries
.Init() != B_OK
)
89 for (int i
= 0; i
< parameter
.parameter_count
; i
++) {
90 const driver_parameter
& subParameter
= parameter
.parameters
[i
];
91 if (strcmp(subParameter
.name
, "EntryBlacklist") != 0)
94 status_t error
= _AddBlackListedEntries(subParameter
);
95 // abort only in case of serious issues (memory shortage)
96 if (error
== B_NO_MEMORY
)
105 PackageSettingsItem::AddEntry(Entry
* entry
)
107 fEntries
.Insert(entry
);
112 PackageSettingsItem::AddEntry(const char* path
, Entry
*& _entry
)
114 Entry
* parent
= NULL
;
116 while (*path
!= '\0') {
117 while (*path
== '/') {
122 const char* componentEnd
= strchr(path
, '/');
123 if (componentEnd
== NULL
)
124 componentEnd
= path
+ strlen(path
);
126 const char* name
= path
;
127 size_t nameLength
= componentEnd
- path
;
129 Entry
* entry
= FindEntry(parent
, name
, nameLength
);
131 entry
= new(std::nothrow
) Entry(parent
);
132 if (entry
== NULL
|| !entry
->SetName(name
, nameLength
)) {
151 PackageSettingsItem::Entry
*
152 PackageSettingsItem::FindEntry(Entry
* parent
, const char* name
) const
154 return fEntries
.Lookup(EntryKey(parent
, name
));
158 PackageSettingsItem::Entry
*
159 PackageSettingsItem::FindEntry(Entry
* parent
, const char* name
,
160 size_t nameLength
) const
162 return fEntries
.Lookup(EntryKey(parent
, name
, nameLength
));
167 PackageSettingsItem::_AddBlackListedEntries(const driver_parameter
& parameter
)
169 for (int i
= 0; i
< parameter
.parameter_count
; i
++) {
171 status_t error
= AddEntry(parameter
.parameters
[i
].name
, entry
);
172 // abort only in case of serious issues (memory shortage)
173 if (error
== B_NO_MEMORY
)
176 entry
->SetBlackListed(true);
183 } // namespace PackageFS