Directory: | cvmfs/ |
---|---|
File: | cvmfs/network/sink_file.cc |
Date: | 2025-07-13 02:35:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 16 | 27 | 59.3% |
Branches: | 8 | 26 | 30.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | #include "sink_file.h" | ||
6 | |||
7 | #include <cerrno> | ||
8 | #include <cstdio> | ||
9 | #include <string> | ||
10 | #include <unistd.h> | ||
11 | |||
12 | namespace cvmfs { | ||
13 | |||
14 | /** | ||
15 | * Appends data to the sink | ||
16 | * | ||
17 | * @returns on success: number of bytes written | ||
18 | * on failure: -errno. | ||
19 | */ | ||
20 | 3925 | int64_t FileSink::Write(const void *buf, uint64_t sz) { | |
21 | 3925 | const size_t ret = fwrite(buf, 1ul, sz, file_); | |
22 | |||
23 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3925 times.
|
3925 | if (ferror(file_) != 0) { |
24 | // ferror does not tell us what exactly the error is | ||
25 | // and errno is also not set | ||
26 | // so just return generic I/O error flag | ||
27 | ✗ | return -EIO; | |
28 | } | ||
29 | |||
30 | 3925 | return static_cast<int64_t>(ret); | |
31 | } | ||
32 | |||
33 | /** | ||
34 | * Truncate all written data and start over at position zero. | ||
35 | * | ||
36 | * @returns Success = 0 | ||
37 | * Failure = -1 | ||
38 | */ | ||
39 | 294 | int FileSink::Reset() { | |
40 |
1/2✓ Branch 3 taken 294 times.
✗ Branch 4 not taken.
|
588 | return ((fflush(file_) == 0) && (ftruncate(fileno(file_), 0) == 0) |
41 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 294 times.
|
294 | && (freopen(NULL, "w", file_) == file_)) |
42 |
1/2✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
|
588 | ? 0 |
43 | 294 | : -errno; | |
44 | } | ||
45 | |||
46 | /** | ||
47 | * Purges all resources leaving the sink in an invalid state. | ||
48 | * More aggressive version of Reset(). | ||
49 | * For some sinks and depending on owner status it might do | ||
50 | * the same as Reset(). | ||
51 | * | ||
52 | * @returns Success = 0 | ||
53 | * Failure = -errno | ||
54 | */ | ||
55 | 125 | int FileSink::Purge() { | |
56 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
125 | if (is_owner_ && file_) { |
57 | 14 | const int ret = fclose(file_); | |
58 | 14 | file_ = NULL; | |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (ret != 0) { |
60 | ✗ | return -errno; | |
61 | } | ||
62 | |||
63 | 14 | return 0; | |
64 | } else { | ||
65 | 111 | return Reset(); | |
66 | } | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Return a string representation describing the type of sink and its status | ||
71 | */ | ||
72 | ✗ | std::string FileSink::Describe() { | |
73 | ✗ | std::string result = "File sink with "; | |
74 | ✗ | result += IsValid() ? " valid file pointer" : " invalid file pointer"; | |
75 | ✗ | return result; | |
76 | } | ||
77 | ✗ | void FileSink::Adopt(FILE *file, bool is_owner) { | |
78 | ✗ | if (is_owner_ && file_) { | |
79 | ✗ | (void)fclose(file_); | |
80 | } | ||
81 | |||
82 | ✗ | is_owner_ = is_owner; | |
83 | ✗ | file_ = file; | |
84 | } | ||
85 | } // namespace cvmfs | ||
86 |