Skip to content

Commit

Permalink
Merge pull request #2109 from elBoberido/iox-2108-public-api-to-creat…
Browse files Browse the repository at this point in the history
…e-access-rights-from-value

iox-#2108 Public API to create 'access_rights' from value
  • Loading branch information
elBoberido authored Nov 25, 2023
2 parents 5872051 + 7dd7cab commit 3621f93
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 164 deletions.
1 change: 1 addition & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
- FixedPositionContainer fails to compile on QNX QCC [#2084](https://github.com/eclipse-iceoryx/iceoryx/issues/2084)
- Chunk fails to be released when more than 4 GiB of chunks have been allocated [#2087](https://github.com/eclipse-iceoryx/iceoryx/issues/2087)
- Fix clang-tidy errors from full-scan nightly build [#2060](https://github.com/eclipse-iceoryx/iceoryx/issues/2060)
- Add public functions to create an 'access_rights' object from integer values [#2108](https://github.com/eclipse-iceoryx/iceoryx/issues/2108)

**Refactoring:**

Expand Down
20 changes: 10 additions & 10 deletions iceoryx_hoofs/filesystem/include/iox/detail/filesystem.inl
Original file line number Diff line number Diff line change
Expand Up @@ -188,53 +188,53 @@ inline bool doesEndWithPathSeparator(const iox::string<StringCapacity>& name) no
return false;
}

constexpr access_rights::value_type access_rights::value() const noexcept
inline constexpr access_rights::value_type access_rights::value() const noexcept
{
return m_value;
}

constexpr bool operator==(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr bool operator==(const access_rights lhs, const access_rights rhs) noexcept
{
return lhs.value() == rhs.value();
}

constexpr bool operator!=(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr bool operator!=(const access_rights lhs, const access_rights rhs) noexcept
{
return !(lhs == rhs);
}

constexpr access_rights operator|(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator|(const access_rights lhs, const access_rights rhs) noexcept
{
return access_rights(lhs.value() | rhs.value());
}

constexpr access_rights operator&(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator&(const access_rights lhs, const access_rights rhs) noexcept
{
return access_rights(lhs.value() & rhs.value());
}

constexpr access_rights operator^(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator^(const access_rights lhs, const access_rights rhs) noexcept
{
return access_rights(lhs.value() ^ rhs.value());
}

constexpr access_rights operator~(const access_rights value) noexcept
inline constexpr access_rights operator~(const access_rights value) noexcept
{
// AXIVION Next Construct AutosarC++19_03-A4.7.1, AutosarC++19_03-M0.3.1, FaultDetection-IntegerOverflow : Cast is safe and required due to integer promotion
return access_rights(static_cast<access_rights::value_type>(~value.value()));
}

constexpr access_rights operator|=(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator|=(const access_rights lhs, const access_rights rhs) noexcept
{
return operator|(lhs, rhs);
}

constexpr access_rights operator&=(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator&=(const access_rights lhs, const access_rights rhs) noexcept
{
return operator&(lhs, rhs);
}

constexpr access_rights operator^=(const access_rights lhs, const access_rights rhs) noexcept
inline constexpr access_rights operator^=(const access_rights lhs, const access_rights rhs) noexcept
{
return operator^(lhs, rhs);
}
Expand Down
213 changes: 75 additions & 138 deletions iceoryx_hoofs/filesystem/include/iox/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,135 +117,57 @@ class access_rights final

~access_rights() noexcept = default;

/// @brief Creates an 'access_rights' object from a sanitized value, ensuring that only the bits defined in
/// 'iox::perms::mask' are set and all other bits are reset
/// @param[in] value for the 'access_rights'
/// @return the 'access_rights' object
static constexpr access_rights from_value_sanitized(const value_type value) noexcept
{
// the code cannot be moved to the '*.inl' file since the function is used in the 'perms' namespace to define
// the 'access_rights' constants
return access_rights{static_cast<value_type>(value & detail::MASK)};
}

constexpr value_type value() const noexcept;

struct detail
{
// AXIVION DISABLE STYLE AutosarC++19_03-M2.13.2 : Filesystem permissions are defined in octal representation

// the code cannot be moved to the '*.inl' file since the functions are used in the 'perms' namespace to define
// the respective constants

/// @note Implementation detail! Please use 'iox::perms::none'.
static constexpr access_rights none() noexcept
{
constexpr value_type VALUE{0};
return access_rights{VALUE};
}
static constexpr value_type NONE{0};

/// @note Implementation detail! Please use 'iox::perms::owner_read'.
static constexpr access_rights owner_read() noexcept
{
constexpr value_type VALUE{0400};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::owner_write'.
static constexpr access_rights owner_write() noexcept
{
constexpr value_type VALUE{0200};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::owner_exec'.
static constexpr access_rights owner_exec() noexcept
{
constexpr value_type VALUE{0100};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::owner_all'.
static constexpr access_rights owner_all() noexcept
{
constexpr value_type VALUE{0700};
return access_rights{VALUE};
}
static constexpr value_type OWNER_READ{0400};
static constexpr value_type OWNER_WRITE{0200};
static constexpr value_type OWNER_EXEC{0100};
static constexpr value_type OWNER_ALL{0700};

/// @note Implementation detail! Please use 'iox::perms::group_read'.
static constexpr access_rights group_read() noexcept
{
constexpr value_type VALUE{040};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::group_write'.
static constexpr access_rights group_write() noexcept
{
constexpr value_type VALUE{020};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::group_exec'.
static constexpr access_rights group_exec() noexcept
{
constexpr value_type VALUE{010};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::group_all'.
static constexpr access_rights group_all() noexcept
{
constexpr value_type VALUE{070};
return access_rights{VALUE};
}
static constexpr value_type GROUP_READ{040};
static constexpr value_type GROUP_WRITE{020};
static constexpr value_type GROUP_EXEC{010};
static constexpr value_type GROUP_ALL{070};

/// @note Implementation detail! Please use 'iox::perms::others_read'.
static constexpr access_rights others_read() noexcept
{
constexpr value_type VALUE{04};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::others_write'.
static constexpr access_rights others_write() noexcept
{
constexpr value_type VALUE{02};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::others_exec'.
static constexpr access_rights others_exec() noexcept
{
constexpr value_type VALUE{01};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::others_all'.
static constexpr access_rights others_all() noexcept
{
constexpr value_type VALUE{07};
return access_rights{VALUE};
}
static constexpr value_type OTHERS_READ{04};
static constexpr value_type OTHERS_WRITE{02};
static constexpr value_type OTHERS_EXEC{01};
static constexpr value_type OTHERS_ALL{07};

/// @note Implementation detail! Please use 'iox::perms::all'.
static constexpr access_rights all() noexcept
{
constexpr value_type VALUE{0777};
return access_rights{VALUE};
}
static constexpr value_type ALL{0777};

/// @note Implementation detail! Please use 'iox::perms::set_uid'.
static constexpr access_rights set_uid() noexcept
{
constexpr value_type VALUE{04000};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::set_gid'.
static constexpr access_rights set_gid() noexcept
{
constexpr value_type VALUE{02000};
return access_rights{VALUE};
}
/// @note Implementation detail! Please use 'iox::perms::sticky_bit'.
static constexpr access_rights sticky_bit() noexcept
{
constexpr value_type VALUE{01000};
return access_rights{VALUE};
}
static constexpr value_type SET_UID{04000};
static constexpr value_type SET_GID{02000};
static constexpr value_type STICKY_BIT{01000};

/// @note Implementation detail! Please use 'iox::perms::mask'.
static constexpr access_rights mask() noexcept
{
constexpr value_type VALUE{07777};
return access_rights{VALUE};
}
static constexpr value_type MASK{07777};

/// @note Implementation detail! Please use 'iox::perms::unknown'.
static constexpr access_rights unknown() noexcept
{
constexpr value_type VALUE{0xFFFFU};
return access_rights{VALUE};
// Intentionally different from 'std::filesystem::perms::unknown' (which is 0xFFFF) to prevent unexpected
// results. Combining a permission set to 'std::filesystem::perms::unknown' with other permission flags
// using bitwise AND may have an unexpected result, e.g. 'std::filesystem::perms::unknown &
// std::filesystem::perms::mask' results in having all permission flags set to 1. By using '0x8000' only the
// MSB is 1 and all permission bits are set to 0 and a bitwise AND will therefore also always result in a 0.
constexpr value_type UNKNOWN{0x8000U};
return access_rights{UNKNOWN};
}

// AXIVION ENABLE STYLE AutosarC++19_03-M2.13.2
Expand All @@ -262,8 +185,6 @@ class access_rights final
friend constexpr access_rights operator^=(const access_rights lhs, const access_rights rhs) noexcept;

private:
template <typename>
friend struct FileManagementInterface;
explicit constexpr access_rights(value_type value) noexcept
: m_value(value)
{
Expand All @@ -278,55 +199,71 @@ namespace perms
// AXIVION DISABLE STYLE AutosarC++19_03-A2.10.5 : Name reuse is intentional since they refer to the same value. Additionally, different namespaces are used.

/// @brief Deny everything
static constexpr access_rights none{access_rights::detail::none()};
/// @note the underlying value is '0'
static constexpr auto none{access_rights::from_value_sanitized(access_rights::detail::NONE)};

/// @brief owner has read permission
static constexpr access_rights owner_read{access_rights::detail::owner_read()};
/// @note the underlying value is '0o400'
static constexpr auto owner_read{access_rights::from_value_sanitized(access_rights::detail::OWNER_READ)};
/// @brief owner has write permission
static constexpr access_rights owner_write{access_rights::detail::owner_write()};
/// @note the underlying value is '0o200'
static constexpr auto owner_write{access_rights::from_value_sanitized(access_rights::detail::OWNER_WRITE)};
/// @brief owner has execution permission
static constexpr access_rights owner_exec{access_rights::detail::owner_exec()};
/// @note the underlying value is '0o100'
static constexpr auto owner_exec{access_rights::from_value_sanitized(access_rights::detail::OWNER_EXEC)};
/// @brief owner has all permissions
static constexpr access_rights owner_all{access_rights::detail::owner_all()};
/// @note the underlying value is '0o700'
static constexpr auto owner_all{access_rights::from_value_sanitized(access_rights::detail::OWNER_ALL)};

/// @brief group has read permission
static constexpr access_rights group_read{access_rights::detail::group_read()};
/// @note the underlying value is '0o040'
static constexpr auto group_read{access_rights::from_value_sanitized(access_rights::detail::GROUP_READ)};
/// @brief group has write permission
static constexpr access_rights group_write{access_rights::detail::group_write()};
/// @note the underlying value is '0o020'
static constexpr auto group_write{access_rights::from_value_sanitized(access_rights::detail::GROUP_WRITE)};
/// @brief group has execution permission
static constexpr access_rights group_exec{access_rights::detail::group_exec()};
/// @note the underlying value is '0o010'
static constexpr auto group_exec{access_rights::from_value_sanitized(access_rights::detail::GROUP_EXEC)};
/// @brief group has all permissions
static constexpr access_rights group_all{access_rights::detail::group_all()};
/// @note the underlying value is '0o070'
static constexpr auto group_all{access_rights::from_value_sanitized(access_rights::detail::GROUP_ALL)};

/// @brief others have read permission
static constexpr access_rights others_read{access_rights::detail::others_read()};
/// @note the underlying value is '0o004'
static constexpr auto others_read{access_rights::from_value_sanitized(access_rights::detail::OTHERS_READ)};
/// @brief others have write permission
static constexpr access_rights others_write{access_rights::detail::others_write()};
/// @note the underlying value is '0o002'
static constexpr auto others_write{access_rights::from_value_sanitized(access_rights::detail::OTHERS_WRITE)};
/// @brief others have execution permission
static constexpr access_rights others_exec{access_rights::detail::others_exec()};
/// @note the underlying value is '0o001'
static constexpr auto others_exec{access_rights::from_value_sanitized(access_rights::detail::OTHERS_EXEC)};
/// @brief others have all permissions
static constexpr access_rights others_all{access_rights::detail::others_all()};
/// @note the underlying value is '0o007'
static constexpr auto others_all{access_rights::from_value_sanitized(access_rights::detail::OTHERS_ALL)};

/// @brief all permissions for everyone
static constexpr access_rights all{access_rights::detail::all()};
/// @note the underlying value is '0o777'
static constexpr auto all{access_rights::from_value_sanitized(access_rights::detail::ALL)};

/// @brief set uid bit
/// @note introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
/// @note the underlying value is '0o4000'; introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
// AXIVION Next Construct AutosarC++19_03-M2.10.1: The constant is in a namespace and mimics the C++17 STL equivalent
static constexpr access_rights set_uid{access_rights::detail::set_uid()};
static constexpr auto set_uid{access_rights::from_value_sanitized(access_rights::detail::SET_UID)};
/// @brief set gid bit
/// @note introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
/// @note the underlying value is '0o2000'; introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
// AXIVION Next Construct AutosarC++19_03-M2.10.1: The constant is in a namespace and mimics the C++17 STL equivalent
static constexpr access_rights set_gid{access_rights::detail::set_gid()};
static constexpr auto set_gid{access_rights::from_value_sanitized(access_rights::detail::SET_GID)};
/// @brief set sticky bit
/// @note sticky bit introduction: https://en.wikipedia.org/wiki/Sticky_bit
static constexpr access_rights sticky_bit{access_rights::detail::sticky_bit()};
/// @note the underlying value is '0o1000'; sticky bit introduction: https://en.wikipedia.org/wiki/Sticky_bit
static constexpr auto sticky_bit{access_rights::from_value_sanitized(access_rights::detail::STICKY_BIT)};

/// @brief all permissions for everyone as well as uid, gid and sticky bit
static constexpr access_rights mask{access_rights::detail::mask()};
/// @note the underlying value is '0o7777'
static constexpr auto mask{access_rights::from_value_sanitized(access_rights::detail::MASK)};

/// @brief unknown permissions
static constexpr access_rights unknown{access_rights::detail::unknown()};
/// @note the underlying value is '0x8000'
static constexpr auto unknown{access_rights::detail::unknown()};

// AXIVION ENABLE STYLE AutosarC++19_03-A2.10.5
} // namespace perms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ inline expected<access_rights, FileStatError> FileManagementInterface<Derived>::

// st_mode also contains the file type, since we only would like to acquire the permissions
// we have to remove the file type
constexpr uint16_t ONLY_FILE_PERMISSIONS = 0777;
return ok(access_rights(static_cast<access_rights::value_type>(result->st_mode & ONLY_FILE_PERMISSIONS)));
auto permissions_only = static_cast<access_rights::value_type>(result->st_mode & iox::perms::all.value());
return ok(access_rights::from_value_sanitized(permissions_only));
}

template <typename Derived>
Expand Down
Loading

0 comments on commit 3621f93

Please sign in to comment.