diff options
author | Justin Bogner <mail@justinbogner.com> | 2014-06-19 19:35:39 +0000 |
---|---|---|
committer | Justin Bogner <mail@justinbogner.com> | 2014-06-19 19:35:39 +0000 |
commit | cd45f963e2bbd97ad7cb657e668ef7ca3de7ee27 (patch) | |
tree | b1ad4681fc97fc066fc3e9934d5cb55d9e1ccda6 /llvm/lib/Support/Path.cpp | |
parent | 03b1c3f4388303714ddf414fd159f62c01630569 (diff) | |
download | llvm-cd45f963e2bbd97ad7cb657e668ef7ca3de7ee27.zip llvm-cd45f963e2bbd97ad7cb657e668ef7ca3de7ee27.tar.gz llvm-cd45f963e2bbd97ad7cb657e668ef7ca3de7ee27.tar.bz2 |
Support: Add llvm::sys::fs::copy_file
A function to copy one file's contents to another.
llvm-svn: 211302
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 15edf0dd..535394c 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/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; } |