Skip to content

Commit

Permalink
Merge pull request #1061 from ApexAI/iox-#1059-create-c++17-perms-enum
Browse files Browse the repository at this point in the history
Iox #1059 create c++17 perms enum
  • Loading branch information
elfenpiff authored Feb 4, 2022
2 parents 2c5f6a2 + 180bb7d commit 1db8749
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 24 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
needs: pre-flight-check
steps:
- uses: actions/checkout@v2
- uses: egor-tensin/setup-gcc@v1.2
- uses: egor-tensin/setup-gcc@v1
with:
version: 8
platform: x64
Expand Down Expand Up @@ -78,6 +78,10 @@ jobs:
runs-on: ubuntu-20.04
needs: pre-flight-check
steps:
- uses: egor-tensin/setup-clang@v1
with:
version: 10
platform: x64
- uses: actions/checkout@v2
- run: ./tools/ci/build-test-ubuntu-with-sanitizers.sh clang

Expand All @@ -94,6 +98,10 @@ jobs:
runs-on: ubuntu-latest
needs: pre-flight-check
steps:
- uses: egor-tensin/setup-clang@v1
with:
version: 13
platform: x64
- uses: actions/checkout@v2
- run: ./tools/ci/build-ubuntu-with-latest-clang-gcc.sh clang

Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Add requirePublisherHistorySupport option at subscriber side (if set to true requires historyRequest <= historyCapacity to be eligible for connection) [#1029](https://github.com/eclipse-iceoryx/iceoryx/issues/1029)
- Add `/tools/scripts/ice_env.sh` shell script to provide simple access to docker containers for CI debugging [#1049](https://github.com/eclipse-iceoryx/iceoryx/issues/1049)
- Introduce `cxx::FunctionalInterface` to enrich nullable classes with `and_then`, `or_else`, `value_or`, `expect` [\#996](https://github.com/eclipse-iceoryx/iceoryx/issues/996)
- Add C++17 `std::perms` as `cxx::perms` to `iceoryx_hoofs/cxx/filesystem.hpp`. [#1059](https://github.com/eclipse-iceoryx/iceoryx/issues/1059)

**Bugfixes:**

Expand Down Expand Up @@ -65,6 +66,18 @@
- Updating Codecov API and enforce CMake version 3.16 for building iceoryx [\#774](https://github.com/eclipse-iceoryx/iceoryx/issues/774) and [\#1031](https://github.com/eclipse-iceoryx/iceoryx/issues/1031)
- Remove `InvalidIdString` and `isValid()` from `ServiceDescription`, replace Wildcard string with `iox::cxx::nullopt` [\#415](https://github.com/eclipse-iceoryx/iceoryx/issues/415)

**New API features:**

- Introduce `iceoryx_hoofs/cxx/filesystem.hpp` which implements `std::perms` as `cxx::perms`.
```cpp
#include "iceoryx_hoofs/cxx/filesystem.hpp"

// ...
cxx::perms filePermissions;
filePermissions = cxx::perms::owner_read | cxx::perms::group_write;
std::cout << filePermissions << std::endl;
```

**API Breaking Changes:**

The CMake files in iceoryx expect to have CMake version 3.16 or greater installed, otherwise the build fails.
Expand Down
1 change: 1 addition & 0 deletions iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ add_library(iceoryx_hoofs
source/concurrent/active_object.cpp
source/concurrent/loffli.cpp
source/cxx/deadline_timer.cpp
source/cxx/filesystem.cpp
source/cxx/helplets.cpp
source/cxx/requires.cpp
source/cxx/generic_raii.cpp
Expand Down
1 change: 1 addition & 0 deletions iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class should be used.
|`attributes` | | | C++17 and C++20 attributes are sometimes available through compiler extensions. The attribute macros defined in here (like `IOX_FALLTHROUGH`, `IOX_MAYBE_UNUSED` ... ) make sure that we are able to use them if the compiler supports it. |
|`convert` | | | Converting a number into a string is easy, converting it back can be hard. You can use functions like `strtoll` but you still have to handle errors like under- and overflow, or converting invalid strings into number. Here we abstract all the error handling so that you can convert strings into numbers safely. |
|`expected` | | | Our base class used in error handling. Every function which can fail should return an expected. With this the user knows that this function can fail and that they have to do some kind of error handling. We got inspired by the [C++ expected proposal]( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0323r7.html) and by the [rust error handling concept](https://doc.rust-lang.org/std/result/enum.Result.html). |
|`filesystem` | | | Implementation of C++17 filesystem features for instance `cxx::perms` to abstract file permissions |
|`forward_list` | | | Heap and exception free, relocatable implementation of `std::forward_list` |
|`function_ref` | | | C++11 implementation of the next-gen C++ feature `std::function_ref` see [function_ref proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0792r2.html). It behaves like `std::function` but does not own the callable. |
|`GenericRAII` | | | This is an abstraction of the C++ RAII idiom. Sometimes you have constructs where you would like to perform a certain task on creation and then again when they are getting out of scope, this is where `GenericRAII` comes in. It is like a `std::lock_guard` or a `std::shared_ptr` but more generic. |
Expand Down
134 changes: 134 additions & 0 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2022 by Apex.AI Inc. 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_FILESYSTEM_HPP
#define IOX_HOOFS_CXX_FILESYSTEM_HPP

#include <cstdint>

namespace iox
{
namespace cxx
{
/// @brief this enum class implements the filesystem perms feature of C++17. The
/// API is identical to the C++17 one so that the class can be removed
/// as soon as iceoryx switches to C++17.
/// The enum satisfies also all requirements of the BitmaskType, this means
/// the operators `|`, `&`, `^`, `~`, `|=`, `&=` and `^=` are implemented as
/// free functions as C++17 requires it.
enum class perms : uint64_t
{
/// @brief Deny everything
none = 0,

/// @brief owner has read permission
owner_read = 0400,
/// @brief owner has write permission
owner_write = 0200,
/// @brief owner has execution permission
owner_exec = 0100,
/// @brief owner has all permissions
owner_all = 0700,

/// @brief group has read permission
group_read = 040,
/// @brief group has write permission
group_write = 020,
/// @brief group has execution permission
group_exec = 010,
/// @brief group has all permissions
group_all = 070,

/// @brief others have read permission
others_read = 04,
/// @brief others have write permission
others_write = 02,
/// @brief others have execution permission
others_exec = 01,
/// @brief others have all permissions
others_all = 07,

/// @brief all permissions for everyone
all = 0777,

/// @brief set uid bit
/// @note introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
set_uid = 04000,
/// @brief set gid bit
/// @note introduction into setgit/setuid: https://en.wikipedia.org/wiki/Setuid
set_gid = 02000,
/// @brief set sticky bit
/// @note sticky bit introduction: https://en.wikipedia.org/wiki/Sticky_bit
sticky_bit = 01000,

/// @brief all permissions for everyone as well as uid, gid and sticky bit
mask = 07777,

/// @brief unknown permissions
unknown = 0xFFFF
};

/// @brief Implements the binary or operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs | rhs
perms operator|(const perms& lhs, const perms& rhs) noexcept;

/// @brief Implements the binary and operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs & rhs
perms operator&(const perms& lhs, const perms& rhs) noexcept;

/// @brief Implements the binary exclusive or operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs ^ rhs
perms operator^(const perms& lhs, const perms& rhs) noexcept;

/// @brief Implements the binary complement operation
/// @param[in] value the value used for the operation
/// @return ~value
perms operator~(const perms& value) noexcept;

/// @brief Implements the binary or assignment operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs = lhs | rhs
perms operator|=(perms& lhs, const perms& rhs) noexcept;

/// @brief Implements the binary and assignment operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs = lhs & rhs
perms operator&=(perms& lhs, const perms& rhs) noexcept;

/// @brief Implements the binary exclusive or assignment operation
/// @param[in] lhs left hand side of the operation
/// @param[in] rhs right hand side of the operation
/// @return lhs = lhs ^ rhs
perms operator^=(perms& lhs, const perms& rhs) noexcept;

/// @brief The streaming operator for the perms enum. It handles the enum as if
/// it was a bitset and always lists the values for owner, group, others, special bits
/// @param[in] stream reference to the stream
/// @param[in] value the file permission
/// @return the reference to the stream
template <typename StreamType>
StreamType& operator<<(StreamType& stream, perms value) noexcept;
} // namespace cxx
} // namespace iox

#endif
188 changes: 188 additions & 0 deletions iceoryx_hoofs/source/cxx/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright (c) 2022 by Apex.AI Inc. 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/cxx/filesystem.hpp"
#include "iceoryx_hoofs/log/logstream.hpp"
#include <iostream>
#include <type_traits>

namespace iox
{
namespace cxx
{
perms operator|(const perms& lhs, const perms& rhs) noexcept
{
using T = std::underlying_type<perms>::type;
return static_cast<perms>(static_cast<T>(lhs) | static_cast<T>(rhs));
}

perms operator&(const perms& lhs, const perms& rhs) noexcept
{
using T = std::underlying_type<perms>::type;
return static_cast<perms>(static_cast<T>(lhs) & static_cast<T>(rhs));
}

perms operator^(const perms& lhs, const perms& rhs) noexcept
{
using T = std::underlying_type<perms>::type;
return static_cast<perms>(static_cast<T>(lhs) ^ static_cast<T>(rhs));
}

perms operator~(const perms& value) noexcept
{
using T = std::underlying_type<perms>::type;
return static_cast<perms>(~static_cast<T>(value));
}

perms operator|=(perms& lhs, const perms& rhs) noexcept
{
return lhs = lhs | rhs;
}

perms operator&=(perms& lhs, const perms& rhs) noexcept
{
return lhs = lhs & rhs;
}

perms operator^=(perms& lhs, const perms& rhs) noexcept
{
return lhs = lhs ^ rhs;
}

template <typename StreamType>
StreamType& operator<<(StreamType& stream, perms value) noexcept
{
if (value == perms::unknown)
{
stream << "unknown permissions";
return stream;
}

bool hasPrecedingEntry = false;
auto outputToStream = [&](const char* text) {
if (hasPrecedingEntry)
{
stream << ", ";
}
hasPrecedingEntry = true;

stream << text;
};

auto finishEntry = [&](bool isLastEntry = false) {
if (hasPrecedingEntry)
{
stream << "}";
}
else
{
stream << "none}";
}

if (!isLastEntry)
{
stream << ", ";
}
hasPrecedingEntry = false;
};

// owner
stream << "owner: {";

if ((value & perms::owner_read) != perms::none)
{
outputToStream("read");
}

if ((value & perms::owner_write) != perms::none)
{
outputToStream("write");
}

if ((value & perms::owner_exec) != perms::none)
{
outputToStream("execute");
}

finishEntry();

// group
stream << "group: {";

if ((value & perms::group_read) != perms::none)
{
outputToStream("read");
}

if ((value & perms::group_write) != perms::none)
{
outputToStream("write");
}

if ((value & perms::group_exec) != perms::none)
{
outputToStream("execute");
}

finishEntry();

// other
stream << "others: {";

if ((value & perms::others_read) != perms::none)
{
outputToStream("read");
}

if ((value & perms::others_write) != perms::none)
{
outputToStream("write");
}

if ((value & perms::others_exec) != perms::none)
{
outputToStream("execute");
}

finishEntry();

// special bits
stream << "special bits: {";
if ((value & perms::set_uid) != perms::none)
{
outputToStream("set_uid");
}

if ((value & perms::set_gid) != perms::none)
{
outputToStream("set_git");
}

if ((value & perms::sticky_bit) != perms::none)
{
outputToStream("sticky_bit");
}

constexpr bool IS_LAST_ENTRY = true;
finishEntry(IS_LAST_ENTRY);

return stream;
}

template std::ostream& operator<<(std::ostream&, perms) noexcept;
template log::LogStream& operator<<(log::LogStream&, perms) noexcept;
} // namespace cxx
} // namespace iox
Loading

0 comments on commit 1db8749

Please sign in to comment.