From 938528840beb2e04c5e2e37b2e1083cbe0526430 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 12 Nov 2015 11:43:11 -0800 Subject: [PATCH] Added --unmount-idle command line option (without functionality yet) And refactor cli (group Cli class and program_options in cli subfolder) --- src/{ => cli}/Cli.cpp | 4 +- src/{ => cli}/Cli.h | 2 +- src/{ => cli}/program_options/Parser.cpp | 7 +++- src/{ => cli}/program_options/Parser.h | 0 .../program_options/ProgramOptions.cpp | 13 ++++-- .../program_options/ProgramOptions.h | 9 +++-- src/{ => cli}/program_options/utils.cpp | 0 src/{ => cli}/program_options/utils.h | 0 src/main.cpp | 2 +- test/{ => cli}/program_options/ParserTest.cpp | 9 ++++- .../program_options/ProgramOptionsTest.cpp | 40 ++++++++++++------- test/{ => cli}/program_options/UtilsTest.cpp | 2 +- .../testutils/ProgramOptionsTestBase.h | 0 test/cli/testutils/CliTest.h | 2 +- 14 files changed, 60 insertions(+), 30 deletions(-) rename src/{ => cli}/Cli.cpp (99%) rename src/{ => cli}/Cli.h (97%) rename src/{ => cli}/program_options/Parser.cpp (92%) rename src/{ => cli}/program_options/Parser.h (100%) rename src/{ => cli}/program_options/ProgramOptions.cpp (74%) rename src/{ => cli}/program_options/ProgramOptions.h (83%) rename src/{ => cli}/program_options/utils.cpp (100%) rename src/{ => cli}/program_options/utils.h (100%) rename test/{ => cli}/program_options/ParserTest.cpp (90%) rename test/{ => cli}/program_options/ProgramOptionsTest.cpp (66%) rename test/{ => cli}/program_options/UtilsTest.cpp (99%) rename test/{ => cli}/program_options/testutils/ProgramOptionsTestBase.h (100%) diff --git a/src/Cli.cpp b/src/cli/Cli.cpp similarity index 99% rename from src/Cli.cpp rename to src/cli/Cli.cpp index ee82c8ce..1ddfe039 100644 --- a/src/Cli.cpp +++ b/src/cli/Cli.cpp @@ -11,8 +11,8 @@ #include "messmer/fspp/fuse/Fuse.h" #include "messmer/fspp/impl/FilesystemImpl.h" #include -#include "filesystem/CryDevice.h" -#include "config/CryConfigLoader.h" +#include "../filesystem/CryDevice.h" +#include "../config/CryConfigLoader.h" #include "program_options/Parser.h" #include diff --git a/src/Cli.h b/src/cli/Cli.h similarity index 97% rename from src/Cli.h rename to src/cli/Cli.h index 552e22fe..340066f8 100644 --- a/src/Cli.h +++ b/src/cli/Cli.h @@ -3,7 +3,7 @@ #define MESSMER_CRYFS_CLI_H #include "program_options/ProgramOptions.h" -#include "config/CryConfigFile.h" +#include "../config/CryConfigFile.h" #include #include #include diff --git a/src/program_options/Parser.cpp b/src/cli/program_options/Parser.cpp similarity index 92% rename from src/program_options/Parser.cpp rename to src/cli/program_options/Parser.cpp index ad50d29d..aa64d966 100644 --- a/src/program_options/Parser.cpp +++ b/src/cli/program_options/Parser.cpp @@ -39,6 +39,10 @@ ProgramOptions Parser::parse(const vector &supportedCiphers) const { if (foreground) { options.second.push_back(const_cast("-f")); } + optional unmountAfterIdleMinutes = none; + if (vm.count("unmount-idle")) { + unmountAfterIdleMinutes = vm["unmount-idle"].as(); + } optional logfile = none; if (vm.count("logfile")) { logfile = vm["logfile"].as(); @@ -53,7 +57,7 @@ ProgramOptions Parser::parse(const vector &supportedCiphers) const { extPass = vm["extpass"].as(); } - 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 &supportedCiphers) { @@ -100,6 +104,7 @@ void Parser::_addAllowedOptions(po::options_description *desc) { ("foreground,f", "Run CryFS in foreground.") ("cipher", po::value(), "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(), "Automatically unmount after specified number of idle minutes.") ("extpass", po::value(), "External program to use for password input") ("logfile", po::value(), "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.") ; diff --git a/src/program_options/Parser.h b/src/cli/program_options/Parser.h similarity index 100% rename from src/program_options/Parser.h rename to src/cli/program_options/Parser.h diff --git a/src/program_options/ProgramOptions.cpp b/src/cli/program_options/ProgramOptions.cpp similarity index 74% rename from src/program_options/ProgramOptions.cpp rename to src/cli/program_options/ProgramOptions.cpp index 223be04a..183f34e1 100644 --- a/src/program_options/ProgramOptions.cpp +++ b/src/cli/program_options/ProgramOptions.cpp @@ -8,10 +8,12 @@ using std::vector; using boost::optional; ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const optional &configFile, - bool foreground, const optional &logFile, const optional &cipher, + bool foreground, const optional &unmountAfterIdleMinutes, + const optional &logFile, const optional &cipher, const optional &extPass, const vector &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 &ProgramOptions::unmountAfterIdleMinutes() const { + return _unmountAfterIdleMinutes; +} + const optional &ProgramOptions::logFile() const { return _logFile; } diff --git a/src/program_options/ProgramOptions.h b/src/cli/program_options/ProgramOptions.h similarity index 83% rename from src/program_options/ProgramOptions.h rename to src/cli/program_options/ProgramOptions.h index a80eef1b..dbec3159 100644 --- a/src/program_options/ProgramOptions.h +++ b/src/cli/program_options/ProgramOptions.h @@ -12,7 +12,8 @@ namespace cryfs { class ProgramOptions final { public: ProgramOptions(const std::string &baseDir, const std::string &mountDir, const boost::optional &configFile, - bool foreground, const boost::optional &logFile, const boost::optional &cipher, + bool foreground, const boost::optional &unmountAfterIdleMinutes, + const boost::optional &logFile, const boost::optional &cipher, const boost::optional &extPass, const std::vector &fuseOptions); ProgramOptions(ProgramOptions &&rhs); ~ProgramOptions(); @@ -21,8 +22,9 @@ namespace cryfs { std::string mountDir() const; const boost::optional &configFile() const; bool foreground() const; - const boost::optional &logFile() const; const boost::optional &cipher() const; + const boost::optional &unmountAfterIdleMinutes() const; + const boost::optional &logFile() const; const boost::optional &extPass() const; const std::vector &fuseOptions() const; @@ -31,8 +33,9 @@ namespace cryfs { char *_mountDir; boost::optional _configFile; bool _foreground; - boost::optional _logFile; boost::optional _cipher; + boost::optional _unmountAfterIdleMinutes; + boost::optional _logFile; boost::optional _extPass; std::vector _fuseOptions; diff --git a/src/program_options/utils.cpp b/src/cli/program_options/utils.cpp similarity index 100% rename from src/program_options/utils.cpp rename to src/cli/program_options/utils.cpp diff --git a/src/program_options/utils.h b/src/cli/program_options/utils.h similarity index 100% rename from src/program_options/utils.h rename to src/cli/program_options/utils.h diff --git a/src/main.cpp b/src/main.cpp index 62c11a7a..cc05ba6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "Cli.h" +#include "cli/Cli.h" #include #include diff --git a/test/program_options/ParserTest.cpp b/test/cli/program_options/ParserTest.cpp similarity index 90% rename from test/program_options/ParserTest.cpp rename to test/cli/program_options/ParserTest.cpp index 9ba27bea..5e601df9 100644 --- a/test/program_options/ParserTest.cpp +++ b/test/cli/program_options/ParserTest.cpp @@ -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"}), diff --git a/test/program_options/ProgramOptionsTest.cpp b/test/cli/program_options/ProgramOptionsTest.cpp similarity index 66% rename from test/program_options/ProgramOptionsTest.cpp rename to test/cli/program_options/ProgramOptionsTest.cpp index ecd18bab..4bcbec9c 100644 --- a/test/program_options/ProgramOptionsTest.cpp +++ b/test/cli/program_options/ProgramOptionsTest.cpp @@ -1,5 +1,5 @@ #include "testutils/ProgramOptionsTestBase.h" -#include "../../src/program_options/ProgramOptions.h" +#include "../../../src/cli/program_options/ProgramOptions.h" #include 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()); } diff --git a/test/program_options/UtilsTest.cpp b/test/cli/program_options/UtilsTest.cpp similarity index 99% rename from test/program_options/UtilsTest.cpp rename to test/cli/program_options/UtilsTest.cpp index 93acf9bd..cd55d9ec 100644 --- a/test/program_options/UtilsTest.cpp +++ b/test/cli/program_options/UtilsTest.cpp @@ -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; diff --git a/test/program_options/testutils/ProgramOptionsTestBase.h b/test/cli/program_options/testutils/ProgramOptionsTestBase.h similarity index 100% rename from test/program_options/testutils/ProgramOptionsTestBase.h rename to test/cli/program_options/testutils/ProgramOptionsTestBase.h diff --git a/test/cli/testutils/CliTest.h b/test/cli/testutils/CliTest.h index 18e1ff0d..e18bbf19 100644 --- a/test/cli/testutils/CliTest.h +++ b/test/cli/testutils/CliTest.h @@ -6,7 +6,7 @@ #include #include #include -#include "../../../src/Cli.h" +#include "../../../src/cli/Cli.h" #include #include