Put CopyFile in the sys namespace.
[oota-llvm.git] / lib / System / Win32 / Path.cpp
index 070ebb312085ea80d7eb1db75725ce94e0f681cd..c50877afb1aca41755bec8358ba51e7c794c1236 100644 (file)
@@ -118,11 +118,6 @@ Path::GetRootDirectory() {
   return result;
 }
 
-std::string
-Path::GetDLLSuffix() {
-  return "dll";
-}
-
 static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
   const char* at = path;
   const char* delim = strchr(at, ';');
@@ -161,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;
@@ -184,11 +172,6 @@ Path::GetLLVMDefaultConfigDir() {
   return Path("/etc/llvm/");
 }
 
-Path
-Path::GetLLVMConfigDir() {
-  return GetLLVMDefaultConfigDir();
-}
-
 Path
 Path::GetUserHomeDirectory() {
   const char* home = getenv("HOME");
@@ -292,6 +275,51 @@ Path::getLast() const {
   return path.substr(pos+1);
 }
 
+void
+Path::getStatusInfo(StatusInfo& info) const {
+  WIN32_FILE_ATTRIBUTE_DATA fi;
+  if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
+    ThrowError(std::string(path) + ": Can't get status: ");
+
+  info.fileSize = fi.nFileSizeHigh;
+  info.fileSize <<= 32;
+  info.fileSize += fi.nFileSizeLow;
+
+  info.mode = 0777;    // Not applicable to Windows, so...
+  info.user = 9999;    // Not applicable to Windows, so...
+  info.group = 9999;   // Not applicable to Windows, so...
+
+  __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
+  info.modTime.fromWin32Time(ft);
+
+  info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
+  if (info.isDir && path[path.length() - 1] != '/')
+    path += '/';
+  else if (!info.isDir && path[path.length() - 1] == '/')
+    path.erase(path.length() - 1);
+}
+
+void Path::makeReadable() {
+  // All files are readable on Windows (ignoring security attributes).
+}
+
+void Path::makeWriteable() {
+  DWORD attr = GetFileAttributes(path.c_str());
+
+  // If it doesn't exist, we're done.
+  if (attr == INVALID_FILE_ATTRIBUTES)
+    return;
+
+  if (attr & FILE_ATTRIBUTE_READONLY) {
+    if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
+      ThrowError(std::string(path) + ": Can't make file writable: ");
+  }
+}
+
+void Path::makeExecutable() {
+  // All files are executable on Windows (ignoring security attributes).
+}
+
 bool
 Path::setDirectory(const std::string& a_path) {
   if (a_path.size() == 0)
@@ -300,7 +328,7 @@ Path::setDirectory(const std::string& a_path) {
   path = a_path;
   FlipBackSlashes(path);
   size_t last = a_path.size() -1;
-  if (last != 0 && a_path[last] != '/')
+  if (a_path[last] != '/')
     path += '/';
   if (!isValid()) {
     path = save.path;
@@ -475,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;
 
@@ -504,7 +532,7 @@ Path::destroyDirectory(bool remove_contents) {
 }
 
 bool
-Path::destroyFile() {
+Path::destroyFile() const {
   if (!isFile()) return false;
 
   DWORD attr = GetFileAttributes(path.c_str());
@@ -525,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;
+}
+
 }
 }