diff --git a/src/cryfs-cli/Cli.cpp b/src/cryfs-cli/Cli.cpp index 2e7d9452..29430f75 100644 --- a/src/cryfs-cli/Cli.cpp +++ b/src/cryfs-cli/Cli.cpp @@ -115,7 +115,10 @@ namespace cryfs_cli { } fspp::fuse::Fuse* Cli::initFilesystem(const ProgramOptions &options, unique_ptr password) { + cpputils::showBacktraceOnCrash(); + cpputils::set_thread_name("cryfs"); try { + _sanityChecks(options); LocalStateDir localStateDir(options.localStateDir()); auto blockStore = make_unique_ref(options.baseDir()); auto config = _loadOrCreateConfig(options, localStateDir, std::move(password)); @@ -172,24 +175,24 @@ namespace cryfs_cli { (*rootDir)->children(); // Load children } - /*int Cli::main(int argc, const char **argv, std::function onMounted) { - cpputils::showBacktraceOnCrash(); - cpputils::set_thread_name("cryfs"); + void Cli::_sanityChecks(const ProgramOptions &options) { + _checkDirAccessible(bf::absolute(options.baseDir()), "base directory", options.createMissingBasedir(), ErrorCode::InaccessibleBaseDir); + } - try { - _showVersion(); - ProgramOptions options = program_options::Parser(argc, argv).parse(CryCiphers::supportedCipherNames()); - _sanityChecks(options); - _runFilesystem(options); - } catch (const CryfsException &e) { - if (e.what() != string()) { - std::cerr << "Error " << static_cast(e.errorCode()) << ": " << e.what() << std::endl; + void Cli::_checkDirAccessible(const bf::path &dir, const std::string &name, bool createMissingDir, ErrorCode errorCode) { + if (!bf::exists(dir)) { + if (createMissingDir) { + LOG(INFO, "Automatically creating {}", name); + if (!bf::create_directory(dir)) { + throw CryfsException("Error creating "+name, errorCode); + } + } else { + //std::cerr << "Exit code: " << exitCode(errorCode) << std::endl; + throw CryfsException(name + " not found.", errorCode); } - return exitCode(e.errorCode()); - } catch (const std::runtime_error &e) { - std::cerr << "Error: " << e.what() << std::endl; - return exitCode(ErrorCode::UnspecifiedError); } - return exitCode(ErrorCode::Success); - }*/ + if (!bf::is_directory(dir)) { + throw CryfsException(name+" is not a directory.", errorCode); + } + } } diff --git a/src/cryfs-cli/Cli.h b/src/cryfs-cli/Cli.h index d565cd74..40236eff 100644 --- a/src/cryfs-cli/Cli.h +++ b/src/cryfs-cli/Cli.h @@ -27,8 +27,9 @@ namespace cryfs_cli { void _checkConfigIntegrity(const boost::filesystem::path& basedir, const cryfs::LocalStateDir& localStateDir, const cryfs::CryConfigFile& config, bool allowReplacedFilesystem); cpputils::either _loadOrCreateConfigFile(boost::filesystem::path configFilePath, cryfs::LocalStateDir localStateDir, std::unique_ptr password, const boost::optional &cipher, const boost::optional &blocksizeBytes, bool allowFilesystemUpgrade, const boost::optional &missingBlockIsIntegrityViolation, bool allowReplacedFilesystem); boost::filesystem::path _determineConfigFile(const program_options::ProgramOptions &options); - void _showVersion(); void _initLogfile(); + void _sanityChecks(const program_options::ProgramOptions &options); + void _checkDirAccessible(const boost::filesystem::path &dir, const std::string &name, bool createMissingDir, cryfs::ErrorCode errorCode); void _sanityCheckFilesystem(cryfs::CryDevice *device); diff --git a/src/fspp/fuse/jni.cpp b/src/fspp/fuse/jni.cpp index a8b33f7c..b810eed7 100644 --- a/src/fspp/fuse/jni.cpp +++ b/src/fspp/fuse/jni.cpp @@ -13,21 +13,29 @@ using fspp::fuse::Fuse; std::set validFusePtrs; -extern "C" jlong cryfs_init(JNIEnv* env, jstring jbaseDir, jstring jlocalStateDir, jbyteArray jpassword) { +extern "C" jlong cryfs_init(JNIEnv* env, jstring jbaseDir, jstring jlocalStateDir, jbyteArray jpassword, jboolean createBaseDir, jstring jcipher) { const char* baseDir = env->GetStringUTFChars(jbaseDir, NULL); const char* localStateDir = env->GetStringUTFChars(jlocalStateDir, NULL); + boost::optional cipher = none; + if (jcipher != NULL) { + const char* cipherName = env->GetStringUTFChars(jcipher, NULL); + cipher = boost::optional(cipherName); + env->ReleaseStringUTFChars(jcipher, cipherName); + } auto &keyGenerator = Random::OSRandom(); - ProgramOptions options = ProgramOptions(baseDir, none, localStateDir, false, false, false, none, none, false, none); + ProgramOptions options = ProgramOptions(baseDir, none, localStateDir, false, false, createBaseDir, cipher, none, false, none); char* password = reinterpret_cast(env->GetByteArrayElements(jpassword, NULL)); Fuse* fuse = Cli(keyGenerator, SCrypt::DefaultSettings).initFilesystem(options, make_unique(password)); + env->ReleaseByteArrayElements(jpassword, reinterpret_cast(password), 0); env->ReleaseStringUTFChars(jbaseDir, baseDir); env->ReleaseStringUTFChars(jlocalStateDir, localStateDir); - env->ReleaseByteArrayElements(jpassword, reinterpret_cast(password), 0); jlong fusePtr = reinterpret_cast(fuse); - validFusePtrs.insert(fusePtr); + if (fusePtr != 0) { + validFusePtrs.insert(fusePtr); + } return fusePtr; }