GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_mem.h
Date: 2025-03-09 02:34:28
Exec Total Coverage
Lines: 16 16 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 196 MemSink() : Sink(true), size_(0), pos_(0),
29 98 data_(NULL), max_size_(kMaxMemSize) { }
30 explicit MemSink(size_t size);
31 196 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 4 virtual int Purge() {
62 4 return Reset();
63 }
64
65 /**
66 * @returns true if the object is correctly initialized.
67 */
68 virtual bool IsValid();
69
70 /**
71 * Commit data to the sink
72 * @returns success = 0
73 * failure = -errno
74 */
75 86 virtual int Flush() {
76 86 return 0;
77 }
78
79 /**
80 * Reserves new space in sinks that require reservation (see RequiresReserve)
81 *
82 * Successful if the requested size is smaller than already space reserved, or
83 * if the sink is the owner of the data and can allocate enough new space.
84 *
85 * @note If successful, always resets the current position to 0.
86 *
87 * Fails if
88 * 1) sink is not the owner of the data and more than the current size is
89 * requested
90 * 2) more space is requested than allowed (max_size_)
91 *
92 * @returns success = true
93 * failure = false
94 */
95 virtual bool Reserve(size_t size);
96
97 /**
98 * Returns if the specific sink type needs reservation of (data) space
99 *
100 * @returns true - reservation is needed
101 * false - no reservation is needed
102 */
103 343 virtual bool RequiresReserve() {
104 343 return true;
105 }
106
107 /**
108 * Return a string representation describing the type of sink and its status
109 */
110 virtual std::string Describe();
111
112 /**
113 * Allows the sink to adopt data that was initialized outside this class.
114 * The sink can become the new owner of the data, or not.
115 */
116 void Adopt(size_t size, size_t pos, unsigned char *data,
117 bool is_owner = true);
118
119 92 size_t size() { return size_; }
120 86 size_t pos() { return pos_; }
121 85 unsigned char* data() { return data_; }
122
123 /**
124 * Do not download files larger than 1M into memory.
125 */
126 static const size_t kMaxMemSize = 1024ul * 1024ul;
127
128 private:
129 188 void FreeData() {
130
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 77 times.
188 if (is_owner_) {
131 111 free(data_);
132 }
133 188 }
134
135 size_t size_;
136 size_t pos_;
137 unsigned char *data_;
138 const size_t max_size_;
139 };
140
141 } // namespace cvmfs
142
143 #endif // CVMFS_NETWORK_SINK_MEM_H_
144