浏览代码

`safe_rename` automatically falls back to copy in case of cross_device_link

default_compile_flags
vector-of-bool 6 年前
父节点
当前提交
6a5859738a
共有 2 个文件被更改,包括 31 次插入0 次删除
  1. +29
    -0
      src/dds/util.cpp
  2. +2
    -0
      src/dds/util.hpp

+ 29
- 0
src/dds/util.cpp 查看文件

#include "./util.hpp" #include "./util.hpp"


#include <spdlog/fmt/fmt.h>

#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
out << file.rdbuf(); out << file.rdbuf();
return std::move(out).str(); return std::move(out).str();
} }

void dds::safe_rename(path_ref source, path_ref dest) {
std::error_code ec;
fs::rename(source, dest, ec);
if (!ec) {
return;
}

if (ec != std::errc::cross_device_link) {
throw std::system_error(ec,
fmt::format("Failed to move item [{}] to [{}]",
source.string(),
dest.string()));
}

auto tmp = dest;
tmp.replace_filename(tmp.filename().string() + ".tmp-dds-mv");
try {
fs::remove_all(tmp);
fs::copy(source, tmp, fs::copy_options::recursive);
} catch (...) {
fs::remove_all(tmp, ec);
throw;
}
fs::rename(tmp, dest);
fs::remove_all(source);
}

+ 2
- 0
src/dds/util.hpp 查看文件

return contents; return contents;
} }


void safe_rename(path_ref source, path_ref dest);

} // namespace file_utils } // namespace file_utils


template <typename Container, typename Predicate> template <typename Container, typename Predicate>

正在加载...
取消
保存