261 lines
4.5 KiB
C++
Executable File
261 lines
4.5 KiB
C++
Executable File
/*
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2009, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "dircontainer.h"
|
|
#include "log.h"
|
|
|
|
|
|
std::string DirContainer::dir_etc = "etc";
|
|
|
|
|
|
DirContainer::DirContainer()
|
|
{
|
|
is_root = false;
|
|
is_etc = false;
|
|
}
|
|
|
|
|
|
|
|
DirContainer::Iterator DirContainer::GetRoot()
|
|
{
|
|
if( !is_root )
|
|
return table.end();
|
|
|
|
return root_iter;
|
|
}
|
|
|
|
|
|
DirContainer::Iterator DirContainer::GetEtc()
|
|
{
|
|
if( !is_etc )
|
|
return table.end();
|
|
|
|
return etc_iter;
|
|
}
|
|
|
|
|
|
DirContainer::Iterator DirContainer::Begin()
|
|
{
|
|
return table.begin();
|
|
}
|
|
|
|
|
|
DirContainer::Iterator DirContainer::End()
|
|
{
|
|
return table.end();
|
|
}
|
|
|
|
|
|
DirContainer::SizeType DirContainer::Size()
|
|
{
|
|
return table.size();
|
|
}
|
|
|
|
bool DirContainer::Empty()
|
|
{
|
|
return table.empty();
|
|
}
|
|
|
|
|
|
// looking for '/etc'
|
|
// 'root' is found beforehand
|
|
// CheckSpecialFolder() may not find everything (when the first is a special folder and then the root)
|
|
void DirContainer::FindSpecialFolders()
|
|
{
|
|
is_etc = false;
|
|
|
|
if( !is_root )
|
|
return;
|
|
|
|
DirContainer::ParentIterator i = FindFirstParent(root_iter->id);
|
|
|
|
for( ; i!=ParentEnd() ; i = NextParent(i) )
|
|
{
|
|
if( i->second->url == dir_etc )
|
|
{
|
|
is_etc = true;
|
|
etc_iter = i->second;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// this is used with PushBack() method
|
|
void DirContainer::CheckSpecialFolder(const Item & item, Iterator iter)
|
|
{
|
|
if( item.parent_id == -1 )
|
|
{
|
|
is_root = true;
|
|
root_iter = iter;
|
|
}
|
|
|
|
if( !is_root )
|
|
return;
|
|
|
|
if( item.parent_id==root_iter->id && item.url==dir_etc )
|
|
{
|
|
is_etc = true;
|
|
etc_iter = iter;
|
|
log << log1 << "DirCont: added special folder: /etc" << logend;
|
|
}
|
|
}
|
|
|
|
|
|
DirContainer::Iterator DirContainer::PushBack(const Item & item)
|
|
{
|
|
if( item.parent_id == -1 && is_root )
|
|
{
|
|
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
|
|
return root_iter;
|
|
}
|
|
|
|
Iterator last_iter = table.insert(table.end(), item);
|
|
CheckSpecialFolder(item, last_iter);
|
|
|
|
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
|
|
|
|
table_id.insert( std::make_pair(last_iter->id, last_iter) );
|
|
table_parent.insert( std::make_pair(last_iter->parent_id, last_iter) );
|
|
|
|
log << log3 << "DirCont: added indexes to dir, id: " << last_iter->id << ", parent_id: " << last_iter->parent_id << logend;
|
|
|
|
return last_iter;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DirContainer::Clear()
|
|
{
|
|
table.clear();
|
|
table_id.clear();
|
|
table_parent.clear();
|
|
is_root = false;
|
|
}
|
|
|
|
|
|
|
|
|
|
DirContainer::Iterator DirContainer::FindId(long id)
|
|
{
|
|
TableId::iterator i = table_id.find(id);
|
|
|
|
if( i == table_id.end() )
|
|
return table.end();
|
|
|
|
return i->second;
|
|
}
|
|
|
|
|
|
|
|
DirContainer::ParentIterator DirContainer::ParentBegin()
|
|
{
|
|
return table_parent.begin();
|
|
}
|
|
|
|
|
|
|
|
DirContainer::ParentIterator DirContainer::ParentEnd()
|
|
{
|
|
return table_parent.end();
|
|
}
|
|
|
|
|
|
DirContainer::ParentSizeType DirContainer::ParentSize()
|
|
{
|
|
return table_parent.size();
|
|
}
|
|
|
|
|
|
bool DirContainer::ParentEmpty()
|
|
{
|
|
return table_parent.empty();
|
|
}
|
|
|
|
|
|
DirContainer::ParentIterator DirContainer::FindFirstParent(long parent)
|
|
{
|
|
ParentIterator i = table_parent.lower_bound(parent);
|
|
|
|
if( i == table_parent.end() || i->first != parent )
|
|
return table_parent.end();
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
DirContainer::ParentIterator DirContainer::NextParent(ParentIterator i)
|
|
{
|
|
if( i == table_parent.end() )
|
|
return table_parent.end();
|
|
|
|
long parent = i->first;
|
|
++i;
|
|
|
|
if( i == table_parent.end() || i->first != parent )
|
|
return table_parent.end();
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DirContainer::DelById(long id)
|
|
{
|
|
TableId::iterator i = table_id.find(id);
|
|
|
|
if( i == table_id.end() )
|
|
{
|
|
log << log1 << "DirCont: delete: there is no directory with id: " << id << logend;
|
|
return false;
|
|
}
|
|
|
|
long parent_id = i->second->parent_id;
|
|
TableParent::iterator z = table_parent.lower_bound(parent_id);
|
|
bool found = false;
|
|
|
|
for( ; z != table_parent.end() && z->first == parent_id ; ++z )
|
|
{
|
|
if( z->second == i->second )
|
|
{
|
|
log << log2 << "DirCont: deleted directory id: " << id << ", url: " << i->second->url;
|
|
|
|
if( i->second->parent_id == -1 )
|
|
{
|
|
log << log2 << " (root directory)";
|
|
is_root = false;
|
|
}
|
|
|
|
log << log2 << logend;
|
|
|
|
table.erase(i->second);
|
|
|
|
log << log3 << "DirCont: deleted indexes into directory id: " << id << logend;
|
|
table_id.erase(i);
|
|
table_parent.erase(z);
|
|
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if( !found )
|
|
{
|
|
log << log1 << "DirCont: can't find an index_parent into directory id: " << id << ", url: " << i->second->url << " (deleting skipped)" << logend;
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|