For PR351:
authorReid Spencer <rspencer@reidspencer.com>
Mon, 13 Dec 2004 19:59:50 +0000 (19:59 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Mon, 13 Dec 2004 19:59:50 +0000 (19:59 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18907 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Path.h
lib/System/Unix/Path.cpp
lib/System/Unix/Path.inc
lib/System/Win32/Path.cpp
lib/System/Win32/Path.inc

index 57cbc6c64d905d3799a0a1e8d717d06f10735f38..3731a9c7a319deabcf3c42f22234b3e96b3f6efd 100644 (file)
@@ -391,6 +391,22 @@ namespace sys {
         StatusInfo info; getStatusInfo(info); return info.fileSize;
       }
 
+      /// This method attempts to make the file referenced by the Path object
+      /// available for reading so that the readable() method will return true.
+      /// @brief Make the file readable;
+      void makeReadable();
+
+      /// This method attempts to make the file referenced by the Path object
+      /// available for writing so that the writable() method will return true.
+      /// @brief Make the file writable;
+      void makeWriteable();
+
+      /// This method attempts to make the file referenced by the Path object
+      /// available for execution so that the executable() method will return 
+      /// true.
+      /// @brief Make the file readable;
+      void makeExecutable();
+
       /// This method attempts to set the Path object to \p unverified_path
       /// and interpret the name as a directory name.  The \p unverified_path 
       /// is verified. If verification succeeds then \p unverified_path 
index 6e07427608db4a0c80a41180dc981da59d33330b..6733f03e441588658f18c0c26690a0d140f004e1 100644 (file)
@@ -256,6 +256,42 @@ Path::getStatusInfo(StatusInfo& info) const {
     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())
index 6e07427608db4a0c80a41180dc981da59d33330b..6733f03e441588658f18c0c26690a0d140f004e1 100644 (file)
@@ -256,6 +256,42 @@ Path::getStatusInfo(StatusInfo& info) const {
     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())
index e847154e583623226ba8ccdb1a49ef677724f61b..43d553bbd99346f6d03a467f7fa517a3c9a046fd 100644 (file)
@@ -287,6 +287,15 @@ Path::getLast() const {
   return path.substr(pos+1);
 }
 
+void Path::makeReadable() {
+}
+
+void Path::makeWriteable() {
+}
+
+void Path::makeExecutable() {
+}
+
 bool
 Path::setDirectory(const std::string& a_path) {
   if (a_path.size() == 0)
index e847154e583623226ba8ccdb1a49ef677724f61b..43d553bbd99346f6d03a467f7fa517a3c9a046fd 100644 (file)
@@ -287,6 +287,15 @@ Path::getLast() const {
   return path.substr(pos+1);
 }
 
+void Path::makeReadable() {
+}
+
+void Path::makeWriteable() {
+}
+
+void Path::makeExecutable() {
+}
+
 bool
 Path::setDirectory(const std::string& a_path) {
   if (a_path.size() == 0)