iceoryx_hoofs  2.0.2
file_lock.hpp
1 // Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_HOOFS_POSIX_WRAPPER_FILE_LOCK_HPP
17 #define IOX_HOOFS_POSIX_WRAPPER_FILE_LOCK_HPP
18 
19 #include "iceoryx_hoofs/cxx/expected.hpp"
20 #include "iceoryx_hoofs/cxx/string.hpp"
21 #include "iceoryx_hoofs/design_pattern/creation.hpp"
22 
23 namespace iox
24 {
25 namespace posix
26 {
27 enum class FileLockError
28 {
29  INVALID_FILE_NAME,
30  LOCKED_BY_OTHER_PROCESS,
31  ACCESS_DENIED,
32  QUOTA_EXHAUSTED,
33  INVALID_CHARACTERS_IN_FILE_NAME,
34  SYSTEM_LIMIT,
35  PROCESS_LIMIT,
36  NO_SUCH_DIRECTORY,
37  SPECIAL_FILE,
38  FILE_TOO_LARGE,
39  FILE_IN_USE,
40  OUT_OF_MEMORY,
41  I_O_ERROR,
42  SYS_CALL_NOT_IMPLEMENTED,
43  INTERNAL_LOGIC_ERROR,
44 };
45 
60 class FileLock : public DesignPattern::Creation<FileLock, FileLockError>
61 {
62  public:
63  static constexpr int32_t ERROR_CODE = -1;
64  static constexpr int32_t INVALID_FD = -1;
65  static constexpr const char LOCK_FILE_SUFFIX[] = ".lock";
66  static constexpr uint64_t FILENAME_LENGTH = platform::IOX_MAX_FILENAME_LENGTH
67  - sizeof(platform::IOX_LOCK_FILE_PATH_PREFIX) / sizeof(char)
68  - sizeof(LOCK_FILE_SUFFIX) / sizeof(char);
69 
72 
73  FileLock(const FileLock&) = delete;
74  FileLock& operator=(const FileLock&) = delete;
75  FileLock(FileLock&& rhs) noexcept;
76  FileLock& operator=(FileLock&& rhs) noexcept;
77 
78  ~FileLock() noexcept;
79 
80  private:
81  int32_t m_fd{INVALID_FD};
82  FileName_t m_name;
83  PathName_t m_fileLockPath;
84 
87  explicit FileLock(const FileName_t& name) noexcept;
88 
89  void invalidate() noexcept;
90 
91  cxx::expected<FileLockError> initializeFileLock() noexcept;
92  FileLockError convertErrnoToFileLockError(const int32_t errnum) const noexcept;
93  cxx::expected<FileLockError> closeFileDescriptor() noexcept;
94 
95  friend class DesignPattern::Creation<FileLock, FileLockError>;
96 };
97 } // namespace posix
98 } // namespace iox
99 
100 #endif // IOX_HOOFS_POSIX_WRAPPER_FILE_LOCK_HPP
This pattern can be used if you write an abstraction where you have to throw an exception in the cons...
Definition: creation.hpp:99
Posix file lock C++ wrapping class Following RAII, the lock is acquired on creation and released on d...
Definition: file_lock.hpp:61
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29