Add implementation of FileOpenable().

Refactor MakeFileExecutable() & MakeFileExecutable() into AddPermissionsBits().


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9892 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Brian Gaeke 2003-11-11 18:27:21 +00:00
parent 6a9127ec00
commit 56be7ffe58

View File

@ -20,6 +20,21 @@
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>
/// FileOpenable - Returns true IFF Filename names an existing regular
/// file which we can successfully open.
///
bool FileOpenable (const std::string &Filename) {
struct stat s;
if (stat (Filename.c_str (), &s) == -1)
return false; // Cannot stat file
if (!S_ISREG (s.st_mode))
return false; // File is not a regular file
std::ifstream FileStream (Filename.c_str ());
if (!FileStream)
return false; // File is not openable
return true;
}
/// DiffFiles - Compare the two files specified, returning true if they are /// DiffFiles - Compare the two files specified, returning true if they are
/// different or if there is a file error. If you specify a string to fill in /// different or if there is a file error. If you specify a string to fill in
/// for the error option, it will set the string to an error message if an error /// for the error option, it will set the string to an error message if an error
@ -100,111 +115,41 @@ std::string getUniqueFilename(const std::string &FilenameBase) {
return Result; return Result;
} }
/// static bool AddPermissionsBits (const std::string &Filename, mode_t bits) {
/// Method: MakeFileExecutable () // Get the umask value from the operating system. We want to use it
/// // when changing the file's permissions. Since calling umask() sets
/// Description: // the umask and returns its old value, we must call it a second
/// This method makes the specified filename executable by giving it // time to reset it to the user's preference.
/// execute permission. It respects the umask value of the process, and it mode_t mask = umask (0777); // The arg. to umask is arbitrary...
/// does not enable any unnecessary access bits.
///
/// Algorithm:
/// o Get file's current permissions.
/// o Get the process's current umask.
/// o Take the set of all execute bits and disable those found in the umask.
/// o Add the remaining permissions to the file's permissions.
///
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); umask (mask);
// // Get the file's current mode.
// Go fetch the file's current permission bits. We want to *add* execute struct stat st;
// access to the file. if ((stat (Filename.c_str(), &st)) == -1)
//
if ((stat (Filename.c_str(), &fstat)) == -1)
{
return false; return false;
}
// // Change the file to have whichever permissions bits from 'bits'
// Make the file executable... // that the umask would not disable.
// if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
if ((chmod(Filename.c_str(), (fstat.st_mode | (0111 & ~mask)))) == -1)
{
return false; return false;
}
return true; return true;
} }
/// MakeFileExecutable - Make the file named Filename executable by
/// setting whichever execute permissions bits the process's current
/// umask would allow. Filename must name an existing file or
/// directory. Returns true on success, false on error.
/// ///
/// Method: MakeFileReadable () bool MakeFileExecutable (const std::string &Filename) {
/// return AddPermissionsBits (Filename, 0111);
/// Description:
/// This method makes the specified filename readable by giving it
/// read permission. It respects the umask value of the process, and it
/// does not enable any unnecessary access bits.
///
/// Algorithm:
/// o Get file's current permissions.
/// o Get the process's current umask.
/// o Take the set of all read bits and disable those found in the umask.
/// o Add the remaining permissions to the file's permissions.
///
bool
MakeFileReadable (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 file executable...
//
if ((chmod(Filename.c_str(), (fstat.st_mode | (0444 & ~mask)))) == -1)
{
return false;
}
return true;
} }
/// MakeFileReadable - Make the file named Filename readable by
/// setting whichever read permissions bits the process's current
/// umask would allow. Filename must name an existing file or
/// directory. Returns true on success, false on error.
///
bool MakeFileReadable (const std::string &Filename) {
return AddPermissionsBits (Filename, 0444);
}