aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Support
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-06-02 00:52:22 +0000
committerChris Lattner <sabre@nondot.org>2004-06-02 00:52:22 +0000
commit5bfac5d2edac78a0c0f91e751dc0c173158a4829 (patch)
tree00d43a35194856f8407d24270e14bb8770f47441 /lib/Support
parent2a7f15e50bf5e6ce0bf75fffb93ec5a4c6e025b6 (diff)
downloadexternal_llvm-5bfac5d2edac78a0c0f91e751dc0c173158a4829.zip
external_llvm-5bfac5d2edac78a0c0f91e751dc0c173158a4829.tar.gz
external_llvm-5bfac5d2edac78a0c0f91e751dc0c173158a4829.tar.bz2
Implement the new CopyFile function
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13945 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/FileUtilities.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index ed594db..bd5cf09 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/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.