GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_mem.h
Date: 2025-06-22 02:36:02
Exec Total Coverage
Lines: 13 13 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_NETWORK_SINK_MEM_H_
6 #define CVMFS_NETWORK_SINK_MEM_H_
7
8 #include <cassert>
9 #include <cstring>
10 #include <string>
11
12 #include "sink.h"
13 #include "util/posix.h"
14 #include "util/smalloc.h"
15
16 namespace cvmfs {
17
18 /**
19 * MemSink is a data sink that writes to a unsigned char* buffer.
20 * The buffer has a fixed size, as such reservation of space is necessary.
21 * It can use Adopt() to write to a different buffer.
22 *
23 * By default, MemSink is the owner of the buffer and takes care of its
24 * creation and deletion.
25 */
26 class MemSink : public Sink {
27 public:
28 4159 MemSink()
29 4159 : Sink(true), size_(0), pos_(0), data_(NULL), max_size_(kMaxMemSize) { }
30 explicit MemSink(size_t size);
31 8318 virtual ~MemSink() { FreeData(); }
32
33 /**
34 * Appends data to the sink
35 * If the sink is too small and
36 * - the sink is the owner of data_: sink size is increased
37 * - the sink is NOT the owner of data_: fails with -ENOSPC
38 *
39 * @returns on success: number of bytes written (can be less than requested)
40 * on failure: -errno.
41 */
42 virtual int64_t Write(const void *buf, uint64_t sz);
43
44 /**
45 * Truncate all written data and start over at position zero.
46 *
47 * @returns Success = 0
48 * Failure = -errno
49 */
50 virtual int Reset();
51
52 /**
53 * Purges all resources leaving the sink in an invalid state.
54 * More aggressive version of Reset().
55 * For some sinks and depending on owner status it might do
56 * the same as Reset().
57 *
58 * @returns Success = 0
59 * Failure = -errno
60 */
61 193 virtual int Purge() { return Reset(); }
62
63 /**
64 * @returns true if the object is correctly initialized.
65 */
66 virtual bool IsValid();
67
68 /**
69 * Commit data to the sink
70 * @returns success = 0
71 * failure = -errno
72 */
73 3769 virtual int Flush() { return 0; }
74
75 /**
76 * Reserves new space in sinks that require reservation (see RequiresReserve)
77 *
78 * Successful if the requested size is smaller than already space reserved, or
79 * if the sink is the owner of the data and can allocate enough new space.
80 *
81 * @note If successful, always resets the current position to 0.
82 *
83 * Fails if
84 * 1) sink is not the owner of the data and more than the current size is
85 * requested
86 * 2) more space is requested than allowed (max_size_)
87 *
88 * @returns success = true
89 * failure = false
90 */
91 virtual bool Reserve(size_t size);
92
93 /**
94 * Returns if the specific sink type needs reservation of (data) space
95 *
96 * @returns true - reservation is needed
97 * false - no reservation is needed
98 */
99 15036 virtual bool RequiresReserve() { return true; }
100
101 /**
102 * Return a string representation describing the type of sink and its status
103 */
104 virtual std::string Describe();
105
106 /**
107 * Allows the sink to adopt data that was initialized outside this class.
108 * The sink can become the new owner of the data, or not.
109 */
110 void Adopt(size_t size, size_t pos, unsigned char *data,
111 bool is_owner = true);
112
113 4060 size_t size() { return size_; }
114 3768 size_t pos() { return pos_; }
115 3720 unsigned char *data() { return data_; }
116
117 /**
118 * Do not download files larger than 1M into memory.
119 */
120 static const size_t kMaxMemSize = 1024ul * 1024ul;
121
122 private:
123 8118 void FreeData() {
124
2/2
✓ Branch 0 taken 4792 times.
✓ Branch 1 taken 3326 times.
8118 if (is_owner_) {
125 4792 free(data_);
126 }
127 8118 }
128
129 size_t size_;
130 size_t pos_;
131 unsigned char *data_;
132 const size_t max_size_;
133 };
134
135 } // namespace cvmfs
136
137 #endif // CVMFS_NETWORK_SINK_MEM_H_
138