Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
RefCountFile.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2022 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: RefCountFile.h
21  *
22  */
23 #ifndef REFCOUNTFILE_H
24 #define REFCOUNTFILE_H
25 
26 #include <Logging.h>
27 #include <sys/file.h>
28 #include <string>
29 
30 // -----------------------------------------------------------------------------
41 {
42 public:
43  RefCountFile(std::string file);
44  ~RefCountFile();
45 
46  bool IsOpen() const
47  {
48  AI_LOG_FN_ENTRY();
49 
50  AI_LOG_FN_EXIT();
51  return mOpen;
52  }
53 
54  const std::string& GetFilePath() const
55  {
56  AI_LOG_FN_ENTRY();
57 
58  AI_LOG_FN_EXIT();
59  return mFilePath;
60  }
61 
62  inline void Lock()
63  {
64  AI_LOG_FN_ENTRY();
65 
66  flock(mFd, LOCK_EX); // lock file exclusively
67 
68  AI_LOG_FN_EXIT();
69  }
70 
71  inline void Unlock()
72  {
73  AI_LOG_FN_ENTRY();
74 
75  flock(mFd, LOCK_UN); // unlock file
76 
77  AI_LOG_FN_EXIT();
78  }
79 
80  void Reset();
81  int Increment();
82  int Decrement();
83 
84 private:
85  int Read();
86  void Close();
87  int Write(int ref);
88 
89 private:
90  std::string mFilePath;
91  int mFd;
92  bool mOpen;
93 };
94 
95 
96 #endif //REFCOUNTFILE_H
Class that represents a reference count file.
Definition: RefCountFile.h:41
int Increment()
Increment the reference count in the file.
Definition: RefCountFile.cpp:105
int Write(int ref)
Write the reference count to file.
Definition: RefCountFile.cpp:189
int Decrement()
Decrement the reference count in the file.
Definition: RefCountFile.cpp:126
void Reset()
Reset the reference count in the file if it is not 0.
Definition: RefCountFile.cpp:87
int Read()
Read the reference count file.
Definition: RefCountFile.cpp:155
void Close()
Close the file descriptor if it's not already closed.
Definition: RefCountFile.cpp:62