[llvm-commits] CVS: llvm/lib/System/Unix/Path.cpp

Reid Spencer reid at x10sys.com
Mon Dec 13 12:00:01 PST 2004



Changes in directory llvm/lib/System/Unix:

Path.cpp updated: 1.20 -> 1.21
---
Log message:

For PR351: http://llvm.cs.uiuc.edu/PR351 :
Implement three new functions to allow setting access/permission bits on 
the file referenced by a path. The makeReadable and makeExecutable methods
replace the FileUtilities MakeFileReadable and MakeFileExecutable 
functions. The makeWritable function is new and provided for consistency
since Path has a writable() method.


---
Diffs of the changes:  (+36 -0)

Index: llvm/lib/System/Unix/Path.cpp
diff -u llvm/lib/System/Unix/Path.cpp:1.20 llvm/lib/System/Unix/Path.cpp:1.21
--- llvm/lib/System/Unix/Path.cpp:1.20	Mon Dec 13 01:51:07 2004
+++ llvm/lib/System/Unix/Path.cpp	Mon Dec 13 13:59:50 2004
@@ -256,6 +256,42 @@
     path += '/';
 }
 
+static bool AddPermissionBits(const std::string& Filename, int bits) {
+  // Get the umask value from the operating system.  We want to use it
+  // when changing the file's permissions. Since calling umask() sets
+  // the umask and returns its old value, we must call it a second
+  // time to reset it to the user's preference.
+  int mask = umask(0777); // The arg. to umask is arbitrary.
+  umask(mask);            // Restore the umask.
+
+  // Get the file's current mode.
+  struct stat st;
+  if ((stat(Filename.c_str(), &st)) == -1)
+    return false;
+
+  // Change the file to have whichever permissions bits from 'bits'
+  // that the umask would not disable.
+  if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
+    return false;
+
+  return true;
+}
+
+void Path::makeReadable() {
+  if (!AddPermissionBits(path,0444))
+    ThrowErrno(path + ": can't make file readable");
+}
+
+void Path::makeWriteable() {
+  if (!AddPermissionBits(path,0222))
+    ThrowErrno(path + ": can't make file writable");
+}
+
+void Path::makeExecutable() {
+  if (!AddPermissionBits(path,0111))
+    ThrowErrno(path + ": can't make file executable");
+}
+
 bool
 Path::getDirectoryContents(std::set<Path>& result) const {
   if (!isDirectory())






More information about the llvm-commits mailing list