proc_uuid.cc
Go to the documentation of this file.
1 
7 /* Copyright (C) 2008 Lemur Consulting Ltd
8  * Copyright (C) 2013,2015,2016,2017 Olly Betts
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <config.h>
26 
27 #include "proc_uuid.h"
28 
29 #include "xapian/error.h"
30 
31 #include <cstring>
32 #include "stringutils.h"
33 
34 #include <sys/types.h>
35 #include "safesysstat.h"
36 #include "safeerrno.h"
37 #include "safefcntl.h"
38 #include "safeunistd.h"
39 
40 using namespace std;
41 
43 const size_t UUID_SIZE = 16;
44 
46 const size_t UUID_STRING_SIZE = 36;
47 
48 void
50 {
51  char buf[UUID_STRING_SIZE];
52  int fd = open("/proc/sys/kernel/random/uuid", O_RDONLY);
53  if (rare(fd == -1)) {
54  throw Xapian::DatabaseCreateError("Opening UUID generator failed", errno);
55  }
56  if (read(fd, buf, UUID_STRING_SIZE) != UUID_STRING_SIZE) {
57  close(fd);
58  throw Xapian::DatabaseCreateError("Generating UUID failed");
59  }
60  close(fd);
61  uuid_parse(buf, uu);
62 }
63 
64 int
65 uuid_parse(const char * in, uuid_t uu)
66 {
67  for (unsigned i = 0; i != UUID_SIZE; ++i) {
68  uu[i] = hex_digit(in[0]) << 4 | hex_digit(in[1]);
69  in += ((0x2a8 >> i) & 1) | 2;
70  }
71  return 0;
72 }
73 
74 void uuid_unparse_lower(const uuid_t uu, char * out)
75 {
76  for (unsigned i = 0; i != UUID_SIZE; ++i) {
77  unsigned char ch = uu[i];
78  *out++ = "0123456789abcdef"[ch >> 4];
79  *out++ = "0123456789abcdef"[ch & 0x0f];
80  if ((0x2a8 >> i) & 1)
81  *out++ = '-';
82  }
83  *out = '\0';
84 }
85 
87 {
88  memset(uu, 0, UUID_SIZE);
89 }
90 
91 int uuid_is_null(const uuid_t uu)
92 {
93  unsigned i = 0;
94  while (i < UUID_SIZE) {
95  if (uu[i++])
96  return 0;
97  }
98  return 1;
99 }
int close(FD &fd)
Definition: fd.h:63
unsigned char uuid_t[16]
Definition: proc_uuid.h:25
int hex_digit(char ch)
Definition: stringutils.h:188
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
Definition: dbfactory.h:104
STL namespace.
#define rare(COND)
Definition: config.h:502
include <sys/stat.h> with portability enhancements
const size_t UUID_STRING_SIZE
The size of a UUID string in bytes (not including trailing &#39;\0&#39;).
Definition: proc_uuid.cc:46
Hierarchy of classes which Xapian can throw as exceptions.
void uuid_generate(uuid_t uu)
Definition: proc_uuid.cc:49
DatabaseCreateError indicates a failure to create a database.
Definition: error.h:438
include <errno.h>, but working around broken platforms.
const size_t UUID_SIZE
The size of a UUID in bytes.
Definition: proc_uuid.cc:43
int uuid_parse(const char *in, uuid_t uu)
Definition: proc_uuid.cc:65
Various handy helpers which std::string really should provide.
void uuid_clear(uuid_t uu)
Definition: proc_uuid.cc:86
int uuid_is_null(const uuid_t uu)
Definition: proc_uuid.cc:91
<unistd.h>, but with compat.
void uuid_unparse_lower(const uuid_t uu, char *out)
Definition: proc_uuid.cc:74
include <fcntl.h>, but working around broken platforms.

Documentation for Xapian (version 1.4.7).
Generated on Fri Jul 20 2018 by Doxygen 1.8.13.