1 /** @file flint_version.h
2  * @brief FlintVersion class
3  */
4 /* Copyright (C) 2006,2007,2008,2009 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 
21 #ifndef OM_HGUARD_FLINT_VERSION_H
22 #define OM_HGUARD_FLINT_VERSION_H
23 
24 #include <cstring>
25 #include <string>
26 
27 #include "common/safeuuid.h"
28 
29 /** The FlintVersion class manages the "iamflint" file.
30  *
31  *  The "iamflint" file (currently) contains a "magic" string identifying
32  *  that this is a flint database and a database format version number.
33  */
34 class FlintVersion {
35     /// The filename of the version file.
36     std::string filename;
37 
38     /// The UUID of this database.
39     mutable uuid_t uuid;
40 
41     /// Generate a UUID if we don't already have one.
42     void ensure_uuid() const;
43 
44   public:
FlintVersion(const std::string & dbdir)45     FlintVersion(const std::string & dbdir)
46 	: filename(dbdir + "/iamflint") { }
47 
48     /** Create the version file. */
49     void create();
50 
51     /** Read the version file and check it's a version we understand.
52      *
53      *  @param readonly    true if the database is being opened readonly.
54      *
55      *  On failure, an exception is thrown.
56      */
57     void read_and_check(bool readonly);
58 
59     /// Return pointer to 16 byte UUID.
get_uuid()60     const char * get_uuid() const {
61 	ensure_uuid();
62 	return reinterpret_cast<const char *>(uuid);
63     }
64 
65     /// Return UUID in the standard 36 character string format.
get_uuid_string()66     std::string get_uuid_string() const {
67 	char buf[37];
68 	ensure_uuid();
69 	uuid_unparse_lower(uuid, buf);
70 	return std::string(buf, 36);
71     }
72 
73 #if 0 // Unused currently.
74     /// Set the UUID from 16 byte binary value @a data.
75     void set_uuid(const void * data) {
76 	std::memcpy(uuid, data, 16);
77     }
78 
79     /** Set the UUID from the standard 36 character string format.
80      *
81      *  @return true if @a s was successfully parsed; false otherwise.
82      */
83     bool set_uuid_string(const std::string & s) {
84 	return uuid_parse(s.c_str(), uuid);
85     }
86 #endif
87 };
88 
89 #endif
90