<commit_before>/* © 2010 <NAME>.
 * LBW is licensed under the MIT open source license. See the COPYING
 * file in this distribution for the full text.
 */

#include "globals.h"
#include "VFS.h"
#include "RootVFSNode.h"
#include <typeinfo>

//#define VERBOSE

static Ref<RootVFSNode> root;
static Ref<VFSNode> cwd;

void VFS::SetRoot(const string& path)
{
#if defined VERBOSE
	log("SetRoot(%s)", path.c_str());
#endif
	root = new RootVFSNode(path);
	cwd = (VFSNode*) root;
}

Ref<VFSNode> VFS::GetRootNode()
{
	return (VFSNode*) root;
}

void VFS::SetCWD(VFSNode* cwd, const string& path)
{
	RAIILock locked;

#if defined VERBOSE
	log("SetCWD(%p, %s)", cwd, path.c_str());
#endif

	Ref<VFSNode> node;
	string leaf;
	Resolve(cwd, path, node, leaf);

	::cwd = node->Traverse(leaf);
}

string VFS::GetCWD()
{
	RAIILock locked;

	string s = cwd->GetPath();
	if (s.empty())
		return "/";
	return s;
}

Ref<VFSNode> VFS::GetCWDNode()
{
	return (VFSNode*) cwd;
}

void VFS::Resolve(VFSNode* cwd, const string& path, Ref<VFSNode>& node,
		string& leaf, bool followlink)
{
	RAIILock locked;

#if defined VERBOSE
	//log("Resolve(%s)", path.c_str());
#endif

	string p = path;
	if (p == "/")
	{
		if (!cwd)
			node = ::cwd;
		else
			node = cwd;
		leaf = ".";
	}
	else if (!p.empty() && (p[0] == '/'))
		root->Resolve(p.substr(1), node, leaf, followlink);
	else
	{
		if (!cwd)
			cwd = ::cwd;
		cwd->Resolve(p, node, leaf, followlink);
	}
}

Ref<FD> VFS::OpenDirectory(VFSNode* cwd, const string& path, bool nofollow)
{
	Ref<VFSNode> node;
	string leaf;
	Resolve(cwd, path, node, leaf, !nofollow);<commit_msg>Fixed bug where chdir(NULL, "/") didn't do anything.<commit_after>/* © 2010 <NAME>.
 * LBW is licensed under the MIT open source license. See the COPYING
 * file in this distribution for the full text.
 */

#include "globals.h"
#include "VFS.h"
#include "RootVFSNode.h"
#include <typeinfo>

//#define VERBOSE

static Ref<RootVFSNode> root;
static Ref<VFSNode> cwd;

void VFS::SetRoot(const string& path)
{
#if defined VERBOSE
	log("SetRoot(%s)", path.c_str());
#endif
	root = new RootVFSNode(path);
	cwd = (VFSNode*) root;
}

Ref<VFSNode> VFS::GetRootNode()
{
	return (VFSNode*) root;
}

void VFS::SetCWD(VFSNode* cwd, const string& path)
{
	RAIILock locked;

#if defined VERBOSE
	log("SetCWD(%p, %s)", cwd, path.c_str());
#endif

	Ref<VFSNode> node;
	string leaf;
	Resolve(cwd, path, node, leaf);

	::cwd = node->Traverse(leaf);
}

string VFS::GetCWD()
{
	RAIILock locked;

	string s = cwd->GetPath();
	if (s.empty())
		return "/";
	return s;
}

Ref<VFSNode> VFS::GetCWDNode()
{
	return (VFSNode*) cwd;
}

void VFS::Resolve(VFSNode* cwd, const string& path, Ref<VFSNode>& node,
		string& leaf, bool followlink)
{
	RAIILock locked;

#if defined VERBOSE
	//log("Resolve(%s)", path.c_str());
#endif

	string p = path;
	if (p == "/")
	{
		node = root;
		if (!cwd)
			node = root;
		else
			node = cwd;
		leaf = ".";
	}
	else if (!p.empty() && (p[0] == '/'))
		root->Resolve(p.substr(1), node, leaf, followlink);
	else
	{
		if (!cwd)
			cwd = ::cwd;
		cwd->Resolve(p, node, leaf, followlink);
	}
}

Ref<FD> VFS::OpenDirectory(VFSNode* cwd, const string& path, bool nofollow)
{
	Ref<VFSNode> node;
	string leaf;
	Resolve(cwd, path, node, leaf, !nofollow);
{'fraction_non_alphanumeric': 0.17924237757930397, 'fraction_numerical': 0.0036957191253464737, 'mean_word_length': 4.213483146067416, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3910507', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1357, 'n_words': 349}