From 77cc91deaaa06913047869726d9d2890f951aba7 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Mon, 13 Dec 2004 19:59:50 +0000 Subject: 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18907 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/System/Unix/Path.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/System/Unix/Path.cpp') diff --git a/lib/System/Unix/Path.cpp b/lib/System/Unix/Path.cpp index 6e07427608..6733f03e44 100644 --- a/lib/System/Unix/Path.cpp +++ b/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& result) const { if (!isDirectory()) -- cgit v1.2.3-70-g09d2