GCC Code Coverage Report


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