GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/uuid.cc
Date: 2026-06-28 02:36:10
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 4302760 Uuid *Uuid::Create(const string &store_path) {
27
3/6
✓ Branch 1 taken 4302760 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4302760 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4302760 times.
✗ Branch 8 not taken.
4302760 UniquePtr<Uuid> uuid(new Uuid());
28
2/2
✓ Branch 1 taken 4301223 times.
✓ Branch 2 taken 1537 times.
4302760 if (store_path == "") {
29
1/2
✓ Branch 2 taken 4301223 times.
✗ Branch 3 not taken.
4301223 uuid->MkUuid();
30 4301223 return uuid.Release();
31 }
32
33
1/2
✓ Branch 2 taken 1537 times.
✗ Branch 3 not taken.
1537 FILE *f = fopen(store_path.c_str(), "r");
34
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 446 times.
1537 if (f == NULL) {
35 // Create and store
36
1/2
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
1091 uuid->MkUuid();
37
1/2
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
1091 const string uuid_str = uuid->uuid();
38 1091 string path_tmp;
39
1/2
✓ Branch 1 taken 1091 times.
✗ Branch 2 not taken.
1091 FILE *f_tmp = CreateTempFile(
40
1/2
✓ Branch 1 taken 1091 times.
✗ Branch 2 not taken.
2182 store_path + "_tmp", S_IWUSR | S_IWGRP | S_IRUSR | S_IRGRP | S_IROTH,
41 "w", &path_tmp);
42
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1043 times.
1091 if (!f_tmp)
43 48 return NULL;
44
1/2
✓ Branch 2 taken 1043 times.
✗ Branch 3 not taken.
1043 const int written = fprintf(f_tmp, "%s\n", uuid_str.c_str());
45
1/2
✓ Branch 1 taken 1043 times.
✗ Branch 2 not taken.
1043 fclose(f_tmp);
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1043 times.
1043 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 1043 times.
1043 if (rename(path_tmp.c_str(), store_path.c_str()) != 0) {
51 unlink(path_tmp.c_str());
52 return NULL;
53 }
54 1043 return uuid.Release();
55 1091 }
56
57 // Read from cached file
58
1/2
✓ Branch 2 taken 446 times.
✗ Branch 3 not taken.
446 const bool retval = GetLineFile(f, &uuid->uuid_);
59
1/2
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
446 fclose(f);
60
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 350 times.
446 if (!retval)
61 96 return NULL;
62 350 const int nitems = sscanf(
63 350 uuid->uuid_.c_str(),
64 "%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16 "-%04" SCNx16 "-%08" SCNx32
65 "%04" SCNx16,
66 350 &uuid->uuid_presentation_.split.a, &uuid->uuid_presentation_.split.b,
67 350 &uuid->uuid_presentation_.split.c, &uuid->uuid_presentation_.split.d,
68 350 &uuid->uuid_presentation_.split.e1, &uuid->uuid_presentation_.split.e2);
69
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 302 times.
350 if (nitems != 6)
70 48 return NULL;
71
72 302 return uuid.Release();
73 4302760 }
74
75
76 96 string Uuid::CreateOneTime() {
77
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 Uuid uuid;
78
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 uuid.MkUuid();
79
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
192 return uuid.uuid_;
80 96 }
81
82
83 /**
84 * Creates a new UUID in canonical string representation and overwrites uuid_
85 * with the result.
86 */
87 4302410 void Uuid::MkUuid() {
88 uuid_t new_uuid;
89
1/2
✓ Branch 1 taken 4302410 times.
✗ Branch 2 not taken.
4302410 uuid_generate(new_uuid);
90 assert(sizeof(new_uuid) == 16);
91 4302410 memcpy(uuid_presentation_.uuid, new_uuid, sizeof(uuid_presentation_.uuid));
92 // Canonical UUID format, including trailing \0
93 4302410 const unsigned uuid_len = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12 + 1;
94 char uuid_cstr[uuid_len];
95 4302410 snprintf(uuid_cstr, uuid_len, "%08x-%04x-%04x-%04x-%08x%04x",
96 4302410 uuid_presentation_.split.a, uuid_presentation_.split.b,
97 4302410 uuid_presentation_.split.c, uuid_presentation_.split.d,
98 4302410 uuid_presentation_.split.e1, uuid_presentation_.split.e2);
99
1/2
✓ Branch 2 taken 4302410 times.
✗ Branch 3 not taken.
4302410 uuid_ = string(uuid_cstr);
100 4302410 }
101
102
103 4302856 Uuid::Uuid() { memset(&uuid_presentation_, 0, sizeof(uuid_presentation_)); }
104
105 } // namespace cvmfs
106