GCC Code Coverage Report


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