aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Mark Loeser <halcy0n@gentoo.org> 2006-02-03 07:22:52 +0000
committerAvatar Mark Loeser <halcy0n@gentoo.org> 2006-02-03 07:22:52 +0000
commit948a1fa5d4a6d09abf20f7101e2445d449909850 (patch)
tree2dbc03d6fff021cd0e032581442124ea427af2c1
parent51cc12beb07be2e7c0bd2c3eed01497525089d48 (diff)
downloadpaludis-948a1fa5d4a6d09abf20f7101e2445d449909850.tar.gz
paludis-948a1fa5d4a6d09abf20f7101e2445d449909850.tar.xz
Replace the split permission checking functions with has_permission(). Add enums for use with the new function, and update the tests.
-rw-r--r--paludis/fs_entry.cc135
-rw-r--r--paludis/fs_entry.hh64
-rw-r--r--paludis/fs_entry_TEST.cc38
3 files changed, 87 insertions, 150 deletions
diff --git a/paludis/fs_entry.cc b/paludis/fs_entry.cc
index c59135a..2ea8f9c 100644
--- a/paludis/fs_entry.cc
+++ b/paludis/fs_entry.cc
@@ -138,100 +138,57 @@ FSEntry::is_regular_file() const
}
bool
-FSEntry::owner_has_read() const
+FSEntry::has_permission(const FSUserGroup & user_group, const FSPermission & fs_perm) const
{
_stat();
if (_exists)
- return (*_stat_info).st_mode & S_IRUSR;
-
- return false;
-}
-
-bool
-FSEntry::owner_has_write() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IWUSR;
-
- return false;
-}
-
-bool
-FSEntry::owner_has_execute() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IXUSR;
-
- return false;
-}
-
-bool
-FSEntry::group_has_read() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IRGRP;
-
- return false;
-}
-
-bool
-FSEntry::group_has_write() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IWGRP;
-
- return false;
-}
-
-bool
-FSEntry::group_has_execute() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IXGRP;
-
- return false;
-}
-
-bool
-FSEntry::others_has_read() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IROTH;
-
- return false;
-}
-
-bool
-FSEntry::others_has_write() const
-{
- _stat();
-
- if (_exists)
- return (*_stat_info).st_mode & S_IWOTH;
-
- return false;
-}
-
-bool
-FSEntry::others_has_execute() const
-{
- _stat();
+ {
+ switch (user_group)
+ {
+ case fs_ug_owner:
+ {
+ switch (fs_perm)
+ {
+ case fs_perm_read:
+ return (*_stat_info).st_mode & S_IRUSR;
+ case fs_perm_write:
+ return (*_stat_info).st_mode & S_IWUSR;
+ case fs_perm_execute:
+ return (*_stat_info).st_mode & S_IXUSR;
+ }
+ throw InternalError(PALUDIS_HERE, "Unhandled FSPermission");
+ }
+ case fs_ug_group:
+ {
+ switch (fs_perm)
+ {
+ case fs_perm_read:
+ return (*_stat_info).st_mode & S_IRGRP;
+ case fs_perm_write:
+ return (*_stat_info).st_mode & S_IWGRP;
+ case fs_perm_execute:
+ return (*_stat_info).st_mode & S_IXGRP;
+ }
+ throw InternalError(PALUDIS_HERE, "Unhandled FSPermission");
+ }
+ case fs_ug_others:
+ {
+ switch (fs_perm)
+ {
+ case fs_perm_read:
+ return (*_stat_info).st_mode & S_IROTH;
+ case fs_perm_write:
+ return (*_stat_info).st_mode & S_IWOTH;
+ case fs_perm_execute:
+ return (*_stat_info).st_mode & S_IXOTH;
+ }
+ throw InternalError(PALUDIS_HERE, "Unhandled FSPermission");
+ }
+ }
- if (_exists)
- return (*_stat_info).st_mode & S_IXOTH;
+ throw InternalError(PALUDIS_HERE, "Unhandled FSUserGroup");
+ }
return false;
}
diff --git a/paludis/fs_entry.hh b/paludis/fs_entry.hh
index 2555c09..2a5749c 100644
--- a/paludis/fs_entry.hh
+++ b/paludis/fs_entry.hh
@@ -52,6 +52,26 @@ namespace paludis
};
/**
+ * File permissions used by FSEntry
+ */
+ enum FSPermission
+ {
+ fs_perm_read, ///< read permission on file
+ fs_perm_write, ///< write permission on file
+ fs_perm_execute ///< execute permission on file
+ };
+
+ /**
+ * User classes used by FSEntry
+ */
+ enum FSUserGroup
+ {
+ fs_ug_owner, ///< owner permission
+ fs_ug_group, ///< group permission
+ fs_ug_others ///< others permission
+ };
+
+ /**
* Represents an entry (which may or may not exist) in the filesystem.
*
* \ingroup Filesystem
@@ -144,49 +164,9 @@ 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?
+ * Check if filesystem entry has `perm` for `user_group`
*/
- bool others_has_execute() const;
+ bool has_permission(const FSUserGroup & user_group, const FSPermission & fs_perm) const;
/**
* Return the last part of our path (eg '/foo/bar' => 'bar').
diff --git a/paludis/fs_entry_TEST.cc b/paludis/fs_entry_TEST.cc
index 6e18524..ed9cb3b 100644
--- a/paludis/fs_entry_TEST.cc
+++ b/paludis/fs_entry_TEST.cc
@@ -107,25 +107,25 @@ namespace test_cases
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_CHECK(a.has_permission(fs_ug_owner, fs_perm_read));
+ TEST_CHECK(a.has_permission(fs_ug_owner, fs_perm_write));
+ TEST_CHECK(a.has_permission(fs_ug_owner, fs_perm_execute));
+ TEST_CHECK(a.has_permission(fs_ug_group, fs_perm_read));
+ TEST_CHECK(a.has_permission(fs_ug_group, fs_perm_write));
+ TEST_CHECK(a.has_permission(fs_ug_group, fs_perm_execute));
+ TEST_CHECK(a.has_permission(fs_ug_others, fs_perm_read));
+ TEST_CHECK(a.has_permission(fs_ug_others, fs_perm_write));
+ TEST_CHECK(a.has_permission(fs_ug_others, fs_perm_execute));
+
+ TEST_CHECK(!b.has_permission(fs_ug_owner, fs_perm_read));
+ TEST_CHECK(!b.has_permission(fs_ug_owner, fs_perm_write));
+ TEST_CHECK(!b.has_permission(fs_ug_owner, fs_perm_execute));
+ TEST_CHECK(!b.has_permission(fs_ug_group, fs_perm_read));
+ TEST_CHECK(!b.has_permission(fs_ug_group, fs_perm_write));
+ TEST_CHECK(!b.has_permission(fs_ug_group, fs_perm_execute));
+ TEST_CHECK(!b.has_permission(fs_ug_others, fs_perm_read));
+ TEST_CHECK(!b.has_permission(fs_ug_others, fs_perm_write));
+ TEST_CHECK(!b.has_permission(fs_ug_others, fs_perm_execute));
}
} test_fs_entry_permission;
}