Update of /cvsroot/wpdev/wolfpack
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16408
Modified Files:
ChangeLog serverconfig.cpp serverconfig.h world.cpp world.h
Log Message:
Binary Save backups and compression.
Index: world.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/world.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** world.h 16 Aug 2004 18:18:03 -0000 1.19
--- world.h 23 Aug 2004 21:37:39 -0000 1.20
***************
*** 36,39 ****
--- 36,40 ----
#include <qvaluevector.h>
#include <qmap.h>
+ #include <qthread.h>
#include "server.h"
***************
*** 64,67 ****
--- 65,79 ----
};
+ class cBackupThread : public QThread {
+ private:
+ QString filename;
+ public:
+ void run();
+
+ void setFilename(const QString &name) {
+ filename = name;
+ }
+ };
+
class cWorld : public cComponent
{
***************
*** 80,83 ****
--- 92,97 ----
void loadTag( cBufferedReader& reader, unsigned int version );
QMap<QString, QString> options;
+ void backupWorld( const QString &filename, unsigned int count, bool compress);
+ QPtrList<cBackupThread> backupThreads;
public:
Index: serverconfig.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/serverconfig.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** serverconfig.h 10 Aug 2004 03:15:57 -0000 1.3
--- serverconfig.h 23 Aug 2004 21:37:39 -0000 1.4
***************
*** 146,149 ****
--- 146,154 ----
unsigned char maxCharsPerAccount_;
+ // Binary Save Driver
+ unsigned int binaryBackups_;
+ QString binarySavepath_;
+ bool binaryCompressBackups_;
+
// AI
float checkAITime_;
***************
*** 232,236 ****
signed int maxStealthSteps() const;
unsigned int runningStamSteps() const;
! unsigned int hungerRate() const;
unsigned int hungerDamageRate() const;
unsigned char hungerDamage() const;
--- 237,241 ----
signed int maxStealthSteps() const;
unsigned int runningStamSteps() const;
! unsigned int hungerRate() const;
unsigned int hungerDamageRate() const;
unsigned char hungerDamage() const;
***************
*** 243,246 ****
--- 248,264 ----
}
+ inline unsigned int binaryBackups() const
+ {
+ return binaryBackups_;
+ }
+
+ inline const QString &binarySavepath() const {
+ return binarySavepath_;
+ }
+
+ inline bool binaryCompressBackups() const {
+ return binaryCompressBackups_;
+ }
+
unsigned int shopRestock() const;
unsigned int snoopdelay() const;
Index: serverconfig.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/serverconfig.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** serverconfig.cpp 13 Aug 2004 08:55:25 -0000 1.4
--- serverconfig.cpp 23 Aug 2004 21:37:39 -0000 1.5
***************
*** 204,207 ****
--- 204,212 ----
pathfindFollowMinCost_ = getDouble( "Path Finding", "Follow min. estimated Cost", 1.5, true );
pathfindFleeRadius_ = getNumber( "Path Finding", "Flee Radius", 10, true );
+
+ // Binary Saves
+ binarySavepath_ = getString( "Binary Save Driver", "Save Path", "world.bin", true);
+ binaryBackups_ = getNumber( "Binary Save Driver", "Number Of Backups", 5, true);
+ binaryCompressBackups_ = getBool( "Binary Save Driver", "Compress Backups", false, true);
}
Index: ChangeLog
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/ChangeLog,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** ChangeLog 22 Aug 2004 16:34:51 -0000 1.12
--- ChangeLog 23 Aug 2004 21:37:39 -0000 1.13
***************
*** 14,17 ****
--- 14,20 ----
- .exportdefinitions now exports data about multis to the
categories.db database.
+ - Backup functionality for the binary save driver.
+ - Binary backups can automatically be compressed using gzip.
+ - The number of backups kept can be limited.
Wolfpack 12.9.8 Beta (19. August 2004)
Index: world.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/world.cpp,v
retrieving revision 1.117
retrieving revision 1.118
diff -C2 -d -r1.117 -r1.118
*** world.cpp 19 Aug 2004 01:22:54 -0000 1.117
--- world.cpp 23 Aug 2004 21:37:39 -0000 1.118
***************
*** 46,49 ****
--- 46,51 ----
#include "basics.h"
#include <sqlite.h>
+ #include <qfileinfo.h>
+ #include <qdir.h>
// Postprocessing stuff, can be deleted later on
***************
*** 345,348 ****
--- 347,351 ----
_lastCharSerial = 0;
_lastItemSerial = ITEM_SPACE;
+ backupThreads.setAutoDelete(true);
}
***************
*** 352,355 ****
--- 355,363 ----
cWorld::~cWorld()
{
+ cBackupThread *thread;
+ for (thread = backupThreads.first(); thread; thread = backupThreads.next()) {
+ thread->wait();
+ }
+
// Free pending objects
p->purgePendingObjects();
***************
*** 415,419 ****
if ( Config::instance()->databaseDriver() == "binary" )
{
! QString filename = "world.bin";
if ( QFile::exists( filename ) )
--- 423,427 ----
if ( Config::instance()->databaseDriver() == "binary" )
{
! QString filename = Config::instance()->binarySavepath();
if ( QFile::exists( filename ) )
***************
*** 830,836 ****
if (Config::instance()->databaseDriver() == "binary") {
// Save in binary format
cBufferedWriter writer( "WOLFPACK", DATABASE_VERSION );
! writer.open( "world.bin" );
cCharIterator charIterator;
--- 838,847 ----
if (Config::instance()->databaseDriver() == "binary") {
+ // Make a backup of the old world.
+ backupWorld(Config::instance()->binarySavepath(), Config::instance()->binaryBackups(), Config::instance()->binaryCompressBackups());
+
// Save in binary format
cBufferedWriter writer( "WOLFPACK", DATABASE_VERSION );
! writer.open( Config::instance()->binarySavepath() );
cCharIterator charIterator;
***************
*** 869,872 ****
--- 880,886 ----
writer.writeByte( 0xFF ); // Terminator Type
writer.close();
+
+ Config::instance()->flush();
+ p->purgePendingObjects();
} else {
if (!PersistentBroker::instance()->openDriver( Config::instance()->databaseDriver())) {
***************
*** 1226,1229 ****
--- 1240,1367 ----
}
+ QMap<QDateTime, QString> listBackups(const QString &filename) {
+ // Get the path the file is in
+ QString name = QFileInfo(filename).baseName(false);
+
+ QDir dir = QFileInfo(filename).dir();
+ QStringList entries = dir.entryList(name + "*", QDir::Files);
+ QMap<QDateTime, QString> backups;
+
+ QStringList::iterator sit;
+ for (sit = entries.begin(); sit != entries.end(); ++sit) {
+ QString backup = QFileInfo(*sit).baseName(false);
+ QString timestamp = backup.right(backup.length() - name.length());
+ QDate date;
+ QTime time;
+
+ // Length has to be -YYYYMMDD-HHMM (14 Characters)
+ if (timestamp.length() != 14) {
+ continue;
+ }
+
+ bool ok[5];
+ int year = timestamp.mid(1, 4).toInt(&ok[0]);
+ int month = timestamp.mid(5, 2).toInt(&ok[1]);
+ int day = timestamp.mid(7, 2).toInt(&ok[2]);
+ int hour = timestamp.mid(10, 2).toInt(&ok[3]);
+ int minute = timestamp.mid(12, 2).toInt(&ok[4]);
+
+ if (!ok[0] || !ok[1] || !ok[2] || !ok[3] || !ok[4]) {
+ continue;
+ }
+
+ date.setYMD(year, month, day);
+ time.setHMS(hour, minute, 0);
+
+ backups.insert(QDateTime(date, time), *sit);
+ }
+
+ return backups;
+ }
+
+ /*
+ Backup old worldfile
+ */
+ void cWorld::backupWorld(const QString &filename, unsigned int count, bool compress) {
+ // Looks like there is nothing to backup
+ if (count == 0 || !QFile::exists(filename)) {
+ return;
+ }
+
+ // Check if we need to remove a previous backup
+ QMap<QDateTime, QString> backups = listBackups(filename);
+
+ if (backups.count() >= count) {
+ // Remove the oldest backup
+ QDateTime current;
+ QString backup = QString::null;
+
+ QMap<QDateTime, QString>::iterator it;
+ for (it = backups.begin(); it != backups.end(); ++it) {
+ if (current.isNull() || it.key() < current) {
+ current = it.key();
+ backup = it.data();
+ }
+ }
+
+ if (!backup.isNull() && !QFile::remove(backup)) {
+ Console::instance()->log(LOG_ERROR, QString("Unable to remove backup %1. No new backup has been created.\n").arg(backup));
+ return;
+ }
+ }
+
+ // Rename the old worldfile to the new backup name
+ QString backupName = QFileInfo(filename).dirPath(true) + QDir::separator() + QFileInfo(filename).baseName(false);
+ QDateTime current = QDateTime::currentDateTime();
+ backupName.append(current.toString("-yyyyMMdd-hhmm")); // Append Timestamp
+ backupName.append(".");
+ backupName.append(QFileInfo(filename).extension(true)); // Append Extension
+
+ // Rename the old worldfile
+ QDir dir = QDir::current();
+ dir.rename(filename, backupName, true);
+
+ // Start the compression thread if requested by the user
+ cBackupThread *backupThread = new cBackupThread();
+ backupThread->setFilename(backupName);
+ backupThread->start(QThread::LowPriority);
+ }
+
+ extern "C" {
+ extern void *gzopen(const char *path, const char *mode);
+ extern int gzwrite(void *file, void *buf, unsigned len);
+ extern int gzclose(void *file);
+ };
+
+ /*
+ Pipe a backup trough
+ */
+ void cBackupThread::run() {
+ // Open the backup input file and the backup output file and compress
+ QFile input(filename);
+ QString outputName = filename + ".gz";
+
+ if (!input.open(IO_ReadOnly)) {
+ return;
+ }
+
+ void *output = gzopen(outputName.latin1(), "wb");
+ if (!output) {
+ input.close();
+ return;
+ }
+
+ int readSize;
+ char buffer[4096];
+ while ((readSize = input.readBlock(buffer, 4096)) > 0) {
+ gzwrite(output, buffer, readSize);
+ }
+
+ input.close();
+ gzclose(output);
+
+ input.remove();
+ }
+
/*****************************************************************************
cItemIterator member functions
|