Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read base path for config files from TMPDIR env var #33846

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions src/platform/Linux/CHIPPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,4 @@ using CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE = const char *;

// ==================== Security Configuration Overrides ====================

#ifndef CHIP_CONFIG_KVS_PATH
#define CHIP_CONFIG_KVS_PATH "/tmp/chip_kvs"
#endif // CHIP_CONFIG_KVS_PATH
#define CHIP_DEFAULT_CONFIG_KVS_FILE_NAME "chip_kvs"
31 changes: 24 additions & 7 deletions src/platform/Linux/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ ChipLinuxStorage * PosixConfig::GetStorageForNamespace(Key key)

CHIP_ERROR PosixConfig::Init()
{
return PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH);
std::string filePath = GetFilePath(CHIP_DEFAULT_CONFIG_KVS_FILE_NAME);
return PersistedStorage::KeyValueStoreMgrImpl().Init(filePath.c_str());
}

CHIP_ERROR PosixConfig::ReadConfigValue(Key key, bool & val)
Expand Down Expand Up @@ -453,25 +454,41 @@ bool PosixConfig::ConfigValueExists(Key key)
return storage->HasValue(key.Name);
}

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

CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ChipLinuxStorage * storage = nullptr;

if (strcmp(ns, kConfigNamespace_ChipFactory) == 0)
{
storage = &gChipLinuxFactoryStorage;
err = storage->Init(CHIP_DEFAULT_FACTORY_PATH);
storage = &gChipLinuxFactoryStorage;
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);
storage = &gChipLinuxConfigStorage;
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);
storage = &gChipLinuxCountersStorage;
std::string filePath = GetFilePath(CHIP_DEFAULT_DATA_FILE_NAME);
err = storage->Init(filePath.c_str());
}

SuccessOrExit(err);
Expand Down
3 changes: 3 additions & 0 deletions src/platform/Linux/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

#pragma once

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

#include <lib/core/CHIPError.h>

Expand Down Expand Up @@ -111,6 +113,7 @@ class PosixConfig

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

struct PosixConfig::Key
Expand Down
Loading