Skip to content

Commit

Permalink
Cleanup code with consts where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jfantinhardesty committed Jul 1, 2024
1 parent b06b226 commit 8349493
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ Result<const ISettingsResponse *> Engine::settingsReceived()
NX_PRINT << it->first << ":" << it->second << std::endl;
NX_OUTPUT << it->first << ":" << it->second << std::endl;
}
std::string keyId = values[kKeyIdTextFieldId];
std::string secretKey = values[kSecretKeyPasswordFieldId];
std::string endpointUrl = values[kEndpointUrlTextFieldId];
std::string endpointRegion = "us-east-1";
std::string bucketName = values[kBucketNameTextFieldId]; // The default empty string will cause cloudfuse to select
// first available bucket
std::string mountDir = cfManager.getMountDir();
std::string fileCacheDir = cfManager.getFileCacheDir();
const std::string keyId = values[kKeyIdTextFieldId];
const std::string secretKey = values[kSecretKeyPasswordFieldId];
const std::string endpointUrl = values[kEndpointUrlTextFieldId];
const std::string endpointRegion = "us-east-1";
const std::string bucketName = values[kBucketNameTextFieldId]; // The default empty string will cause cloudfuse to
// select first available bucket
const std::string mountDir = cfManager.getMountDir();
const std::string fileCacheDir = cfManager.getFileCacheDir();
std::string passphrase = "";
// Generate passphrase for config file
unsigned char key[32]; // AES-256 key
Expand Down Expand Up @@ -263,9 +263,9 @@ Result<const ISettingsResponse *> Engine::settingsReceived()
}

#if defined(__linux__)
processReturn dryGenConfig = cfManager.genS3Config(endpointRegion, endpointUrl, bucketName, passphrase);
const processReturn dryGenConfig = cfManager.genS3Config(endpointRegion, endpointUrl, bucketName, passphrase);
#elif defined(_WIN32)
processReturn dryGenConfig =
const processReturn dryGenConfig =
cfManager.genS3Config(keyId, secretKey, endpointRegion, endpointUrl, bucketName, passphrase);
#endif

Expand All @@ -275,9 +275,9 @@ Result<const ISettingsResponse *> Engine::settingsReceived()
}

#if defined(__linux__)
processReturn dryRunRet = cfManager.dryRun(keyId, secretKey, passphrase);
const processReturn dryRunRet = cfManager.dryRun(keyId, secretKey, passphrase);
#elif defined(_WIN32)
processReturn dryRunRet = cfManager.dryRun(passphrase);
const processReturn dryRunRet = cfManager.dryRun(passphrase);
#endif

if (dryRunRet.errCode != 0)
Expand Down Expand Up @@ -323,9 +323,9 @@ Result<const ISettingsResponse *> Engine::settingsReceived()
}

#if defined(__linux__)
processReturn mountRet = cfManager.mount(keyId, secretKey, passphrase);
const processReturn mountRet = cfManager.mount(keyId, secretKey, passphrase);
#elif defined(_WIN32)
processReturn mountRet = cfManager.mount(passphrase);
const processReturn mountRet = cfManager.mount(passphrase);
#endif

if (mountRet.errCode != 0)
Expand Down
16 changes: 10 additions & 6 deletions src/cloudfuse/child_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class CloudfuseMngr
CloudfuseMngr(const std::string mountDir, const std::string configFile, const std::string fileCachePath);
processReturn dryRun(const std::string passphrase);
processReturn mount(const std::string passphrase);
processReturn genS3Config(const std::string accessKeyId, const std::string secretAccessKey, const std::string region,
const std::string endpoint, const std::string bucketName, const std::string passphrase);
processReturn genS3Config(const std::string accessKeyId, const std::string secretAccessKey,
const std::string region, const std::string endpoint, const std::string bucketName,
const std::string passphrase);
#elif defined(__linux__) || defined(__APPLE__)
CloudfuseMngr(std::string mountDir, std::string fileCacheDir, std::string configFile, std::string templateFile);
processReturn dryRun(std::string accessKeyId, std::string secretAccessKey, std::string passphrase);
processReturn mount(std::string accessKeyId, std::string secretAccessKey, std::string passphrase);
processReturn genS3Config(std::string region, std::string endpoint, std::string bucketName, std::string passphrase);
CloudfuseMngr(const std::string mountDir, const std::string fileCacheDir, const std::string configFile,
const std::string templateFile);
processReturn dryRun(const std::string accessKeyId, const std::string secretAccessKey,
const std::string passphrase);
processReturn mount(const std::string accessKeyId, const std::string secretAccessKey, const std::string passphrase);
processReturn genS3Config(const std::string region, const std::string endpoint, const std::string bucketName,
const std::string passphrase);
#endif
std::string getMountDir();
std::string getFileCacheDir();
Expand Down
55 changes: 29 additions & 26 deletions src/cloudfuse/child_process_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <sys/wait.h>
#include <unistd.h>

std::string config_template = R"(
const std::string config_template = R"(
allow-other: true
logging:
level: log_err
Expand Down Expand Up @@ -65,8 +65,8 @@ CloudfuseMngr::CloudfuseMngr()
}
}

