CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
uuid.cc
Go to the documentation of this file.
1 
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 Uuid *Uuid::Create(const string &store_path) {
24  UniquePtr<Uuid> uuid(new Uuid());
25  if (store_path == "") {
26  uuid->MkUuid();
27  return uuid.Release();
28  }
29 
30  FILE *f = fopen(store_path.c_str(), "r");
31  if (f == NULL) {
32  // Create and store
33  uuid->MkUuid();
34  string uuid_str = uuid->uuid();
35  string path_tmp;
36  FILE *f_tmp = CreateTempFile(
37  store_path + "_tmp", S_IWUSR | S_IWGRP | S_IRUSR | S_IRGRP | S_IROTH,
38  "w", &path_tmp);
39  if (!f_tmp)
40  return NULL;
41  int written = fprintf(f_tmp, "%s\n", uuid_str.c_str());
42  fclose(f_tmp);
43  if (written != static_cast<int>(uuid_str.length() + 1)) {
44  unlink(path_tmp.c_str());
45  return NULL;
46  }
47  if (rename(path_tmp.c_str(), store_path.c_str()) != 0) {
48  unlink(path_tmp.c_str());
49  return NULL;
50  }
51  return uuid.Release();
52  }
53 
54  // Read from cached file
55  bool retval = GetLineFile(f, &uuid->uuid_);
56  fclose(f);
57  if (!retval)
58  return NULL;
59  int nitems = sscanf(
60  uuid->uuid_.c_str(),
61  "%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16 "-%04" SCNx16 "-%08" SCNx32
62  "%04" SCNx16,
63  &uuid->uuid_presentation_.split.a, &uuid->uuid_presentation_.split.b,
64  &uuid->uuid_presentation_.split.c, &uuid->uuid_presentation_.split.d,
65  &uuid->uuid_presentation_.split.e1, &uuid->uuid_presentation_.split.e2);
66  if (nitems != 6)
67  return NULL;
68 
69  return uuid.Release();
70 }
71 
72 
73 string Uuid::CreateOneTime() {
74  Uuid uuid;
75  uuid.MkUuid();
76  return uuid.uuid_;
77 }
78 
79 
84 void Uuid::MkUuid() {
85  uuid_t new_uuid;
86  uuid_generate(new_uuid);
87  assert(sizeof(new_uuid) == 16);
88  memcpy(uuid_presentation_.uuid, new_uuid, sizeof(uuid_presentation_.uuid));
89  // Canonical UUID format, including trailing \0
90  unsigned uuid_len = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12 + 1;
91  char uuid_cstr[uuid_len];
92  snprintf(uuid_cstr, uuid_len, "%08x-%04x-%04x-%04x-%08x%04x",
93  uuid_presentation_.split.a, uuid_presentation_.split.b,
94  uuid_presentation_.split.c, uuid_presentation_.split.d,
95  uuid_presentation_.split.e1, uuid_presentation_.split.e2);
96  uuid_ = string(uuid_cstr);
97 }
98 
99 
100 Uuid::Uuid() { memset(&uuid_presentation_, 0, sizeof(uuid_presentation_)); }
101 
102 } // namespace cvmfs
static Publisher * Create(const SettingsPublisher &settings)
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1013
assert((mem||(size==0))&&"Out Of Memory")
bool GetLineFile(FILE *f, std::string *line)
Definition: string.cc:422
std::string uuid_
Definition: uuid.h:39
void MkUuid()
Definition: uuid.cc:84