⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfile.cpp

📁 GiPS是一个面向数据密集型应用的分布式文件系统
💻 CPP
字号:
#include "GFile.h"
#include <iostream>
using namespace std;
using namespace Cluster;


/* 
*	Function of GFnode
*/

std::string
GFNode::name()
{
	return _name;
}

GFNode::GFNode(const std::string & name, GFNode* parent) :
	_name(name), _parent(parent)
{
}

bool
GFNode::isDirectory()
{
	return false;
}

GFNode*
GFNode::locate(const ::std::string & name)
{
	return NULL;
}

std::string
GFNode::blockTableName()
{
	return string("");
}

void
GFNode::addChild(GFNode* child)
{
}

string
GFNode::absolutePath()
{
	if (!this->_parent)
		return this->_name;

	GFNode* parent = this->_parent;
	string name = this->_name;
	while (parent->_parent)
	{
		name = parent->_name + "/" + name;
		parent = parent->_parent;
	}

	return ("/"+name);
}

FileMap*
GFNode::children()
{
	return NULL;
}

vector<GFNode*>
GFNode::allChildren()
{
	vector<GFNode*> children;
	return children;
}

void
GFNode::deleteFile(string filename)
{
}

void
GFNode::destory()
{
}


/* 
*	Function of GFile
*/


GFile::GFile(const std::string & name, GFNode* parent, const std::string & blockTableName)	:	GFNode(name, parent), _blockTableName(blockTableName)
{
}

std::string
GFile::blockTableName()
{
	return _blockTableName;
}


/* 
*	Function of GDirectory
*/


GDirectory::GDirectory(const std::string & name, GFNode* parent) : GFNode(name, parent)
{
	this->m_children = new FileMap();
}

bool
GDirectory::isDirectory()
{
	return true;
}

bool
GDirectory::createFile(string filename, int fileType, string* blockTableName)
{
//	cout << "create file " << filename << endl;
	if (filename.size() < 2)
		return false;

	GFNode* parent;
	string name;
	int head = filename.find_last_of('/');
	if (head == 0) {
		parent = this;
		name = filename.substr(1, filename.size()-1);
	}
	else {
		parent = this->locate(filename.substr(0, head));
		name = filename.substr(head+1, filename.size()-1);
	}
	
	if (!parent)
		return false;
//	cout << "find parent name is " << parent->name() << endl;

//	cout << "name is " << name << endl;
	if (fileType) {
		GDirectory* dir = new GDirectory(name, parent);
		parent->addChild(dir);
		cout << "create diectory" << endl;
	} else {
		GFile* file = new GFile(name, parent, *blockTableName);
		parent->addChild(file);
		cout << "create file" << endl;
	}

	return true;
}

void
GDirectory::addChild(GFNode* child)
{
	m_children->insert(FileMap::value_type(child->name(), child));
}

GFNode*
GDirectory::locate(const ::std::string & name)
{
//	cout << name << "__" << endl;
	int head = 0;
	while (name.at(head) == '/') {
		head++;
		if (head == name.length())
			return NULL;
	}

	int length = head+1;
	while ((length < name.size()) && (name.at(length) != '/') ) {
		length++;
	}

	string childName(name, head, length-head);
//	cout << " childName _" << childName << "_" << endl;

	FileMap::iterator it = m_children->find(childName);
	if (it == m_children->end())
		return NULL;
	else {
//		cout << "find child" << endl;
		GFNode* child = it->second;
//		cout << length << " " << name.length() << endl;
		if (length == name.length())
			return child;

		string cname(name, length, name.length()-length);
		return child->locate(cname);
	}
}

FileMap*
GDirectory::children()
{
	return this->m_children;
}

void travelThroughDirectory(GFNode *parent, vector<GFNode*>* paths)
{
	FileMap* _children = parent->children();
	for (FileMap::iterator it = _children->begin(); it != _children->end(); it++)
	{
		paths->push_back(it->second);
		if (it->second->isDirectory())
			travelThroughDirectory(it->second, paths);
	}
}

vector<GFNode*>
GDirectory::allChildren()
{
	vector<GFNode*> paths;
	for (FileMap::iterator it = this->m_children->begin(); it != this->m_children->end(); it++)
	{
		paths.push_back(it->second);
		if (it->second->isDirectory())
			travelThroughDirectory(it->second, &paths);
	}

	return paths;
}

void
GDirectory::deleteFile(string name)
{
	int head = 0;
	while (name.at(head) == '/') {
		head++;
		if (head == name.length())
			return;
	}

	int length = head+1;
	while ((length < name.size()) && (name.at(length) != '/') ) {
		length++;
	}

	string childName(name, head, length-head);
	FileMap::iterator it = m_children->find(childName);
	if (it != m_children->end()) {
		if (length == name.length()) {
			cout << "delete file: " << it->second->absolutePath() << endl;
			m_children->erase(it);
			return;
		}

//		cout << "find parent: " << childName << endl;
		GFNode* child = it->second;
		string cname(name, length, name.length()-length);
		child->deleteFile(cname);
	} else
		cout << "No File " << endl;
}

void
GDirectory::destory()
{
	while(m_children->size() > 0) {
		FileMap::iterator cit = m_children->begin();
		if (cit->second->isDirectory())
			cit->second->destory();
		else
			m_children->erase(cit);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -