2013-08-29 11:45:22 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
OpenSpades is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-29 11:45:22 +09:00
|
|
|
*/
|
2013-08-18 16:18:06 +09:00
|
|
|
|
2014-03-16 00:53:28 +09:00
|
|
|
#include <list>
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "Debug.h"
|
2013-08-18 16:18:06 +09:00
|
|
|
#include "Exception.h"
|
2016-12-03 18:23:47 +09:00
|
|
|
#include "FileManager.h"
|
|
|
|
#include "IFileSystem.h"
|
2013-08-18 16:18:06 +09:00
|
|
|
#include "IStream.h"
|
|
|
|
|
|
|
|
namespace spades {
|
2014-03-16 00:53:28 +09:00
|
|
|
static std::list<IFileSystem *> g_fileSystems;
|
2013-08-18 16:18:06 +09:00
|
|
|
IStream *FileManager::OpenForReading(const char *fn) {
|
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!fn)
|
|
|
|
SPInvalidArgument("fn");
|
|
|
|
if (fn[0] == 0)
|
|
|
|
SPFileNotFound(fn);
|
|
|
|
|
|
|
|
// check each file system
|
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
if (fs->FileExists(fn))
|
2013-08-18 16:18:06 +09:00
|
|
|
return fs->OpenForReading(fn);
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
|
|
|
// check weak files, too
|
|
|
|
auto weak_fn = std::string(fn) + ".weak";
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
if (fs->FileExists(weak_fn.c_str()))
|
2016-11-20 19:13:00 +09:00
|
|
|
return fs->OpenForReading(weak_fn.c_str());
|
|
|
|
}
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
SPFileNotFound(fn);
|
|
|
|
}
|
|
|
|
IStream *FileManager::OpenForWriting(const char *fn) {
|
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!fn)
|
|
|
|
SPInvalidArgument("fn");
|
|
|
|
if (fn[0] == 0)
|
|
|
|
SPFileNotFound(fn);
|
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
if (fs->FileExists(fn))
|
2013-08-18 16:18:06 +09:00
|
|
|
return fs->OpenForWriting(fn);
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
|
|
|
// FIXME: handling of weak files
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
// create file
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
try {
|
2013-08-18 16:18:06 +09:00
|
|
|
return fs->OpenForWriting(fn);
|
2016-12-03 18:23:47 +09:00
|
|
|
} catch (...) {
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
SPRaise("No filesystem is writable");
|
|
|
|
}
|
|
|
|
bool FileManager::FileExists(const char *fn) {
|
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!fn)
|
|
|
|
SPInvalidArgument("fn");
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
if (fs->FileExists(fn))
|
2013-08-18 16:18:06 +09:00
|
|
|
return true;
|
2016-11-20 19:13:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// check weak files, too
|
|
|
|
auto weak_fn = std::string(fn) + ".weak";
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *fs : g_fileSystems) {
|
|
|
|
if (fs->FileExists(weak_fn.c_str()))
|
2016-11-20 19:13:00 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
void FileManager::AddFileSystem(spades::IFileSystem *fs) {
|
2014-03-16 00:53:28 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
|
|
|
AppendFileSystem(fs);
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
void FileManager::AppendFileSystem(spades::IFileSystem *fs) {
|
2013-08-18 16:18:06 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!fs)
|
|
|
|
SPInvalidArgument("fs");
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
g_fileSystems.push_back(fs);
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
void FileManager::PrependFileSystem(spades::IFileSystem *fs) {
|
2014-03-16 00:53:28 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!fs)
|
|
|
|
SPInvalidArgument("fs");
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2014-03-16 00:53:28 +09:00
|
|
|
g_fileSystems.push_front(fs);
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
std::string FileManager::ReadAllBytes(const char *fn) {
|
|
|
|
SPADES_MARK_FUNCTION();
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
IStream *stream = OpenForReading(fn);
|
2016-12-03 18:23:47 +09:00
|
|
|
try {
|
2013-08-18 16:18:06 +09:00
|
|
|
std::string ret = stream->ReadAllBytes();
|
|
|
|
delete stream;
|
|
|
|
return ret;
|
2016-12-03 18:23:47 +09:00
|
|
|
} catch (...) {
|
2013-08-18 16:18:06 +09:00
|
|
|
delete stream;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
std::vector<std::string> FileManager::EnumFiles(const char *path) {
|
|
|
|
std::vector<std::string> list;
|
|
|
|
std::set<std::string> set;
|
2016-12-03 18:23:47 +09:00
|
|
|
if (!path)
|
|
|
|
SPInvalidArgument("path");
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto *fs : g_fileSystems) {
|
2013-08-18 16:18:06 +09:00
|
|
|
std::vector<std::string> l = fs->EnumFiles(path);
|
2016-12-03 18:23:47 +09:00
|
|
|
for (size_t i = 0; i < l.size(); i++)
|
2013-08-18 16:18:06 +09:00
|
|
|
set.insert(l[i]);
|
|
|
|
}
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2016-12-03 18:23:47 +09:00
|
|
|
for (auto &s : set)
|
2014-03-16 00:53:28 +09:00
|
|
|
list.push_back(s);
|
2016-11-20 19:13:00 +09:00
|
|
|
|
2013-08-18 16:18:06 +09:00
|
|
|
return list;
|
|
|
|
}
|
2017-03-04 11:18:12 +00:00
|
|
|
|
|
|
|
void FileManager::Close() {
|
|
|
|
for (auto *fs: g_fileSystems) {
|
|
|
|
delete fs;
|
|
|
|
}
|
|
|
|
}
|
2013-08-18 16:18:06 +09:00
|
|
|
}
|