From f21de942f3785d86d1f82cd19072ca26de9167da Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Thu, 19 Jun 2014 19:35:39 +0000 Subject: Support: Add llvm::sys::fs::copy_file A function to copy one file's contents to another. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211302 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Path.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/Support') diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 15edf0ddbb..535394c5b3 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -846,6 +846,40 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting) { return create_directory(P, IgnoreExisting); } +std::error_code copy_file(const Twine &From, const Twine &To) { + int ReadFD, WriteFD; + if (std::error_code EC = openFileForRead(From, ReadFD)) + return EC; + if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) { + close(ReadFD); + return EC; + } + + const size_t BufSize = 4096; + char *Buf = new char[BufSize]; + int BytesRead = 0, BytesWritten = 0; + for (;;) { + BytesRead = read(ReadFD, Buf, BufSize); + if (BytesRead <= 0) + break; + while (BytesRead) { + BytesWritten = write(WriteFD, Buf, BytesRead); + if (BytesWritten < 0) + break; + BytesRead -= BytesWritten; + } + if (BytesWritten < 0) + break; + } + close(ReadFD); + close(WriteFD); + delete[] Buf; + + if (BytesRead < 0 || BytesWritten < 0) + return std::error_code(errno, std::generic_category()); + return std::error_code(); +} + bool exists(file_status status) { return status_known(status) && status.type() != file_type::file_not_found; } -- cgit v1.2.3