2014-12-16 18:07:37 +00:00
|
|
|
#include "fs_spec.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2015-01-18 19:28:05 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/param.h>
|
2014-12-16 18:07:37 +00:00
|
|
|
|
|
|
|
namespace OS {
|
|
|
|
|
2015-01-15 19:11:55 +00:00
|
|
|
const int RootPathID = 2;
|
|
|
|
|
2014-12-16 18:07:37 +00:00
|
|
|
std::deque<FSSpecManager::Entry> FSSpecManager::_pathQueue;
|
|
|
|
|
|
|
|
void FSSpecManager::Init()
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
2015-01-18 19:28:05 +00:00
|
|
|
// "/" is item #2. 0 and 1 are reserved.
|
|
|
|
//can't just call IDForPath because that calls... Init();
|
2014-12-16 18:07:37 +00:00
|
|
|
static std::string RootPath("/");
|
|
|
|
|
|
|
|
std::hash<std::string> hasher;
|
|
|
|
size_t hash = hasher(RootPath);
|
|
|
|
_pathQueue.emplace_back(FSSpecManager::Entry(RootPath, hash));
|
|
|
|
assert(_pathQueue.size() == 1);
|
|
|
|
initialized = true;
|
|
|
|
}
|
2015-01-18 19:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t FSSpecManager::IDForCWD()
|
|
|
|
{
|
|
|
|
char buffer[MAXPATHLEN];
|
|
|
|
char *cp;
|
2014-12-16 18:07:37 +00:00
|
|
|
|
2015-01-18 19:28:05 +00:00
|
|
|
cp = getcwd(buffer, sizeof(buffer));
|
|
|
|
if (cp < 0) return 0;
|
|
|
|
|
|
|
|
std::string path(cp);
|
|
|
|
|
|
|
|
return FSSpecManager::IDForPath(std::move(path), true);
|
2014-12-16 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 19:28:05 +00:00
|
|
|
|
2014-12-16 18:07:37 +00:00
|
|
|
int32_t FSSpecManager::IDForPath(const std::string &path, bool insert)
|
|
|
|
{
|
2015-01-07 19:44:26 +00:00
|
|
|
if (path.empty()) return -1;
|
|
|
|
if (path.back() != '/')
|
|
|
|
{
|
|
|
|
std::string tmp(path);
|
|
|
|
return IDForPath(std::move(tmp), insert);
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:07:37 +00:00
|
|
|
/*
|
|
|
|
char buffer[PATH_MAX + 1];
|
|
|
|
|
|
|
|
char *cp = realpath(path.c_str(), buffer);
|
|
|
|
if (!cp) return -1;
|
|
|
|
|
|
|
|
std::string s(cp);
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::hash<std::string> hasher;
|
|
|
|
size_t hash = hasher(path);
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
2015-01-15 19:11:55 +00:00
|
|
|
// this only works well for a small number of directories,
|
|
|
|
// which should be the common case.
|
|
|
|
int i = RootPathID;
|
2014-12-16 18:07:37 +00:00
|
|
|
for (const auto &e : _pathQueue)
|
|
|
|
{
|
|
|
|
if (e.hash == hash && e.path == path) return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!insert) return -1;
|
|
|
|
|
|
|
|
_pathQueue.emplace_back(FSSpecManager::Entry(path, hash));
|
2015-01-16 00:48:03 +00:00
|
|
|
return _pathQueue.size() + RootPathID - 1;
|
2014-12-16 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t FSSpecManager::IDForPath(std::string &&path, bool insert)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
char buffer[PATH_MAX + 1];
|
|
|
|
|
|
|
|
char *cp = realpath(path.c_str(), buffer);
|
|
|
|
if (!cp) return -1;
|
|
|
|
|
|
|
|
std::string s(cp);
|
|
|
|
*/
|
|
|
|
|
2015-01-07 19:44:26 +00:00
|
|
|
// trailing / is required.
|
|
|
|
if (path.empty()) return -1;
|
|
|
|
if (path.back() != '/') path.push_back('/');
|
|
|
|
|
2014-12-16 18:07:37 +00:00
|
|
|
std::hash<std::string> hasher;
|
|
|
|
size_t hash = hasher(path);
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
2015-01-15 19:11:55 +00:00
|
|
|
int i = RootPathID;
|
2014-12-16 18:07:37 +00:00
|
|
|
for (const auto &e : _pathQueue)
|
|
|
|
{
|
|
|
|
if (e.hash == hash && e.path == path) return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!insert) return -1;
|
|
|
|
|
|
|
|
_pathQueue.emplace_back(FSSpecManager::Entry(std::move(path), hash));
|
2015-01-16 00:48:03 +00:00
|
|
|
return _pathQueue.size() + RootPathID - 1;
|
2014-12-16 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::string &FSSpecManager::PathForID(int32_t id)
|
|
|
|
{
|
|
|
|
static std::string NullString;
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
2015-01-15 19:11:55 +00:00
|
|
|
id -= RootPathID;
|
|
|
|
if (id < 0) return NullString;
|
|
|
|
if (id >= _pathQueue.size()) return NullString;
|
2014-12-16 18:07:37 +00:00
|
|
|
|
2015-01-15 19:11:55 +00:00
|
|
|
return _pathQueue[id].path;
|
2014-12-16 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FSSpecManager::ExpandPath(const std::string &path, int32_t id)
|
|
|
|
{
|
2015-01-07 19:44:26 +00:00
|
|
|
if (path.length() && path.front() == '/') return path;
|
2014-12-16 18:07:37 +00:00
|
|
|
if (id == 0) return path;
|
|
|
|
|
|
|
|
const std::string &dir = PathForID(id);
|
|
|
|
|
|
|
|
if (dir.empty()) return dir;
|
|
|
|
return dir + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|