If a configuration file is not specified on command line, it will be put into the base directory

This commit is contained in:
Sebastian Messmer 2015-10-24 19:56:05 +02:00
parent 371303ae6a
commit 61ca163339
6 changed files with 56 additions and 33 deletions

View File

@ -78,11 +78,20 @@ string askPassword() {
return password; return password;
}; };
CryConfigFile loadOrCreateConfig(const bf::path &filename) { bf::path determineConfigFile(const ProgramOptions &options) {
auto configFile = options.configFile();
if (configFile == none) {
return options.baseDir() + "cryfs.config";
}
return *configFile;
}
CryConfigFile loadOrCreateConfig(const ProgramOptions &options) {
try { try {
auto configFile = determineConfigFile(options);
auto console = make_unique_ref<IOStreamConsole>(); auto console = make_unique_ref<IOStreamConsole>();
auto &keyGenerator = Random::OSRandom(); auto &keyGenerator = Random::OSRandom();
return CryConfigLoader(std::move(console), keyGenerator, &askPassword).loadOrCreate(filename); return CryConfigLoader(std::move(console), keyGenerator, &askPassword).loadOrCreate(configFile);
} catch (const std::exception &e) { } catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
exit(1); exit(1);
@ -90,7 +99,7 @@ CryConfigFile loadOrCreateConfig(const bf::path &filename) {
} }
void runFilesystem(const ProgramOptions &options) { void runFilesystem(const ProgramOptions &options) {
auto config = loadOrCreateConfig(options.configFile()); auto config = loadOrCreateConfig(options);
//TODO This daemonize causes error messages from CryDevice initialization to get lost. //TODO This daemonize causes error messages from CryDevice initialization to get lost.
// However, initializing CryDevice might (?) already spawn threads and we have to do daemonization before that // However, initializing CryDevice might (?) already spawn threads and we have to do daemonization before that
// because it doesn't fork threads. What to do? // because it doesn't fork threads. What to do?

View File

@ -29,14 +29,17 @@ 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>(); optional<string> configfile = none;
if (vm.count("config")) {
configfile = vm["config"].as<string>();
}
bool foreground = vm.count("foreground"); bool foreground = vm.count("foreground");
optional<string> logfile = none; optional<string> logfile = none;
if (vm.count("logfile")) { if (vm.count("logfile")) {
logfile = vm["logfile"].as<string>(); logfile = vm["logfile"].as<string>();
} }
return ProgramOptions(baseDir, mountDir, configFile, foreground, logfile, options.second); return ProgramOptions(baseDir, mountDir, configfile, foreground, logfile, options.second);
} }
po::variables_map Parser::_parseOptionsOrShowHelp(const vector<char*> options) { po::variables_map Parser::_parseOptionsOrShowHelp(const vector<char*> options) {
@ -68,7 +71,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(), "Configuration file") ("config,c", po::value<string>(), "Configuration file")
("foreground,f", "Run CryFS in foreground.") ("foreground,f", "Run CryFS in foreground.")
("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.") ("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

@ -7,7 +7,7 @@ using std::string;
using std::vector; using std::vector;
using boost::optional; using boost::optional;
ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const string &configFile, ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const optional<string> &configFile,
bool foreground, const optional<string> &logFile, const vector<char*> &fuseOptions) bool foreground, const optional<string> &logFile, const vector<char*> &fuseOptions)
:_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _foreground(foreground), :_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _foreground(foreground),
_logFile(logFile), _fuseOptions(fuseOptions) { _logFile(logFile), _fuseOptions(fuseOptions) {
@ -38,7 +38,7 @@ string ProgramOptions::mountDir() const {
return string(_mountDir); return string(_mountDir);
} }
const string &ProgramOptions::configFile() const { const optional<string> &ProgramOptions::configFile() const {
return _configFile; return _configFile;
} }
@ -46,7 +46,7 @@ bool ProgramOptions::foreground() const {
return _foreground; return _foreground;
} }
const optional<string> ProgramOptions::logFile() const { const optional<string> &ProgramOptions::logFile() const {
return _logFile; return _logFile;
} }

View File

@ -11,7 +11,7 @@ namespace cryfs {
namespace program_options { namespace program_options {
class ProgramOptions final { class ProgramOptions final {
public: public:
ProgramOptions(const std::string &baseDir, const std::string &mountDir, const std::string &configFile, ProgramOptions(const std::string &baseDir, const std::string &mountDir, const boost::optional<std::string> &configFile,
bool foreground, const boost::optional<std::string> &logFile, bool foreground, const boost::optional<std::string> &logFile,
const std::vector<char *> &fuseOptions); const std::vector<char *> &fuseOptions);
ProgramOptions(ProgramOptions &&rhs); ProgramOptions(ProgramOptions &&rhs);
@ -19,15 +19,15 @@ namespace cryfs {
const std::string &baseDir() const; const std::string &baseDir() const;
std::string mountDir() const; std::string mountDir() const;
const std::string &configFile() const; const boost::optional<std::string> &configFile() const;
bool foreground() const; bool foreground() const;
const boost::optional<std::string> logFile() const; const boost::optional<std::string> &logFile() 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; boost::optional<std::string> _configFile;
bool _foreground; bool _foreground;
boost::optional<std::string> _logFile; boost::optional<std::string> _logFile;
std::vector<char *> _fuseOptions; std::vector<char *> _fuseOptions;

View File

@ -3,6 +3,7 @@
using namespace cryfs::program_options; using namespace cryfs::program_options;
using std::vector; using std::vector;
using boost::none;
class ProgramOptionsParserTest: public ProgramOptionsTestBase { class ProgramOptionsParserTest: public ProgramOptionsTestBase {
public: public:
@ -28,13 +29,6 @@ TEST_F(ProgramOptionsParserDeathTest, MissingDir) {
); );
} }
TEST_F(ProgramOptionsParserDeathTest, ConfigFileMissing) {
EXPECT_DEATH(
parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"}),
"Usage:"
);
}
TEST_F(ProgramOptionsParserDeathTest, HelpLongOption) { TEST_F(ProgramOptionsParserDeathTest, HelpLongOption) {
EXPECT_DEATH( EXPECT_DEATH(
parse({"./myExecutable", "--help"}), parse({"./myExecutable", "--help"}),
@ -50,14 +44,26 @@ TEST_F(ProgramOptionsParserDeathTest, HelpShortOption) {
} }
TEST_F(ProgramOptionsParserTest, NoSpecialOptions) { TEST_F(ProgramOptionsParserTest, NoSpecialOptions) {
ProgramOptions options = parse({"./myExecutable", "--config", "/home/user/configFile", "/home/user/baseDir", "/home/user/mountDir"}); ProgramOptions options = parse({"./myExecutable", "/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_EQ(none, options.logFile());
EXPECT_EQ(none, options.configFile());
EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir"}, options.fuseOptions()); EXPECT_VECTOR_EQ({"./myExecutable", "/home/user/mountDir"}, options.fuseOptions());
} }
TEST_F(ProgramOptionsParserTest, LogfileGiven) {
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--logfile", "/home/user/logfile", "/home/user/mountDir"});
EXPECT_EQ("/home/user/mylogfile", options.logFile().value());
}
TEST_F(ProgramOptionsParserTest, ConfigfileGiven) {
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--configfile", "/home/user/configfile", "/home/user/mountDir"});
EXPECT_EQ("/home/user/myconfigfile", options.configFile().value());
}
TEST_F(ProgramOptionsParserTest, FuseOptionGiven) { TEST_F(ProgramOptionsParserTest, FuseOptionGiven) {
ProgramOptions options = parse({"./myExecutable", "--config", "/home/user/configFile", "/home/user/baseDir", "/home/user/mountDir", "--", "-f"}); ProgramOptions options = parse({"./myExecutable", "/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

@ -10,48 +10,53 @@ using std::string;
class ProgramOptionsTest: public ProgramOptionsTestBase {}; class ProgramOptionsTest: public ProgramOptionsTestBase {};
TEST_F(ProgramOptionsTest, BaseDir) { TEST_F(ProgramOptionsTest, BaseDir) {
ProgramOptions testobj("/home/user/mydir", "", "", false, none, options({"./myExecutable"})); ProgramOptions testobj("/home/user/mydir", "", none, false, none, 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", "", false, none, options({"./myExecutable"})); ProgramOptions testobj("", "/home/user/mydir", none, false, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/mydir", testobj.mountDir()); EXPECT_EQ("/home/user/mydir", testobj.mountDir());
} }
TEST_F(ProgramOptionsTest, ConfigFile) { TEST_F(ProgramOptionsTest, ConfigfileNone) {
ProgramOptions testobj("", "", "/home/user/configfile", false, none, options({"./myExecutable"})); ProgramOptions testobj("", "", none, true, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/configfile", testobj.configFile()); EXPECT_EQ(none, testobj.configFile());
}
TEST_F(ProgramOptionsTest, ConfigfileSome) {
ProgramOptions testobj("", "", string("/home/user/configfile"), true, none, options({"./myExecutable"}));
EXPECT_EQ("/home/user/configfile", testobj.configFile().get());
} }
TEST_F(ProgramOptionsTest, ForegroundFalse) { TEST_F(ProgramOptionsTest, ForegroundFalse) {
ProgramOptions testobj("", "", "/home/user/configfile", false, none, options({"./myExecutable"})); ProgramOptions testobj("", "", none, false, none, options({"./myExecutable"}));
EXPECT_FALSE(testobj.foreground()); EXPECT_FALSE(testobj.foreground());
} }
TEST_F(ProgramOptionsTest, ForegroundTrue) { TEST_F(ProgramOptionsTest, ForegroundTrue) {
ProgramOptions testobj("", "", "/home/user/configfile", true, none, options({"./myExecutable"})); ProgramOptions testobj("", "", none, true, none, options({"./myExecutable"}));
EXPECT_TRUE(testobj.foreground()); EXPECT_TRUE(testobj.foreground());
} }
TEST_F(ProgramOptionsTest, LogfileNone) { TEST_F(ProgramOptionsTest, LogfileNone) {
ProgramOptions testobj("", "", "/home/user/configfile", true, none, options({"./myExecutable"})); ProgramOptions testobj("", "", none, true, none, options({"./myExecutable"}));
EXPECT_EQ(none, testobj.logFile()); EXPECT_EQ(none, testobj.logFile());
} }
TEST_F(ProgramOptionsTest, LogfileSome) { TEST_F(ProgramOptionsTest, LogfileSome) {
ProgramOptions testobj("", "", "/home/user/configfile", true, string("logfile"), options({"./myExecutable"})); ProgramOptions testobj("", "", none, true, string("logfile"), options({"./myExecutable"}));
EXPECT_EQ("logfile", testobj.logFile().get()); EXPECT_EQ("logfile", testobj.logFile().get());
} }
TEST_F(ProgramOptionsTest, EmptyFuseOptions) { TEST_F(ProgramOptionsTest, EmptyFuseOptions) {
ProgramOptions testobj("/rootDir", "/home/user/mydir", "/home/user/configfile", false, none, options({"./myExecutable"})); ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, 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", "/home/user/configfile", false, none, options({"./myExecutable", "-f", "--longoption"})); ProgramOptions testobj("/rootDir", "/home/user/mydir", none, false, none, 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());
} }