fs::rename, use renameat2 on Linux

This commit is contained in:
Nekotekina 2019-04-13 13:46:56 +03:00
parent fe593de6d7
commit d52df9352c
2 changed files with 22 additions and 5 deletions

View file

@ -137,6 +137,8 @@ static fs::error to_error(DWORD e)
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#elif defined(__linux__) || defined(__sun) #elif defined(__linux__) || defined(__sun)
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/syscall.h>
#include <linux/fs.h>
#else #else
#include <fstream> #include <fstream>
#endif #endif
@ -192,7 +194,7 @@ namespace fs
// Do notning // Do notning
} }
native_handle file_base::get_native_handle() fs::native_handle fs::file_base::get_handle()
{ {
#ifdef _WIN32 #ifdef _WIN32
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
@ -609,6 +611,21 @@ bool fs::rename(const std::string& from, const std::string& to, bool overwrite)
return true; return true;
#else #else
#ifdef __linux__
if (syscall(SYS_renameat2, AT_FDCWD, from.c_str(), AT_FDCWD, to.c_str(), overwrite ? 0 : 1 /* RENAME_NOREPLACE */) == 0)
{
return true;
}
// If the filesystem doesn't support RENAME_NOREPLACE, it returns EINVAL. Retry with fallback method in that case.
if (errno != EINVAL || overwrite)
{
g_tls_error = to_error(errno);
return false;
}
#endif
if (!overwrite && exists(to)) if (!overwrite && exists(to))
{ {
g_tls_error = fs::error::exist; g_tls_error = fs::error::exist;
@ -996,7 +1013,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
return size.QuadPart; return size.QuadPart;
} }
native_handle get_native_handle() override native_handle get_handle() override
{ {
return m_handle; return m_handle;
} }
@ -1136,7 +1153,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
return file_info.st_size; return file_info.st_size;
} }
native_handle get_native_handle() override native_handle get_handle() override
{ {
return m_fd; return m_fd;
} }
@ -1219,7 +1236,7 @@ fs::native_handle fs::file::get_handle() const
{ {
if (m_file) if (m_file)
{ {
return m_file->get_native_handle(); return m_file->get_handle();
} }
#ifdef _WIN32 #ifdef _WIN32

View file

@ -77,7 +77,7 @@ namespace fs
virtual u64 write(const void* buffer, u64 size) = 0; virtual u64 write(const void* buffer, u64 size) = 0;
virtual u64 seek(s64 offset, seek_mode whence) = 0; virtual u64 seek(s64 offset, seek_mode whence) = 0;
virtual u64 size() = 0; virtual u64 size() = 0;
virtual native_handle get_native_handle(); virtual native_handle get_handle();
}; };
// Directory entry (TODO) // Directory entry (TODO)