Config file is also configurable with an option

This commit is contained in:
Sebastian Messmer 2015-09-29 14:39:10 +02:00
parent 27d3ffc472
commit dfb030e51b
6 changed files with 32 additions and 12 deletions

View File

@ -37,7 +37,7 @@ void showVersion() {
} }
void runFilesystem(const ProgramOptions &options) { void runFilesystem(const ProgramOptions &options) {
auto config = CryConfigLoader().loadOrCreate(bf::path("/home/heinzi/cryfstest/config.json")); auto config = CryConfigLoader().loadOrCreate(bf::path(options.configFile()));
auto blockStore = make_unique_ref<OnDiskBlockStore>(bf::path(options.baseDir())); auto blockStore = make_unique_ref<OnDiskBlockStore>(bf::path(options.baseDir()));
CryDevice device(std::move(config), std::move(blockStore)); CryDevice device(std::move(config), std::move(blockStore));
fspp::FilesystemImpl fsimpl(&device); fspp::FilesystemImpl fsimpl(&device);

View File

@ -25,8 +25,9 @@ ProgramOptions Parser::parse() const {
string baseDir = vm["base-dir"].as<string>(); string baseDir = vm["base-dir"].as<string>();
string mountDir = vm["mount-dir"].as<string>(); string mountDir = vm["mount-dir"].as<string>();
string configFile = vm["config"].as<string>();
return ProgramOptions(baseDir, mountDir, options.second); return ProgramOptions(baseDir, mountDir, configFile, options.second);
} }
po::variables_map Parser::_parseOptionsOrShowHelp(const vector<char*> options) { po::variables_map Parser::_parseOptionsOrShowHelp(const vector<char*> options) {
@ -58,6 +59,7 @@ void Parser::_addAllowedOptions(po::options_description *desc) {
po::options_description options("Allowed options"); po::options_description options("Allowed options");
options.add_options() options.add_options()
("help,h", "show help message") ("help,h", "show help message")
("config,c", po::value<string>()->required(), "Config file")
; ;
desc->add(options); desc->add(options);
} }
@ -74,7 +76,7 @@ void Parser::_addPositionalOptionForBaseDir(po::options_description *desc, po::p
} }
[[noreturn]] void Parser::_showHelpAndExit() { [[noreturn]] void Parser::_showHelpAndExit() {
cerr << "Usage: cryfs [options] rootDir mountPoint [-- [FUSE Mount Options]]\n"; cerr << "Usage: cryfs --config configFile [other options] rootDir mountPoint [-- [FUSE Mount Options]]\n";
po::options_description desc; po::options_description desc;
_addAllowedOptions(&desc); _addAllowedOptions(&desc);
cerr << desc << "\n"; cerr << desc << "\n";

View File

@ -6,8 +6,8 @@ using namespace cryfs::program_options;
using std::string; using std::string;
using std::vector; using std::vector;
ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const vector<char*> &fuseOptions) ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const std::string &configFile, const vector<char*> &fuseOptions)
:_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _fuseOptions(fuseOptions) { :_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _fuseOptions(fuseOptions) {
std::memcpy(_mountDir, mountDir.c_str(), mountDir.size()+1); 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) // 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"); ASSERT(_fuseOptions.size() >= 1, "There has to be one parameter at least for the executable name");
@ -26,6 +26,10 @@ string ProgramOptions::mountDir() const {
return string(_mountDir); return string(_mountDir);
} }
const string &ProgramOptions::configFile() const {
return _configFile;
}
const vector<char *> &ProgramOptions::fuseOptions() const { const vector<char *> &ProgramOptions::fuseOptions() const {
return _fuseOptions; return _fuseOptions;
} }

View File

@ -8,16 +8,18 @@ namespace cryfs {
namespace program_options { namespace program_options {
class ProgramOptions { class ProgramOptions {
public: public:
ProgramOptions(const std::string &baseDir, const std::string &mountDir, const std::vector<char *> &fuseOptions); ProgramOptions(const std::string &baseDir, const std::string &mountDir, const std::string &configFile, const std::vector<char *> &fuseOptions);
~ProgramOptions(); ~ProgramOptions();
const std::string &baseDir() const; const std::string &baseDir() const;
std::string mountDir() const; std::string mountDir() const;
const std::string &configFile() const;
const std::vector<char *> &fuseOptions() const; const std::vector<char *> &fuseOptions() const;
private: private:
std::string _baseDir; std::string _baseDir;
char *_mountDir; char *_mountDir;
std::string _configFile;
std::vector<char *> _fuseOptions; std::vector<char *> _fuseOptions;
}; };
} }

View File

@ -26,6 +26,13 @@ TEST_F(ProgramOptionsParserTest, MissingDir) {
); );
} }
TEST_F(ProgramOptionsParserTest, ConfigFileMissing) {
EXPECT_DEATH(
parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"}),
"Usage:"
);
}
TEST_F(ProgramOptionsParserTest, HelpLongOption) { TEST_F(ProgramOptionsParserTest, HelpLongOption) {
EXPECT_DEATH( EXPECT_DEATH(
parse({"./myExecutable", "--help"}), parse({"./myExecutable", "--help"}),
@ -41,14 +48,14 @@ TEST_F(ProgramOptionsParserTest, HelpShortOption) {
} }
TEST_F(ProgramOptionsParserTest, NoSpecialOptions) { TEST_F(ProgramOptionsParserTest, NoSpecialOptions) {
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"}); ProgramOptions options = parse({"./myExecutable", "--config", "/home/user/configFile", "/home/user/baseDir", "/home/user/mountDir"});
EXPECT_EQ("/home/user/baseDir", options.baseDir()); EXPECT_EQ("/home/user/baseDir", options.baseDir());
EXPECT_EQ("/home/user/mountDir", options.mountDir()); EXPECT_EQ("/home/user/mountDir", options.mountDir());
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir"}, options.fuseOptions()); EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir"}, options.fuseOptions());
} }
TEST_F(ProgramOptionsParserTest, FuseOptionGiven) { TEST_F(ProgramOptionsParserTest, FuseOptionGiven) {
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir", "--", "-f"}); ProgramOptions options = parse({"./myExecutable", "--config", "/home/user/configFile", "/home/user/baseDir", "/home/user/mountDir", "--", "-f"});
EXPECT_EQ("/home/user/baseDir", options.baseDir()); EXPECT_EQ("/home/user/baseDir", options.baseDir());
EXPECT_EQ("/home/user/mountDir", options.mountDir()); EXPECT_EQ("/home/user/mountDir", options.mountDir());
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir", "-f"}, options.fuseOptions()); EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir", "-f"}, options.fuseOptions());

View File

@ -7,23 +7,28 @@ using std::vector;
class ProgramOptionsTest: public ProgramOptionsTestBase {}; class ProgramOptionsTest: public ProgramOptionsTestBase {};
TEST_F(ProgramOptionsTest, BaseDir) { TEST_F(ProgramOptionsTest, BaseDir) {
ProgramOptions testobj("/home/user/mydir", "", options({"./myExecutable"})); ProgramOptions testobj("/home/user/mydir", "", "", options({"./myExecutable"}));
EXPECT_EQ("/home/user/mydir", testobj.baseDir()); EXPECT_EQ("/home/user/mydir", testobj.baseDir());
} }
TEST_F(ProgramOptionsTest, MountDir) { TEST_F(ProgramOptionsTest, MountDir) {
ProgramOptions testobj("", "/home/user/mydir", options({"./myExecutable"})); ProgramOptions testobj("", "/home/user/mydir", "", options({"./myExecutable"}));
EXPECT_EQ("/home/user/mydir", testobj.mountDir()); EXPECT_EQ("/home/user/mydir", testobj.mountDir());
} }
TEST_F(ProgramOptionsTest, ConfigFile) {
ProgramOptions testobj("", "", "/home/user/configfile", options({"./myExecutable"}));
EXPECT_EQ("/home/user/configfile", testobj.configFile());
}
TEST_F(ProgramOptionsTest, EmptyFuseOptions) { TEST_F(ProgramOptionsTest, EmptyFuseOptions) {
ProgramOptions testobj("/rootDir", "/home/user/mydir", options({"./myExecutable"})); ProgramOptions testobj("/rootDir", "/home/user/mydir", "/home/user/configfile", options({"./myExecutable"}));
//Fuse should have the mount dir as first parameter //Fuse should have the mount dir as first parameter
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir"}, testobj.fuseOptions()); EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir"}, testobj.fuseOptions());
} }
TEST_F(ProgramOptionsTest, SomeFuseOptions) { TEST_F(ProgramOptionsTest, SomeFuseOptions) {
ProgramOptions testobj("/rootDir", "/home/user/mydir", options({"./myExecutable", "-f", "--longoption"})); ProgramOptions testobj("/rootDir", "/home/user/mydir", "/home/user/configfile", options({"./myExecutable", "-f", "--longoption"}));
//Fuse should have the mount dir as first parameter //Fuse should have the mount dir as first parameter
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir", "-f", "--longoption"}, testobj.fuseOptions()); EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mydir", "-f", "--longoption"}, testobj.fuseOptions());
} }