GCC Code Coverage Report


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