1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007-2009 Grigor Iliev, Benno Senoner * |
* Copyright (C) 2007-2013 Grigor Iliev, Benno Senoner * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
27 |
#include <iostream> |
#include <iostream> |
28 |
#include <sstream> |
#include <sstream> |
29 |
#include <vector> |
#include <vector> |
30 |
|
#include <algorithm> |
31 |
#include <errno.h> |
#include <errno.h> |
32 |
#ifndef WIN32 |
#ifndef WIN32 |
33 |
#include <fnmatch.h> |
#include <fnmatch.h> |
111 |
} |
} |
112 |
|
|
113 |
void InstrumentsDb::SetDbFile(String File) { |
void InstrumentsDb::SetDbFile(String File) { |
114 |
DbInstrumentsMutex.Lock(); |
LockGuard lock(DbInstrumentsMutex); |
115 |
if (File.empty() || DbFile.length() > 0) { |
if (File.empty() || DbFile.length() > 0) { |
|
DbInstrumentsMutex.Unlock(); |
|
116 |
throw Exception("Failed to set the database file"); |
throw Exception("Failed to set the database file"); |
117 |
} |
} |
118 |
DbFile = File; |
DbFile = File; |
|
DbInstrumentsMutex.Unlock(); |
|
119 |
} |
} |
120 |
|
|
121 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
462 |
|
|
463 |
bool InstrumentsDb::DirectoryExist(String Dir) { |
bool InstrumentsDb::DirectoryExist(String Dir) { |
464 |
dmsg(2,("InstrumentsDb: DirectoryExist(Dir=%s)\n", Dir.c_str())); |
dmsg(2,("InstrumentsDb: DirectoryExist(Dir=%s)\n", Dir.c_str())); |
465 |
bool b; |
{ |
466 |
|
LockGuard lock(DbInstrumentsMutex); |
467 |
DbInstrumentsMutex.Lock(); |
return GetDirectoryId(Dir) != -1; |
|
try { b = GetDirectoryId(Dir) != -1; } |
|
|
catch (Exception e) { |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
throw e; |
|
468 |
} |
} |
|
DbInstrumentsMutex.Unlock(); |
|
|
|
|
|
return b; |
|
469 |
} |
} |
470 |
|
|
471 |
DbDirectory InstrumentsDb::GetDirectoryInfo(String Dir) { |
DbDirectory InstrumentsDb::GetDirectoryInfo(String Dir) { |
705 |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); |
706 |
if (DbDir.empty() || FilePath.empty()) return; |
if (DbDir.empty() || FilePath.empty()) return; |
707 |
|
|
708 |
DbInstrumentsMutex.Lock(); |
{ |
709 |
try { |
LockGuard lock(DbInstrumentsMutex); |
710 |
|
|
711 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
712 |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
713 |
|
|
726 |
|
|
727 |
String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; |
String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; |
728 |
AddInstrumentsFromFile(dir, FilePath, Index, pProgress); |
AddInstrumentsFromFile(dir, FilePath, Index, pProgress); |
|
} catch (Exception e) { |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
throw e; |
|
729 |
} |
} |
|
|
|
|
DbInstrumentsMutex.Unlock(); |
|
730 |
} |
} |
731 |
|
|
732 |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { |
733 |
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir)); |
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir)); |
734 |
if (DbDir.empty() || FsDir.empty()) return; |
if (DbDir.empty() || FsDir.empty()) return; |
735 |
|
|
736 |
DbInstrumentsMutex.Lock(); |
{ |
737 |
try { |
LockGuard lock(DbInstrumentsMutex); |
738 |
|
|
739 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
740 |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
741 |
|
|
762 |
} |
} |
763 |
} catch(Exception e) { |
} catch(Exception e) { |
764 |
e.PrintMessage(); |
e.PrintMessage(); |
|
DbInstrumentsMutex.Unlock(); |
|
|
return; |
|
765 |
} |
} |
|
} catch (Exception e) { |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
throw e; |
|
766 |
} |
} |
|
|
|
|
DbInstrumentsMutex.Unlock(); |
|
767 |
} |
} |
768 |
|
|
769 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { |
1447 |
} |
} |
1448 |
InTransaction = false; |
InTransaction = false; |
1449 |
|
|
1450 |
if(db == NULL) { |
if (db == NULL) { |
1451 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
1452 |
return; |
return; |
1453 |
} |
} |
1651 |
|
|
1652 |
void InstrumentsDb::BindTextParam(sqlite3_stmt* pStmt, int Index, String Text) { |
void InstrumentsDb::BindTextParam(sqlite3_stmt* pStmt, int Index, String Text) { |
1653 |
if (pStmt == NULL) return; |
if (pStmt == NULL) return; |
1654 |
int res = sqlite3_bind_text(pStmt, Index, Text.c_str(), -1, SQLITE_STATIC); |
int res = sqlite3_bind_text(pStmt, Index, Text.c_str(), -1, SQLITE_TRANSIENT); |
1655 |
if (res != SQLITE_OK) { |
if (res != SQLITE_OK) { |
1656 |
sqlite3_finalize(pStmt); |
sqlite3_finalize(pStmt); |
1657 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1722 |
} |
} |
1723 |
|
|
1724 |
void InstrumentsDb::Format() { |
void InstrumentsDb::Format() { |
1725 |
DbInstrumentsMutex.Lock(); |
{ |
1726 |
if (db != NULL) { |
LockGuard lock(DbInstrumentsMutex); |
|
sqlite3_close(db); |
|
|
db = NULL; |
|
|
} |
|
1727 |
|
|
1728 |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
if (db != NULL) { |
1729 |
String bkp = DbFile + ".bkp"; |
sqlite3_close(db); |
1730 |
remove(bkp.c_str()); |
db = NULL; |
1731 |
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
} |
1732 |
DbInstrumentsMutex.Unlock(); |
|
1733 |
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
1734 |
} |
String bkp = DbFile + ".bkp"; |
1735 |
|
remove(bkp.c_str()); |
1736 |
|
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
1737 |
|
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
1738 |
|
} |
1739 |
|
|
1740 |
String f = DbFile; |
String f = DbFile; |
1741 |
DbFile = ""; |
DbFile = ""; |
1742 |
try { CreateInstrumentsDb(f); } |
CreateInstrumentsDb(f); |
|
catch(Exception e) { |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
throw e; |
|
1743 |
} |
} |
|
DbInstrumentsMutex.Unlock(); |
|
|
|
|
1744 |
FireDirectoryCountChanged("/"); |
FireDirectoryCountChanged("/"); |
1745 |
FireInstrumentCountChanged("/"); |
FireInstrumentCountChanged("/"); |
1746 |
} |
} |