Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/tmp/file
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public:
/// @returns The underlying implementation-defined handle
native_handle_type native_handle() const noexcept;

/// Links this file to the given path if it is on the same filesystem,
/// or copies it otherwise
/// Flushes buffered input and copies this file to the given path
/// @param to The target path
/// @throws std::filesystem::filesystem_error if cannot move the file
void move(const std::filesystem::path& to);
Expand Down
9 changes: 7 additions & 2 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,15 @@ file::native_handle_type file::native_handle() const noexcept {
}

void file::move(const fs::path& to) {
flush();
seekg(0, std::ios::beg);

// FIXME: I couldn't figure out how to create a hard link to a file without
// other hard links, so I just copy it even within the same file system
// There is no way to create a hard link to a file without hard links
// on POSIX and Windows. The only option is to use `AT_EMPTY_PATH`
// with `linkat` on Linux platforms; but given that the file is created
// in tmpfs, and the persistent storage where the file is moved
// is not in tmpfs, this optimization is useless.
// So, we copy the file

std::error_code ec;
native_handle_type target = open_file(to, /*readonly=*/false, ec);
Expand Down
2 changes: 1 addition & 1 deletion tests/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ TEST(file, move_to_existing_file) {

{
file tmpfile = file();
tmpfile << "Hello, world!" << std::flush;
tmpfile << "Hello, world!";

tmpfile.move(to);
}
Expand Down