CernVM-FS  2.12.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(store_path + "_tmp",
37  S_IWUSR | S_IWGRP | S_IRUSR | S_IRGRP | S_IROTH, "w", &path_tmp);
38  if (!f_tmp)
39  return NULL;
40  int written = fprintf(f_tmp, "%s\n", uuid_str.c_str());
41  fclose(f_tmp);
42  if (written != static_cast<int>(uuid_str.length() + 1)) {
43  unlink(path_tmp.c_str());
44  return NULL;
45  }
46  if (rename(path_tmp.c_str(), store_path.c_str()) != 0) {
47  unlink(path_tmp.c_str());
48  return NULL;
49  }
50  return uuid.Release();
51  }
52 
53  // Read from cached file
54  bool retval = GetLineFile(f, &uuid->uuid_);
55  fclose(f);
56  if (!retval)
57  return NULL;
58  int nitems = sscanf(uuid->uuid_.c_str(),
59  "%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16 "-%04" SCNx16 "-%08" SCNx32 "%04"
60  SCNx16,
61  &uuid->uuid_presentation_.split.a, &uuid->uuid_presentation_.split.b,
62  &uuid->uuid_presentation_.split.c, &uuid->uuid_presentation_.split.d,
63  &uuid->uuid_presentation_.split.e1, &uuid->uuid_presentation_.split.e2);
64  if (nitems != 6)
65  return NULL;
66 
67  return uuid.Release();
68 }
69 
70 
71 string Uuid::CreateOneTime() {
72  Uuid uuid;
73  uuid.MkUuid();
74  return uuid.uuid_;
75 }
76 
77 
82 void Uuid::MkUuid() {
83  uuid_t new_uuid;
84  uuid_generate(new_uuid);
85  assert(sizeof(new_uuid) == 16);
86  memcpy(uuid_presentation_.uuid, new_uuid, sizeof(uuid_presentation_.uuid));
87  // Canonical UUID format, including trailing \0
88  unsigned uuid_len = 8+1+4+1+4+1+4+1+12+1;
89  char uuid_cstr[uuid_len];
90  snprintf(uuid_cstr, uuid_len, "%08x-%04x-%04x-%04x-%08x%04x",
91  uuid_presentation_.split.a, uuid_presentation_.split.b,
92  uuid_presentation_.split.c, uuid_presentation_.split.d,
93  uuid_presentation_.split.e1, uuid_presentation_.split.e2);
94  uuid_ = string(uuid_cstr);
95 }
96 
97 
98 Uuid::Uuid() {
99  memset(&uuid_presentation_, 0, sizeof(uuid_presentation_));
100 }
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:1005
assert((mem||(size==0))&&"Out Of Memory")
bool GetLineFile(FILE *f, std::string *line)
Definition: string.cc:386
std::string uuid_
Definition: uuid.h:39
void MkUuid()
Definition: uuid.cc:82