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