Moved enum and command-line option in separate file. Also added function that returns...
[oota-llvm.git] / lib / Support / FileUtilities.cpp
index 3e4c270da564d83dad0d055e00bc63464fd09422..ca4603628cab6b15384dc3db56c27ae6a3c861f2 100644 (file)
@@ -7,6 +7,8 @@
 
 #include "Support/FileUtilities.h"
 #include "Config/unistd.h"
+#include "Config/sys/stat.h"
+#include "Config/sys/types.h"
 #include <fstream>
 #include <iostream>
 #include <cstdio>
@@ -83,10 +85,119 @@ std::string getUniqueFilename(const std::string &FilenameBase) {
     exit(1);
   }
 
-  // We don't need to hold the temp file descriptor... we will trust that noone
+  // We don't need to hold the temp file descriptor... we will trust that no one
   // will overwrite/delete the file while we are working on it...
   close(TempFD);
   std::string Result(FNBuffer);
   delete[] FNBuffer;
   return Result;
 }
+
+///
+/// Method: MakeFileExecutable ()
+///
+/// Description:
+///    This method makes the specified filename executable by giving it
+///    execute permission.  It respects the umask value of the process, and it
+///    does not enable any unnecessary access bits.
+///
+/// Algorithm:
+///    o Get file's current permissions.
+///    o Get the process's current umask.
+///    o Take the set of all execute bits and disable those found in the umask.
+///    o Add the remaining permissions to the file's permissions.
+///
+bool
+MakeFileExecutable (const std::string & Filename)
+{
+  // Permissions masking value of the user
+  mode_t mask;
+
+  // Permissions currently enabled on the file
+  struct stat fstat;
+
+  //
+  // Grab the umask value from the operating system.  We want to use it when
+  // changing the file's permissions.
+  //
+  // Note:
+  //  Umask() is one of those annoying system calls.  You have to call it
+  //  to get the current value and then set it back.
+  //
+  mask = umask (0x777);
+  umask (mask);
+
+  //
+  // Go fetch the file's current permission bits.  We want to *add* execute
+  // access to the file.
+  //
+  if ((stat (Filename.c_str(), &fstat)) == -1)
+  {
+    return false;
+  }
+
+  //
+  // Make the file executable...
+  //
+  if ((chmod(Filename.c_str(), (fstat.st_mode | (0111 & ~mask)))) == -1)
+  {
+    return false;
+  }
+
+  return true;
+}
+
+///
+/// Method: MakeFileReadable ()
+///
+/// Description:
+///    This method makes the specified filename readable by giving it
+///    read permission.  It respects the umask value of the process, and it
+///    does not enable any unnecessary access bits.
+///
+/// Algorithm:
+///    o Get file's current permissions.
+///    o Get the process's current umask.
+///    o Take the set of all read bits and disable those found in the umask.
+///    o Add the remaining permissions to the file's permissions.
+///
+bool
+MakeFileReadable (const std::string & Filename)
+{
+  // Permissions masking value of the user
+  mode_t mask;
+
+  // Permissions currently enabled on the file
+  struct stat fstat;
+
+  //
+  // Grab the umask value from the operating system.  We want to use it when
+  // changing the file's permissions.
+  //
+  // Note:
+  //  Umask() is one of those annoying system calls.  You have to call it
+  //  to get the current value and then set it back.
+  //
+  mask = umask (0x777);
+  umask (mask);
+
+  //
+  // Go fetch the file's current permission bits.  We want to *add* execute
+  // access to the file.
+  //
+  if ((stat (Filename.c_str(), &fstat)) == -1)
+  {
+    return false;
+  }
+
+  //
+  // Make the file executable...
+  //
+  if ((chmod(Filename.c_str(), (fstat.st_mode | (0444 & ~mask)))) == -1)
+  {
+    return false;
+  }
+
+  return true;
+}
+