1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2020 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author : http://dar.linux.free.fr/email.html
/*********************************************************************/
#include "../my_config.h"
extern "C"
{
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
} // end extern "C"
#include "database_header.hpp"
#include "tools.hpp"
#include "user_interaction.hpp"
#include "integers.hpp"
#include "cygwin_adapt.hpp"
#include "fichier_local.hpp"
#include "compressor.hpp"
using namespace std;
namespace libdar
{
static const unsigned char database_version = 5;
#define HEADER_OPTION_NONE 0x00
#define HEADER_OPTION_COMPRESSOR 0x01
#define HEADER_OPTION_EXTENSION 0x80
// if EXTENSION bit is set, the option field is two bytes wide
// this mechanism can be extended in the future by a second extension bit 0x8080
// and so on
class database_header
{
public:
database_header() { version = database_version; options = HEADER_OPTION_NONE; algo = compression::gzip; };
database_header(const database_header & ref) = default;
database_header(database_header && ref) noexcept = default;
database_header & operator = (const database_header & ref) = default;
database_header & operator = (database_header && ref) noexcept = default;
~database_header() = default;
void read(generic_file & f);
void write(generic_file & f);
void set_compression(compression algozip);
U_I get_version() const { return version; };
compression get_compression() const { return algo; };
private:
unsigned char version;
unsigned char options;
compression algo;
};
void database_header::read(generic_file & f)
{
f.read((char *)&version, 1);
if(version > database_version)
throw Erange("database_header::read", gettext("The format version of this database is too high for that software version, use a more recent software to read or modify this database"));
f.read((char *)&options, 1);
if((options & HEADER_OPTION_EXTENSION) != 0)
throw Erange("database_header::read", gettext("Unknown header option in database, aborting\n"));
if((options & HEADER_OPTION_COMPRESSOR) != 0)
{
char tmp;
f.read(&tmp, 1);
algo = char2compression(tmp);
}
else
algo = compression::gzip; // was the default before choice was available
}
void database_header::write(generic_file & f)
{
f.write((char *)&version, 1);
f.write((char *)&options, 1);
if((options & HEADER_OPTION_COMPRESSOR) != 0)
{
char tmp = compression2char(algo);
f.write(&tmp, 1);
}
}
void database_header::set_compression(compression algozip)
{
algo = algozip;
if(algo != compression::gzip)
options |= HEADER_OPTION_COMPRESSOR;
else
options &= ~HEADER_OPTION_COMPRESSOR;
}
generic_file *database_header_create(const shared_ptr<user_interaction> & dialog,
const string & filename,
bool overwrite,
compression algozip)
{
generic_file *ret = nullptr;
struct stat buf;
database_header h;
compressor *comp;
if(stat(filename.c_str(), &buf) >= 0 && !overwrite)
throw Erange("database_header_create", gettext("Cannot create database, file exists"));
ret = new (nothrow) fichier_local(dialog, filename, gf_write_only, 0666, !overwrite, overwrite, false);
if(ret == nullptr)
throw Ememory("database_header_create");
try
{
h.set_compression(algozip);
h.write(*ret);
comp = new (nothrow) compressor(algozip, ret); // upon success, ret is owned by compr
if(comp == nullptr)
throw Ememory("database_header_create");
else
ret = comp;
}
catch(...)
{
delete ret;
throw;
}
return ret;
}
generic_file *database_header_open(const shared_ptr<user_interaction> & dialog,
const string & filename,
unsigned char & db_version,
compression & algozip)
{
generic_file *ret = nullptr;
try
{
database_header h;
compressor *comp;
try
{
ret = new (nothrow) fichier_local(filename, false);
}
catch(Erange & e)
{
throw Erange("database_header_open", tools_printf(gettext("Error reading database %S : "), &filename) + e.get_message());
}
if(ret == nullptr)
throw Ememory("database_header_open");
h.read(*ret);
db_version = h.get_version();
algozip = h.get_compression();
comp = new (nothrow) compressor(h.get_compression(), ret);
if(comp == nullptr)
throw Ememory("database_header_open");
else
ret = comp;
}
catch(...)
{
if(ret != nullptr)
delete ret;
throw;
}
return ret;
}
extern const unsigned char database_header_get_supported_version()
{
return database_version;
}
} // end of namespace
|