From a50b98c517cc90e6c8929b721049c9a589682e6d Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 3 Dec 2010 17:53:55 +0000 Subject: [PATCH] Support/FileSystem: Add rename implementation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120818 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Unix/PathV2.inc | 13 +++++++++++++ lib/Support/Windows/PathV2.inc | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc index e6972f83363..8ecf2a4222d 100644 --- a/lib/Support/Unix/PathV2.inc +++ b/lib/Support/Unix/PathV2.inc @@ -200,6 +200,19 @@ error_code remove(const Twine &path, bool &existed) { return make_error_code(errc::success); } +error_code rename(const Twine &from, const Twine &to) { + // Get arguments. + SmallString<128> from_storage; + SmallString<128> to_storage; + StringRef f = from.toNullTerminatedStringRef(from_storage); + StringRef t = to.toNullTerminatedStringRef(to_storage); + + if (::rename(f.begin(), t.begin()) == -1) + return error_code(errno, system_category()); + + return make_error_code(errc::success); +} + error_code exists(const Twine &path, bool &result) { SmallString<128> path_storage; StringRef p = path.toNullTerminatedStringRef(path_storage); diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index c86c24e811e..c97ba5df593 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -271,6 +271,25 @@ error_code remove(const Twine &path, bool &existed) { return make_error_code(errc::success); } +error_code rename(const Twine &from, const Twine &to) { + // Get arguments. + SmallString<128> from_storage; + SmallString<128> to_storage; + StringRef f = from.toStringRef(from_storage); + StringRef t = to.toStringRef(to_storage); + + // Convert to utf-16. + SmallVector wide_from; + SmallVector wide_to; + if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec; + if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec; + + if (!::MoveFileW(wide_from.begin(), wide_to.begin())) + return make_error_code(windows_error(::GetLastError())); + + return make_error_code(errc::success); +} + error_code exists(const Twine &path, bool &result) { SmallString<128> path_storage; SmallVector path_utf16;