GCC Code Coverage Report


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