bool canRead(string _path)
{
assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
{
if (geteuid() == 0) // 程式的ueid 是 root使用者 ;root使用者,一定對檔案可讀可寫
return true;
//*****不是root使用者時 ,與_path這個檔案的 user group other 依次判斷
else if (st.st_uid == geteuid()) // owner 屬性
return (st.st_mode & S_IRUSR) != 0;
else if (st.st_gid == getegid()) // group 組屬性
return (st.st_mode & S_IRGRP) != 0;
else
return (st.st_mode & S_IROTH) != 0; // other 組屬性
}
return false;
}
=====================
bool FileImpl::canExecuteImpl() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
{
if (st.st_uid == geteuid() || geteuid() == 0) // 注意 root使用者可能沒有執行許可權
return (st.st_mode & S_IXUSR) != 0;
else if (st.st_gid == getegid())
return (st.st_mode & S_IXGRP) != 0;
else
return (st.st_mode & S_IXOTH) != 0;
}
else handleLastErrorImpl(_path);
return false;
}