Skip to content

Commit

Permalink
Read base path for config files from TMPDIR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
jpm-canonical committed Jun 11, 2024
1 parent 23e0f3b commit 76750ee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
24 changes: 3 additions & 21 deletions src/platform/Linux/CHIPLinuxStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,9 @@
#include <platform/Linux/CHIPLinuxStorageIni.h>
#include <string>

#ifndef FATCONFDIR
#define FATCONFDIR "/tmp"
#endif

#ifndef SYSCONFDIR
#define SYSCONFDIR "/tmp"
#endif

#ifndef LOCALSTATEDIR
#define LOCALSTATEDIR "/tmp"
#endif

#define CHIP_DEFAULT_FACTORY_PATH \
FATCONFDIR "/" \
"chip_factory.ini"
#define CHIP_DEFAULT_CONFIG_PATH \
SYSCONFDIR "/" \
"chip_config.ini"
#define CHIP_DEFAULT_DATA_PATH \
LOCALSTATEDIR "/" \
"chip_counters.ini"
#define CHIP_DEFAULT_FACTORY_FILE_NAME "chip_factory.ini"
#define CHIP_DEFAULT_CONFIG_FILE_NAME "chip_config.ini"
#define CHIP_DEFAULT_DATA_FILE_NAME "chip_counters.ini"

namespace chip {
namespace DeviceLayer {
Expand Down
22 changes: 19 additions & 3 deletions src/platform/Linux/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,19 @@ bool PosixConfig::ConfigValueExists(Key key)
return storage->HasValue(key.Name);
}

std::string PosixConfig::GetFilePath(std::string defaultFileName)
{
// Match what GetFilename in ExamplePersistentStorage.cpp does.
const char * dir = getenv("TMPDIR");
if (dir == nullptr)
{
dir = "/tmp";
}
std::string storageDir = dir;

return storageDir + "/" + defaultFileName;
}

CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -461,17 +474,20 @@ CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
if (strcmp(ns, kConfigNamespace_ChipFactory) == 0)
{
storage = &gChipLinuxFactoryStorage;
err = storage->Init(CHIP_DEFAULT_FACTORY_PATH);
std::string filePath = GetFilePath(CHIP_DEFAULT_FACTORY_FILE_NAME);
err = storage->Init(filePath.c_str());
}
else if (strcmp(ns, kConfigNamespace_ChipConfig) == 0)
{
storage = &gChipLinuxConfigStorage;
err = storage->Init(CHIP_DEFAULT_CONFIG_PATH);
std::string filePath = GetFilePath(CHIP_DEFAULT_CONFIG_FILE_NAME);
err = storage->Init(filePath.c_str());
}
else if (strcmp(ns, kConfigNamespace_ChipCounters) == 0)
{
storage = &gChipLinuxCountersStorage;
err = storage->Init(CHIP_DEFAULT_DATA_PATH);
std::string filePath = GetFilePath(CHIP_DEFAULT_DATA_FILE_NAME);
err = storage->Init(filePath.c_str());
}

SuccessOrExit(err);
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include <functional>
#include <string>
#include <inttypes.h>

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

private:
static ChipLinuxStorage * GetStorageForNamespace(Key key);
static std::string GetFilePath(std::string defaultFileName);
};

struct PosixConfig::Key
Expand Down

0 comments on commit 76750ee

Please sign in to comment.