CloudfuseMngr::CloudfuseMngr(std::string mountDir, std::string fileCacheDir, std::string configFile,
std::string templateFile)
CloudfuseMngr::CloudfuseMngr(const std::string mountDir, const std::string fileCacheDir, const std::string configFile,
const std::string templateFile)
{
this->mountDir = mountDir;
this->configFile = configFile;
Expand All @@ -81,14 +81,16 @@ processReturn CloudfuseMngr::spawnProcess(char *const argv[], char *const envp[]
int pipefd[2];
if (pipe(pipefd) == -1)
{
throw std::runtime_error("Failed to create pipe.");
ret.errCode = 1;
return ret;
}

pid_t pid = fork();
if (pid == -1)
{
// Fork failed
throw std::runtime_error("Failed to fork process.");
ret.errCode = 1;
return ret;
}
else if (pid != 0)
{
Expand Down Expand Up @@ -142,13 +144,13 @@ processReturn CloudfuseMngr::spawnProcess(char *const argv[], char *const envp[]
}
}

processReturn CloudfuseMngr::genS3Config(std::string region, std::string endpoint, std::string bucketName,
std::string passphrase)
processReturn CloudfuseMngr::genS3Config(const std::string region, const std::string endpoint,
const std::string bucketName, const std::string passphrase)
{
std::string configArg = "--config-file=" + templateFile;
std::string outputArg = "--output-file=" + configFile;
std::string fileCachePathArg = "--temp-path=" + fileCacheDir;
std::string passphraseArg = "--passphrase=" + passphrase;
const std::string configArg = "--config-file=" + templateFile;
const std::string outputArg = "--output-file=" + configFile;
const std::string fileCachePathArg = "--temp-path=" + fileCacheDir;
const std::string passphraseArg = "--passphrase=" + passphrase;
char *const argv[] = {const_cast<char *>("/bin/cloudfuse"),
const_cast<char *>("gen-config"),
const_cast<char *>(configArg.c_str()),
Expand All @@ -157,19 +159,20 @@ processReturn CloudfuseMngr::genS3Config(std::string region, std::string endpoin
const_cast<char *>(passphraseArg.c_str()),
NULL};

std::string bucketNameEnv = "BUCKET_NAME=" + bucketName;
std::string endpointEnv = "ENDPOINT=" + endpoint;
std::string regionEnv = "AWS_REGION=" + region;
const std::string bucketNameEnv = "BUCKET_NAME=" + bucketName;
const std::string endpointEnv = "ENDPOINT=" + endpoint;
const std::string regionEnv = "AWS_REGION=" + region;
char *const envp[] = {const_cast<char *>(bucketNameEnv.c_str()), const_cast<char *>(endpointEnv.c_str()),
const_cast<char *>(regionEnv.c_str()), NULL};

return spawnProcess(argv, envp);
}

processReturn CloudfuseMngr::dryRun(std::string accessKeyId, std::string secretAccessKey, std::string passphrase)
processReturn CloudfuseMngr::dryRun(const std::string accessKeyId, const std::string secretAccessKey,
const std::string passphrase)
{
std::string configArg = "--config-file=" + configFile;
std::string passphraseArg = "--passphrase=" + passphrase;
const std::string configArg = "--config-file=" + configFile;
const std::string passphraseArg = "--passphrase=" + passphrase;
char *const argv[] = {const_cast<char *>("/bin/cloudfuse"),
const_cast<char *>("mount"),
const_cast<char *>(mountDir.c_str()),
Expand All @@ -178,24 +181,25 @@ processReturn CloudfuseMngr::dryRun(std::string accessKeyId, std::string secretA
const_cast<char *>("--dry-run"),
NULL};

std::string awsAccessKeyIdEnv = "AWS_ACCESS_KEY_ID=" + accessKeyId;
std::string awsSecretAccessKeyEnv = "AWS_SECRET_ACCESS_KEY=" + secretAccessKey;
const std::string awsAccessKeyIdEnv = "AWS_ACCESS_KEY_ID=" + accessKeyId;
const std::string awsSecretAccessKeyEnv = "AWS_SECRET_ACCESS_KEY=" + secretAccessKey;
char *const envp[] = {const_cast<char *>(awsAccessKeyIdEnv.c_str()),
const_cast<char *>(awsSecretAccessKeyEnv.c_str()), NULL};

return spawnProcess(argv, envp);
}

processReturn CloudfuseMngr::mount(std::string accessKeyId, std::string secretAccessKey, std::string passphrase)
processReturn CloudfuseMngr::mount(const std::string accessKeyId, const std::string secretAccessKey,
const std::string passphrase)
{
std::string configArg = "--config-file=" + configFile;
std::string passphraseArg = "--passphrase=" + passphrase;
const std::string configArg = "--config-file=" + configFile;
const std::string passphraseArg = "--passphrase=" + passphrase;
char *const argv[] = {const_cast<char *>("/bin/cloudfuse"), const_cast<char *>("mount"),
const_cast<char *>(mountDir.c_str()), const_cast<char *>(configArg.c_str()),
const_cast<char *>(passphraseArg.c_str()), NULL};

std::string awsAccessKeyIdEnv = "AWS_ACCESS_KEY_ID=" + accessKeyId;
std::string awsSecretAccessKeyEnv = "AWS_SECRET_ACCESS_KEY=" + secretAccessKey;
const std::string awsAccessKeyIdEnv = "AWS_ACCESS_KEY_ID=" + accessKeyId;
const std::string awsSecretAccessKeyEnv = "AWS_SECRET_ACCESS_KEY=" + secretAccessKey;
char *const envp[] = {const_cast<char *>(awsAccessKeyIdEnv.c_str()),
const_cast<char *>(awsSecretAccessKeyEnv.c_str()), NULL};

Expand All @@ -221,7 +225,6 @@ bool CloudfuseMngr::isInstalled()
bool CloudfuseMngr::isMounted()
{
// Logic based on os.ismount implementation in Python.

struct stat buf1, buf2;

if (lstat(mountDir.c_str(), &buf1) != 0)
Expand All @@ -236,7 +239,7 @@ bool CloudfuseMngr::isMounted()
return false;
}

std::string parent = mountDir + "/..";
const std::string parent = mountDir + "/..";
if (lstat(parent.c_str(), &buf2) != 0)
{
return false;
Expand Down

0 comments on commit 8349493

Please sign in to comment.