we can create links (hard links, symbolic links) now
added winix functions: ln winix function 'default' can be used without redirecting now added new tickets types: TypeProgress, TypeString, TypeMultistring, TypeImages, TypeFiles now tickets are combined with files added winix functions: showtickets fixed mountpoints: when the default root mount was created its parameter table was empty and it caused accessing to a non-existing objects fixed logger: modifiers (log1, log2, log3) were incorrectly treated added modifier: log4 (debug info) now we are moving threads to a new plugin 'thread' created directory: plugins/thread (not finished yet) git-svn-id: svn://ttmath.org/publicrep/winix/trunk@704 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
344
functions/cp.cpp
344
functions/cp.cpp
@@ -7,7 +7,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include "cp.h"
|
||||
#include "core/misc.h"
|
||||
#include "functions.h"
|
||||
@@ -22,11 +21,17 @@ Cp::Cp()
|
||||
}
|
||||
|
||||
|
||||
bool Cp::CpCheckAccessFrom()
|
||||
bool Cp::HasAccess()
|
||||
{
|
||||
return CheckAccessFrom();
|
||||
}
|
||||
|
||||
|
||||
bool Cp::CheckAccessFrom()
|
||||
{
|
||||
if( request->is_item )
|
||||
{
|
||||
if( !system->HasReadAccess(request->item) )
|
||||
if( !system->HasReadAccess(request->item) || request->item.type == Item::symlink )
|
||||
{
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
return false;
|
||||
@@ -39,22 +44,15 @@ bool Cp::CpCheckAccessFrom()
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Cp::CheckAccessTo(long dir_id)
|
||||
bool Cp::CheckAccessTo()
|
||||
{
|
||||
Item * pdir = system->dirs.GetDir(dir_id);
|
||||
|
||||
if( !pdir )
|
||||
{
|
||||
request->status = WINIX_ERR_INCORRECT_DIR;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !system->HasReadExecAccessToPath(dir_id) || !system->HasWriteAccess(*pdir) )
|
||||
if( dir_tab.empty() ||
|
||||
!system->HasReadExecAccessToPath(dir_tab) ||
|
||||
!system->HasWriteAccess(*dir_tab.back()) )
|
||||
{
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
return false;
|
||||
@@ -65,157 +63,230 @@ return true;
|
||||
|
||||
|
||||
|
||||
bool Cp::ParseDir(long & dir_id, std::wstring & dir, std::wstring & file)
|
||||
bool Cp::ParseDir()
|
||||
{
|
||||
int res = system->dirs.AnalyzePath(request->PostVar(L"to"), dir_id, dir, file);
|
||||
const std::wstring & new_dir = request->PostVar(L"to");
|
||||
int res = system->dirs.FollowLink(request->dir_tab, new_dir, dir_tab, file);
|
||||
|
||||
if( res == 1 )
|
||||
if( res == 3 )
|
||||
request->status = WINIX_ERR_NO_ROOT_DIR;
|
||||
else
|
||||
if( res != 0 )
|
||||
if( res != 0 && res != 1 )
|
||||
request->status = WINIX_ERR_INCORRECT_DIR;
|
||||
|
||||
return res == 0;
|
||||
return res == 0 || res == 1;
|
||||
}
|
||||
|
||||
|
||||
void Cp::CpStaticFile(Item & item)
|
||||
bool Cp::CopyStaticFile(const std::wstring & from, const std::wstring & to)
|
||||
{
|
||||
bool res1 = system->MakeFilePath(item, old_path);
|
||||
bool res2 = system->CreateNewFile(item);
|
||||
bool res3 = system->MakeFilePath(item, mv_new_path, false, true, config->upload_dirs_chmod);
|
||||
if( from == to )
|
||||
{
|
||||
log << log3 << "Cp: the same path to a static file: " << to << logend;
|
||||
return true;
|
||||
}
|
||||
|
||||
if( !res1 || !res2 || !res3 )
|
||||
if( ::CopyFile(from, to) )
|
||||
{
|
||||
log << log2 << "Cp: copied a static file from: " << from << ", to: " << to << logend;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Cp: can't copy a file from: " << from << ", to: " << to << logend;
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::CopyStaticFile(Item & item)
|
||||
{
|
||||
bool res1, res2, res3, res4, res5;
|
||||
|
||||
res1 = system->MakeFilePath(item, old_path, false);
|
||||
res2 = !item.has_thumb || system->MakeFilePath(item, old_path_thumb, true);
|
||||
res3 = system->CreateNewFile(item);
|
||||
res4 = system->MakeFilePath(item, new_path, false, true, config->upload_dirs_chmod);
|
||||
res5 = !item.has_thumb || system->MakeFilePath(item, new_path_thumb, true, true, config->upload_dirs_chmod);
|
||||
|
||||
if( !res1 || !res2 || !res3 || !res4 || !res5 )
|
||||
{
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
return;
|
||||
}
|
||||
|
||||
if( CopyFile(old_path, mv_new_path) )
|
||||
{
|
||||
log << log1 << "Cp: copied static file from: " << old_path << ", to: " << mv_new_path << logend;
|
||||
request->status = db->EditFileById(item, item.id);
|
||||
}
|
||||
else
|
||||
if( CopyStaticFile(old_path, new_path) )
|
||||
{
|
||||
log << log1 << "Cp: can't copy a file from: " << old_path << ", to: " << mv_new_path << logend;
|
||||
request->status = WINIX_ERR_PERMISSION_DENIED;
|
||||
request->status = db->EditFileById(item, item.id);
|
||||
|
||||
if( item.has_thumb )
|
||||
CopyStaticFile(old_path_thumb, new_path_thumb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Cp::CpSetNewAttributes(Item & item)
|
||||
|
||||
void Cp::SetNewAttributes(Item & item)
|
||||
{
|
||||
item.user_id = cp_new_user;
|
||||
item.group_id = cp_new_group;
|
||||
item.user_id = new_user;
|
||||
item.group_id = new_group;
|
||||
item.SetDateModifyToNow();
|
||||
}
|
||||
|
||||
|
||||
void Cp::CpItem(Item & item, long dst_dir_id)
|
||||
void Cp::CopyFile(Item & item, long dst_dir_id)
|
||||
{
|
||||
if( !system->HasReadAccess(item) )
|
||||
return; // !! w przyszlosci bedziemy dodawac komunikaty do specjalnej tablicy (narazie nie zaimplementowane)
|
||||
if( !preserve_attr )
|
||||
SetNewAttributes(item);
|
||||
|
||||
item.parent_id = dst_dir_id;
|
||||
|
||||
if( !cp_preserve_attr )
|
||||
CpSetNewAttributes(item);
|
||||
|
||||
request->status = system->AddFile(item);
|
||||
item.parent_id = dst_dir_id;
|
||||
request->status = db->AddItem(item);
|
||||
|
||||
if( request->status == WINIX_ERR_OK )
|
||||
{
|
||||
if( item.file_type != WINIX_ITEM_FILETYPE_NONE )
|
||||
CpStaticFile(item);
|
||||
CopyStaticFile(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Cp::CopyFileOrSymlink(Item & item, long dst_dir_id)
|
||||
{
|
||||
if( !system->HasReadAccess(item) )
|
||||
return; // !! w przyszlosci bedziemy dodawac komunikaty do specjalnej tablicy (narazie nie zaimplementowane)
|
||||
|
||||
if( item.type == Item::symlink && follow_symlinks )
|
||||
{
|
||||
if( system->dirs.CreateDirTab(item.parent_id, symlink_dir_tab) )
|
||||
{
|
||||
int res = system->FollowAllLinks(symlink_dir_tab, item.link_to, symlink_dir_tab, item);
|
||||
|
||||
if( res == 0 )
|
||||
CopyDirTree(*symlink_dir_tab.back(), dst_dir_id);
|
||||
else
|
||||
if( res == 1 )
|
||||
CopyFile(item, dst_dir_id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CopyFile(item, dst_dir_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Cp::CpPrepare()
|
||||
void Cp::Prepare()
|
||||
{
|
||||
cp_iq.SetAll(true, false);
|
||||
cp_iq.WhereType(Item::file);
|
||||
iq.SetAll(true, false);
|
||||
iq.WhereType(Item::dir, false);
|
||||
|
||||
cp_new_user = -1;
|
||||
cp_new_group = -1;
|
||||
new_user = -1;
|
||||
new_group = dir_tab.back()->group_id;
|
||||
|
||||
if( request->session->puser )
|
||||
cp_new_user = request->session->puser->id;
|
||||
new_user = request->session->puser->id;
|
||||
|
||||
Item * pdir = system->dirs.GetDir(mv_dir_id);
|
||||
|
||||
if( pdir )
|
||||
cp_new_group = pdir->group_id;
|
||||
loop_checker.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::CpFilesInDir(const Item & dir, long dst_dir_id)
|
||||
{
|
||||
cp_iq.WhereParentId(dir.id);
|
||||
db->GetItems(request->item_tab, cp_iq);
|
||||
|
||||
for(size_t i=0 ; i<request->item_tab.size() ; ++i)
|
||||
CpItem(request->item_tab[i], dst_dir_id);
|
||||
void Cp::CopyFilesInDir(const Item & dir, long dst_dir_id)
|
||||
{
|
||||
iq.WhereParentId(dir.id);
|
||||
db->GetItems(item_tab, iq);
|
||||
|
||||
for(size_t i=0 ; i<item_tab.size() ; ++i)
|
||||
CopyFileOrSymlink(item_tab[i], dst_dir_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::CpContentOfDir(const Item & item, long dst_dir_id)
|
||||
void Cp::CopyDirContentTree(const Item & dir, long dst_dir_id)
|
||||
{
|
||||
DirContainer::ParentIterator i = system->dirs.FindFirstParent(item.id);
|
||||
DirContainer::ParentIterator i = system->dirs.FindFirstChild(dir.id);
|
||||
|
||||
// go through all directories
|
||||
for( ; i != system->dirs.ParentEnd() ; i = system->dirs.NextParent(i) )
|
||||
CpDir(*(i->second), dst_dir_id);
|
||||
for( ; i != system->dirs.ParentEnd() ; i = system->dirs.NextChild(i) )
|
||||
CopyDirTree(*(i->second), dst_dir_id);
|
||||
|
||||
CpFilesInDir(item, dst_dir_id);
|
||||
CopyFilesInDir(dir, dst_dir_id);
|
||||
}
|
||||
|
||||
|
||||
bool Cp::WasThisDir(const Item & dir)
|
||||
{
|
||||
for(size_t i=0 ; i<loop_checker.size() ; ++i)
|
||||
if( loop_checker[i] == dir.id )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// we shouldn't change 'item' because we have references to our app.dirs objects
|
||||
long Cp::CpDir(const Item & item, long dst_dir_id)
|
||||
long Cp::CopyDirTree(const Item & dir, long dst_dir_id)
|
||||
{
|
||||
cp_temp = item;
|
||||
cp_temp.parent_id = dst_dir_id;
|
||||
|
||||
if( !mv_file.empty() )
|
||||
if( WasThisDir(dir) )
|
||||
{
|
||||
cp_temp.url = mv_file;
|
||||
mv_file.clear();
|
||||
functions->PrepareUrl(cp_temp);
|
||||
log << log1 << "Cp: a loop between directories found (created by a symlink), "
|
||||
<< "dir_id: " << dir.id << ", dir_url: " << dir.url << logend;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( !cp_preserve_attr )
|
||||
CpSetNewAttributes(cp_temp);
|
||||
loop_checker.push_back(dir.id);
|
||||
temp = dir;
|
||||
temp.parent_id = dst_dir_id;
|
||||
|
||||
if( cp_remove_defaults )
|
||||
cp_temp.default_item = -1;
|
||||
if( !file.empty() )
|
||||
{
|
||||
temp.url = file;
|
||||
functions->PrepareUrl(temp);
|
||||
file.clear();
|
||||
}
|
||||
|
||||
request->status = system->dirs.AddDirectory(cp_temp);
|
||||
long new_dir_id = cp_temp.id; // remember the new dir_id
|
||||
if( !preserve_attr )
|
||||
SetNewAttributes(temp);
|
||||
|
||||
if( system->HasReadExecAccess(item) )
|
||||
CpContentOfDir(item, cp_temp.id);
|
||||
if( remove_defaults )
|
||||
{
|
||||
temp.link_to.clear();
|
||||
temp.link_redirect = 0;
|
||||
}
|
||||
|
||||
request->status = system->dirs.AddDirectory(temp);
|
||||
loop_checker.push_back(temp.id);
|
||||
|
||||
// remember the new dir_id because temp can be changed
|
||||
// this method is called in recurrences
|
||||
long new_dir_id = temp.id;
|
||||
|
||||
if( system->HasReadExecAccess(dir) )
|
||||
CopyDirContentTree(dir, temp.id);
|
||||
|
||||
return new_dir_id; // and return it
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool Cp::IsTheSameFile(const Item & item)
|
||||
{
|
||||
if( mv_file.empty() )
|
||||
if( file.empty() )
|
||||
{
|
||||
if( item.parent_id == mv_dir_id )
|
||||
if( item.parent_id == dir_tab.back()->id )
|
||||
return true; // nothing to do
|
||||
}
|
||||
else
|
||||
{
|
||||
if( item.parent_id == mv_dir_id && item.url == mv_file )
|
||||
if( item.parent_id == dir_tab.back()->id && item.url == file )
|
||||
return true; // nothing to do
|
||||
}
|
||||
|
||||
@@ -226,101 +297,94 @@ return false;
|
||||
|
||||
|
||||
// here 'item' can be changed in place
|
||||
void Cp::CpItemCheck(Item & item, bool redirect)
|
||||
void Cp::PostCopyFile(Item & item, bool redirect)
|
||||
{
|
||||
if( IsTheSameFile(item) )
|
||||
return;
|
||||
|
||||
if( !mv_file.empty() )
|
||||
if( !file.empty() )
|
||||
{
|
||||
item.url = mv_file;
|
||||
item.url = file;
|
||||
functions->PrepareUrl(item);
|
||||
file.clear();
|
||||
}
|
||||
|
||||
CpItem(item, mv_dir_id);
|
||||
CopyFileOrSymlink(item, dir_tab.back()->id);
|
||||
|
||||
if( request->status==WINIX_ERR_OK && redirect )
|
||||
if( request->status == WINIX_ERR_OK && redirect )
|
||||
system->RedirectTo(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::CpContentOfDirCheck(const Item & item, bool redirect)
|
||||
|
||||
|
||||
void Cp::PostCopyDirContent(const Item & dir, bool redirect)
|
||||
{
|
||||
if( !mv_file.empty() )
|
||||
if( !file.empty() )
|
||||
{
|
||||
request->status = WINIX_ERR_INCORRECT_DIR;
|
||||
return;
|
||||
}
|
||||
|
||||
if( mv_dir_id == item.id )
|
||||
if( dir_tab.back()->id == dir.id )
|
||||
return; // nothing to do
|
||||
|
||||
if( system->dirs.HasParent(mv_dir_id, item.id) )
|
||||
{
|
||||
log << log1 << "Content: cannot copy directory to inside it" << logend;
|
||||
request->status = WINIX_ERR_INCORRECT_DIR;
|
||||
return;
|
||||
}
|
||||
CopyDirContentTree(dir, dir_tab.back()->id);
|
||||
|
||||
CpContentOfDir(item, mv_dir_id);
|
||||
|
||||
if( request->status==WINIX_ERR_OK && redirect )
|
||||
system->RedirectTo(mv_dir_id);
|
||||
if( request->status == WINIX_ERR_OK && redirect )
|
||||
system->RedirectTo(dir_tab.back()->id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::CpDirCheck(const Item & item, bool redirect)
|
||||
void Cp::PostCopyDir(const Item & dir, bool redirect)
|
||||
{
|
||||
if( mv_file.empty() && mv_dir_id == item.id )
|
||||
long dir_id = dir_tab.back()->id;
|
||||
|
||||
if( file.empty() && dir_id == dir.id )
|
||||
return; // nothing to do
|
||||
|
||||
if( mv_dir_id == item.id || system->dirs.HasParent(mv_dir_id, item.id) )
|
||||
{
|
||||
log << log1 << "Content: cannot copy directory to inside it" << logend;
|
||||
request->status = WINIX_ERR_INCORRECT_DIR;
|
||||
return;
|
||||
}
|
||||
long new_dir_id = CopyDirTree(dir, dir_id);
|
||||
|
||||
long new_dir_id = CpDir(item, mv_dir_id);
|
||||
|
||||
if( request->status==WINIX_ERR_OK && redirect )
|
||||
if( new_dir_id != -1 && request->status == WINIX_ERR_OK && redirect )
|
||||
system->RedirectTo(new_dir_id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::MakePost()
|
||||
void Cp::Clear()
|
||||
{
|
||||
if( CpCheckAccessFrom() &&
|
||||
ParseDir(mv_dir_id, mv_dir, mv_file) &&
|
||||
CheckAccessTo(mv_dir_id) )
|
||||
{
|
||||
CpPrepare();
|
||||
cp_preserve_attr = request->IsPostVar(L"preserveattr");
|
||||
|
||||
if( request->is_item )
|
||||
{
|
||||
CpItemCheck(request->item);
|
||||
}
|
||||
else
|
||||
{
|
||||
cp_remove_defaults = request->IsPostVar(L"removedefaults");
|
||||
|
||||
if( request->IsPostVar(L"onlycontent") )
|
||||
CpContentOfDirCheck(*request->dir_tab.back());
|
||||
else
|
||||
CpDirCheck(*request->dir_tab.back());
|
||||
}
|
||||
}
|
||||
loop_checker.clear();
|
||||
dir_tab.clear();
|
||||
item_tab.clear();
|
||||
symlink_dir_tab.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cp::MakeGet()
|
||||
void Cp::MakePost()
|
||||
{
|
||||
CpCheckAccessFrom();
|
||||
if( ParseDir() && CheckAccessTo() )
|
||||
{
|
||||
Prepare();
|
||||
|
||||
preserve_attr = request->IsPostVar(L"preserveattr");
|
||||
remove_defaults = request->IsPostVar(L"removedefaults");
|
||||
follow_symlinks = request->IsPostVar(L"followsymlinks");
|
||||
|
||||
if( request->is_item )
|
||||
{
|
||||
PostCopyFile(request->item);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( request->IsPostVar(L"onlycontent") )
|
||||
PostCopyDirContent(*request->dir_tab.back());
|
||||
else
|
||||
PostCopyDir(*request->dir_tab.back());
|
||||
}
|
||||
|
||||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user