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-05-11 23:43:26 +08:00
|
|
|
#include <fcntl.h>
|
2015-09-16 06:49:18 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
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"
|
2016-10-19 21:28:06 +08:00
|
|
|
#include "fs_base_close.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"
|
2016-10-21 04:51:49 +08:00
|
|
|
#include "fs_base_lseek.hpp"
|
2016-10-19 21:28:06 +08:00
|
|
|
#include "fs_base_mkdir.hpp"
|
|
|
|
#include "fs_base_open.hpp"
|
2016-10-21 04:51:49 +08:00
|
|
|
#include "fs_base_read.hpp"
|
|
|
|
#include "fs_base_stat.hpp"
|
2016-10-24 11:54:06 +08:00
|
|
|
#include "fs_base_utime.hpp"
|
2016-10-21 04:51:49 +08:00
|
|
|
#include "fs_base_write.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-25 05:22:57 +08:00
|
|
|
#ifndef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_NOATIME
|
|
|
|
# define O_NOATIME 0
|
|
|
|
#endif
|
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
int
|
|
|
|
writen(const int fd,
|
|
|
|
const char *buf,
|
|
|
|
const size_t count)
|
2015-09-16 06:49:18 +08:00
|
|
|
{
|
2016-10-24 11:54:06 +08:00
|
|
|
size_t nleft;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
nleft = count;
|
2017-04-14 00:55:34 +08:00
|
|
|
do
|
2016-10-24 11:54:06 +08:00
|
|
|
{
|
|
|
|
nwritten = fs::write(fd,buf,nleft);
|
2017-04-14 00:55:34 +08:00
|
|
|
if((nwritten == -1) && (errno == EINTR))
|
|
|
|
continue;
|
2016-10-24 11:54:06 +08:00
|
|
|
if(nwritten == -1)
|
2017-04-14 00:55:34 +08:00
|
|
|
return -1;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
nleft -= nwritten;
|
|
|
|
buf += nwritten;
|
|
|
|
}
|
2017-04-14 00:55:34 +08:00
|
|
|
while(nleft > 0);
|
2016-10-24 11:54:06 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
copyfile_rw(const int fdin,
|
|
|
|
const int fdout,
|
|
|
|
const size_t count,
|
|
|
|
const size_t blocksize)
|
|
|
|
{
|
|
|
|
ssize_t nr;
|
|
|
|
ssize_t nw;
|
|
|
|
ssize_t bufsize;
|
|
|
|
size_t totalwritten;
|
|
|
|
vector<char> buf;
|
|
|
|
|
|
|
|
bufsize = (blocksize * 16);
|
|
|
|
buf.resize(bufsize);
|
|
|
|
|
|
|
|
fs::lseek(fdin,0,SEEK_SET);
|
|
|
|
|
|
|
|
totalwritten = 0;
|
|
|
|
while(totalwritten < count)
|
|
|
|
{
|
|
|
|
nr = fs::read(fdin,&buf[0],bufsize);
|
2017-04-14 00:55:34 +08:00
|
|
|
if(nr == 0)
|
|
|
|
return totalwritten;
|
|
|
|
if((nr == -1) && (errno == EINTR))
|
|
|
|
continue;
|
2016-10-24 11:54:06 +08:00
|
|
|
if(nr == -1)
|
2017-04-14 00:55:34 +08:00
|
|
|
return -1;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
nw = writen(fdout,&buf[0],nr);
|
|
|
|
if(nw == -1)
|
|
|
|
return -1;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
totalwritten += nw;
|
|
|
|
}
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2017-04-14 00:55:34 +08:00
|
|
|
return totalwritten;
|
2016-10-24 11:54:06 +08:00
|
|
|
}
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
copydata(const int fdin,
|
|
|
|
const int fdout,
|
|
|
|
const size_t count,
|
|
|
|
const size_t blocksize)
|
|
|
|
{
|
|
|
|
int rv;
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2017-04-11 21:02:05 +08:00
|
|
|
fs::fadvise_willneed(fdin,0,count);
|
|
|
|
fs::fadvise_sequential(fdin,0,count);
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
fs::fallocate(fdout,0,0,count);
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
rv = fs::sendfile(fdin,fdout,count);
|
|
|
|
if((rv == -1) && ((errno == EINVAL) || (errno == ENOSYS)))
|
|
|
|
return ::copyfile_rw(fdin,fdout,count,blocksize);
|
2015-09-16 06:49:18 +08:00
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool
|
|
|
|
ignorable_error(const int err)
|
|
|
|
{
|
|
|
|
switch(err)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
clonefile(const int fdin,
|
|
|
|
const int fdout)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct stat stin;
|
|
|
|
|
2016-10-21 04:51:49 +08:00
|
|
|
rv = fs::fstat(fdin,stin);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
rv = ::copydata(fdin,fdout,stin.st_size,stin.st_blksize);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = fs::attr::copy(fdin,fdout);
|
2016-10-24 11:54:06 +08:00
|
|
|
if((rv == -1) && !ignorable_error(errno))
|
2015-09-16 06:49:18 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = fs::xattr::copy(fdin,fdout);
|
2016-10-24 11:54:06 +08:00
|
|
|
if((rv == -1) && !ignorable_error(errno))
|
2015-09-16 06:49:18 +08:00
|
|
|
return -1;
|
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
rv = fs::fchown_check_on_error(fdout,stin);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
rv = fs::fchmod_check_on_error(fdout,stin);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2016-10-24 11:54:06 +08:00
|
|
|
rv = fs::utime(fdout,stin);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
clonefile(const string &in,
|
|
|
|
const string &out)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int fdin;
|
|
|
|
int fdout;
|
|
|
|
int error;
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
fdin = fs::open(in,O_RDONLY|O_NOFOLLOW);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(fdin == -1)
|
|
|
|
return -1;
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
const int flags = O_CREAT|O_LARGEFILE|O_NOATIME|O_NOFOLLOW|O_TRUNC|O_WRONLY;
|
|
|
|
const mode_t mode = S_IWUSR;
|
|
|
|
fdout = fs::open(out,flags,mode);
|
2015-09-16 06:49:18 +08:00
|
|
|
if(fdout == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = fs::clonefile(fdin,fdout);
|
|
|
|
error = errno;
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
fs::close(fdin);
|
|
|
|
fs::close(fdout);
|
2015-09-16 06:49:18 +08:00
|
|
|
|
|
|
|
errno = error;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|