[llvm-commits] CVS: llvm/lib/Support/FileUtilities.cpp
John Criswell
criswell at cs.uiuc.edu
Tue Sep 2 15:16:05 PDT 2003
Changes in directory llvm/lib/Support:
FileUtilities.cpp updated: 1.3 -> 1.4
---
Log message:
Added the MakeFileExecutable() method. This method takes a filename and
gives it execute access while respecting the user's umask.
---
Diffs of the changes:
Index: llvm/lib/Support/FileUtilities.cpp
diff -u llvm/lib/Support/FileUtilities.cpp:1.3 llvm/lib/Support/FileUtilities.cpp:1.4
--- llvm/lib/Support/FileUtilities.cpp:1.3 Thu Aug 7 16:35:41 2003
+++ llvm/lib/Support/FileUtilities.cpp Tue Sep 2 15:14:55 2003
@@ -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>
@@ -90,3 +92,51 @@
delete[] FNBuffer;
return Result;
}
+
+///
+/// Method: MakeFileExecutable ()
+///
+/// Description:
+/// This method makes the specified filename executable by giving it
+/// execute permission.
+///
+/// For the UNIX version of this method, we turn on all of the read and
+/// execute bits and then turn off anything specified in the umask. This
+/// should help ensure that access to the file remains at the level that
+/// the user desires.
+///
+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 script executable...
+ chmod(Filename.c_str(), (fstat.st_mode | (0111 & ~mask)));
+
+ return true;
+}
+
More information about the llvm-commits
mailing list