GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/uuid.cc
Date: 2026-07-19 02:35:15
Exec Total Coverage
Lines: 49 53 92.5%
Branches: 30 50 60.0%

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