GCC Code Coverage Report


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