2015-09-16 06:49:18 +08:00
|
|
|
/*
|
2016-01-15 05:50:22 +08:00
|
|
|
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2015-09-16 06:49:18 +08:00
|
|
|
*/
|
|
|
|
|
2016-09-14 20:36:06 +08:00
|
|
|
#include "errno.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_attr.hpp"
|
2016-10-19 21:28:06 +08:00
|
|
|
#include "fs_base_chmod.hpp"
|
2016-10-21 04:51:49 +08:00
|
|
|
#include "fs_base_chown.hpp"
|
2017-04-07 12:12:57 +08:00
|
|
|
#include "fs_base_fadvise.hpp"
|
2017-04-07 11:35:31 +08:00
|
|
|
#include "fs_base_fallocate.hpp"
|
2018-10-10 22:43:35 +08:00
|
|
|
#include "fs_base_ftruncate.hpp"
|
2016-10-21 04:51:49 +08:00
|
|
|
#include "fs_base_stat.hpp"
|
2016-10-24 11:54:06 +08:00
|
|
|
#include "fs_base_utime.hpp"
|
2018-10-10 22:43:35 +08:00
|
|
|
#include "fs_copy_file_range.hpp"
|
|
|
|
#include "fs_copyfile.hpp"
|
|
|
|
#include "fs_ficlone.hpp"
|
2016-05-11 23:43:26 +08:00
|
|
|
#include "fs_sendfile.hpp"
|
2015-09-16 06:49:18 +08:00
|
|
|
#include "fs_xattr.hpp"
|
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
static
|
|
|
|
int
|
2018-10-03 02:22:37 +08:00
|
|
|
copydata(const int src_fd_,
|
|
|
|
const int dst_fd_,
|
2018-10-10 22:43:35 +08:00
|
|
|
const size_t count_)
|
2016-10-24 11:54:06 +08:00
|
|
|
{
|
|
|
|
int rv;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
fs::fadvise_willneed(src_fd_,0,count_);
|
|
|
|
fs::fadvise_sequential(src_fd_,0,count_);
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
fs::fallocate(dst_fd_,0,0,count_);
|
2018-10-10 22:43:35 +08:00
|
|
|
fs::ftruncate(dst_fd_,count_);
|
|
|
|
|
|
|
|
rv = fs::ficlone(src_fd_,dst_fd_);
|
|
|
|
if((rv != -1) || ((rv == -1) && (errno != EOPNOTSUPP)))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = fs::copy_file_range(src_fd_,dst_fd_,count_);
|
|
|
|
if((rv != -1) || ((rv == -1) && (errno != EOPNOTSUPP)))
|
|
|
|
return rv;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::sendfile(src_fd_,dst_fd_,count_);
|
2018-10-10 22:43:35 +08:00
|
|
|
if((rv != -1) || ((rv == -1) && (errno != EINVAL) && (errno != ENOSYS)))
|
|
|
|
return rv;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2018-10-10 22:43:35 +08:00
|
|
|
return fs::copyfile(src_fd_,dst_fd_);
|
2016-10-24 11:54:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool
|
2018-10-03 02:22:37 +08:00
|
|
|
ignorable_error(const int err_)
|
2016-10-24 11:54:06 +08:00
|
|
|
{
|
2018-10-03 02:22:37 +08:00
|
|
|
switch(err_)
|
2016-10-24 11:54:06 +08:00
|
|
|
{
|
|
|
|
case ENOTTY:
|
|
|
|
case ENOTSUP:
|
2015-09-16 06:49:18 +08:00
|
|
|
#if ENOTSUP != EOPNOTSUPP
|
2016-10-24 11:54:06 +08:00
|
|
|
case EOPNOTSUPP:
|
2015-09-16 06:49:18 +08:00
|
|
|
#endif
|
2016-10-24 11:54:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
namespace fs
|
|
|
|
{
|
2015-09-16 06:49:18 +08:00
|
|
|
int
|
2018-10-03 02:22:37 +08:00
|
|
|
clonefile(const int src_fd_,
|
|
|
|
const int dst_fd_)
|
2015-09-16 06:49:18 +08:00
|
|
|
{
|
|
|
|
int rv;
|
2018-10-03 02:22:37 +08:00
|
|
|
struct stat src_st;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::fstat(src_fd_,src_st);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-10 22:43:35 +08:00
|
|
|
rv = ::copydata(src_fd_,dst_fd_,src_st.st_size);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::attr::copy(src_fd_,dst_fd_);
|
2016-10-24 11:54:06 +08:00
|
|
|
if((rv == -1) && !ignorable_error(errno))
|
2015-09-16 06:49:18 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::xattr::copy(src_fd_,dst_fd_);
|
2016-10-24 11:54:06 +08:00
|
|
|
if((rv == -1) && !ignorable_error(errno))
|
2015-09-16 06:49:18 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::fchown_check_on_error(dst_fd_,src_st);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::fchmod_check_on_error(dst_fd_,src_st);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-03 02:22:37 +08:00
|
|
|
rv = fs::utime(dst_fd_,src_st);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|