iceoryx_doc  1.0.1
file_lock.hpp
1 // Copyright (c) 2021 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_UTILS_POSIX_WRAPPER_FILE_LOCK_HPP
17 #define IOX_UTILS_POSIX_WRAPPER_FILE_LOCK_HPP
18 
19 #include "iceoryx_utils/cxx/expected.hpp"
20 #include "iceoryx_utils/cxx/string.hpp"
21 #include "iceoryx_utils/design_pattern/creation.hpp"
22 
23 namespace iox
24 {
25 namespace posix
26 {
27 enum class FileLockError
28 {
29  INVALID_STATE,
30  NO_FILE_NAME_PROVIDED,
31  LOCKED_BY_OTHER_PROCESS,
32  ACCESS_DENIED,
33  INVALID_FILE_NAME,
34  QUOTA_EXHAUSTED,
35  INVALID_CHARACTERS_IN_FILE_NAME,
36  SYSTEM_LIMIT,
37  PROCESS_LIMIT,
38  NO_SUCH_DIRECTORY,
39  SPECIAL_FILE,
40  FILE_TOO_LARGE,
41  FILE_IN_USE,
42  OUT_OF_MEMORY,
43  I_O_ERROR,
44  SYS_CALL_NOT_IMPLEMENTED,
45  INTERNAL_LOGIC_ERROR,
46 };
47 
48 
49 #if defined(QNX) || defined(QNX__) || defined(__QNX__)
50 constexpr char PATH_PREFIX[] = "/var/lock/";
51 #else
52 constexpr char PATH_PREFIX[] = "/tmp/";
53 #endif
54 
55 
56 
71 class FileLock : public DesignPattern::Creation<FileLock, FileLockError>
72 {
73  public:
74  static constexpr int32_t ERROR_CODE = -1;
75  static constexpr int32_t INVALID_FD = -1;
78 
79  FileLock(const FileLock&) = delete;
80  FileLock& operator=(const FileLock&) = delete;
81  FileLock(FileLock&& rhs) noexcept;
82  FileLock& operator=(FileLock&& rhs) noexcept;
83 
84  ~FileLock() noexcept;
85 
86  private:
87  int32_t m_fd{INVALID_FD};
88  FileName_t m_name{""};
89 
92  FileLock(const FileName_t& name) noexcept;
93 
94  cxx::expected<FileLockError> initializeFileLock() noexcept;
95  FileLockError convertErrnoToFileLockError(const int32_t errnum) const noexcept;
96  cxx::expected<FileLockError> closeFileDescriptor() noexcept;
97 
98  friend class DesignPattern::Creation<FileLock, FileLockError>;
99 };
100 } // namespace posix
101 } // namespace iox
102 
103 #endif // IOX_UTILS_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:72
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28