2010-08-10 18:12:50 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2008-2014, Tomasz Sowa
|
2010-08-10 18:12:50 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-08-10 18:12:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include "mv.h"
|
2010-08-12 21:10:12 +02:00
|
|
|
#include "functions.h"
|
2011-09-13 08:08:34 +02:00
|
|
|
#include "core/plugin.h"
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
namespace Fun
|
|
|
|
{
|
|
|
|
|
|
|
|
Mv::Mv()
|
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
fun.url = L"mv";
|
2011-01-05 22:24:11 +01:00
|
|
|
follow_symlinks = false;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
Prepare();
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// !! CHECK ME
|
|
|
|
// check if everywhere correct messages are sent (prepare_to, modified item/dir)
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
bool Mv::HasAccess()
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( cur->request->is_item )
|
|
|
|
{
|
|
|
|
if( !system->CanRemoveRenameChild(*cur->request->dir_tab.back(), cur->request->item.user_id) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool only_content = (cur->request->IsParam(L"c") || cur->request->IsPostVar(L"c"));
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !CheckAccessFromToDir(*cur->request->dir_tab.back(), only_content) )
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
bool Mv::CheckAccessFromToDir(const Item & dir, bool only_content)
|
|
|
|
{
|
|
|
|
if( dir.parent_id == -1 )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !only_content )
|
|
|
|
{
|
|
|
|
// the root directory cannot be moved anywhere
|
2010-08-10 18:12:50 +02:00
|
|
|
return false;
|
2012-01-25 00:03:36 +01:00
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
else
|
2012-01-25 00:03:36 +01:00
|
|
|
if( only_content )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
// sticky bit for a specified child will be checked later
|
|
|
|
if( !system->HasWriteAccess(dir) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Item * last_but_one_dir = system->dirs.GetDir(dir.parent_id);
|
|
|
|
|
|
|
|
if( !last_but_one_dir )
|
|
|
|
// ops, there is no a parent dir
|
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !system->CanRemoveRenameChild(*last_but_one_dir, dir.user_id) )
|
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// !! IMPROVE ME: may a better name?
|
|
|
|
void Mv::Prepare()
|
|
|
|
{
|
|
|
|
content_dir_iq.SetAll(false, false);
|
|
|
|
content_dir_iq.sel_parent_id = true;
|
|
|
|
content_dir_iq.sel_type = true;
|
|
|
|
content_dir_iq.sel_url = true;
|
|
|
|
content_dir_iq.sel_file = true;
|
|
|
|
content_dir_iq.sel_user_id = true;
|
|
|
|
content_dir_iq.sel_group_id = true;
|
|
|
|
content_dir_iq.sel_privileges = true;
|
|
|
|
content_dir_iq.sel_meta = true;
|
|
|
|
|
|
|
|
files_iq.SetAll(false, false);
|
|
|
|
files_iq.sel_parent_id = true;
|
|
|
|
files_iq.sel_type = true;
|
|
|
|
files_iq.sel_url = true;
|
|
|
|
files_iq.sel_file = true;
|
|
|
|
files_iq.sel_user_id = true;
|
|
|
|
files_iq.sel_group_id = true;
|
|
|
|
files_iq.sel_privileges = true;
|
|
|
|
files_iq.sel_meta = true;
|
|
|
|
files_iq.WhereType(Item::dir, false);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
void Mv::Clear()
|
|
|
|
{
|
|
|
|
out_dir_tab.clear();
|
|
|
|
out_item.Clear();
|
|
|
|
out_filename.clear();
|
|
|
|
files_item_tab.clear();
|
|
|
|
item_tab.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
bool Mv::ParseDirCheckLastName()
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( out_has_file )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << log1 << "Mv: incorrent path" << logend;
|
|
|
|
slog << logerror << T("mv_incorrect_path") << logend;
|
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
Item * dir = system->dirs.GetDir(out_filename, out_dir_tab.back()->id);
|
|
|
|
|
|
|
|
if( dir )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
out_dir_tab.push_back(dir);
|
|
|
|
out_filename.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( db->GetItem(out_dir_tab.back()->id, out_filename, out_item) == WINIX_ERR_OK )
|
|
|
|
{
|
|
|
|
out_has_file = true;
|
|
|
|
out_filename.clear();
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
bool Mv::ParseDir(const std::wstring & dst_path, bool check_access)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( dst_path.empty() )
|
2010-08-10 18:12:50 +02:00
|
|
|
return false;
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
// first we should remove the last name from the dst_path
|
|
|
|
// (it may not exist in current file system and FollowAllLinks will fail)
|
|
|
|
size_t last_slash = dst_path.find_last_of('/');
|
|
|
|
out_path = dst_path;
|
|
|
|
out_filename.clear();
|
|
|
|
|
|
|
|
if( last_slash != std::wstring::npos && last_slash + 1 < dst_path.size() )
|
|
|
|
{
|
|
|
|
out_path.erase(last_slash + 1); // leaving the slash at the end
|
|
|
|
out_filename = dst_path.c_str() + last_slash + 1;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
int res = system->FollowAllLinks(cur->request->dir_tab, out_path, out_dir_tab, out_item, false, false, check_access);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( res != 0 && res != 1 )
|
|
|
|
{
|
|
|
|
slog << logerror << T("mv_incorrect_dst_path") << logend;
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
out_has_file = (res == 1);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !out_filename.empty() )
|
|
|
|
return ParseDirCheckLastName();
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
bool Mv::CanRemoveRenameChild(const Item & child)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
Item * parent_dir = system->dirs.GetDir(child.parent_id);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !parent_dir || !system->CanRemoveRenameChild(*parent_dir, child.user_id) )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: permission denied to: " << child.url << logend;
|
|
|
|
slog << logerror << T("mv_permission_denied_to") << ": " << child.url << logend;
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return true;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
bool Mv::MoveStaticFile(const std::wstring & from, const std::wstring & to)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2011-01-05 22:24:11 +01:00
|
|
|
if( from == to )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2011-01-05 22:24:11 +01:00
|
|
|
log << log3 << "Mv: the same path to a static file: " << to << " (skipped)" << logend;
|
|
|
|
return true;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
if( RenameFile(from, to) )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2011-01-05 22:24:11 +01:00
|
|
|
log << log2 << "Mv: moved static file from: " << from << ", to: " << to << logend;
|
|
|
|
return true;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << log1 << "Mv: cannot move a static file from: " << from << ", to: " << to << logend;
|
|
|
|
slog << logerror << T("internal_error") << logend;
|
2011-01-05 22:24:11 +01:00
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
void Mv::MoveStaticFile(Item & item)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2011-01-05 22:24:11 +01:00
|
|
|
bool res1, res2, res3, res4, res5;
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
res1 = system->MakeFilePath(item, old_static_path, false);
|
|
|
|
res2 = !item.has_thumb || system->MakeFilePath(item, old_static_thumb_path, true);
|
2011-01-05 22:24:11 +01:00
|
|
|
res3 = system->CreateNewFile(item);
|
2012-01-25 00:03:36 +01:00
|
|
|
res4 = system->MakeFilePath(item, new_static_path, false, true, config->upload_dirs_chmod);
|
|
|
|
res5 = !item.has_thumb || system->MakeFilePath(item, new_static_thumb_path, true, true, config->upload_dirs_chmod);
|
2011-01-05 22:24:11 +01:00
|
|
|
|
|
|
|
if( !res1 || !res2 || !res3 || !res4 || !res5 )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << log1 << "Mv: cannot create a static path" << logend;
|
|
|
|
slog << logerror << T("internal_error") << logend;
|
2011-01-05 22:24:11 +01:00
|
|
|
return;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( MoveStaticFile(old_static_path, new_static_path) )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( db->EditFileById(item, item.id) != WINIX_ERR_OK )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: cannot move static file (database problem)" << logend;
|
|
|
|
slog << logerror << T("internal_error") << logend;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
if( item.has_thumb )
|
2012-01-25 00:03:36 +01:00
|
|
|
MoveStaticFile(old_static_thumb_path, new_static_thumb_path);
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
void Mv::MoveFilesPrepareTreeGo(const Item & src_dir)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
DirContainer::ParentIterator i = system->dirs.FindFirstChild(src_dir.id);
|
|
|
|
|
|
|
|
// go through all directories
|
|
|
|
for( ; i != system->dirs.ParentEnd() ; i = system->dirs.NextChild(i) )
|
2012-02-27 18:43:59 +01:00
|
|
|
MoveFilesPrepareTreeGo(*(i->second));
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
files_iq.WhereParentId(src_dir.id);
|
|
|
|
db->GetItems(files_item_tab, files_iq);
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<files_item_tab.size() ; ++i)
|
|
|
|
plugin.Call(WINIX_FILE_PREPARE_TO_MOVE, &files_item_tab[i]);
|
|
|
|
}
|
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
void Mv::MoveFilesPrepareTree(const Item & src_dir)
|
|
|
|
{
|
|
|
|
// we only calling plugins here
|
|
|
|
// so if there is no WINIX_FILE_PREPARE_TO_MOVE message
|
|
|
|
// we can immediately return and the database will not be bothered
|
|
|
|
if( plugin.HasMessage(WINIX_FILE_PREPARE_TO_MOVE) )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-02-27 18:43:59 +01:00
|
|
|
MoveFilesPrepareTreeGo(src_dir);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
void Mv::MoveFilesTree(const Item & dir)
|
|
|
|
{
|
|
|
|
DirContainer::ParentIterator i = system->dirs.FindFirstChild(dir.id);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// go through all directories
|
|
|
|
for( ; i != system->dirs.ParentEnd() ; i = system->dirs.NextChild(i) )
|
|
|
|
MoveFilesTree(*(i->second));
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
files_iq.WhereParentId(dir.id);
|
|
|
|
db->GetItems(files_item_tab, files_iq);
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<files_item_tab.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( files_item_tab[i].file_type != WINIX_ITEM_FILETYPE_NONE )
|
|
|
|
{
|
|
|
|
plugin.Call(WINIX_FILE_PREPARE_TO_MOVE, &files_item_tab[i]);
|
|
|
|
MoveStaticFile(files_item_tab[i]);
|
|
|
|
plugin.Call(WINIX_FILE_MOVED, &files_item_tab[i]);
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
// private
|
|
|
|
// uses: out_dir_tab, out_filename
|
|
|
|
bool Mv::MoveDir(Item & src_dir, std::vector<Item*> & dst_dir_tab, const std::wstring & dst_name)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
long dst_dir_id = dst_dir_tab.back()->id;
|
|
|
|
old_url = src_dir.url;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( dst_dir_id == src_dir.id || system->dirs.HasParent(dst_dir_id, src_dir.id) )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << log1 << "Mv: cannot move directory to inside it" << logend;
|
|
|
|
slog << logerror << T("mv_cannot_move_to_inside");
|
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
MoveFilesPrepareTree(src_dir);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !system->dirs.ChangeParent(src_dir.id, dst_dir_id) )
|
|
|
|
return false;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
src_dir.parent_id = dst_dir_id;
|
|
|
|
old_url = src_dir.url;
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !dst_name.empty() )
|
|
|
|
{
|
|
|
|
src_dir.url = dst_name;
|
|
|
|
functions->PrepareUrl(src_dir);
|
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
Error status = db->EditParentUrlById(src_dir, src_dir.id);
|
|
|
|
|
|
|
|
if( status == WINIX_ERR_OK )
|
|
|
|
{
|
|
|
|
log << log3 << "Mv: directory: " << old_url << " was moved to: ";
|
|
|
|
system->dirs.LogDir(dst_dir_tab);
|
|
|
|
log << src_dir.url << logend;
|
|
|
|
|
|
|
|
MoveFilesTree(src_dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// public
|
|
|
|
bool Mv::MoveDir(Item & src_dir, long dst_dir_id, const std::wstring & new_url, bool check_access)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
bool res = MoveDir2(src_dir, dst_dir_id, new_url, check_access);
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
return res;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
bool Mv::MoveDir2(Item & src_dir, long dst_dir_id, const std::wstring & new_url, bool check_access)
|
2011-09-13 08:08:34 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_dir.type != Item::dir )
|
|
|
|
{
|
|
|
|
log << "Mv: a directory required" << logend;
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_dir.parent_id == -1 )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: the root directory cannot be moved anywhere" << logend;
|
|
|
|
slog << logerror << T("mv_cant_move_root_dir") << logend;
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( check_access && !CanRemoveRenameChild(src_dir) )
|
|
|
|
return false;
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_dir.id == dst_dir_id )
|
|
|
|
{
|
|
|
|
if( new_url.empty() || src_dir.url == new_url )
|
|
|
|
return true; // the same directory -- there is nothing to do
|
|
|
|
}
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !system->dirs.CreateDirTab(dst_dir_id, out_dir_tab) )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: incorrect directory" << logend;
|
|
|
|
slog << logerror << T("mv_incorrect_dir") << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MoveDir(src_dir, out_dir_tab, new_url);
|
2011-09-13 08:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
// public
|
|
|
|
bool Mv::MoveDir(Item & src_dir, const std::wstring & dst_path, bool check_access)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
bool res = MoveDir2(src_dir, dst_path, check_access);
|
|
|
|
Clear();
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return res;
|
|
|
|
}
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
bool Mv::MoveDir2(Item & src_dir, const std::wstring & dst_path, bool check_access)
|
|
|
|
{
|
|
|
|
if( src_dir.type != Item::dir )
|
2011-09-13 08:08:34 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << "Mv: a directory required" << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( src_dir.parent_id == -1 )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: the root directory cannot be moved anywhere" << logend;
|
|
|
|
slog << logerror << T("mv_cant_move_root_dir") << logend;
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( check_access && !CanRemoveRenameChild(src_dir) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !ParseDir(dst_path, check_access) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( out_has_file )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: directory can be moved only to a directory" << logend;
|
|
|
|
slog << logerror << T("mv_dir_can_be_moved_to_dir") << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( src_dir.id == out_dir_tab.back()->id )
|
|
|
|
{
|
|
|
|
if( out_filename.empty() || src_dir.url == out_filename )
|
|
|
|
return true; // the same directory -- there is nothing to do
|
2011-09-13 08:08:34 +02:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
return MoveDir(src_dir, out_dir_tab, out_filename);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
// private
|
|
|
|
bool Mv::MoveFileOrSymlink(Item & src_file, std::vector<Item*> & dst_dir_tab, const std::wstring & new_url)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
plugin.Call(WINIX_FILE_PREPARE_TO_MOVE, &src_file);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
old_url = src_file.url;
|
|
|
|
|
|
|
|
if( !new_url.empty() )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
src_file.url = new_url;
|
|
|
|
functions->PrepareUrl(src_file);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
src_file.parent_id = dst_dir_tab.back()->id;
|
|
|
|
Error status = db->EditParentUrlById(src_file, src_file.id);
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( status == WINIX_ERR_OK )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_file.type == Item::file )
|
|
|
|
log << log3 << "Mv: file: ";
|
|
|
|
else
|
|
|
|
log << log3 << "Mv: symlink: ";
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
log << old_url << " was moved to: ";
|
|
|
|
system->dirs.LogDir(dst_dir_tab);
|
|
|
|
log << src_file.url << logend;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_file.file_type != WINIX_ITEM_FILETYPE_NONE )
|
|
|
|
MoveStaticFile(src_file);
|
|
|
|
|
|
|
|
plugin.Call(WINIX_FILE_MOVED, &src_file);
|
|
|
|
return true;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
// public
|
|
|
|
bool Mv::MoveFileOrSymlink(Item & src_file, long dst_dir_id, const std::wstring & new_url, bool check_access)
|
|
|
|
{
|
|
|
|
bool res = MoveFileOrSymlink2(src_file, dst_dir_id, new_url, check_access);
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private
|
|
|
|
bool Mv::MoveFileOrSymlink2(Item & src_file, long dst_dir_id, const std::wstring & new_url, bool check_access)
|
|
|
|
{
|
|
|
|
if( src_file.type == Item::dir )
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << "Mv: a file/symlink required" << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( check_access && !CanRemoveRenameChild(src_file) )
|
|
|
|
return false;
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_file.parent_id == dst_dir_id )
|
|
|
|
{
|
|
|
|
if( new_url.empty() || src_file.url == new_url )
|
|
|
|
return true; // the same file -- there is nothing to do
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
if( !system->dirs.CreateDirTab(dst_dir_id, out_dir_tab) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return MoveFileOrSymlink(src_file, out_dir_tab, new_url);
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// public
|
|
|
|
bool Mv::MoveFileOrSymlink(Item & src_file, const std::wstring & dst_path, bool check_access)
|
|
|
|
{
|
|
|
|
bool res = MoveFileOrSymlink2(src_file, dst_path, check_access);
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
bool Mv::MoveFileOrSymlink2(Item & src_file, const std::wstring & dst_path, bool check_access)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_file.type == Item::dir )
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << "Mv: a file/symlink required" << logend;
|
|
|
|
return false;
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
if( check_access && !CanRemoveRenameChild(src_file) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !ParseDir(dst_path, check_access) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( src_file.parent_id == out_dir_tab.back()->id )
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
// actually out_filename is here empty
|
|
|
|
// because ParseDir() have been read it to out_item
|
|
|
|
if( out_filename.empty() || src_file.url == out_filename )
|
|
|
|
return true; // the same file -- there is nothing to do
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
return MoveFileOrSymlink(src_file, out_dir_tab, out_filename);
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
void Mv::MoveAllFilesFromDir(Item & src_dir, std::vector<Item*> & dst_dir_tab, bool check_access)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
content_dir_iq.WhereParentId(src_dir.id);
|
|
|
|
db->GetItems(item_tab, content_dir_iq);
|
|
|
|
out_filename.clear();
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<item_tab.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( check_access && !system->CanRemoveRenameChild(src_dir, item_tab[i].user_id) )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: permission denied to: " << src_dir.url << logend;
|
|
|
|
slog << logerror << T("mv_permission_denied_to") << ": " << src_dir.url << logend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( item_tab[i].type == Item::dir )
|
|
|
|
MoveDir(item_tab[i], dst_dir_tab, out_filename);
|
|
|
|
else
|
|
|
|
MoveFileOrSymlink(item_tab[i], dst_dir_tab, out_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// public
|
|
|
|
void Mv::MoveDirContent(Item & src_dir, long dst_dir_id, bool check_access)
|
|
|
|
{
|
|
|
|
MoveDirContent2(src_dir, dst_dir_id, check_access);
|
|
|
|
Clear();
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
void Mv::MoveDirContent2(Item & src_dir, long dst_dir_id, bool check_access)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
if( src_dir.type != Item::dir )
|
|
|
|
{
|
|
|
|
log << "Mv: a directory required" << logend;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( src_dir.parent_id == -1 )
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
log << log1 << "Mv: the root directory cannot be moved anywhere" << logend;
|
|
|
|
slog << logerror << T("mv_cant_move_root_dir") << logend;
|
2011-01-05 22:24:11 +01:00
|
|
|
return;
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( !system->dirs.CreateDirTab(dst_dir_id, out_dir_tab) )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: incorrect directory" << logend;
|
|
|
|
slog << logerror << T("mv_incorrect_dir") << logend;
|
|
|
|
return;
|
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
MoveAllFilesFromDir(src_dir, out_dir_tab, check_access);
|
2011-01-05 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// public
|
|
|
|
void Mv::MoveDirContent(Item & src_dir, const std::wstring & dst_dir, bool check_access)
|
2011-01-05 22:24:11 +01:00
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
MoveDirContent2(src_dir, dst_dir, check_access);
|
|
|
|
Clear();
|
|
|
|
}
|
2011-01-05 22:24:11 +01:00
|
|
|
|
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
// private
|
|
|
|
void Mv::MoveDirContent2(Item & src_dir, const std::wstring & dst_dir, bool check_access)
|
|
|
|
{
|
|
|
|
if( !ParseDir(dst_dir, check_access) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( out_has_file || !out_filename.empty() )
|
|
|
|
{
|
|
|
|
log << log1 << "Mv: directory content can be moved only to a directory" << logend;
|
|
|
|
slog << logerror << T("mv_dir_content_can_be_moved_to_dir") << logend;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MoveDirContent2(src_dir, out_dir_tab.back()->id, check_access);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Mv::MakePost()
|
|
|
|
{
|
2012-01-25 00:03:36 +01:00
|
|
|
const std::wstring & dst_path = cur->request->PostVar(L"dst_path");
|
|
|
|
bool ok = true;
|
2011-01-05 22:24:11 +01:00
|
|
|
|
2012-01-25 00:03:36 +01:00
|
|
|
if( cur->request->is_item )
|
|
|
|
{
|
|
|
|
ok = MoveFileOrSymlink(cur->request->item, dst_path, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( cur->request->IsParam(L"c") || cur->request->IsPostVar(L"c") )
|
|
|
|
MoveDirContent(*cur->request->dir_tab.back(), dst_path, true);
|
2010-08-10 18:12:50 +02:00
|
|
|
else
|
2012-01-25 00:03:36 +01:00
|
|
|
ok = MoveDir(*cur->request->dir_tab.back(), dst_path, true);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
2012-01-25 00:03:36 +01:00
|
|
|
|
|
|
|
if( ok )
|
|
|
|
system->RedirectToLastItem();
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|