Added --unmount-idle command line option (without functionality yet)

And refactor cli (group Cli class and program_options in cli subfolder)
This commit is contained in:
Sebastian Messmer 2015-11-12 11:43:11 -08:00
parent 30ae2fc45c
commit 938528840b
14 changed files with 60 additions and 30 deletions

View File

@ -11,8 +11,8 @@
#include "messmer/fspp/fuse/Fuse.h"
#include "messmer/fspp/impl/FilesystemImpl.h"
#include <messmer/cpp-utils/process/subprocess.h>
#include "filesystem/CryDevice.h"
#include "config/CryConfigLoader.h"
#include "../filesystem/CryDevice.h"
#include "../config/CryConfigLoader.h"
#include "program_options/Parser.h"
#include <gitversion/version.h>

View File

@ -3,7 +3,7 @@
#define MESSMER_CRYFS_CLI_H
#include "program_options/ProgramOptions.h"
#include "config/CryConfigFile.h"
#include "../config/CryConfigFile.h"
#include <boost/filesystem/path.hpp>
#include <messmer/cpp-utils/tempfile/TempFile.h>
#include <messmer/cpp-utils/random/RandomGenerator.h>

View File

@ -39,6 +39,10 @@ ProgramOptions Parser::parse(const vector<string> &supportedCiphers) const {
if (foreground) {
options.second.push_back(const_cast<char*>("-f"));
}
optional<unsigned int> unmountAfterIdleMinutes = none;
if (vm.count("unmount-idle")) {
unmountAfterIdleMinutes = vm["unmount-idle"].as<unsigned int>();
}
optional<string> logfile = none;
if (vm.count("logfile")) {
logfile = vm["logfile"].as<string>();
@ -53,7 +57,7 @@ ProgramOptions Parser::parse(const vector<string> &supportedCiphers) const {
extPass = vm["extpass"].as<string>();
}
return ProgramOptions(baseDir, mountDir, configfile, foreground, logfile, cipher, extPass, options.second);
return ProgramOptions(baseDir, mountDir, configfile, foreground, unmountAfterIdleMinutes, logfile, cipher, extPass, options.second);
}
void Parser::_checkValidCipher(const string &cipher, const vector<string> &supportedCiphers) {
@ -100,6 +104,7 @@ void Parser::_addAllowedOptions(po::options_description *desc) {
("foreground,f", "Run CryFS in foreground.")
("cipher", po::value<string>(), "Cipher to use for encryption. See possible values by calling cryfs with --show-ciphers")
("show-ciphers", "Show list of supported ciphers.")
("unmount-idle", po::value<unsigned int>(), "Automatically unmount after specified number of idle minutes.")
("extpass", po::value<string>(), "External program to use for password input")
("logfile", po::value<string>(), "Specify the file to write log messages to. If this is not specified, log messages will go to stdout, or syslog if CryFS is running in the background.")
;

View File

@ -8,10 +8,12 @@ using std::vector;
using boost::optional;
ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const optional<string> &configFile,
bool foreground, const optional<string> &logFile, const optional<string> &cipher,
bool foreground, const optional<unsigned int> &unmountAfterIdleMinutes,
const optional<string> &logFile, const optional<string> &cipher,
const optional<string> &extPass, const vector<char*> &fuseOptions)
:_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _foreground(foreground),
_logFile(logFile), _cipher(cipher), _extPass(extPass), _fuseOptions(fuseOptions) {
_cipher(cipher), _unmountAfterIdleMinutes(unmountAfterIdleMinutes), _logFile(logFile), _extPass(extPass),
_fuseOptions(fuseOptions) {
std::memcpy(_mountDir, mountDir.c_str(), mountDir.size()+1);
// Fuse needs the mountDir passed as first option (first option = position 1, since 0 is the executable name)
ASSERT(_fuseOptions.size() >= 1, "There has to be one parameter at least for the executable name");
@ -20,7 +22,8 @@ ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, co
ProgramOptions::ProgramOptions(ProgramOptions &&rhs)
:_baseDir(std::move(rhs._baseDir)), _mountDir(std::move(rhs._mountDir)), _configFile(std::move(rhs._configFile)),
_foreground(std::move(rhs._foreground)), _logFile(std::move(rhs._logFile)), _cipher(std::move(rhs._cipher)),
_foreground(std::move(rhs._foreground)), _cipher(std::move(rhs._cipher)),
_unmountAfterIdleMinutes(std::move(rhs._unmountAfterIdleMinutes)), _logFile(std::move(rhs._logFile)),
_extPass(std::move(rhs._extPass)), _fuseOptions(std::move(rhs._fuseOptions)) {
rhs._mountDir = nullptr;
}
@ -47,6 +50,10 @@ bool ProgramOptions::foreground() const {
return _foreground;
}
const optional<unsigned int> &ProgramOptions::unmountAfterIdleMinutes() const {
return _unmountAfterIdleMinutes;
}
const optional<string> &ProgramOptions::logFile() const {
return _logFile;
}

View File

@ -12,7 +12,8 @@ namespace cryfs {
class ProgramOptions final {
public:
ProgramOptions(const std::string &baseDir, const std::string &mountDir, const boost::optional<std::string> &configFile,
bool foreground, const boost::optional<std::string> &logFile, const boost::optional<std::string> &cipher,
bool foreground, const boost::optional<unsigned int> &unmountAfterIdleMinutes,
const boost::optional<std::string> &logFile, const boost::optional<std::string> &cipher,
const boost::optional<std::string> &extPass, const std::vector<char *> &fuseOptions);
ProgramOptions(ProgramOptions &&rhs);
~ProgramOptions();
@ -21,8 +22,9 @@ namespace cryfs {
std::string mountDir() const;
const boost::optional<std::string> &configFile() const;
bool foreground() const;
const boost::optional<std::string> &logFile() const;
const boost::optional<std::string> &cipher() const;
const boost::optional<unsigned int> &unmountAfterIdleMinutes() const;
const boost::optional<std::string> &logFile() const;
const boost::optional<std::string> &extPass() const;
const std::vector<char *> &fuseOptions() const;
@ -31,8 +33,9 @@ namespace cryfs {
char *_mountDir;
boost::optional<std::string> _configFile;
bool _foreground;
boost::optional<std::string> _logFile;
boost::optional<std::string> _cipher;
boost::optional<unsigned int> _unmountAfterIdleMinutes;
boost::optional<std::string> _logFile;
boost::optional<std::string> _extPass;
std::vector<char *> _fuseOptions;

View File

@ -1,4 +1,4 @@
#include "Cli.h"
#include "cli/Cli.h"
#include <messmer/cpp-utils/random/Random.h>
#include <messmer/cpp-utils/crypto/kdf/Scrypt.h>

View File

@ -1,6 +1,6 @@
#include "testutils/ProgramOptionsTestBase.h"
#include "../../src/program_options/Parser.h"
#include "../../src/config/CryCipher.h"
#include "../../../src/cli/program_options/Parser.h"
#include "../../../src/config/CryCipher.h"
using namespace cryfs;
using namespace cryfs::program_options;
@ -76,6 +76,11 @@ TEST_F(ProgramOptionsParserTest, CipherGiven) {
EXPECT_EQ("aes-256-gcm", options.cipher().value());
}
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesGiven) {
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--unmount-idle", "10", "/home/user/mountDir"});
EXPECT_EQ(10, options.unmountAfterIdleMinutes().value());
}
TEST_F(ProgramOptionsParserTest, InvalidCipher) {
EXPECT_DEATH(
parse({"./myExecutable", "/home/user/baseDir", "--cipher", "invalid-cipher", "/home/user/mountDir"}),

View File

@ -1,5 +1,5 @@
#include "testutils/ProgramOptionsTestBase.h"
#include "../../src/program_options/ProgramOptions.h"
#include "../../../src/cli/program_options/ProgramOptions.h"
#include <messmer/cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
using namespace cryfs::program_options;
@ -10,73 +10,83 @@ using std::string;
class ProgramOptionsTest: public ProgramOptionsTestBase {};
TEST_F(ProgramOptionsTest, BaseDir) {
ProgramOptions testobj("/home/user/mydir", "", none, false, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("/home/user/mydir", "", none, false, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/mydir", testobj.baseDir());
}
TEST_F(ProgramOptionsTest, MountDir) {
ProgramOptions testobj("", "/home/user/mydir", none, false, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "/home/user/mydir", none, false, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/mydir", testobj.mountDir());
}
TEST_F(ProgramOptionsTest, ConfigfileNone) {
ProgramOptions testobj("", "", none, true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.configFile());
}
TEST_F(ProgramOptionsTest, ConfigfileSome) {
ProgramOptions testobj("", "", string("/home/user/configfile"), true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", string("/home/user/configfile"), true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/configfile", testobj.configFile().get());
}
TEST_F(ProgramOptionsTest, ForegroundFalse) {
ProgramOptions testobj("", "", none, false, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, false, none, none, none, none, options({"./myExecutable"}));
EXPECT_FALSE(testobj.foreground());
}
TEST_F(ProgramOptionsTest, ForegroundTrue) {
ProgramOptions testobj("", "", none, true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_TRUE(testobj.foreground());
}
TEST_F(ProgramOptionsTest, LogfileNone) {
ProgramOptions testobj("", "", none, true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.logFile());
}
TEST_F(ProgramOptionsTest, LogfileSome) {
ProgramOptions testobj("", "", none, true, string("logfile"), none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, string("logfile"), none, none, options({"./myExecutable"}));
EXPECT_EQ("logfile", testobj.logFile().get());
}
TEST_F(ProgramOptionsTest, UnmountAfterIdleMinutesNone) {
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.unmountAfterIdleMinutes());
}
TEST_F(ProgramOptionsTest, UnmountAfterIdleMinutesSome) {
ProgramOptions testobj("", "", none, true, 10, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(10, testobj.unmountAfterIdleMinutes().get());
}
TEST_F(ProgramOptionsTest, CipherNone) {
ProgramOptions testobj("", "", none, true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.cipher());
}
TEST_F(ProgramOptionsTest, CipherSome) {
ProgramOptions testobj("", "", none, true, none, string("aes-256-gcm"), none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, string("aes-256-gcm"), none, options({"./myExecutable"}));
EXPECT_EQ("aes-256-gcm", testobj.cipher().get());
}
TEST_F(ProgramOptionsTest, ExtPassNone) {
ProgramOptions testobj("", "", none, true, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.extPass());
}
TEST_F(ProgramOptionsTest, ExtPassSome) {
ProgramOptions testobj("", "", none, true, none, none, string("echo mypassword"), options({"./myExecutable"}));
ProgramOptions testobj("", "", none, true, none, none, none, string("echo mypassword"), options({"./myExecutable"}));
EXPECT_EQ("echo mypassword", testobj.extPass().get());
}
TEST_F(ProgramOptionsTest, EmptyFuseOptions) {
ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, none, none, options({"./myExecutable"}));
ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, none, none, none, options({"./myExecutable"}));
//Fuse should have the mount dir as first parameter
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir"}, testobj.fuseOptions());
}
TEST_F(ProgramOptionsTest, SomeFuseOptions) {
ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, none, none, options({"./myExecutable", "-f", "--longoption"}));
ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, none, none, none, options({"./myExecutable", "-f", "--longoption"}));
//Fuse should have the mount dir as first parameter
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir", "-f", "--longoption"}, testobj.fuseOptions());
}

View File

@ -1,5 +1,5 @@
#include "testutils/ProgramOptionsTestBase.h"
#include "../../src/program_options/utils.h"
#include "../../../src/cli/program_options/utils.h"
using namespace cryfs::program_options;
using std::pair;

View File

@ -6,7 +6,7 @@
#include <google/gmock/gmock.h>
#include <messmer/cpp-utils/tempfile/TempDir.h>
#include <messmer/cpp-utils/tempfile/TempFile.h>
#include "../../../src/Cli.h"
#include "../../../src/cli/Cli.h"
#include <messmer/cpp-utils/logging/logging.h>
#include <messmer/cpp-utils/process/subprocess.h>