mirror of
https://github.com/trapexit/mergerfs.git
synced 2024-12-11 19:53:39 +08:00
538467b86d
Also added unit tests. Should have done separately but found a number of bugs.
97 lines
2.6 KiB
Plaintext
97 lines
2.6 KiB
Plaintext
/*
|
|
ISC License
|
|
|
|
Copyright (c) 2018, 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.
|
|
*/
|
|
|
|
#ifndef _GNU_SOURCE
|
|
# define _GNU_SOURCE
|
|
#endif
|
|
|
|
#include "errno.hpp"
|
|
|
|
#include <cstdint>
|
|
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
namespace l
|
|
{
|
|
static
|
|
int64_t
|
|
copy_file_range_(int src_fd_,
|
|
int64_t *src_off_,
|
|
int tgt_fd_,
|
|
int64_t *tgt_off_,
|
|
const uint64_t len_,
|
|
const unsigned int flags_)
|
|
{
|
|
#ifdef SYS_copy_file_range
|
|
int64_t rv;
|
|
loff_t src_off;
|
|
loff_t tgt_off;
|
|
loff_t *src_off_ptr;
|
|
loff_t *tgt_off_ptr;
|
|
|
|
src_off = ((src_off_ == NULL) ? 0 : *src_off_);
|
|
tgt_off = ((tgt_off_ == NULL) ? 0 : *tgt_off_);
|
|
src_off_ptr = ((src_off_ == NULL) ? NULL : &src_off);
|
|
tgt_off_ptr = ((tgt_off_ == NULL) ? NULL : &tgt_off);
|
|
rv = ::syscall(SYS_copy_file_range,
|
|
src_fd_,
|
|
src_off_ptr,
|
|
tgt_fd_,
|
|
tgt_off_ptr,
|
|
len_,
|
|
flags_);
|
|
|
|
if(rv != -1)
|
|
{
|
|
if(src_off_ != NULL)
|
|
*src_off_ = src_off;
|
|
if(tgt_off_ != NULL)
|
|
*tgt_off_ = tgt_off;
|
|
}
|
|
|
|
return rv;
|
|
#else
|
|
return (errno=EOPNOTSUPP,-1);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
namespace fs
|
|
{
|
|
int64_t
|
|
copy_file_range(const int src_fd_,
|
|
int64_t *src_off_,
|
|
const int tgt_fd_,
|
|
int64_t *tgt_off_,
|
|
const uint64_t len_,
|
|
const unsigned int flags_)
|
|
{
|
|
return l::copy_file_range_(src_fd_,
|
|
src_off_,
|
|
tgt_fd_,
|
|
tgt_off_,
|
|
len_,
|
|
flags_);
|
|
}
|
|
}
|