aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Mark Loeser <halcy0n@gentoo.org> 2006-01-26 20:35:58 +0000
committerAvatar Mark Loeser <halcy0n@gentoo.org> 2006-01-26 20:35:58 +0000
commit18ffe63eb8b91364a75125decd595603b2000ff3 (patch)
tree05841b78edc04d29ff39237828018272b87036ad
parentec291b1880d1128aa266cc6c8df1d61ca77babad (diff)
downloadpaludis-18ffe63eb8b91364a75125decd595603b2000ff3.tar.gz
paludis-18ffe63eb8b91364a75125decd595603b2000ff3.tar.xz
Make FSEntry use a counted_ptr. Moved FSError exception to the constructor (when we do the stat() call), and added {owner,group,others}_has_{read,write,execute}. New testcases too.
-rw-r--r--paludis/fs_entry.cc133
-rw-r--r--paludis/fs_entry.hh52
-rw-r--r--paludis/fs_entry_TEST.cc35
-rwxr-xr-xpaludis/fs_entry_TEST_setup.sh5
4 files changed, 193 insertions, 32 deletions
diff --git a/paludis/fs_entry.cc b/paludis/fs_entry.cc
index bbd1f91..7411e52 100644
--- a/paludis/fs_entry.cc
+++ b/paludis/fs_entry.cc
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ * Copyright (c) 2006 Mark Loeser <halcy0n@gentoo.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -43,14 +44,27 @@ FSError::FSError(const std::string & message) throw () :
FSEntry::FSEntry(const std::string & path) :
ComparisonPolicyType(&FSEntry::_path),
- _path(path)
+ _path(path),
+ _stat_info(new struct stat),
+ _exists(true)
{
_normalise();
+
+ if (0 != stat(_path.c_str(), _stat_info.raw_pointer()))
+ {
+ if (errno != ENOENT)
+ throw FSError("Error running stat() on '" + stringify(_path) + "': "
+ + strerror(errno));
+
+ _exists = false;
+ }
}
FSEntry::FSEntry(const FSEntry & other) :
ComparisonPolicyType(&FSEntry::_path),
- _path(other._path)
+ _path(other._path),
+ _stat_info(other._stat_info),
+ _exists(other._exists)
{
}
@@ -94,49 +108,106 @@ FSEntry::operator/= (const FSEntry & rhs)
bool
FSEntry::exists() const
{
- struct stat s;
+ return _exists;
+}
- if (0 != stat(_path.c_str(), &s))
- {
- if (errno != ENOENT)
- throw FSError("Error checking whether '" + stringify(_path) + "' exists: "
- + strerror(errno));
- return false;
- }
+bool
+FSEntry::is_directory() const
+{
+ if(_exists)
+ return S_ISDIR((*_stat_info).st_mode);
- return true;
+ return false;
}
bool
-FSEntry::is_directory() const
+FSEntry::is_regular_file() const
{
- struct stat s;
+ if(_exists)
+ return S_ISREG((*_stat_info).st_mode);
- if (0 != stat(_path.c_str(), &s))
- {
- if (errno != ENOENT)
- throw FSError("Error checking whether '" + stringify(_path) + "' is a directory: "
- + strerror(errno));
- return false;
- }
+ return false;
+}
+
+bool
+FSEntry::owner_has_read() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IRUSR;
- return S_ISDIR(s.st_mode);
+ return false;
}
bool
-FSEntry::is_regular_file() const
+FSEntry::owner_has_write() const
{
- struct stat s;
+ if(_exists)
+ return (*_stat_info).st_mode & S_IWUSR;
- if (0 != stat(_path.c_str(), &s))
- {
- if (errno != ENOENT)
- throw FSError("Error checking whether '" + stringify(_path) + "' is a regular file: "
- + strerror(errno));
- return false;
- }
+ return false;
+}
+
+bool
+FSEntry::owner_has_execute() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IXUSR;
+
+ return false;
+}
+
+bool
+FSEntry::group_has_read() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IRGRP;
+
+ return false;
+}
+
+bool
+FSEntry::group_has_write() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IWGRP;
+
+ return false;
+}
+
+bool
+FSEntry::group_has_execute() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IXGRP;
+
+ return false;
+}
+
+bool
+FSEntry::others_has_read() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IROTH;
+
+ return false;
+}
+
+bool
+FSEntry::others_has_write() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IWOTH;
+
+ return false;
+}
+
+bool
+FSEntry::others_has_execute() const
+{
+ if(_exists)
+ return (*_stat_info).st_mode & S_IXOTH;
- return S_ISREG(s.st_mode);
+ return false;
}
void
diff --git a/paludis/fs_entry.hh b/paludis/fs_entry.hh
index d5f8a46..3243047 100644
--- a/paludis/fs_entry.hh
+++ b/paludis/fs_entry.hh
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ * Copyright (c) 2006 Mark Loeser <halcy0n@gentoo.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -24,6 +25,8 @@
#include <ostream>
#include <paludis/comparison_policy.hh>
#include <paludis/exception.hh>
+#include <paludis/counted_ptr.hh>
+#include <sys/stat.h>
/** \file
* Declarations for paludis::Filesystem.
@@ -61,6 +64,10 @@ namespace paludis
private:
std::string _path;
+ CountedPtr<struct stat, count_policy::ExternalCountTag> _stat_info;
+
+ bool _exists;
+
void _normalise();
public:
@@ -130,6 +137,51 @@ namespace paludis
bool is_regular_file() const;
/**
+ * Does the owner have read permission?
+ */
+ bool owner_has_read() const;
+
+ /**
+ * Does the owner have write permission?
+ */
+ bool owner_has_write() const;
+
+ /**
+ * Does the owner have execute permission?
+ */
+ bool owner_has_execute() const;
+
+ /**
+ * Does the group have read permission?
+ */
+ bool group_has_read() const;
+
+ /**
+ * Does the group have write permission?
+ */
+ bool group_has_write() const;
+
+ /**
+ * Does the group have execute permission?
+ */
+ bool group_has_execute() const;
+
+ /**
+ * Do the others have read permission?
+ */
+ bool others_has_read() const;
+
+ /**
+ * Do the others have write permission?
+ */
+ bool others_has_write() const;
+
+ /**
+ * Do the others have execute permission?
+ */
+ bool others_has_execute() const;
+
+ /**
* Return the last part of our path (eg '/foo/bar' => 'bar').
*/
std::string basename() const;
diff --git a/paludis/fs_entry_TEST.cc b/paludis/fs_entry_TEST.cc
index 1f25b79..c7cb456 100644
--- a/paludis/fs_entry_TEST.cc
+++ b/paludis/fs_entry_TEST.cc
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ * Copyright (c) 2006 Mark Loeser <halcy0n@gentoo.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -79,4 +80,38 @@ namespace test_cases
TEST_CHECK_EQUAL(std::string(r).substr(std::string(r).length() - e.length()), e);
}
} test_fs_entry_realpath;
+
+ /**
+ * \test Test FSEntry has_permission methods.
+ */
+ struct FSEntryHasPermission: TestCase
+ {
+ FSEntryHasPermission() : TestCase("has_permission") {}
+
+ void run()
+ {
+ FSEntry a("fs_entry_TEST_dir/all_perms");
+ FSEntry b("fs_entry_TEST_dir/no_perms");
+
+ TEST_CHECK(a.owner_has_read());
+ TEST_CHECK(a.owner_has_write());
+ TEST_CHECK(a.owner_has_execute());
+ TEST_CHECK(a.group_has_read());
+ TEST_CHECK(a.group_has_write());
+ TEST_CHECK(a.group_has_execute());
+ TEST_CHECK(a.others_has_read());
+ TEST_CHECK(a.others_has_write());
+ TEST_CHECK(a.others_has_execute());
+
+ TEST_CHECK(!b.owner_has_read());
+ TEST_CHECK(!b.owner_has_write());
+ TEST_CHECK(!b.owner_has_execute());
+ TEST_CHECK(!b.group_has_read());
+ TEST_CHECK(!b.group_has_write());
+ TEST_CHECK(!b.group_has_execute());
+ TEST_CHECK(!b.others_has_read());
+ TEST_CHECK(!b.others_has_write());
+ TEST_CHECK(!b.others_has_execute());
+ }
+ } test_fs_entry_permission;
}
diff --git a/paludis/fs_entry_TEST_setup.sh b/paludis/fs_entry_TEST_setup.sh
index 2d98823..a3d3789 100755
--- a/paludis/fs_entry_TEST_setup.sh
+++ b/paludis/fs_entry_TEST_setup.sh
@@ -6,4 +6,7 @@ cd fs_entry_TEST_dir || exit 3
mkdir dir_a || exit 4
ln -s dir_a symlink_to_dir_a || exit 5
touch dir_a/file_in_a || exit 6
-
+touch all_perms || exit 7
+chmod 777 all_perms || exit 8
+touch no_perms || exit 9
+chmod 000 no_perms || exit 10