Skip to content

Commit

Permalink
Dont overwrite template (#47)
Browse files Browse the repository at this point in the history
* Track config template versions and overwrite config when they don't match.
  • Loading branch information
foodprocessor authored Aug 15, 2024
1 parent a54410d commit e235b2c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 74 deletions.
18 changes: 18 additions & 0 deletions src/lib/cloudfuse/child_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include "child_process.h"
#include <fstream>

std::string CloudfuseMngr::getMountDir()
{
Expand All @@ -33,3 +34,20 @@ std::string CloudfuseMngr::getFileCacheDir()
{
return fileCacheDir;
}

bool CloudfuseMngr::templateOutdated(std::string templateFilePath)
{
// check the first line of the template file for a matching version
std::string firstLine;
// read the first line of the template file
std::ifstream templateFileStream(templateFilePath);
// if the file doesn't exist (or can't be opened), we need to write it
if (templateFileStream.fail())
{
return true;
}
getline(templateFileStream, firstLine);
templateFileStream.close();
// if the versions don't match, we need to overwrite the template
return templateVersionString.compare(firstLine) != 0;
}
2 changes: 2 additions & 0 deletions src/lib/cloudfuse/child_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class CloudfuseMngr
std::string configFile;
std::string fileCacheDir;
std::string templateFile;
std::string templateVersionString;
bool templateOutdated(std::string templateFilePath);
#ifdef _WIN32
processReturn spawnProcess(wchar_t *argv, std::wstring envp);
processReturn encryptConfig(const std::string passphrase);
Expand Down
76 changes: 40 additions & 36 deletions src/lib/cloudfuse/child_process_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,40 @@ std::string getSystemName()
CloudfuseMngr::CloudfuseMngr()
{
std::string systemName = getSystemName();
std::string config_template = R"(
allow-other: true
logging:
type: base
components:
- libfuse
- file_cache
- attr_cache
- s3storage
libfuse:
attribute-expiration-sec: 1800
entry-expiration-sec: 1800
negative-entry-expiration-sec: 1800
ignore-open-flags: true
network-share: true
file_cache:
path: { 0 }
timeout-sec: 180
allow-non-empty-temp: true
cleanup-on-start: false
attr_cache:
timeout-sec: 3600
s3storage:
bucket-name: { BUCKET_NAME }
endpoint: { ENDPOINT }
region: { AWS_REGION }
subdirectory: )" + systemName +
"\n";
// NOTE: increment the version number when the config template changes
templateVersionString = "template-version: 0.1";
std::string config_template = templateVersionString + R"(
allow-other: true
logging:
type: base
components:
- libfuse
- file_cache
- attr_cache
- s3storage
libfuse:
attribute-expiration-sec: 1800
entry-expiration-sec: 1800
negative-entry-expiration-sec: 1800
ignore-open-flags: true
network-share: true
file_cache:
path: { 0 }
timeout-sec: 180
allow-non-empty-temp: true
cleanup-on-start: false
attr_cache:
timeout-sec: 3600
s3storage:
bucket-name: { BUCKET_NAME }
endpoint: { ENDPOINT }
region: { AWS_REGION }
subdirectory: )" + systemName + "\n";

std::string homeEnv;
const char *home = std::getenv("HOME");
Expand All @@ -94,9 +95,12 @@ CloudfuseMngr::CloudfuseMngr()
configFile = homeEnv + "/nx_plugin_config.aes";
templateFile = homeEnv + "/nx_plugin_config.yaml";

std::ofstream out(templateFile, std::ios::trunc);
out << config_template;
out.close();
if (templateOutdated(templateFile))
{
std::ofstream out(templateFile, std::ios::trunc);
out << config_template;
out.close();
}
}

processReturn CloudfuseMngr::spawnProcess(char *const argv[], char *const envp[])
Expand Down
80 changes: 42 additions & 38 deletions src/lib/cloudfuse/child_process_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,42 @@ std::string getSystemName()
CloudfuseMngr::CloudfuseMngr()
{
std::string systemName = getSystemName();
std::string config_template = R"(
allow-other: true
logging:
type: base
components:
- libfuse
- file_cache
- attr_cache
- s3storage
libfuse:
attribute-expiration-sec: 1800
entry-expiration-sec: 1800
negative-entry-expiration-sec: 1800
ignore-open-flags: true
network-share: true
file_cache:
path: { 0 }
timeout-sec: 180
allow-non-empty-temp: true
cleanup-on-start: false
attr_cache:
timeout-sec: 3600
s3storage:
key-id: { AWS_ACCESS_KEY_ID }
secret-key: { AWS_SECRET_ACCESS_KEY }
bucket-name: { BUCKET_NAME }
endpoint: { ENDPOINT }
region: { AWS_REGION }
subdirectory: )" + systemName +
"\n";
// NOTE: increment the version number when the config template changes
templateVersionString= "template-version: 0.1";
std::string config_template = templateVersionString + R"(
allow-other: true
logging:
type: base
components:
- libfuse
- file_cache
- attr_cache
- s3storage
libfuse:
attribute-expiration-sec: 1800
entry-expiration-sec: 1800
negative-entry-expiration-sec: 1800
ignore-open-flags: true
network-share: true
file_cache:
path: { 0 }
timeout-sec: 180
allow-non-empty-temp: true
cleanup-on-start: false
attr_cache:
timeout-sec: 3600
s3storage:
key-id: { AWS_ACCESS_KEY_ID }
secret-key: { AWS_SECRET_ACCESS_KEY }
bucket-name: { BUCKET_NAME }
endpoint: { ENDPOINT }
region: { AWS_REGION }
subdirectory: )" + systemName + "\n";

std::string appdataEnv;
char *buf = nullptr;
Expand All @@ -130,9 +131,12 @@ CloudfuseMngr::CloudfuseMngr()
configFile = configFilePath.generic_string();
templateFile = templateFilePath.generic_string();

std::ofstream out(templateFile, std::ios::trunc);
out << config_template;
out.close();
if (templateOutdated(templateFile))
{
std::ofstream out(templateFile, std::ios::trunc);
out << config_template;
out.close();
}
}

processReturn CloudfuseMngr::spawnProcess(wchar_t *argv, std::wstring envp)
Expand Down

0 comments on commit e235b2c

Please sign in to comment.