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