GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/uuid.cc
Date: 2026-09-13 02:40:16
Exec Total Coverage
Lines: 49 53 92.5%
Branches: 29 48 60.4%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * UUID generation and caching.
5 */
6
7 #include "util/uuid.h"
8
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <uuid/uuid.h>
12
13 #include <cassert>
14 #include <cinttypes>
15 #include <cstdio>
16 #include <cstring>
17 #include <memory>
18 #include <string>
19
20 #include "util/posix.h"
21 #include "util/string.h"
22
23 using namespace std; // NOLINT
24
25 namespace cvmfs {
26
27 4904390 Uuid *Uuid::Create(const string &store_path) {
28
2/4
✓ Branch 1 taken 4904390 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4904390 times.
✗ Branch 5 not taken.
4904390 std::unique_ptr<Uuid> uuid(new Uuid());
29
2/2
✓ Branch 1 taken 4902682 times.
✓ Branch 2 taken 1708 times.
4904390 if (store_path == "") {
30
1/2
✓ Branch 2 taken 4902682 times.
✗ Branch 3 not taken.
4902682 uuid->MkUuid();
31 4902682 return uuid.release();
32 }
33
34
1/2
✓ Branch 2 taken 1708 times.
✗ Branch 3 not taken.
1708 FILE *f = fopen(store_path.c_str(), "r");
35
2/2
✓ Branch 0 taken 1588 times.
✓ Branch 1 taken 120 times.
1708 if (f == NULL) {
36 // Create and store
37
1/2
✓ Branch 2 taken 1588 times.
✗ Branch 3 not taken.
1588 uuid->MkUuid();
38
1/2
✓ Branch 2 taken 1588 times.
✗ Branch 3 not taken.
1588 const string uuid_str = uuid->uuid();
39 1588 string path_tmp;
40
1/2
✓ Branch 1 taken 1588 times.
✗ Branch 2 not taken.
1588 FILE *f_tmp = CreateTempFile(
41
1/2
✓ Branch 1 taken 1588 times.
✗ Branch 2 not taken.
3176 store_path + "_tmp", S_IWUSR | S_IWGRP | S_IRUSR | S_IRGRP | S_IROTH,
42 "w", &path_tmp);
43
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1571 times.
1588 if (!f_tmp)
44 17 return NULL;
45
1/2
✓ Branch 2 taken 1571 times.
✗ Branch 3 not taken.
1571 const int written = fprintf(f_tmp, "%s\n", uuid_str.c_str());
46
1/2
✓ Branch 1 taken 1571 times.
✗ Branch 2 not taken.
1571 fclose(f_tmp);
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1571 times.
1571 if (written != static_cast<int>(uuid_str.length() + 1)) {
48 unlink(path_tmp.c_str());
49 return NULL;
50 }
51
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1571 times.
1571 if (rename(path_tmp.c_str(), store_path.c_str()) != 0) {
52 unlink(path_tmp.c_str());
53 return NULL;
54 }
55 1571 return uuid.release();
56 1588 }
57
58 // Read from cached file
59
1/2
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
120 const bool retval = GetLineFile(f, &uuid->uuid_);
60
1/2
✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
120 fclose(f);
61
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 86 times.
120 if (!retval)
62 34 return NULL;
63 86 const int nitems = sscanf(
64 86 uuid->uuid_.c_str(),
65 "%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16 "-%04" SCNx16 "-%08" SCNx32
66 "%04" SCNx16,
67 86 &uuid->uuid_presentation_.split.a, &uuid->uuid_presentation_.split.b,
68 86 &uuid->uuid_presentation_.split.c, &uuid->uuid_presentation_.split.d,
69 86 &uuid->uuid_presentation_.split.e1, &uuid->uuid_presentation_.split.e2);
70
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 69 times.
86 if (nitems != 6)
71 17 return NULL;
72
73 69 return uuid.release();
74 4904390 }
75
76
77 34 string Uuid::CreateOneTime() {
78
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 Uuid uuid;
79
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 uuid.MkUuid();
80
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
68 return uuid.uuid_;
81 34 }
82
83
84 /**
85 * Creates a new UUID in canonical string representation and overwrites uuid_
86 * with the result.
87 */
88 4904304 void Uuid::MkUuid() {
89 uuid_t new_uuid;
90
1/2
✓ Branch 1 taken 4904304 times.
✗ Branch 2 not taken.
4904304 uuid_generate(new_uuid);
91 assert(sizeof(new_uuid) == 16);
92 4904304 memcpy(uuid_presentation_.uuid, new_uuid, sizeof(uuid_presentation_.uuid));
93 // Canonical UUID format, including trailing \0
94 4904304 const unsigned uuid_len = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12 + 1;
95 char uuid_cstr[uuid_len];
96 4904304 snprintf(uuid_cstr, uuid_len, "%08x-%04x-%04x-%04x-%08x%04x",
97 4904304 uuid_presentation_.split.a, uuid_presentation_.split.b,
98 4904304 uuid_presentation_.split.c, uuid_presentation_.split.d,
99 4904304 uuid_presentation_.split.e1, uuid_presentation_.split.e2);
100
1/2
✓ Branch 2 taken 4904304 times.
✗ Branch 3 not taken.
4904304 uuid_ = string(uuid_cstr);
101 4904304 }
102
103
104 4904424 Uuid::Uuid() { memset(&uuid_presentation_, 0, sizeof(uuid_presentation_)); }
105
106 } // namespace cvmfs
107