summaryrefslogtreecommitdiffstats
path: root/llvm/lib/System/Unix/Path.cpp
diff options
context:
space:
mode:
authorReid Spencer <rspencer@reidspencer.com>2004-12-13 19:59:50 +0000
committerReid Spencer <rspencer@reidspencer.com>2004-12-13 19:59:50 +0000
commit94bf2265df04e74faca356eefb77fdf53b152ae9 (patch)
treeabb8b6521ad01e3a607b22eb803dd8f6d1451f2d /llvm/lib/System/Unix/Path.cpp
parentacc4e545523c98fb55ae9ea0ef17f08192c9eb42 (diff)
downloadbcm5719-llvm-94bf2265df04e74faca356eefb77fdf53b152ae9.tar.gz
bcm5719-llvm-94bf2265df04e74faca356eefb77fdf53b152ae9.zip
For 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. llvm-svn: 18907
Diffstat (limited to 'llvm/lib/System/Unix/Path.cpp')
-rw-r--r--llvm/lib/System/Unix/Path.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/llvm/lib/System/Unix/Path.cpp b/llvm/lib/System/Unix/Path.cpp
index 6e07427608d..6733f03e441 100644
--- a/llvm/lib/System/Unix/Path.cpp
+++ b/llvm/lib/System/Unix/Path.cpp
@@ -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())
OpenPOWER on IntegriCloud