Skip to content

Commit e1c1c94

Browse files
sedmichaLukas Hutak
authored andcommitted
Iemgr: Improve API consistency, read aliases and mappings when reading dir
1 parent e14676d commit e1c1c94

5 files changed

Lines changed: 34 additions & 42 deletions

File tree

include/libfds/iemgr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,12 @@ fds_iemgr_last_err(const fds_iemgr_t *mgr);
654654

655655
/**
656656
* Load an aliases file and save the information to the manager.
657-
* \param mgr The information element manager.
658-
* \param dir Directory to look for the aliases.
657+
* \param mgr The information element manager.
658+
* \param file_path Path to the file to read the aliases from.
659659
* \return FDS_OK on success, FDS error code otherwise.
660660
*/
661661
FDS_API int
662-
fds_iemgr_read_aliases(fds_iemgr_t *mgr, const char *file_path);
662+
fds_iemgr_alias_read_file(fds_iemgr_t *mgr, const char *file_path);
663663

664664
/**
665665
* \brief Find an alias by name
@@ -682,12 +682,12 @@ fds_iemgr_mapping_find(const fds_iemgr_t *mgr, const char *name, const char *key
682682

683683
/**
684684
* Load a mappings file and save the information to the manager.
685-
* \param mgr The information element manager.
686-
* \param dir Directory to look for the mappings.
685+
* \param mgr The information element manager.
686+
* \param file_path Path to the file to read the mappings from.
687687
* \return FDS_OK on success, FDS error code otherwise.
688688
*/
689689
FDS_API int
690-
fds_iemgr_read_mappings(fds_iemgr_t *mgr, const char *dir);
690+
fds_iemgr_mapping_read_file(fds_iemgr_t *mgr, const char *file_path);
691691

692692

693693
#ifdef __cplusplus

src/iemgr/iemgr.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ dir_read(fds_iemgr_t* mgr, const char* path, fds_xml_t* parser, const string& na
279279
}
280280
}
281281

282+
int rc;
283+
284+
rc = fds_iemgr_alias_read_file(mgr, (string(path) + "/" + string(name) + "/aliases.xml").c_str());
285+
if (rc != FDS_OK && rc != FDS_ERR_NOTFOUND) {
286+
return false;
287+
}
288+
mgr->err_msg.clear();
289+
290+
rc = fds_iemgr_mapping_read_file(mgr, (string(path) + "/" + string(name) + "/mappings.xml").c_str());
291+
if (rc != FDS_OK && rc != FDS_ERR_NOTFOUND) {
292+
return false;
293+
}
294+
mgr->err_msg.clear();
295+
282296
return true;
283297
}
284298

@@ -403,18 +417,6 @@ fds_iemgr_read_dir(fds_iemgr_t *mgr, const char *path)
403417
if (!dirs_read(mgr, path)) {
404418
return FDS_ERR_FORMAT;
405419
}
406-
407-
int rc;
408-
409-
rc = fds_iemgr_read_aliases(mgr, path);
410-
if (rc != FDS_OK && rc != FDS_ERR_NOTFOUND) {
411-
return rc;
412-
}
413-
414-
rc = fds_iemgr_read_mappings(mgr, path);
415-
if (rc != FDS_OK && rc != FDS_ERR_NOTFOUND) {
416-
return rc;
417-
}
418420
}
419421
catch (...) {
420422
mgr->err_msg = "Error in function 'fds_iemgr_read_dir' while allocating memory for directory reading.";
@@ -770,26 +772,12 @@ fds_iemgr_str2unit(const char *str)
770772
return FDS_EU_UNASSIGNED;
771773
}
772774

773-
int
774-
fds_iemgr_read_aliases(fds_iemgr_t *mgr, const char *dir)
775-
{
776-
int rc = read_aliases_file(mgr, (std::string(dir) + "system/aliases.xml").c_str());
777-
return rc;
778-
}
779-
780775
const struct fds_iemgr_alias *
781776
fds_iemgr_alias_find(const fds_iemgr_t *mgr, const char *aliased_name)
782777
{
783778
return binary_find(mgr->aliased_names, std::string(aliased_name));
784779
}
785780

