metaforce/hecl/lib/database/Project.cpp

380 lines
9.6 KiB
C++
Raw Normal View History

2015-05-21 02:33:05 +00:00
#include <sys/stat.h>
#include <errno.h>
2015-06-09 22:19:59 +00:00
#include <stdio.h>
2015-05-21 02:33:05 +00:00
#include <string.h>
#include <system_error>
2015-06-10 23:34:14 +00:00
#if _WIN32
#else
#include <unistd.h>
#endif
2015-05-21 02:33:05 +00:00
#include "HECLDatabase.hpp"
2015-06-10 02:40:03 +00:00
namespace HECL
{
namespace Database
2015-05-21 02:33:05 +00:00
{
2015-06-09 23:21:45 +00:00
/**********************************************
* Project::ConfigFile
**********************************************/
2015-06-09 22:19:59 +00:00
static inline bool CheckNewLineAdvance(std::string::const_iterator& it)
{
2015-06-10 23:34:14 +00:00
if (*it == '\n')
2015-06-09 22:19:59 +00:00
{
it += 1;
return true;
}
else if (*it == '\r')
{
if (*(it+1) == '\n')
{
it += 2;
return true;
}
2015-06-10 23:34:14 +00:00
it += 1;
return true;
2015-06-09 22:19:59 +00:00
}
return false;
}
2015-06-10 02:40:03 +00:00
Project::ConfigFile::ConfigFile(const Project& project, const SystemString& name)
2015-05-21 02:33:05 +00:00
{
2015-06-10 02:40:03 +00:00
m_filepath = project.m_rootPath.getAbsolutePath() + _S("/.hecl/config/") + name;
2015-06-09 22:57:21 +00:00
}
2015-06-09 22:19:59 +00:00
2015-06-10 23:34:14 +00:00
const std::vector<std::string>& Project::ConfigFile::lockAndRead()
2015-06-09 22:57:21 +00:00
{
2015-06-10 23:34:14 +00:00
if (m_lockedFile)
return m_lines;
m_lockedFile = HECL::Fopen(m_filepath.c_str(), _S("r+"), LWRITE);
2015-06-09 22:19:59 +00:00
2015-06-09 22:57:21 +00:00
std::string mainString;
char readBuf[1024];
size_t readSz;
2015-06-10 23:34:14 +00:00
while ((readSz = fread(readBuf, 1, 1024, m_lockedFile)))
2015-06-09 22:57:21 +00:00
mainString += std::string(readBuf, readSz);
2015-06-09 22:19:59 +00:00
2015-06-09 22:57:21 +00:00
std::string::const_iterator begin = mainString.begin();
std::string::const_iterator end = mainString.begin();
2015-06-09 22:19:59 +00:00
2015-06-10 23:34:14 +00:00
m_lines.clear();
2015-06-09 22:57:21 +00:00
while (end != mainString.end())
{
std::string::const_iterator origEnd = end;
if (*end == '\0')
break;
else if (CheckNewLineAdvance(end))
2015-06-09 22:19:59 +00:00
{
2015-06-09 22:57:21 +00:00
if (begin != origEnd)
2015-06-10 23:34:14 +00:00
m_lines.push_back(std::string(begin, origEnd));
2015-06-09 22:57:21 +00:00
begin = end;
continue;
2015-06-09 22:19:59 +00:00
}
2015-06-09 22:57:21 +00:00
++end;
}
if (begin != end)
2015-06-10 23:34:14 +00:00
m_lines.push_back(std::string(begin, end));
2015-06-09 22:19:59 +00:00
2015-06-10 23:34:14 +00:00
return m_lines;
2015-06-09 22:57:21 +00:00
}
2015-06-09 22:19:59 +00:00
2015-06-09 22:57:21 +00:00
void Project::ConfigFile::addLine(const std::string& line)
{
2015-06-10 23:34:14 +00:00
if (!checkForLine(line))
m_lines.push_back(line);
2015-06-09 22:57:21 +00:00
}
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
void Project::ConfigFile::removeLine(const std::string& refLine)
{
2015-06-10 23:34:14 +00:00
if (!m_lockedFile)
throw HECL::Exception(_S("Project::ConfigFile::lockAndRead not yet called"));
2015-05-21 02:33:05 +00:00
2015-06-10 23:34:14 +00:00
for (std::vector<std::string>::const_iterator it=m_lines.begin();
it != m_lines.end();
++it)
2015-05-21 02:33:05 +00:00
{
2015-06-10 23:34:14 +00:00
if (!(*it).compare(refLine))
it = m_lines.erase(it);
2015-05-21 02:33:05 +00:00
}
2015-06-09 22:57:21 +00:00
}
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
bool Project::ConfigFile::checkForLine(const std::string& refLine)
{
2015-06-10 23:34:14 +00:00
if (!m_lockedFile)
throw HECL::Exception(_S("Project::ConfigFile::lockAndRead not yet called"));
for (const std::string& line : m_lines)
2015-05-21 02:33:05 +00:00
{
2015-06-09 22:57:21 +00:00
if (!line.compare(refLine))
return true;
2015-05-21 02:33:05 +00:00
}
2015-06-09 22:57:21 +00:00
return false;
}
2015-05-21 02:33:05 +00:00
2015-06-10 23:34:14 +00:00
void Project::ConfigFile::unlockAndDiscard()
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::ConfigFile::lockAndRead not yet called"));
m_lines.clear();
fclose(m_lockedFile);
m_lockedFile = NULL;
}
void Project::ConfigFile::unlockAndCommit()
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::ConfigFile::lockAndRead not yet called"));
fseek(m_lockedFile, 0, SEEK_SET);
#if _WIN32
SetEndOfFile((HANDLE)fileno(m_lockedFile));
#else
ftruncate(fileno(m_lockedFile), 0);
#endif
for (const std::string& line : m_lines)
{
fwrite(line.c_str(), 1, line.size(), m_lockedFile);
fwrite("\n", 1, 1, m_lockedFile);
}
m_lines.clear();
fclose(m_lockedFile);
m_lockedFile = NULL;
}
/**********************************************
* Project::IndexFile
**********************************************/
struct SIndexHeader
{
HECL::FourCC magic;
uint32_t version;
uint32_t entryCount;
uint32_t maxPathLen;
void swapWithNative()
{
version = ToBig(version);
entryCount = ToBig(entryCount);
maxPathLen = ToBig(maxPathLen);
}
};
Project::IndexFile::IndexFile(const Project& project)
: m_project(project)
{
m_filepath = project.m_rootPath.getAbsolutePath() + _S("/.hecl/index");
}
const std::vector<Project::IndexFile::Entry>& Project::IndexFile::lockAndRead()
{
if (m_lockedFile)
return m_entryStore;
m_lockedFile = HECL::Fopen(m_filepath.c_str(), _S("r+"), LWRITE);
SIndexHeader header;
if (fread(&header, 1, sizeof(header), m_lockedFile) != sizeof(header))
return m_entryStore;
header.swapWithNative();
if (header.magic != "HECL")
throw HECL::Exception(_S("unrecognized HECL index"));
if (header.version != 1)
throw HECL::Exception(_S("unrecognized HECL version"));
char* pathBuf = new char[header.maxPathLen];
for (uint32_t e=0 ; e<header.entryCount ; ++e)
{
uint64_t mt;
fread(&mt, 1, 8, m_lockedFile);
mt = ToBig(mt);
uint32_t strLen;
fread(&strLen, 1, 4, m_lockedFile);
strLen = ToBig(strLen);
if (strLen > m_maxPathLen)
m_maxPathLen = strLen;
fread(pathBuf, 1, strLen, m_lockedFile);
std::string pathStr(pathBuf, strLen);
SystemStringView pathView(pathStr);
ProjectPath path(m_project.getProjectRootPath(), pathView.sys_str());
if (m_entryLookup.find(path) == m_entryLookup.end())
{
m_entryStore.push_back(Project::IndexFile::Entry(path, mt));
m_entryLookup[path] = &m_entryStore.back();
}
}
delete[] pathBuf;
}
const std::vector<ProjectPath*> Project::IndexFile::getChangedPaths()
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::IndexFile::lockAndRead not yet called"));
std::vector<ProjectPath*> retval;
for (Project::IndexFile::Entry& ent : m_entryStore)
{
if (ent.m_removed)
continue;
if (ent.m_lastModtime != ent.m_path.getModtime())
retval.push_back(&ent.m_path);
}
return retval;
}
void Project::IndexFile::addOrUpdatePath(const ProjectPath& path)
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::IndexFile::lockAndRead not yet called"));
std::unordered_map<ProjectPath, Entry*>::iterator it = m_entryLookup.find(path);
if (it == m_entryLookup.end())
{
m_entryStore.push_back(Project::IndexFile::Entry(path, path.getModtime()));
m_entryLookup[path] = &m_entryStore.back();
return;
}
(*it).second->m_lastModtime = path.getModtime();
}
void Project::IndexFile::removePath(const ProjectPath& path)
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::IndexFile::lockAndRead not yet called"));
std::unordered_map<ProjectPath, Entry*>::iterator it = m_entryLookup.find(path);
if (it != m_entryLookup.end())
{
(*it).second->m_removed = true;
m_entryLookup.erase(it);
}
}
void Project::IndexFile::unlockAndDiscard()
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::IndexFile::lockAndRead not yet called"));
m_entryLookup.clear();
m_entryStore.clear();
fclose(m_lockedFile);
m_lockedFile = NULL;
}
void Project::IndexFile::unlockAndCommit()
{
if (!m_lockedFile)
throw HECL::Exception(_S("Project::IndexFile::lockAndRead not yet called"));
fseek(m_lockedFile, 0, SEEK_SET);
#if _WIN32
SetEndOfFile((HANDLE)fileno(m_lockedFile));
#else
ftruncate(fileno(m_lockedFile), 0);
#endif
SIndexHeader header =
{
HECL::FourCC("HECL"),
1, (uint32_t)m_entryStore.size(), (uint32_t)m_maxPathLen
};
header.swapWithNative();
fwrite(&header, 1, sizeof(header), m_lockedFile);
for (Project::IndexFile::Entry& ent : m_entryStore)
{
uint64_t mt = ToBig(ent.m_lastModtime.getTs());
fwrite(&mt, 1, 8, m_lockedFile);
size_t strLen = strlen(ent.m_path.getRelativePathUTF8());
uint32_t strLenb = ToBig(strLen);
fwrite(&strLenb, 1, 4, m_lockedFile);
fwrite(ent.m_path.getRelativePathUTF8(), 1, strLen, m_lockedFile);
}
m_entryLookup.clear();
m_entryStore.clear();
fclose(m_lockedFile);
m_lockedFile = NULL;
}
2015-06-09 23:21:45 +00:00
/**********************************************
* Project
**********************************************/
2015-06-10 02:40:03 +00:00
Project::Project(const ProjectRootPath& rootPath)
2015-06-09 22:57:21 +00:00
: m_rootPath(rootPath)
{
/* Stat for existing project directory (must already exist) */
struct stat myStat;
2015-06-10 02:40:03 +00:00
if (HECL::Stat(m_rootPath.getAbsolutePath().c_str(), &myStat))
2015-06-09 22:57:21 +00:00
throw std::error_code(errno, std::system_category());
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
if (!S_ISDIR(myStat.st_mode))
2015-06-10 02:40:03 +00:00
throw std::invalid_argument("provided path must be a directory; '" +
m_rootPath.getAbsolutePathUTF8() + "' isn't");
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
/* Create project directory structure */
2015-06-10 02:40:03 +00:00
HECL::MakeDir(m_rootPath.getAbsolutePath() + _S("/.hecl"));
HECL::MakeDir(m_rootPath.getAbsolutePath() + _S("/.hecl/cooked"));
HECL::MakeDir(m_rootPath.getAbsolutePath() + _S("/.hecl/config"));
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
/* Create or open databases */
}
2015-05-21 02:33:05 +00:00
2015-06-10 02:40:03 +00:00
void Project::registerLogger(FLogger logger)
2015-06-09 22:57:21 +00:00
{
}
2015-06-09 22:19:59 +00:00
2015-06-10 02:40:03 +00:00
bool Project::addPaths(const std::vector<ProjectPath>& paths)
2015-06-09 22:57:21 +00:00
{
}
2015-06-09 22:19:59 +00:00
2015-06-10 02:40:03 +00:00
bool Project::removePaths(const std::vector<ProjectPath>& paths, bool recursive)
2015-06-09 22:57:21 +00:00
{
}
2015-05-21 02:33:05 +00:00
2015-06-09 22:57:21 +00:00
bool Project::addGroup(const HECL::ProjectPath& path)
{
}
2015-05-21 02:33:05 +00:00
2015-06-10 02:40:03 +00:00
bool Project::removeGroup(const ProjectPath& path)
2015-06-09 22:57:21 +00:00
{
}
2015-05-21 02:33:05 +00:00
2015-06-09 23:21:45 +00:00
const std::map<const std::string, const bool>& Project::listDataSpecs()
2015-06-09 22:57:21 +00:00
{
}
2015-05-21 02:33:05 +00:00
2015-06-09 23:21:45 +00:00
bool Project::enableDataSpecs(const std::vector<std::string>& specs)
2015-06-09 22:57:21 +00:00
{
}
2015-06-09 23:21:45 +00:00
bool Project::disableDataSpecs(const std::vector<std::string>& specs)
2015-06-09 22:57:21 +00:00
{
}
2015-06-10 02:40:03 +00:00
bool Project::cookPath(const ProjectPath& path,
2015-06-09 22:57:21 +00:00
std::function<void(std::string&, Cost, unsigned)> feedbackCb,
bool recursive)
{
}
void Project::interruptCook()
{
}
2015-06-10 02:40:03 +00:00
bool Project::cleanPath(const ProjectPath& path, bool recursive)
2015-06-09 22:57:21 +00:00
{
}
2015-05-21 02:33:05 +00:00
2015-06-10 02:40:03 +00:00
Project::PackageDepsgraph Project::buildPackageDepsgraph(const ProjectPath& path)
2015-05-21 02:33:05 +00:00
{
}
}
2015-06-10 02:40:03 +00:00
}