00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #include "safeerrno.h"
00025
00026 #include <xapian/error.h>
00027
00028 #include "flint_version.h"
00029 #include "io_utils.h"
00030 #include "str.h"
00031 #include "stringutils.h"
00032 #include "utils.h"
00033
00034 #ifdef __WIN32__
00035 # include "msvc_posix_wrapper.h"
00036 #endif
00037
00038 #include <cstdio>
00039 #include <cstring>
00040 #include <string>
00041
00042 #include "common/safeuuid.h"
00043
00044 using namespace std;
00045
00046
00047 #define FLINT_VERSION 200709120
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #define MAGIC_STRING "IAmFlint"
00060
00061 #define MAGIC_LEN CONST_STRLEN(MAGIC_STRING)
00062 #define VERSIONFILE_SIZE (MAGIC_LEN + 4)
00063
00064 void FlintVersion::create()
00065 {
00066 char buf[VERSIONFILE_SIZE] = MAGIC_STRING;
00067 unsigned char *v = reinterpret_cast<unsigned char *>(buf) + MAGIC_LEN;
00068 v[0] = static_cast<unsigned char>(FLINT_VERSION & 0xff);
00069 v[1] = static_cast<unsigned char>((FLINT_VERSION >> 8) & 0xff);
00070 v[2] = static_cast<unsigned char>((FLINT_VERSION >> 16) & 0xff);
00071 v[3] = static_cast<unsigned char>((FLINT_VERSION >> 24) & 0xff);
00072
00073 int fd = ::open(filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
00074
00075 if (fd < 0) {
00076 string msg("Failed to create flint version file: ");
00077 msg += filename;
00078 throw Xapian::DatabaseOpeningError(msg, errno);
00079 }
00080
00081 try {
00082 io_write(fd, buf, VERSIONFILE_SIZE);
00083 } catch (...) {
00084 (void)close(fd);
00085 throw;
00086 }
00087
00088 if (close(fd) != 0) {
00089 string msg("Failed to create flint version file: ");
00090 msg += filename;
00091 throw Xapian::DatabaseOpeningError(msg, errno);
00092 }
00093
00094 uuid_clear(uuid);
00095 ensure_uuid();
00096 }
00097
00098 void FlintVersion::read_and_check(bool readonly)
00099 {
00100 int fd = ::open(filename.c_str(), O_RDONLY|O_BINARY);
00101
00102 if (fd < 0) {
00103 string msg("Failed to open flint version file for reading: ");
00104 msg += filename;
00105 throw Xapian::DatabaseOpeningError(msg, errno);
00106 }
00107
00108
00109 char buf[VERSIONFILE_SIZE + 1];
00110 size_t size;
00111 try {
00112 size = io_read(fd, buf, VERSIONFILE_SIZE + 1, 0);
00113 } catch (...) {
00114 (void)close(fd);
00115 throw;
00116 }
00117 (void)close(fd);
00118
00119 if (size != VERSIONFILE_SIZE) {
00120 string msg("Flint version file ");
00121 msg += filename;
00122 msg += " should be "STRINGIZE(VERSIONFILE_SIZE)" bytes, actually ";
00123 msg += str(size);
00124 throw Xapian::DatabaseCorruptError(msg);
00125 }
00126
00127 if (memcmp(buf, MAGIC_STRING, MAGIC_LEN) != 0) {
00128 string msg("Flint version file doesn't contain the right magic string: ");
00129 msg += filename;
00130 throw Xapian::DatabaseCorruptError(msg);
00131 }
00132
00133 const unsigned char *v;
00134 v = reinterpret_cast<const unsigned char *>(buf) + MAGIC_LEN;
00135 unsigned int version = v[0] | (v[1] << 8) | (v[2] << 16) | (v[3] << 24);
00136 if (version >= 200704230 && version < 200709120) {
00137 if (readonly) return;
00138
00139
00140 string filename_save = filename;
00141 filename += ".tmp";
00142 create();
00143 int result;
00144 #ifdef __WIN32__
00145 result = msvc_posix_rename(filename.c_str(), filename_save.c_str());
00146 #else
00147 result = rename(filename.c_str(), filename_save.c_str());
00148 #endif
00149 filename = filename_save;
00150 if (result == -1) {
00151 string msg("Failed to update flint version file: ");
00152 msg += filename;
00153 throw Xapian::DatabaseOpeningError(msg);
00154 }
00155 return;
00156 }
00157 if (version != FLINT_VERSION) {
00158 string msg("Flint version file ");
00159 msg += filename;
00160 msg += " is version ";
00161 msg += str(version);
00162 msg += " but I only understand "STRINGIZE(FLINT_VERSION);
00163 throw Xapian::DatabaseVersionError(msg);
00164 }
00165
00166 string f = filename;
00167 f.resize(f.size() - CONST_STRLEN("iamflint"));
00168 f += "uuid";
00169 fd = ::open(f.c_str(), O_RDONLY|O_BINARY);
00170
00171 if (fd < 0) {
00172 uuid_clear(uuid);
00173 return;
00174 }
00175
00176 try {
00177 (void)io_read(fd, reinterpret_cast<char*>(uuid), 16, 16);
00178 } catch (...) {
00179 uuid_clear(uuid);
00180 (void)close(fd);
00181 throw;
00182 }
00183 (void)close(fd);
00184 }
00185
00186 void
00187 FlintVersion::ensure_uuid() const
00188 {
00189 if (uuid_is_null(uuid)) {
00190 string f = filename;
00191 f.resize(f.size() - CONST_STRLEN("iamflint"));
00192 f += "uuid";
00193 int fd = ::open(f.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
00194
00195
00196
00197 if (fd < 0) {
00198 struct stat statbuf;
00199 if (stat(filename, &statbuf) != 0) {
00200 throw Xapian::DatabaseError("Couldn't stat " + filename, errno);
00201 }
00202 unsigned long mtime = statbuf.st_mtime;
00203
00204
00205
00206 unsigned char *v = reinterpret_cast<unsigned char *>(uuid);
00207 v[0] = static_cast<unsigned char>(mtime & 0xff);
00208 v[1] = static_cast<unsigned char>((mtime >> 8) & 0xff);
00209 v[2] = static_cast<unsigned char>((mtime >> 16) & 0xff);
00210 v[3] = static_cast<unsigned char>((mtime >> 24) & 0xff);
00211 return;
00212 }
00213
00214 uuid_generate(uuid);
00215 try {
00216 io_write(fd, reinterpret_cast<const char*>(uuid), 16);
00217 } catch (...) {
00218 (void)close(fd);
00219 throw;
00220 }
00221
00222 if (close(fd) != 0) {
00223 string msg("Failed to create flint uuid file: ");
00224 msg += f;
00225 throw Xapian::DatabaseError(msg, errno);
00226 }
00227 }
00228 }