786-
int
787-
fds_iemgr_read_mappings(fds_iemgr_t *mgr, const char *dir)
788-
{
789-
int rc = read_mappings_file(mgr, (std::string(dir) + "system/mappings.xml").c_str());
790-
return rc;
791-
}
792-
793781
const struct fds_iemgr_mapping_item *
794782
fds_iemgr_mapping_find(const fds_iemgr_t *mgr, const char *name, const char *key)
795783
{
@@ -803,15 +791,15 @@ fds_iemgr_mapping_find(const fds_iemgr_t *mgr, const char *name, const char *key
803791
return item;
804792
}
805793
}
806-
}
794+
}
807795

808796
const fds_iemgr_elem *elem = fds_iemgr_elem_find_name(mgr, name);
809797
if (elem != nullptr) {
810798
item = find_mapping_in_elem(elem, key);
811799
if (item != nullptr) {
812800
return item;
813801
}
814-
}
802+
}
815803

816804
return nullptr;
817805
}

src/iemgr/iemgr_alias.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ alias_migrate_sources(fds_iemgr *mgr, fds_iemgr_alias *alias)
115115
static int
116116
alias_save_to_mgr(fds_iemgr *mgr, fds_iemgr_alias *alias)
117117
{
118+
for (size_t i = 0; i < alias->aliased_names_cnt; i++) {
119+
if (fds_iemgr_alias_find(mgr, alias->aliased_names[i])) {
120+
mgr->err_msg = "Duplicate aliased name '" + std::string(alias->aliased_names[i]) + "'";
121+
return FDS_ERR_FORMAT;
122+
}
123+
}
124+
118125
mgr->aliases.push_back(alias);
119126
for (size_t i = 0; i < alias->aliased_names_cnt; i++) {
120127
mgr->aliased_names.emplace_back(std::string(alias->aliased_names[i]), alias);
@@ -197,17 +204,17 @@ static int
197204
read_source(fds_iemgr_t *mgr, fds_xml_ctx_t *xml_ctx, fds_iemgr_alias *alias);
198205

199206
int
200-
read_aliases_file(fds_iemgr_t *mgr, const char *path)
207+
fds_iemgr_alias_read_file(fds_iemgr_t *mgr, const char *file_path)
201208
{
202209
// Open file
203-
auto file = unique_file(fopen(path, "r"), &::fclose);
210+
auto file = unique_file(fopen(file_path, "r"), &::fclose);
204211
if (file == nullptr) {
205-
mgr->err_msg = "Cannot open file " + std::string(path) + ": " + std::strerror(errno);
212+
mgr->err_msg = "Cannot open file " + std::string(file_path) + ": " + std::strerror(errno);
206213
return FDS_ERR_NOTFOUND;
207214
}
208215

209216
// Save modification time to the manager
210-
if (!mtime_save(mgr, path)) {
217+
if (!mtime_save(mgr, file_path)) {
211218
return FDS_ERR_DENIED;
212219
}
213220

src/iemgr/iemgr_mapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ read_item(fds_iemgr_t *mgr, fds_xml_ctx_t *xml_ctx, fds_iemgr_mapping *mapping);
251251
* Read the mappings file, process it and store it to the iemgr
252252
*/
253253
int
254-
read_mappings_file(fds_iemgr_t *mgr, const char *path)
254+
fds_iemgr_mapping_read_file(fds_iemgr_t *mgr, const char *path)
255255
{
256256
// Open file
257257
auto file = unique_file(fopen(path, "r"), &::fclose);

src/iemgr/iemgr_mapping.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
#include <libfds/iemgr.h>
44
#include "iemgr_common.h"
55

6-
int
7-
read_mappings_file(fds_iemgr_t *mgr, const char *path);
8-
96
void
107
mappings_destroy(fds_iemgr_t *mgr);
118

0 commit comments

Comments
 (0)