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