diff options
author | Chris Lattner <sabre@nondot.org> | 2004-06-02 00:52:22 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-06-02 00:52:22 +0000 |
commit | 80adb79be2565ab4978c0f539897fa7de414f312 (patch) | |
tree | 21f6c207847c4d3bb96e86c00d37a20431a11fc5 /llvm/lib/Support/FileUtilities.cpp | |
parent | 5bcd2323ff0ee9aadab6ad9196adf3c6afb786ca (diff) | |
download | bcm5719-llvm-80adb79be2565ab4978c0f539897fa7de414f312.tar.gz bcm5719-llvm-80adb79be2565ab4978c0f539897fa7de414f312.zip |
Implement the new CopyFile function
llvm-svn: 13945
Diffstat (limited to 'llvm/lib/Support/FileUtilities.cpp')
-rw-r--r-- | llvm/lib/Support/FileUtilities.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/llvm/lib/Support/FileUtilities.cpp b/llvm/lib/Support/FileUtilities.cpp index ed594db96ea..bd5cf09be7c 100644 --- a/llvm/lib/Support/FileUtilities.cpp +++ b/llvm/lib/Support/FileUtilities.cpp @@ -109,6 +109,41 @@ bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB, } +/// CopyFile - Copy the specified source file to the specified destination, +/// overwriting destination if it exists. This returns true on failure. +/// +bool llvm::CopyFile(const std::string &Dest, const std::string &Src) { + FDHandle InFD(open(Src.c_str(), O_RDONLY)); + if (InFD == -1) return true; + + FileRemover FR(Dest); + + FDHandle OutFD(open(Dest.c_str(), O_WRONLY|O_CREAT, 0666)); + if (OutFD == -1) return true; + + char Buffer[16*1024]; + while (ssize_t Amt = read(InFD, Buffer, 16*1024)) { + if (Amt == -1) { + if (errno != EINTR) return true; // Error reading the file. + } else { + char *BufPtr = Buffer; + while (Amt) { + ssize_t AmtWritten = write(OutFD, BufPtr, Amt); + if (AmtWritten == -1) { + if (errno != EINTR) return true; // Error writing the file. + } else { + Amt -= AmtWritten; + BufPtr += AmtWritten; + } + } + } + } + + FR.releaseFile(); // Success! + return false; +} + + /// MoveFileOverIfUpdated - If the file specified by New is different than Old, /// or if Old does not exist, move the New file over the Old file. Otherwise, /// remove the New file. |