GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/fd_table.h
Date: 2026-06-21 02:37:04
Exec Total Coverage
Lines: 62 63 98.4%
Branches: 40 59 67.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_FD_TABLE_H_
6 #define CVMFS_FD_TABLE_H_
7
8 #include <errno.h>
9 #include <inttypes.h>
10 #include <stdint.h>
11
12 #include <cassert>
13 #include <cstddef>
14 #include <vector>
15
16 #include "util/single_copy.h"
17
18 /**
19 * Maintains integers mapped to custom open file descriptors. File descriptors
20 * can be added, removed, and accessed. All operations take constant time. The
21 * maximum file descriptor number needs to be known upfront.
22 *
23 * Note that new file descriptors do not necessarily have the smallest available
24 * number but any number between 0..max_open_fds.
25 *
26 * This class is used by a couple of cache managers.
27 */
28 template<class HandleT>
29 class FdTable : SingleCopy {
30 public:
31 980 FdTable(unsigned max_open_fds, const HandleT &invalid_handle)
32 980 : invalid_handle_(invalid_handle)
33 980 , fd_pivot_(0)
34
1/2
✓ Branch 2 taken 980 times.
✗ Branch 3 not taken.
980 , fd_index_(max_open_fds)
35
4/7
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 978 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
1960 , open_fds_(max_open_fds, FdWrapper(invalid_handle_, 0)) {
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980 times.
980 assert(max_open_fds > 0);
37
2/2
✓ Branch 0 taken 1496584 times.
✓ Branch 1 taken 980 times.
1497564 for (unsigned i = 0; i < max_open_fds; ++i) {
38 1496584 fd_index_[i] = i;
39 1496584 open_fds_[i].index = i;
40 }
41 980 }
42
43 /**
44 * Used to restore the state.
45 */
46 52 void AssignFrom(const FdTable<HandleT> &other) {
47 52 invalid_handle_ = other.invalid_handle_;
48 52 fd_pivot_ = other.fd_pivot_;
49 52 fd_index_.resize(other.fd_index_.size());
50
1/5
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
52 open_fds_.resize(other.open_fds_.size(), FdWrapper(invalid_handle_, 0));
51
2/2
✓ Branch 1 taken 6656 times.
✓ Branch 2 taken 52 times.
6708 for (unsigned i = 0; i < fd_index_.size(); ++i) {
52 6656 fd_index_[i] = other.fd_index_[i];
53 6656 open_fds_[i] = other.open_fds_[i];
54 }
55 52 }
56
57 /**
58 * Used to save the state.
59 */
60 52 FdTable<HandleT> *Clone() {
61 52 FdTable<HandleT> *result = new FdTable<HandleT>(open_fds_.size(),
62
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 invalid_handle_);
63 52 result->fd_pivot_ = fd_pivot_;
64
2/2
✓ Branch 1 taken 6656 times.
✓ Branch 2 taken 52 times.
6708 for (unsigned i = 0; i < fd_index_.size(); ++i) {
65 6656 result->fd_index_[i] = fd_index_[i];
66 6656 result->open_fds_[i] = open_fds_[i];
67 }
68 52 return result;
69 }
70
71
72 /**
73 * Registers fd with a currently unused number. If the table is full,
74 * returns -ENFILE;
75 */
76 10236243 int OpenFd(const HandleT &handle) {
77
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 10007940 times.
✓ Branch 2 taken 228303 times.
10236243 if (handle == invalid_handle_)
78 return -EINVAL;
79
2/2
✓ Branch 1 taken 1668532 times.
✓ Branch 2 taken 8567711 times.
10236243 if (fd_pivot_ >= fd_index_.size())
80 1668532 return -ENFILE;
81
82 8567711 size_t const next_fd = fd_index_[fd_pivot_];
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8567711 times.
8567711 assert(next_fd < open_fds_.size());
84
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 8339480 times.
✓ Branch 3 taken 228231 times.
8567711 assert(open_fds_[next_fd].handle == invalid_handle_);
85
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8567711 open_fds_[next_fd] = FdWrapper(handle, fd_pivot_);
86 8567711 ++fd_pivot_;
87 8567711 return next_fd;
88 }
89
90 /**
91 * For invalid and unused numbers, the invalid handle is returned.
92 */
93 10452239 HandleT GetHandle(int fd) {
94
2/2
✓ Branch 1 taken 8792413 times.
✓ Branch 2 taken 1659826 times.
10452239 return IsValid(fd) ? open_fds_[fd].handle : invalid_handle_;
95 }
96
97
98 /**
99 * Releases fd back to the set of available numbers. Gracefully handles
100 * invalid handles (-EBADFD)
101 */
102 10220431 int CloseFd(int fd) {
103
2/2
✓ Branch 1 taken 1653020 times.
✓ Branch 2 taken 8567411 times.
10220431 if (!IsValid(fd))
104 1653020 return -EBADF;
105
106 8567411 unsigned const index = open_fds_[fd].index;
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8567411 times.
8567411 assert(index < fd_index_.size());
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8567411 times.
8567411 assert(fd_pivot_ <= fd_index_.size());
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8567411 times.
8567411 assert(fd_pivot_ > 0);
110 8567411 open_fds_[fd].handle = invalid_handle_;
111 8567411 --fd_pivot_;
112
2/2
✓ Branch 0 taken 4763458 times.
✓ Branch 1 taken 3803953 times.
8567411 if (index < fd_pivot_) {
113 4763458 unsigned const other = fd_index_[fd_pivot_];
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4763458 times.
4763458 assert(other < open_fds_.size());
115
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 4539680 times.
✓ Branch 3 taken 223778 times.
4763458 assert(open_fds_[other].handle != invalid_handle_);
116 4763458 open_fds_[other].index = index;
117 4763458 fd_index_[index] = other;
118 4763458 fd_index_[fd_pivot_] = fd;
119 }
120 8567411 return 0;
121 }
122
123 6656 unsigned GetMaxFds() const { return fd_index_.size(); }
124
125 private:
126 struct FdWrapper {
127 8568743 FdWrapper(HandleT h, unsigned i) : handle(h), index(i) { }
128
129 HandleT handle;
130 /**
131 * Back-pointer into fd_index_, which is needed when closing a file.
132 */
133 unsigned index;
134 };
135
136
137 20672670 inline bool IsValid(int fd) {
138
6/6
✓ Branch 0 taken 20672630 times.
✓ Branch 1 taken 40 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 20672590 times.
✓ Branch 5 taken 80 times.
✓ Branch 6 taken 20672590 times.
20672670 if ((fd < 0) || (static_cast<unsigned>(fd) >= open_fds_.size()))
139 80 return false;
140 20672590 return open_fds_[fd].handle != invalid_handle_;
141 }
142
143 /**
144 * An unused (available) file descriptor.
145 */
146 HandleT invalid_handle_;
147 /**
148 * The index of the first available file descriptor in fd_index_.
149 */
150 unsigned fd_pivot_;
151 /**
152 * Maps into open_fds_. Until fd_pivot_, file descriptors are used. As of
153 * fd_pivot_, points to free file descriptors. Used to acquire new file
154 * descriptors in constant time.
155 */
156 std::vector<unsigned> fd_index_;
157 /**
158 * The file descriptor number mapped to a user-defined file descriptor
159 * (struct). The fd integer passed to users of the file descriptor table
160 * points into this array.
161 */
162 std::vector<FdWrapper> open_fds_;
163 }; // class FdTable
164
165 #endif // CVMFS_FD_TABLE_H_
166