winix/functions/ln.cpp

151 lines
3.1 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010-2014, Tomasz Sowa
* All rights reserved.
*
*/
#include "ln.h"
#include "core/misc.h"
#include "functions/functions.h"
namespace Winix
{
namespace Fun
{
Ln::Ln()
{
fun.url = L"ln";
}
bool Ln::HasAccess()
{
return system->HasWriteAccess(*cur->request->dir_tab.back());
}
bool Ln::CreateSymbolicLink(long parent_id, const wchar_t * link_to, const wchar_t * url, bool link_redirect)
{
item.Clear(); // setting the date to now
item.type = Item::symlink;
item.parent_id = parent_id;
item.url = url;
item.link_to = link_to;
item.link_redirect = static_cast<int>(link_redirect);
item.privileges = system->NewFilePrivileges();
functions->SetUser(item);
functions->PrepareUrl(item);
return db->AddItem(item) == WINIX_ERR_OK;
}
bool Ln::CreateSymbolicLink(long parent_id, const std::wstring & link_to, const std::wstring & url, bool link_redirect)
{
return CreateSymbolicLink(parent_id, link_to.c_str(), url.c_str(), link_redirect);
}
void Ln::CreateSymbolicLink(const std::wstring & link_to)
{
long parent_id = cur->request->dir_tab.back()->id;
const std::wstring & url = cur->request->PostVar(L"url");
bool link_redirect = cur->request->IsPostVar(L"makeredirect") ? 1 : 0;
if( CreateSymbolicLink(parent_id, link_to, url, link_redirect) )
log << log3 << "Ln: created a symbolic link to: " << link_to << logend;
else
cur->request->status = WINIX_ERR_PERMISSION_DENIED;
}
/*
!! IMPROVE ME
we need a better public interface
instead of std::vector<Item*> may std::wstring for path?
dirs - directory from we start looking for a destination file
link_to - destination file (name or path - relative or not)
url - suggested url name of the link
*/
bool Ln::CreateHardLink(const std::vector<Item*> & dirs, const std::wstring & link_to, const std::wstring & url)
{
if( dirs.empty() )
return false;
int res = system->FollowAllLinks(dirs, link_to, dir_tab, item, false, false);
if( res == 0 )
{
log << log2 << "Ln: " << link_to << " is a directory (can't create a hard link)" << logend;
return false;
}
else
if( res == 1 )
{
item.type = Item::file;
item.parent_id = dirs.back()->id;
item.url = url;
functions->PrepareUrl(item);
return db->AddHardLink(item) == WINIX_ERR_OK;
}
return false;
}
void Ln::CreateHardLink(const std::wstring & link_to)
{
const std::vector<Item*> & dirs = cur->request->dir_tab;
const std::wstring & url = cur->request->PostVar(L"url");
if( CreateHardLink(dirs, link_to, url) )
log << log3 << "Ln: created a hard link to: " << link_to << logend;
else
cur->request->status = WINIX_ERR_PERMISSION_DENIED;
}
// we do not use notifications for links
void Ln::MakePost()
{
link_to = cur->request->PostVar(L"linkto");
TrimWhite(link_to);
if( link_to.empty() )
return;
int type = Toi(cur->request->PostVar(L"linktype"));
if( type == 0 )
CreateHardLink(link_to);
else
CreateSymbolicLink(link_to);
if( cur->request->status == WINIX_ERR_OK )
system->RedirectTo(item);
}
} // namespace
} // namespace Winix