321 lines
5.7 KiB
C++
Executable File
321 lines
5.7 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2009-2011, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "mounts.h"
|
|
#include "request.h"
|
|
#include "log.h"
|
|
#include "db/db.h"
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
|
|
Mounts::Mounts()
|
|
{
|
|
pmount = &empty_mount;
|
|
skip_static = false;
|
|
}
|
|
|
|
|
|
void Mounts::SkipStaticDirs(bool skip)
|
|
{
|
|
skip_static = skip;
|
|
}
|
|
|
|
|
|
|
|
void Mounts::CreateMountType()
|
|
{
|
|
mount_type_cms = AddMountType(L"cms");
|
|
mount_type_static = AddMountType(L"static");
|
|
}
|
|
|
|
|
|
void Mounts::CreateMountFs()
|
|
{
|
|
mount_fs_simplefs = AddMountFs(L"simplefs");
|
|
mount_fs_hashfs = AddMountFs(L"hashfs");
|
|
}
|
|
|
|
|
|
void Mounts::CreateMountPar()
|
|
{
|
|
mount_par_page = AddMountPar(L"page");
|
|
mount_par_thumb_size = AddMountPar(L"thumb_size");
|
|
mount_par_thumb_mode = AddMountPar(L"thumb_mode");
|
|
//mount_par_thread = AddMountPar(L"thread");
|
|
//mount_par_createthread_on = AddMountPar(L"createthread_on");
|
|
mount_par_only_root_remove = AddMountPar(L"only_root_remove");
|
|
mount_par_emacs_on = AddMountPar(L"emacs_on");
|
|
mount_par_mkdir_on = AddMountPar(L"mkdir_on");
|
|
mount_par_app = AddMountPar(L"app");
|
|
mount_par_html_template = AddMountPar(L"html_template");
|
|
mount_par_static = AddMountPar(L"static");
|
|
mount_par_css = AddMountPar(L"css");
|
|
}
|
|
|
|
|
|
void Mounts::CreateMounts()
|
|
{
|
|
CreateMountType();
|
|
CreateMountFs();
|
|
CreateMountPar();
|
|
|
|
plugin.Call(WINIX_ADD_MOUNTS);
|
|
|
|
empty_mount.param.resize(mount_par_tab.size());
|
|
empty_mount.ClearParams();
|
|
}
|
|
|
|
|
|
|
|
void Mounts::SetDirs(Dirs * pdirs)
|
|
{
|
|
dirs = pdirs;
|
|
}
|
|
|
|
|
|
void Mounts::SetDb(Db * pdb)
|
|
{
|
|
db = pdb;
|
|
}
|
|
|
|
void Mounts::SetCur(Cur * pcur)
|
|
{
|
|
cur = pcur;
|
|
}
|
|
|
|
|
|
|
|
int Mounts::AddMountType(const wchar_t * type)
|
|
{
|
|
mount_type_tab.push_back(type);
|
|
|
|
return static_cast<int>(mount_type_tab.size()) - 1;
|
|
}
|
|
|
|
|
|
int Mounts::AddMountType(const std::wstring & type)
|
|
{
|
|
return AddMountType(type.c_str());
|
|
}
|
|
|
|
|
|
int Mounts::AddMountFs(const wchar_t * fs)
|
|
{
|
|
mount_fs_tab.push_back(fs);
|
|
|
|
return static_cast<int>(mount_fs_tab.size()) - 1;
|
|
}
|
|
|
|
|
|
const std::wstring & Mounts::GetMountType(int id)
|
|
{
|
|
if( id < 0 || id >= (int)mount_type_tab.size() )
|
|
return empty_str;
|
|
|
|
return mount_type_tab[id];
|
|
}
|
|
|
|
|
|
int Mounts::FindMountType(const std::wstring & type)
|
|
{
|
|
for(size_t i=0 ; i<mount_type_tab.size() ; ++i)
|
|
if( mount_type_tab[i] == type )
|
|
return (int)i;
|
|
|
|
return -1;
|
|
}
|
|
|
|
|
|
|
|
int Mounts::AddMountFs(const std::wstring & fs)
|
|
{
|
|
return AddMountFs(fs.c_str());
|
|
}
|
|
|
|
|
|
const std::wstring & Mounts::GetMountFs(int id)
|
|
{
|
|
if( id < 0 || id >= (int)mount_fs_tab.size() )
|
|
return empty_str;
|
|
|
|
return mount_fs_tab[id];
|
|
}
|
|
|
|
|
|
int Mounts::AddMountPar(const wchar_t * par)
|
|
{
|
|
mount_par_tab.push_back(par);
|
|
|
|
return static_cast<int>(mount_par_tab.size()) - 1;
|
|
}
|
|
|
|
|
|
|
|
int Mounts::AddMountPar(const std::wstring & par)
|
|
{
|
|
return AddMountPar(par.c_str());
|
|
}
|
|
|
|
|
|
const std::wstring & Mounts::GetMountPar(int id)
|
|
{
|
|
if( id < 0 || id >= (int)mount_par_tab.size() )
|
|
return empty_str;
|
|
|
|
return mount_par_tab[id];
|
|
}
|
|
|
|
|
|
|
|
// reading from 'mounts'
|
|
void Mounts::ReadMounts(const std::wstring & mounts)
|
|
{
|
|
mount_parser.SkipStaticDirs(skip_static);
|
|
mount_parser.SetStaticMountId(mount_type_static);
|
|
mount_parser.SetDirs(dirs);
|
|
mount_parser.SetMountTypeTab(mount_type_tab);
|
|
mount_parser.SetMountFsTab(mount_fs_tab);
|
|
mount_parser.SetMountParTab(mount_par_tab);
|
|
|
|
mount_parser.Parse(mounts, mount_tab);
|
|
|
|
CalcCurMount();
|
|
plugin.Call(WINIX_FSTAB_CHANGED);
|
|
}
|
|
|
|
|
|
|
|
// reading from /etc/fstab
|
|
Error Mounts::ReadMounts()
|
|
{
|
|
static std::wstring file = L"fstab";
|
|
|
|
Item * etc = dirs->GetEtcDir();
|
|
|
|
if( !etc )
|
|
{
|
|
log << log1 << "M: there is no /etc directory" << logend;
|
|
return WINIX_ERR_NO_ITEM;
|
|
}
|
|
|
|
Item fstab;
|
|
Error err = db->GetItem(etc->id, file, fstab);
|
|
|
|
if( err == WINIX_ERR_NO_ITEM )
|
|
{
|
|
log << log1 << "M: there is no /etc/fstab file" << logend;
|
|
return err;
|
|
}
|
|
|
|
if( err != WINIX_ERR_OK )
|
|
{
|
|
log << log1 << "M: cannot read /etc/fstab" << logend;
|
|
return err;
|
|
}
|
|
|
|
ReadMounts(fstab.content);
|
|
|
|
return WINIX_ERR_OK;
|
|
}
|
|
|
|
|
|
void Mounts::MountCmsForRoot()
|
|
{
|
|
Mount mount;
|
|
mount.type = MountTypeCms();
|
|
mount.fs = MountFsSimplefs();
|
|
|
|
Item * proot = dirs->GetRootDir();
|
|
|
|
if( proot )
|
|
mount.dir_id = proot->id;
|
|
else
|
|
{
|
|
mount.dir_id = -1;
|
|
log << log1 << "M: there is no a root dir" << logend;
|
|
}
|
|
|
|
mount.param.resize(mount_par_tab.size());
|
|
mount.ClearParams();
|
|
|
|
std::pair<MountTab::iterator, bool> res = mount_tab.insert( std::make_pair(mount.dir_id, mount) );
|
|
pmount = &(res.first->second);
|
|
}
|
|
|
|
|
|
|
|
|
|
Mount * Mounts::CalcCurMount()
|
|
{
|
|
std::vector<Item*>::reverse_iterator i;
|
|
|
|
pmount = &empty_mount;
|
|
|
|
// when the program starts (when the dir_tab is empty()
|
|
// we don't want to call MountCmsForRoot()
|
|
if( cur->request->dir_tab.empty() )
|
|
return pmount;
|
|
|
|
for(i = cur->request->dir_tab.rbegin() ; i!=cur->request->dir_tab.rend() ; ++i)
|
|
{
|
|
std::map<long, Mount>::iterator m = mount_tab.find( (*i)->id );
|
|
|
|
if( m != mount_tab.end() )
|
|
{
|
|
pmount = &(m->second);
|
|
log << log2 << "M: current mount point is: " << GetMountType(pmount->type)
|
|
<< ", fs: " << GetMountFs(pmount->fs) << logend;
|
|
return pmount;
|
|
}
|
|
}
|
|
|
|
// if nothing was found
|
|
// we assume that 'cms' mount point is used
|
|
MountCmsForRoot();
|
|
log << log2 << "M: current mount point is: " << GetMountType(pmount->type) << " (default)"
|
|
<< ", fs: " << GetMountFs(pmount->fs) << logend;
|
|
|
|
return pmount;
|
|
}
|
|
|
|
|
|
// can return null pointer
|
|
// and we don't assume cms as a default mount point if nothing is found
|
|
Mount * Mounts::CalcMount(long dir_id)
|
|
{
|
|
while( true )
|
|
{
|
|
Item * pdir = dirs->GetDir(dir_id);
|
|
|
|
if( !pdir )
|
|
return 0;
|
|
|
|
std::map<long, Mount>::iterator m = mount_tab.find( pdir->id );
|
|
|
|
if( m != mount_tab.end() )
|
|
return &(m->second);
|
|
|
|
dir_id = pdir->parent_id;
|
|
}
|
|
}
|
|
|
|
|
|
const Mounts::MountTab * Mounts::GetMountTab()
|
|
{
|
|
return &mount_tab;
|
|
}
|
|
|
|
|
|
Mount * Mounts::GetEmptyMount()
|
|
{
|
|
return &empty_mount;
|
|
}
|