2025-07-25 17:53:48 -05:00
|
|
|
/*
|
|
|
|
|
ISC License
|
|
|
|
|
|
2026-02-23 13:22:21 -06:00
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-23 21:57:55 -05:00
|
|
|
#include "fs_copyfile.hpp"
|
|
|
|
|
|
2025-06-28 17:27:40 -05:00
|
|
|
#include "fs_attr.hpp"
|
2025-06-23 21:57:55 -05:00
|
|
|
#include "fs_close.hpp"
|
2025-06-28 17:27:40 -05:00
|
|
|
#include "fs_copydata.hpp"
|
|
|
|
|
#include "fs_fchmod.hpp"
|
|
|
|
|
#include "fs_fchown.hpp"
|
|
|
|
|
#include "fs_fcntl.hpp"
|
|
|
|
|
#include "fs_file_unchanged.hpp"
|
|
|
|
|
#include "fs_fstat.hpp"
|
|
|
|
|
#include "fs_futimens.hpp"
|
2025-06-23 21:57:55 -05:00
|
|
|
#include "fs_mktemp.hpp"
|
|
|
|
|
#include "fs_open.hpp"
|
2025-09-17 23:51:16 -04:00
|
|
|
#include "fs_path.hpp"
|
2025-06-23 21:57:55 -05:00
|
|
|
#include "fs_rename.hpp"
|
2025-06-28 17:27:40 -05:00
|
|
|
#include "fs_unlink.hpp"
|
|
|
|
|
#include "fs_xattr.hpp"
|
fix: leave the source branch out before moveonenospc picks a branch
- The source branch was dropped from the create policy's result. ff,
mfs, lfs, lus and pfrd return a single branch, so when that branch
was the source nothing was left and the write kept ENOSPC even with
room elsewhere. That happens when the source has free space by
statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
inode, as before) and run the policy on that, so it picks another
branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
past a source listed first, by its own path or an alias, and that a
list made only of the source is refused.
Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
2026-11-22 01:24:45 -05:00
|
|
|
#include "to_neg_errno.hpp"
|
2025-06-28 17:27:40 -05:00
|
|
|
|
2026-03-16 22:50:36 -05:00
|
|
|
#include "scope_guard/scope_guard.hpp"
|
2025-06-23 21:57:55 -05:00
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2025-06-28 17:27:40 -05:00
|
|
|
#include <signal.h>
|
fix: leave the source branch out before moveonenospc picks a branch
- The source branch was dropped from the create policy's result. ff,
mfs, lfs, lus and pfrd return a single branch, so when that branch
was the source nothing was left and the write kept ENOSPC even with
room elsewhere. That happens when the source has free space by
statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
inode, as before) and run the policy on that, so it picks another
branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
past a source listed first, by its own path or an alias, and that a
list made only of the source is refused.
Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
2026-11-22 01:24:45 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD__)
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
#endif
|
2025-06-28 17:27:40 -05:00
|
|
|
|
2025-06-23 21:57:55 -05:00
|
|
|
|
2025-06-28 17:27:40 -05:00
|
|
|
static
|
|
|
|
|
bool
|
2025-07-27 16:23:45 -05:00
|
|
|
_ignorable_error(const s64 err_)
|
2025-06-28 17:27:40 -05:00
|
|
|
{
|
|
|
|
|
switch(err_)
|
|
|
|
|
{
|
|
|
|
|
case ENOTTY:
|
|
|
|
|
case ENOTSUP:
|
|
|
|
|
#if ENOTSUP != EOPNOTSUPP
|
|
|
|
|
case EOPNOTSUPP:
|
|
|
|
|
#endif
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
fix: leave the source branch out before moveonenospc picks a branch
- The source branch was dropped from the create policy's result. ff,
mfs, lfs, lus and pfrd return a single branch, so when that branch
was the source nothing was left and the write kept ENOSPC even with
room elsewhere. That happens when the source has free space by
statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
inode, as before) and run the policy on that, so it picks another
branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
past a source listed first, by its own path or an alias, and that a
list made only of the source is refused.
Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
2026-11-22 01:24:45 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_publish_noreplace(const char *tmppath_,
|
|
|
|
|
const char *dstpath_)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
#if defined(SYS_renameat2) && defined(RENAME_NOREPLACE)
|
|
|
|
|
rv = ::to_neg_errno(::syscall(SYS_renameat2,AT_FDCWD,tmppath_,
|
|
|
|
|
AT_FDCWD,dstpath_,RENAME_NOREPLACE));
|
|
|
|
|
#elif defined(__APPLE__) && defined(RENAME_EXCL)
|
|
|
|
|
rv = ::to_neg_errno(::renamex_np(tmppath_,dstpath_,RENAME_EXCL));
|
|
|
|
|
#else
|
|
|
|
|
rv = -ENOSYS;
|
|
|
|
|
#endif
|
|
|
|
|
if((rv != -ENOSYS) && (rv != -EINVAL) &&
|
|
|
|
|
(rv != -ENOTSUP) && (rv != -EOPNOTSUPP))
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
// Some filesystems reject the rename flag. The staging file is a
|
|
|
|
|
// regular file in the same directory, so link can publish it atomically
|
|
|
|
|
// without replacement. Never fall back to a replacing rename.
|
|
|
|
|
rv = ::to_neg_errno(::link(tmppath_,dstpath_));
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
// The destination is now published. A cleanup error cannot undo that.
|
|
|
|
|
fs::unlink(tmppath_);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 16:23:45 -05:00
|
|
|
s64
|
2025-06-28 17:27:40 -05:00
|
|
|
fs::copyfile(const int src_fd_,
|
|
|
|
|
const struct stat &src_st_,
|
|
|
|
|
const int dst_fd_)
|
|
|
|
|
{
|
2025-07-27 16:23:45 -05:00
|
|
|
s64 rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::copydata(src_fd_,dst_fd_,src_st_.st_size);
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::xattr::copy(src_fd_,dst_fd_);
|
2025-07-25 17:53:48 -05:00
|
|
|
if((rv < 0) && !::_ignorable_error(-rv))
|
|
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::attr::copy(src_fd_,dst_fd_,FS_ATTR_CLEAR_IMMUTABLE);
|
2025-07-21 18:18:59 -05:00
|
|
|
if((rv < 0) && !::_ignorable_error(-rv))
|
2025-07-25 17:53:48 -05:00
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::fchown_check_on_error(dst_fd_,src_st_);
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::fchmod_check_on_error(dst_fd_,src_st_);
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
rv = fs::futimens(dst_fd_,src_st_);
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
2025-06-28 17:27:40 -05:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Limitations:
|
|
|
|
|
// * Doesn't handle immutable files well. Will ignore the flag on attr
|
|
|
|
|
// copy.
|
|
|
|
|
// * Does not handle non-regular files.
|
2025-06-23 21:57:55 -05:00
|
|
|
|
2025-07-27 16:23:45 -05:00
|
|
|
s64
|
2025-09-17 23:51:16 -04:00
|
|
|
fs::copyfile(const int src_fd_,
|
|
|
|
|
const fs::path &dst_filepath_,
|
|
|
|
|
const fs::CopyFileFlags &flags_)
|
2025-06-23 21:57:55 -05:00
|
|
|
{
|
2025-07-27 16:23:45 -05:00
|
|
|
s64 rv;
|
2025-06-23 21:57:55 -05:00
|
|
|
int dst_fd;
|
2025-10-29 22:42:42 -05:00
|
|
|
struct stat src_st = {};
|
2025-06-23 21:57:55 -05:00
|
|
|
std::string dst_tmppath;
|
2025-06-28 17:27:40 -05:00
|
|
|
struct sigaction old_act;
|
|
|
|
|
struct sigaction new_act;
|
|
|
|
|
|
|
|
|
|
// Done in case fcntl(F_SETLEASE) works.
|
|
|
|
|
new_act.sa_handler = SIG_IGN;
|
|
|
|
|
new_act.sa_flags = 0;
|
|
|
|
|
sigemptyset(&new_act.sa_mask);
|
|
|
|
|
sigaction(SIGIO,&new_act,&old_act);
|
|
|
|
|
DEFER { sigaction(SIGIO,&old_act,NULL); };
|
2025-06-23 21:57:55 -05:00
|
|
|
|
2025-06-28 17:27:40 -05:00
|
|
|
while(true)
|
|
|
|
|
{
|
2025-07-27 16:23:45 -05:00
|
|
|
// For comparison after the copy to see if the file was
|
|
|
|
|
// modified. This could be made more thorough by adding some
|
|
|
|
|
// hashing but probably overkill. Also used to provide size and
|
|
|
|
|
// other details for data copying.
|
|
|
|
|
rv = fs::fstat(src_fd_,&src_st);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
if(!S_ISREG(src_st.st_mode))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
std::tie(dst_fd,dst_tmppath) = fs::mktemp(dst_filepath_,O_RDWR);
|
2025-06-28 17:27:40 -05:00
|
|
|
if(dst_fd < 0)
|
|
|
|
|
return dst_fd;
|
|
|
|
|
DEFER { fs::close(dst_fd); };
|
|
|
|
|
|
|
|
|
|
// If it fails or is unsupported... so be it. This will help
|
|
|
|
|
// limit the possibility of the file being modified while the
|
|
|
|
|
// copy happens. Opening read-only is fine but open for write or
|
|
|
|
|
// truncate will block for others till this finishes or the
|
|
|
|
|
// kernel wide timeout (/proc/sys/fs/lease-break-time).
|
2025-07-27 16:23:45 -05:00
|
|
|
fs::fcntl_setlease_rdlck(src_fd_);
|
|
|
|
|
DEFER { fs::fcntl_setlease_unlck(src_fd_); };
|
2025-06-28 17:27:40 -05:00
|
|
|
|
2025-07-27 16:23:45 -05:00
|
|
|
rv = fs::copyfile(src_fd_,src_st,dst_fd);
|
2025-06-28 17:27:40 -05:00
|
|
|
if(rv < 0)
|
|
|
|
|
{
|
2025-07-27 16:23:45 -05:00
|
|
|
if(flags_.cleanup_failure)
|
2025-06-28 17:27:40 -05:00
|
|
|
fs::unlink(dst_tmppath);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2025-06-23 21:57:55 -05:00
|
|
|
|
2025-07-27 16:23:45 -05:00
|
|
|
rv = fs::file_changed(src_fd_,src_st);
|
2026-03-19 20:07:17 -05:00
|
|
|
if(rv != FS_FILE_UNCHANGED)
|
2025-06-28 17:27:40 -05:00
|
|
|
{
|
|
|
|
|
fs::unlink(dst_tmppath);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-06-23 21:57:55 -05:00
|
|
|
|
fix: leave the source branch out before moveonenospc picks a branch
- The source branch was dropped from the create policy's result. ff,
mfs, lfs, lus and pfrd return a single branch, so when that branch
was the source nothing was left and the write kept ENOSPC even with
room elsewhere. That happens when the source has free space by
statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
inode, as before) and run the policy on that, so it picks another
branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
past a source listed first, by its own path or an alias, and that a
list made only of the source is refused.
Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
2026-11-22 01:24:45 -05:00
|
|
|
if(flags_.no_replace)
|
|
|
|
|
rv = ::_publish_noreplace(dst_tmppath.c_str(),dst_filepath_.c_str());
|
|
|
|
|
else
|
|
|
|
|
rv = fs::rename(dst_tmppath.c_str(),dst_filepath_.c_str());
|
2025-07-27 16:23:45 -05:00
|
|
|
if((rv < 0) && (flags_.cleanup_failure))
|
2025-06-28 17:27:40 -05:00
|
|
|
fs::unlink(dst_tmppath);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-06-23 21:57:55 -05:00
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2025-07-27 16:23:45 -05:00
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
// For FreeBSD
|
|
|
|
|
#ifndef O_NOATIME
|
|
|
|
|
#define O_NOATIME 0
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-07-27 16:23:45 -05:00
|
|
|
s64
|
2025-09-17 23:51:16 -04:00
|
|
|
fs::copyfile(const fs::path &src_filepath_,
|
|
|
|
|
const fs::path &dst_filepath_,
|
|
|
|
|
const fs::CopyFileFlags &flags_)
|
2025-07-27 16:23:45 -05:00
|
|
|
{
|
|
|
|
|
int src_fd;
|
|
|
|
|
|
|
|
|
|
src_fd = fs::open(src_filepath_,O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_NOATIME);
|
|
|
|
|
if(src_fd < 0)
|
|
|
|
|
return src_fd;
|
|
|
|
|
DEFER { fs::close(src_fd); };
|
|
|
|
|
|
|
|
|
|
return fs::copyfile(src_fd,dst_filepath_,flags_);
|
|
|
|
|
}
|