Put CopyFile in the sys namespace.
[oota-llvm.git] / lib / System / Win32 / Path.cpp
index 88e20cd354f417fcc97b7fbb8daec5223afa5de0..c50877afb1aca41755bec8358ba51e7c794c1236 100644 (file)
@@ -156,13 +156,6 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
   if (env_var != 0) {
     getPathList(env_var,Paths);
   }
-#ifdef LLVMGCCDIR
-  {
-    Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
-    if (tmpPath.readable())
-      Paths.push_back(tmpPath);
-  }
-#endif
 #ifdef LLVM_LIBDIR
   {
     Path tmpPath;
@@ -179,11 +172,6 @@ Path::GetLLVMDefaultConfigDir() {
   return Path("/etc/llvm/");
 }
 
-Path
-Path::GetLLVMConfigDir() {
-  return GetLLVMDefaultConfigDir();
-}
-
 Path
 Path::GetUserHomeDirectory() {
   const char* home = getenv("HOME");
@@ -515,7 +503,7 @@ Path::createFile() {
 }
 
 bool
-Path::destroyDirectory(bool remove_contents) {
+Path::destroyDirectory(bool remove_contents) const {
   // Make sure we're dealing with a directory
   if (!isDirectory()) return false;
 
@@ -544,7 +532,7 @@ Path::destroyDirectory(bool remove_contents) {
 }
 
 bool
-Path::destroyFile() {
+Path::destroyFile() const {
   if (!isFile()) return false;
 
   DWORD attr = GetFileAttributes(path.c_str());
@@ -565,6 +553,59 @@ Path::destroyFile() {
   return true;
 }
 
+bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
+  if (!isFile())
+    return false;
+  assert(len < 1024 && "Request for magic string too long");
+  char* buf = (char*) alloca(1 + len);
+  std::ofstream ofs(path.c_str(),std::ofstream::in);
+  if (!ofs.is_open())
+    return false;
+  std::ifstream ifs(path.c_str());
+  if (!ifs.is_open())
+    return false;
+  ifs.read(buf, len);
+  ofs.close();
+  ifs.close();
+  buf[len] = '\0';
+  Magic = buf;
+  return true;
+}
+
+void 
+sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
+  if (!::CopyFile(Src.c_str(), Dest.c_str(), false))
+    ThrowError("Can't copy '" + Src.toString() + 
+               "' to '" + Dest.toString() + "'");
+}
+
+void 
+Path::makeUnique( bool reuse_current ) {
+  if (reuse_current && !exists())
+    return; // File doesn't exist already, just use it!
+
+  Path dir (*this);
+  dir.elideFile();
+  std::string fname = this->getLast();
+
+  char newName[MAX_PATH + 1];
+  if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
+    ThrowError("Cannot make unique filename for '" + path + "'");
+
+  path = newName;
+}
+
+bool
+Path::createTemporaryFile(bool reuse_current) {
+  // Make sure we're dealing with a file
+  if (!isFile()) 
+    return false;
+
+  // Make this into a unique file name
+  makeUnique( reuse_current );
+  return true;
+}
+
 }
 }