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
|
/****************************************************************************************
* Copyright (c) 2008 Edward Toroshchin <edward.hades@gmail.com> *
* Copyright (c) 2009 Jeff Mitchell <mitchell@kde.org> *
* *
* 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, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "MySqlEmbeddedStorage.h"
#include "core/support/Amarok.h"
#include "core/support/Debug.h"
#include "amarokconfig.h"
#include <QDir>
#include <QString>
#include <QMutexLocker>
#include <QThreadStorage>
#include <QVarLengthArray>
#include <KStandardDirs>
#define MYSQLE_ERRMSG_FILENAME "errmsg.sys"
#define MYSQLE_ERRMSG_LOCAL_DIR "amarok/mysqle"
#include <mysql.h>
MySqlEmbeddedStorage::MySqlEmbeddedStorage( const QString &storageLocation )
: MySqlStorage()
{
m_debugIdent = "MySQLe";
QString storagePath = storageLocation;
QString defaultsFile;
QString databaseDir;
QString languageDataDir;
if( storageLocation.isEmpty() )
{
storagePath = Amarok::saveLocation();
defaultsFile = Amarok::config( "MySQLe" ).readEntry( "config", QString(storagePath + "my.cnf") );
databaseDir = Amarok::config( "MySQLe" ).readEntry( "data", QString(storagePath + "mysqle") );
}
else
{
QDir dir( storageLocation );
dir.mkpath( "." ); //ensure directory exists
defaultsFile = QDir::cleanPath( dir.absoluteFilePath( "my.cnf" ) );
databaseDir = dir.absolutePath() + QDir::separator() + "mysqle";
}
languageDataDir = KGlobal::dirs()->findResourceDir( "data", MYSQLE_ERRMSG_LOCAL_DIR "/" MYSQLE_ERRMSG_FILENAME );
if( !Amarok::config( "MySQLe" ).readEntry( "keepUserMyCnf", false ) )
{
QFile df( defaultsFile );
if ( !df.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) {
error() << "Unable to open " << defaultsFile << " for writing.";
reportError( "init" );
}
QTextStream out( &df );
out << "[embedded]" << endl;
out << "datadir = " << databaseDir.toAscii().data() << endl;
// CAUTION: if we ever change the table type we will need to fix a number of MYISAM specific
// functions, such as FULLTEXT indexing.
out << "default-storage-engine = MyISAM" << endl;
out << "loose-innodb = 0" << endl;
out << "skip-grant-tables = 1" << endl;
out << "myisam-recover = FORCE" << endl;
out << "key_buffer_size = 16777216" << endl; // (16Mb)
out << "character-set-server = utf8" << endl;
out << "collation-server = utf8_bin" << endl;
if (!languageDataDir.isNull())
out << "language = " << languageDataDir.toAscii() << MYSQLE_ERRMSG_LOCAL_DIR << endl;
//If the file is world-writable MySQL won't even read it
df.setPermissions( QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther );
df.close();
}
if( !QFile::exists( databaseDir ) )
{
QDir dir( databaseDir );
dir.mkpath( "." );
}
setenv( "MYSQL_HOME", storagePath.toAscii().data(), 1 );
setenv( "DEFAULT_HOME_ENV", storagePath.toAscii().data(), 1 );
QByteArray defaultsFileArg = QByteArray("--defaults-file=") + QFile::encodeName(defaultsFile);
char *args[] = {
(char*) "amarok",
defaultsFileArg.data()
};
if( mysql_library_init( 2 , args, 0 ) != 0 )
{
error() << "MySQL library initialization failed.";
reportError( "init" );
return;
}
m_db = mysql_init( NULL );
if( !m_db )
{
error() << "MySQLe initialization failed";
return;
}
if( mysql_options( m_db, MYSQL_READ_DEFAULT_GROUP, "amarokclient" ) )
reportError( "Error setting options for READ_DEFAULT_GROUP" );
if( mysql_options( m_db, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL ) )
reportError( "Error setting option to use embedded connection" );
if( !mysql_real_connect( m_db, NULL,NULL,NULL, 0, 0,NULL, 0 ) )
{
error() << "Could not connect to mysql!";
reportError( "na" );
mysql_close( m_db );
m_db = 0;
}
else
{
sharedInit( "amarok" );
debug() << "Connected to MySQL server" << mysql_get_server_info( m_db );
}
MySqlStorage::initThreadInitializer();
}
MySqlEmbeddedStorage::~MySqlEmbeddedStorage()
{}
QString
MySqlEmbeddedStorage::type() const
{
return "MySQLe";
}
|