GCC Code Coverage Report


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