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 1 commit
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
Next Next commit
Read base path for config files from TMPDIR env var
  • Loading branch information
jpm-canonical committed Jun 19, 2024
commit b32e67c434144efed906d1d833c12f40e7315c79
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;
}

jpm-canonical marked this conversation as resolved.
Show resolved Hide resolved
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