interface/fs: Remove crazy

master
Perttu Ahola 2014-10-29 14:05:08 +02:00
parent 86976cbc6c
commit 75e0ae5cbf
13 changed files with 121 additions and 142 deletions

View File

@ -63,20 +63,19 @@ struct Module: public interface::Module
ss_ module_name = event.name;
ss_ module_path = m_server->get_module_path(module_name);
ss_ client_data_path = module_path+"/client_data";
auto list = interface::getGlobalFilesystem()
->list_directory(client_data_path);
auto list = interface::fs::list_directory(client_data_path);
if(list.empty())
return;
sv_<ss_> log_list;
for(const interface::Filesystem::Node &n : list){
for(const interface::fs::Node &n : list){
if(n.is_directory)
continue;
log_list.push_back(n.name);
}
log_i(MODULE, "client_data: %s: %s", cs(module_name), cs(dump(log_list)));
for(const interface::Filesystem::Node &n : list){
for(const interface::fs::Node &n : list){
if(n.is_directory)
continue;
const ss_ &file_path = client_data_path+"/"+n.name;

View File

@ -288,7 +288,7 @@ struct Module: public interface::Module, public client_file::Interface
// Tell path to server so that it can be used in network-synced Scene
m_server->add_file_path(name, path);
ss_ dir_path = interface::Filesystem::strip_file_name(path);
ss_ dir_path = interface::fs::strip_file_name(path);
m_watch->add(dir_path, [this, name, path](const ss_ &path_){
if(path_ != path){
//log_d(MODULE, "Ignoring file watch callback: %s (we want %s)",

View File

@ -63,20 +63,19 @@ struct Module: public interface::Module
ss_ module_name = event.name;
ss_ module_path = m_server->get_module_path(module_name);
ss_ client_lua_path = module_path+"/client_lua";
auto list = interface::getGlobalFilesystem()
->list_directory(client_lua_path);
auto list = interface::fs::list_directory(client_lua_path);
if(list.empty())
return;
sv_<ss_> log_list;
for(const interface::Filesystem::Node &n : list){
for(const interface::fs::Node &n : list){
if(n.is_directory)
continue;
log_list.push_back(n.name);
}
log_i(MODULE, "client_lua: %s: %s", cs(module_name), cs(dump(log_list)));
for(const interface::Filesystem::Node &n : list){
for(const interface::fs::Node &n : list){
if(n.is_directory)
continue;
const ss_ &file_path = client_lua_path+"/"+n.name;

View File

@ -314,8 +314,7 @@ struct Module: public interface::Module, public loader::Interface
for(const ss_ &base_path : m_module_load_paths){
ss_ module_path = base_path+"/"+name;
ss_ meta_path = module_path+"/meta.json";
auto *fs = interface::getGlobalFilesystem();
if(!fs->path_exists(meta_path)){
if(!interface::fs::path_exists(meta_path)){
log_t(MODULE, "%s: Doesn't exist", cs(meta_path));
continue;
} else {
@ -357,15 +356,14 @@ struct Module: public interface::Module, public loader::Interface
void load_modules()
{
log_v(MODULE, "loader::load_modules()");
auto *fs = interface::getGlobalFilesystem();
ss_ builtin = m_server->get_builtin_modules_path();
ss_ current = m_server->get_modules_path();
// Get a list of required modules; that is, everything in the main
// module path
set_<ss_> required_modules;
auto list = fs->list_directory(current);
for(const interface::Filesystem::Node &n : list){
auto list = interface::fs::list_directory(current);
for(const interface::fs::Node &n : list){
if(n.name == "__loader" || !n.is_directory)
continue;
log_t(MODULE, "Requirement from main module path: \"%s\"", cs(n.name));

View File

@ -124,12 +124,11 @@ struct Module: public interface::Module, public main_context::Interface
server_config.get<ss_>("urho3d_path")+"/Bin/CoreData",
server_config.get<ss_>("urho3d_path")+"/Bin/Data",
};
auto *fs = interface::getGlobalFilesystem();
ss_ resource_paths_s;
for(const ss_ &path : resource_paths){
if(!resource_paths_s.empty())
resource_paths_s += ";";
resource_paths_s += fs->get_absolute_path(path);
resource_paths_s += interface::fs::get_absolute_path(path);
}
VariantMap params;

View File

@ -125,21 +125,20 @@ struct CApp: public App, public magic::Application
g_client_config.urho3d_path+"/Bin/CoreData",
g_client_config.urho3d_path+"/Bin/Data",
};
auto *fs = interface::getGlobalFilesystem();
ss_ resource_paths_s;
for(const ss_ &path : resource_paths){
if(!resource_paths_s.empty())
resource_paths_s += ";";
resource_paths_s += fs->get_absolute_path(path);
resource_paths_s += interface::fs::get_absolute_path(path);
}
// Set allowed paths in urho3d filesystem (part of sandbox)
magic::FileSystem *magic_fs = GetSubsystem<magic::FileSystem>();
for(const ss_ &path : resource_paths){
magic_fs->RegisterPath(fs->get_absolute_path(path).c_str());
magic_fs->RegisterPath(interface::fs::get_absolute_path(path).c_str());
}
magic_fs->RegisterPath(
fs->get_absolute_path(g_client_config.cache_path).c_str());
magic_fs->RegisterPath(interface::fs::get_absolute_path(
g_client_config.cache_path).c_str());
// Useful for saving stuff for inspection when debugging
magic_fs->RegisterPath("/tmp");

View File

@ -28,10 +28,9 @@ static bool check_file_writable(const ss_ &path)
void Config::make_paths_absolute()
{
auto *fs = interface::getGlobalFilesystem();
share_path = fs->get_absolute_path(share_path);
cache_path = fs->get_absolute_path(cache_path);
urho3d_path = fs->get_absolute_path(urho3d_path);
share_path = interface::fs::get_absolute_path(share_path);
cache_path = interface::fs::get_absolute_path(cache_path);
urho3d_path = interface::fs::get_absolute_path(urho3d_path);
}
bool Config::check_paths()
@ -56,8 +55,7 @@ bool Config::check_paths()
fail = true;
}
auto *fs = interface::getGlobalFilesystem();
if(!fs->create_directories(cache_path)){
if(!interface::fs::create_directories(cache_path)){
log_e(MODULE, "Could not create directory \"%s\"", cs(cache_path));
fail = true;
}

View File

@ -75,9 +75,8 @@ struct CState: public State
m_tmp_path(g_client_config.cache_path+"/tmp")
{
// Create directory for cached files
auto *fs = interface::getGlobalFilesystem();
fs->create_directories(m_remote_cache_path);
fs->create_directories(m_tmp_path);
interface::fs::create_directories(m_remote_cache_path);
interface::fs::create_directories(m_tmp_path);
setup_packet_handlers();
}

View File

@ -10,119 +10,111 @@
#endif
namespace interface {
namespace fs {
bool Filesystem::check_file_extension(const char *path, const char *ext)
bool check_file_extension(const char *path, const char *ext)
{
return c55fs::checkFileExtension(path, ext);
}
ss_ Filesystem::strip_file_extension(const ss_ &path)
ss_ strip_file_extension(const ss_ &path)
{
return c55fs::stripFileExtension(path);
}
ss_ Filesystem::strip_file_name(const ss_ &path)
ss_ strip_file_name(const ss_ &path)
{
return c55fs::stripFilename(path);
}
struct CFilesystem: public Filesystem
sv_<Node> list_directory(const ss_ &path)
{
sv_<Node> list_directory(const ss_ &path)
{
sv_<Node> result;
auto list = c55fs::GetDirListing(path);
for(auto n2 : list){
Node n;
n.name = n2.name;
n.is_directory = n2.dir;
result.push_back(n);
}
return result;
sv_<Node> result;
auto list = c55fs::GetDirListing(path);
for(auto n2 : list){
Node n;
n.name = n2.name;
n.is_directory = n2.dir;
result.push_back(n);
}
bool create_directories(const ss_ &path)
{
return c55fs::CreateAllDirs(get_absolute_path(path));
}
ss_ get_cwd()
{
char path[10000];
return result;
}
bool create_directories(const ss_ &path)
{
return c55fs::CreateAllDirs(get_absolute_path(path));
}
ss_ get_cwd()
{
char path[10000];
#ifdef _WIN32
GetCurrentDirectory(10000, path);
GetCurrentDirectory(10000, path);
#else
getcwd(path, 10000);
getcwd(path, 10000);
#endif
return path;
}
// Doesn't collapse .. and .
ss_ get_basic_absolute_path(const ss_ &path0)
{
ss_ path = c55::trim(path0);
#ifdef _WIN32
if(path.size() >= 2 && path.substr(0, 2) == "\\\\")
return path;
}
// Doesn't collapse .. and .
ss_ get_basic_absolute_path(const ss_ &path0)
{
ss_ path = c55::trim(path0);
#ifdef _WIN32
if(path.size() >= 2 && path.substr(0, 2) == "\\\\")
return path;
if(path.size() >= 1 && (path[0] == '/' || path[0] == '\\'))
return path;
if(path.size() >= 2 && path[1] == ':')
return path;
return get_cwd()+"/"+path;
if(path.size() >= 1 && (path[0] == '/' || path[0] == '\\'))
return path;
if(path.size() >= 2 && path[1] == ':')
return path;
return get_cwd()+"/"+path;
#else
if(path.size() >= 1 && path[0] == '/')
return path;
return get_cwd()+"/"+path;
if(path.size() >= 1 && path[0] == '/')
return path;
return get_cwd()+"/"+path;
#endif
}
// Collapses .. and .
ss_ get_absolute_path(const ss_ &path0)
{
ss_ path = get_basic_absolute_path(path0);
for(size_t i = 0; i<path.size(); i++){
if(path[i] == '\\')
path[i] = '/';
}
sv_<ss_> path_parts;
c55::Strfnd f(path);
for(;;){
if(f.atend())
break;
ss_ part = f.next("/");
if(part == ""){
// Nop
} else if(part == "."){
// Nop
} else if(part == ".."){
path_parts.pop_back();
} else {
path_parts.push_back(part);
}
}
ss_ path2;
for(const ss_ &part : path_parts){
path2 += "/" + part;
}
#ifdef _WIN32
if(path.size() >= 2 && path.substr(0, 2) == "//"){
// Preserve network path
path2 = "\\\\"+path2.substr(1);
} else {
// Path will be in a silly format like "/Z:/home/"
path2 = path2.substr(1);
}
#endif
return path2;
}
bool path_exists(const ss_ &path)
{
return c55fs::PathExists(path);
}
};
Filesystem* getGlobalFilesystem()
}
// Collapses .. and .
ss_ get_absolute_path(const ss_ &path0)
{
static CFilesystem fs;
return &fs;
ss_ path = get_basic_absolute_path(path0);
for(size_t i = 0; i<path.size(); i++){
if(path[i] == '\\')
path[i] = '/';
}
sv_<ss_> path_parts;
c55::Strfnd f(path);
for(;;){
if(f.atend())
break;
ss_ part = f.next("/");
if(part == ""){
// Nop
} else if(part == "."){
// Nop
} else if(part == ".."){
path_parts.pop_back();
} else {
path_parts.push_back(part);
}
}
ss_ path2;
for(const ss_ &part : path_parts){
path2 += "/" + part;
}
#ifdef _WIN32
if(path.size() >= 2 && path.substr(0, 2) == "//"){
// Preserve network path
path2 = "\\\\"+path2.substr(1);
} else {
// Path will be in a silly format like "/Z:/home/"
path2 = path2.substr(1);
}
#endif
return path2;
}
bool path_exists(const ss_ &path)
{
return c55fs::PathExists(path);
}
}}
// vim: set noet ts=4 sw=4:

View File

@ -5,32 +5,27 @@
namespace interface
{
// TODO: Get some sanity into this; what's up with the virtual methods?
struct Filesystem
namespace fs
{
virtual ~Filesystem(){}
struct Node {
ss_ name;
bool is_directory;
};
virtual sv_<Node> list_directory(const ss_ &path) = 0;
sv_<Node> list_directory(const ss_ &path);
virtual bool create_directories(const ss_ &path) = 0;
bool create_directories(const ss_ &path);
virtual ss_ get_cwd() = 0;
ss_ get_cwd();
virtual ss_ get_absolute_path(const ss_ &path) = 0;
ss_ get_absolute_path(const ss_ &path);
virtual bool path_exists(const ss_ &path) = 0;
bool path_exists(const ss_ &path);
// "image.png", "png" -> true
static bool check_file_extension(const char *path, const char *ext);
static ss_ strip_file_extension(const ss_ &path);
static ss_ strip_file_name(const ss_ &path);
bool check_file_extension(const char *path, const char *ext);
ss_ strip_file_extension(const ss_ &path);
ss_ strip_file_name(const ss_ &path);
};
Filesystem* getGlobalFilesystem();
}
// vim: set noet ts=4 sw=4:

View File

@ -35,7 +35,7 @@ static int l_print_log(lua_State *L)
static int l_mkdir(lua_State *L)
{
ss_ path = lua_tocppstring(L, 1);
bool ok = interface::getGlobalFilesystem()->create_directories(path);
bool ok = interface::fs::create_directories(path);
if(!ok)
log_w(MODULE, "Failed to create directory: \"%s\"", cs(path));
else

View File

@ -45,10 +45,12 @@ static bool check_runnable(const ss_ &command)
Config::Config()
{
set_default("rccpp_build_path", "../cache/rccpp_build");
set_default("interface_path", "../src/interface");
set_default("share_path", "..");
set_default("urho3d_path", "../../Urho3D");
// Paths are filled in by autodetection
set_default("rccpp_build_path", "");
set_default("interface_path", "");
set_default("share_path", "");
set_default("urho3d_path", "");
set_default("compiler_command", "c++");
set_default("skip_compiling_modules", json::object());
}
@ -82,8 +84,7 @@ bool Config::check_paths()
fail = true;
}
auto *fs = interface::getGlobalFilesystem();
fs->create_directories(rccpp_build_path);
interface::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));

View File

@ -491,7 +491,7 @@ struct CState: public State, public interface::Server
if(m_module_file_watches.count(info.name) == 0){
sp_<interface::FileWatch> w(interface::createFileWatch());
for(const ss_ &watch_path : files_to_watch){
ss_ dir_path = interface::Filesystem::strip_file_name(watch_path);
ss_ dir_path = interface::fs::strip_file_name(watch_path);
w->add(dir_path, [this, info, watch_path](const ss_ &modified_path){
if(modified_path != watch_path)
return;