[llvm-commits] CVS: llvm/lib/Support/FileUtilities.cpp
Chris Lattner
lattner at cs.uiuc.edu
Tue Jun 1 19:57:01 PDT 2004
Changes in directory llvm/lib/Support:
FileUtilities.cpp updated: 1.21 -> 1.22
---
Log message:
Implement the new CopyFile function
---
Diffs of the changes: (+35 -0)
Index: llvm/lib/Support/FileUtilities.cpp
diff -u llvm/lib/Support/FileUtilities.cpp:1.21 llvm/lib/Support/FileUtilities.cpp:1.22
--- llvm/lib/Support/FileUtilities.cpp:1.21 Thu May 27 19:34:42 2004
+++ llvm/lib/Support/FileUtilities.cpp Tue Jun 1 19:52:22 2004
@@ -109,6 +109,41 @@
}
+/// 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.
More information about the llvm-commits
mailing list