Skip to content

Commit b32e67c

Browse files
committed
Read base path for config files from TMPDIR env var
1 parent f6ac926 commit b32e67c

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

src/platform/Linux/CHIPLinuxStorage.h

+3-21
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,9 @@
3737
#include <platform/Linux/CHIPLinuxStorageIni.h>
3838
#include <string>
3939

40-
#ifndef FATCONFDIR
41-
#define FATCONFDIR "/tmp"
42-
#endif
43-
44-
#ifndef SYSCONFDIR
45-
#define SYSCONFDIR "/tmp"
46-
#endif
47-
48-
#ifndef LOCALSTATEDIR
49-
#define LOCALSTATEDIR "/tmp"
50-
#endif
51-
52-
#define CHIP_DEFAULT_FACTORY_PATH \
53-
FATCONFDIR "/" \
54-
"chip_factory.ini"
55-
#define CHIP_DEFAULT_CONFIG_PATH \
56-
SYSCONFDIR "/" \
57-
"chip_config.ini"
58-
#define CHIP_DEFAULT_DATA_PATH \
59-
LOCALSTATEDIR "/" \
60-
"chip_counters.ini"
40+
#define CHIP_DEFAULT_FACTORY_FILE_NAME "chip_factory.ini"
41+
#define CHIP_DEFAULT_CONFIG_FILE_NAME "chip_config.ini"
42+
#define CHIP_DEFAULT_DATA_FILE_NAME "chip_counters.ini"
6143

6244
namespace chip {
6345
namespace DeviceLayer {

src/platform/Linux/PosixConfig.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ bool PosixConfig::ConfigValueExists(Key key)
453453
return storage->HasValue(key.Name);
454454
}
455455

456+
std::string PosixConfig::GetFilePath(std::string defaultFileName)
457+
{
458+
// Match what GetFilename in ExamplePersistentStorage.cpp does.
459+
const char * dir = getenv("TMPDIR");
460+
if (dir == nullptr)
461+
{
462+
dir = "/tmp";
463+
}
464+
std::string storageDir = dir;
465+
466+
return storageDir + "/" + defaultFileName;
467+
}
468+
456469
CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
457470
{
458471
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -461,17 +474,20 @@ CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
461474
if (strcmp(ns, kConfigNamespace_ChipFactory) == 0)
462475
{
463476
storage = &gChipLinuxFactoryStorage;
464-
err = storage->Init(CHIP_DEFAULT_FACTORY_PATH);
477+
std::string filePath = GetFilePath(CHIP_DEFAULT_FACTORY_FILE_NAME);
478+
err = storage->Init(filePath.c_str());
465479
}
466480
else if (strcmp(ns, kConfigNamespace_ChipConfig) == 0)
467481
{
468482
storage = &gChipLinuxConfigStorage;
469-
err = storage->Init(CHIP_DEFAULT_CONFIG_PATH);
483+
std::string filePath = GetFilePath(CHIP_DEFAULT_CONFIG_FILE_NAME);
484+
err = storage->Init(filePath.c_str());
470485
}
471486
else if (strcmp(ns, kConfigNamespace_ChipCounters) == 0)
472487
{
473488
storage = &gChipLinuxCountersStorage;
474-
err = storage->Init(CHIP_DEFAULT_DATA_PATH);
489+
std::string filePath = GetFilePath(CHIP_DEFAULT_DATA_FILE_NAME);
490+
err = storage->Init(filePath.c_str());
475491
}
476492

477493
SuccessOrExit(err);

src/platform/Linux/PosixConfig.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#pragma once
2525

2626
#include <functional>
27+
#include <string>
2728
#include <inttypes.h>
2829

2930
#include <lib/core/CHIPError.h>
@@ -111,6 +112,7 @@ class PosixConfig
111112

112113
private:
113114
static ChipLinuxStorage * GetStorageForNamespace(Key key);
115+
static std::string GetFilePath(std::string defaultFileName);
114116
};
115117

116118
struct PosixConfig::Key

0 commit comments

Comments
 (0)