From 928706b36ebd3b25427f78690a98a929b95e8701 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 19 Sep 2014 19:11:42 +0300 Subject: [PATCH] client/config, server/config: Check configured paths at startup --- CMakeLists.txt | 2 ++ src/client/config.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ src/client/config.h | 4 ++- src/client/main.cpp | 4 +++ src/server/config.cpp | 57 ++++++++++++++++++++++++++++++++++++++++ src/server/config.h | 2 ++ src/server/main.cpp | 4 +++ 7 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/client/config.cpp create mode 100644 src/server/config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b6c5ae..5962c08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ set(CLIENT_SRCS src/client/main.cpp src/client/state.cpp src/client/app.cpp + src/client/config.cpp src/core/log.cpp src/impl/fs.cpp src/impl/tcpsocket.cpp @@ -76,6 +77,7 @@ set(SERVER_SRCS src/server/main.cpp src/server/state.cpp src/server/rccpp.cpp + src/server/config.cpp src/core/log.cpp src/impl/fs.cpp src/impl/event.cpp diff --git a/src/client/config.cpp b/src/client/config.cpp new file mode 100644 index 0000000..919ffb7 --- /dev/null +++ b/src/client/config.cpp @@ -0,0 +1,60 @@ +// http://www.apache.org/licenses/LICENSE-2.0 +// Copyright 2014 Perttu Ahola +#include "client/config.h" +#include "core/log.h" +#include "interface/fs.h" +#include +#define MODULE "config" + +namespace client { + +static bool check_file_readable(const ss_ &path) +{ + std::ifstream ifs(path); + bool readable = ifs.good(); + if(!readable) + log_e(MODULE, "File is not readable: \"%s\"", cs(path)); + return readable; +} + +static bool check_file_writable(const ss_ &path) +{ + std::ofstream ofs(path); + bool writable = ofs.good(); + if(!writable) + log_e(MODULE, "File is not writable: \"%s\"", cs(path)); + return writable; +} + +bool Config::check_paths() +{ + bool fail = false; + + if(!check_file_readable(polycode_path+"/Core/Contents/Include/Polycode.h")){ + log_e(MODULE, "Polycode doesn't seem to exist in polycode_path=\"%s\"", + cs(polycode_path)); + fail = true; + } + + if(!check_file_readable(share_path+"/extensions/test/init.lua")){ + log_e(MODULE, "Static files don't seem to exist in share_path=\"%s\"", + cs(share_path)); + fail = true; + } + else if(!check_file_readable(share_path+"/client/init.lua")){ + log_e(MODULE, "Static files don't seem to exist in share_path=\"%s\"", + cs(share_path)); + fail = true; + } + + auto *fs = interface::getGlobalFilesystem(); + fs->create_directories(cache_path); + if(!check_file_writable(cache_path+"/write.test")){ + log_e(MODULE, "Cannot write into cache_path=\"%s\"", cs(cache_path)); + fail = true; + } + + return !fail; +} + +} diff --git a/src/client/config.h b/src/client/config.h index 0b06c79..ff58836 100644 --- a/src/client/config.h +++ b/src/client/config.h @@ -8,8 +8,10 @@ namespace client struct Config { ss_ server_address; - ss_ polycode_path = "/home/celeron55/softat/polycode/"; + ss_ polycode_path = "../../polycode"; ss_ share_path = ".."; ss_ cache_path = "../cache"; + + bool check_paths(); }; } diff --git a/src/client/main.cpp b/src/client/main.cpp index 33c8fb4..9b1acee 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -94,6 +94,10 @@ int main(int argc, char *argv[]) } } + if(!config.check_paths()){ + return 1; + } + Polycode::PolycodeView *view = new Polycode::PolycodeView("Hello Polycode!"); sp_ app0(app::createApp(view)); diff --git a/src/server/config.cpp b/src/server/config.cpp new file mode 100644 index 0000000..b848640 --- /dev/null +++ b/src/server/config.cpp @@ -0,0 +1,57 @@ +// http://www.apache.org/licenses/LICENSE-2.0 +// Copyright 2014 Perttu Ahola +#include "server/config.h" +#include "core/log.h" +#include "interface/fs.h" +#include +#define MODULE "config" + +namespace server { + +static bool check_file_readable(const ss_ &path) +{ + std::ifstream ifs(path); + bool readable = ifs.good(); + if(!readable) + log_e(MODULE, "File is not readable: \"%s\"", cs(path)); + return readable; +} + +static bool check_file_writable(const ss_ &path) +{ + std::ofstream ofs(path); + bool writable = ofs.good(); + if(!writable) + log_e(MODULE, "File is not writable: \"%s\"", cs(path)); + return writable; +} + +bool Config::check_paths() +{ + bool fail = false; + + if(!check_file_readable(share_path+"/builtin/network/network.cpp")){ + log_e(MODULE, "Static files don't seem to exist in share_path=\"%s\"", + cs(share_path)); + fail = true; + } + + if(!check_file_readable(interface_path+"/event.h")){ + log_e(MODULE, "Static files don't seem to exist in interface_path=\"%s\"", + cs(interface_path)); + fail = true; + } + + auto *fs = interface::getGlobalFilesystem(); + fs->create_directories(rccpp_build_path); + if(!check_file_writable(rccpp_build_path+"/write.test")){ + log_e(MODULE, "Cannot write into rccpp_build_path=\"%s\"", + cs(rccpp_build_path)); + fail = true; + } + + return !fail; +} + +} + diff --git a/src/server/config.h b/src/server/config.h index 07a66b3..58a38a6 100644 --- a/src/server/config.h +++ b/src/server/config.h @@ -10,6 +10,8 @@ namespace server ss_ rccpp_build_path = "../cache/rccpp_build"; ss_ interface_path = "../src/interface"; ss_ share_path = ".."; + + bool check_paths(); }; } diff --git a/src/server/main.cpp b/src/server/main.cpp index 5bef897..f073726 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -97,6 +97,10 @@ int main(int argc, char *argv[]) std::cerr<<"Buildat server"<