Volume creation

This commit is contained in:
Matéo Duparc 2022-06-19 17:54:18 +02:00
parent e6d735dbab
commit 56c7d96bbf
3 changed files with 34 additions and 22 deletions

View File

@ -115,7 +115,10 @@ namespace cryfs_cli {
}
fspp::fuse::Fuse* Cli::initFilesystem(const ProgramOptions &options, unique_ptr<string> password) {
cpputils::showBacktraceOnCrash();
cpputils::set_thread_name("cryfs");
try {
_sanityChecks(options);
LocalStateDir localStateDir(options.localStateDir());
auto blockStore = make_unique_ref<OnDiskBlockStore2>(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<void()> 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<int>(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);
}
}
}

View File

@ -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<cryfs::CryConfigFile::LoadError, cryfs::CryConfigLoader::ConfigLoadResult> _loadOrCreateConfigFile(boost::filesystem::path configFilePath, cryfs::LocalStateDir localStateDir, std::unique_ptr<string> password, const boost::optional<std::string> &cipher, const boost::optional<uint32_t> &blocksizeBytes, bool allowFilesystemUpgrade, const boost::optional<bool> &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);

View File

@ -13,21 +13,29 @@ using fspp::fuse::Fuse;
std::set<jlong> 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<string> cipher = none;
if (jcipher != NULL) {
const char* cipherName = env->GetStringUTFChars(jcipher, NULL);
cipher = boost::optional<string>(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<char*>(env->GetByteArrayElements(jpassword, NULL));
Fuse* fuse = Cli(keyGenerator, SCrypt::DefaultSettings).initFilesystem(options, make_unique<string>(password));
env->ReleaseByteArrayElements(jpassword, reinterpret_cast<jbyte*>(password), 0);
env->ReleaseStringUTFChars(jbaseDir, baseDir);
env->ReleaseStringUTFChars(jlocalStateDir, localStateDir);
env->ReleaseByteArrayElements(jpassword, reinterpret_cast<jbyte*>(password), 0);
jlong fusePtr = reinterpret_cast<jlong>(fuse);
validFusePtrs.insert(fusePtr);
if (fusePtr != 0) {
validFusePtrs.insert(fusePtr);
}
return fusePtr;
}