1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007 Grigor Iliev * |
* Copyright (C) 2007, 2008 Grigor Iliev * |
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 * |
20 |
|
|
21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#if HAVE_SQLITE3 |
#include "../common/File.h" |
24 |
|
#include "../common/global_private.h" |
25 |
|
|
26 |
#include <iostream> |
#include <iostream> |
27 |
#include <sstream> |
#include <sstream> |
28 |
#include <vector> |
#include <vector> |
|
#include <dirent.h> |
|
29 |
#include <errno.h> |
#include <errno.h> |
30 |
#include <fnmatch.h> |
#include <fnmatch.h> |
31 |
|
|
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
36 |
InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb; |
InstrumentsDb InstrumentsDb::instance; |
37 |
|
|
38 |
void InstrumentsDb::CreateInstrumentsDb(String File) { |
void InstrumentsDb::CreateInstrumentsDb(String FilePath) { |
39 |
struct stat statBuf; |
File f = File(FilePath); |
40 |
int res = stat(File.c_str(), &statBuf); |
if (f.Exist()) { |
41 |
if (!res) { |
throw Exception("File exists: " + FilePath); |
|
throw Exception("File exists: " + File); |
|
42 |
} |
} |
43 |
|
|
44 |
GetInstrumentsDb()->SetDbFile(File); |
GetInstrumentsDb()->SetDbFile(FilePath); |
45 |
|
|
46 |
String sql = |
String sql = |
47 |
" CREATE TABLE instr_dirs ( " |
" CREATE TABLE instr_dirs ( " |
57 |
|
|
58 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
59 |
|
|
60 |
sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, 0, '/');"; |
sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');"; |
61 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
62 |
|
|
63 |
sql = |
sql = |
94 |
if (db != NULL) sqlite3_close(db); |
if (db != NULL) sqlite3_close(db); |
95 |
} |
} |
96 |
|
|
|
void InstrumentsDb::Destroy() { |
|
|
if (pInstrumentsDb != NULL) { |
|
|
delete pInstrumentsDb; |
|
|
pInstrumentsDb = NULL; |
|
|
} |
|
|
} |
|
|
|
|
97 |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
98 |
llInstrumentsDbListeners.AddListener(l); |
llInstrumentsDbListeners.AddListener(l); |
99 |
} |
} |
103 |
} |
} |
104 |
|
|
105 |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
106 |
return pInstrumentsDb; |
return &instance; |
107 |
} |
} |
108 |
|
|
109 |
void InstrumentsDb::SetDbFile(String File) { |
void InstrumentsDb::SetDbFile(String File) { |
119 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
120 |
if ( db != NULL) return db; |
if ( db != NULL) return db; |
121 |
|
|
122 |
if (DbFile.empty()) DbFile = "/var/lib/linuxsampler/instruments.db"; |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
123 |
|
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
124 |
|
if (DbFile.find("~") == 0) |
125 |
|
DbFile.replace(0, 1, getenv("HOME")); |
126 |
|
#endif |
127 |
int rc = sqlite3_open(DbFile.c_str(), &db); |
int rc = sqlite3_open(DbFile.c_str(), &db); |
128 |
if (rc) { |
if (rc) { |
129 |
sqlite3_close(db); |
sqlite3_close(db); |
132 |
} |
} |
133 |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
134 |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
135 |
|
|
136 |
|
// TODO: remove this in the next version |
137 |
|
try { |
138 |
|
int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0"); |
139 |
|
// The parent ID of the root directory should be -2 now. |
140 |
|
if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0"); |
141 |
|
} catch(Exception e) { } |
142 |
|
//////////////////////////////////////// |
143 |
|
|
144 |
return db; |
return db; |
145 |
} |
} |
153 |
|
|
154 |
int count = ExecSqlInt(sql.str()); |
int count = ExecSqlInt(sql.str()); |
155 |
|
|
|
// While the root dir has ID 0 and parent ID 0, the directory |
|
|
// count for the root dir will be incorrect, so we should fix it. |
|
|
if (count != -1 && DirId == 0) count--; |
|
156 |
return count; |
return count; |
157 |
} |
} |
158 |
|
|
174 |
throw e; |
throw e; |
175 |
} |
} |
176 |
EndTransaction(); |
EndTransaction(); |
177 |
if (i == -1) throw Exception("Unkown DB directory: " + Dir); |
if (i == -1) throw Exception("Unkown DB directory: " + toEscapedPath(Dir)); |
178 |
|
|
179 |
return i; |
return i; |
180 |
} |
} |
193 |
BeginTransaction(); |
BeginTransaction(); |
194 |
try { |
try { |
195 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
196 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
197 |
|
|
198 |
StringListPtr pDirs; |
StringListPtr pDirs; |
199 |
if (Recursive) { |
if (Recursive) { |
216 |
std::stringstream sql; |
std::stringstream sql; |
217 |
sql << "SELECT dir_name FROM instr_dirs "; |
sql << "SELECT dir_name FROM instr_dirs "; |
218 |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
219 |
return ExecSqlStringList(sql.str()); |
StringListPtr dirs = ExecSqlStringList(sql.str()); |
220 |
|
|
221 |
|
for (int i = 0; i < dirs->size(); i++) { |
222 |
|
for (int j = 0; j < dirs->at(i).length(); j++) { |
223 |
|
if (dirs->at(i).at(j) == '/') dirs->at(i).at(j) = '\0'; |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
|
return dirs; |
228 |
} |
} |
229 |
|
|
230 |
int InstrumentsDb::GetDirectoryId(String Dir) { |
int InstrumentsDb::GetDirectoryId(String Dir) { |
253 |
|
|
254 |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
255 |
dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str())); |
dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str())); |
256 |
|
DirName = toDbName(DirName); |
257 |
std::stringstream sql; |
std::stringstream sql; |
258 |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
259 |
sql << ParentDirId << " AND dir_name=?"; |
sql << ParentDirId << " AND dir_name=?"; |
306 |
|
|
307 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
308 |
if(ParentDir.empty() || dirName.empty()) { |
if(ParentDir.empty() || dirName.empty()) { |
309 |
throw Exception("Failed to add DB directory: " + Dir); |
throw Exception("Failed to add DB directory: " + toEscapedPath(Dir)); |
310 |
} |
} |
311 |
|
|
312 |
int id = GetDirectoryId(ParentDir); |
int id = GetDirectoryId(ParentDir); |
313 |
if (id == -1) throw Exception("DB directory doesn't exist: " + ParentDir); |
if (id == -1) throw Exception("DB directory doesn't exist: " + toEscapedPath(ParentDir)); |
314 |
int id2 = GetDirectoryId(id, dirName); |
int id2 = GetDirectoryId(id, dirName); |
315 |
if (id2 != -1) throw Exception("DB directory already exist: " + Dir); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(Dir)); |
316 |
id2 = GetInstrumentId(id, dirName); |
id2 = GetInstrumentId(id, dirName); |
317 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + Dir); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(Dir)); |
318 |
|
|
319 |
std::stringstream sql; |
std::stringstream sql; |
320 |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
321 |
sql << id << ", ?)"; |
sql << id << ", ?)"; |
322 |
|
|
323 |
ExecSql(sql.str(), dirName); |
ExecSql(sql.str(), toDbName(dirName)); |
324 |
} catch (Exception e) { |
} catch (Exception e) { |
325 |
EndTransaction(); |
EndTransaction(); |
326 |
throw e; |
throw e; |
339 |
BeginTransaction(); |
BeginTransaction(); |
340 |
try { |
try { |
341 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
342 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
343 |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
344 |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
345 |
if (Force) RemoveDirectoryContent(dirId); |
if (Force) RemoveDirectoryContent(dirId); |
426 |
|
|
427 |
try { |
try { |
428 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
429 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
430 |
|
|
431 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
432 |
std::stringstream sql; |
std::stringstream sql; |
449 |
if (res != SQLITE_DONE) { |
if (res != SQLITE_DONE) { |
450 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
451 |
} else { |
} else { |
452 |
throw Exception("Unknown DB directory: " + Dir); |
throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
453 |
} |
} |
454 |
} |
} |
455 |
|
|
466 |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
467 |
dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str())); |
dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str())); |
468 |
CheckFileName(Name); |
CheckFileName(Name); |
469 |
|
String dbName = toDbName(Name); |
470 |
|
|
471 |
BeginTransaction(); |
BeginTransaction(); |
472 |
try { |
try { |
473 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
474 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedText(Dir)); |
475 |
|
|
476 |
std::stringstream sql; |
std::stringstream sql; |
477 |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
478 |
|
|
479 |
int parent = ExecSqlInt(sql.str()); |
int parent = ExecSqlInt(sql.str()); |
480 |
if (parent == -1) throw Exception("Unknown parent directory: " + Dir); |
if (parent == -1) throw Exception("Unknown parent directory: " + toEscapedPath(Dir)); |
481 |
if (GetDirectoryId(parent, Name) != -1) { |
|
482 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
if (GetDirectoryId(parent, dbName) != -1) { |
483 |
|
String s = toEscapedPath(Name); |
484 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
485 |
} |
} |
486 |
|
|
487 |
if (GetInstrumentId(parent, Name) != -1) { |
if (GetInstrumentId(parent, dbName) != -1) { |
488 |
throw Exception("Cannot rename. Instrument with that name exist: " + Dir); |
throw Exception("Cannot rename. Instrument with that name exist: " + toEscapedPath(Dir)); |
489 |
} |
} |
490 |
|
|
491 |
sql.str(""); |
sql.str(""); |
492 |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
493 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), dbName); |
494 |
} catch (Exception e) { |
} catch (Exception e) { |
495 |
EndTransaction(); |
EndTransaction(); |
496 |
throw e; |
throw e; |
497 |
} |
} |
498 |
|
|
499 |
EndTransaction(); |
EndTransaction(); |
500 |
FireDirectoryNameChanged(Dir, Name); |
FireDirectoryNameChanged(Dir, toAbstractName(Name)); |
501 |
} |
} |
502 |
|
|
503 |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
510 |
BeginTransaction(); |
BeginTransaction(); |
511 |
try { |
try { |
512 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
513 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
514 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
515 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
516 |
if (dirId == dstId) { |
if (dirId == dstId) { |
517 |
throw Exception("Cannot move directory to itself"); |
throw Exception("Cannot move directory to itself"); |
518 |
} |
} |
528 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
529 |
|
|
530 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
531 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
532 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
533 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
534 |
|
|
535 |
std::stringstream sql; |
std::stringstream sql; |
536 |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
556 |
BeginTransaction(); |
BeginTransaction(); |
557 |
try { |
try { |
558 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
559 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
560 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
561 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
562 |
if (dirId == dstId) { |
if (dirId == dstId) { |
563 |
throw Exception("Cannot copy directory to itself"); |
throw Exception("Cannot copy directory to itself"); |
564 |
} |
} |
574 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
575 |
|
|
576 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
577 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
578 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
579 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
580 |
|
|
581 |
DirectoryCopier directoryCopier(ParentDir, Dst); |
DirectoryCopier directoryCopier(ParentDir, Dst); |
582 |
DirectoryTreeWalk(Dir, &directoryCopier); |
DirectoryTreeWalk(Dir, &directoryCopier); |
594 |
BeginTransaction(); |
BeginTransaction(); |
595 |
try { |
try { |
596 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
597 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
598 |
|
|
599 |
std::stringstream sql; |
std::stringstream sql; |
600 |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
658 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
659 |
try { |
try { |
660 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
661 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
662 |
|
|
663 |
struct stat statBuf; |
File f = File(FilePath); |
664 |
int res = stat(FilePath.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
665 |
std::stringstream ss; |
std::stringstream ss; |
666 |
ss << "Fail to stat `" << FilePath << "`: " << strerror(errno); |
ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg(); |
667 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
668 |
} |
} |
669 |
|
|
670 |
if (!S_ISREG(statBuf.st_mode)) { |
if (!f.IsFile()) { |
671 |
std::stringstream ss; |
std::stringstream ss; |
672 |
ss << "`" << FilePath << "` is not an instrument file"; |
ss << "`" << FilePath << "` is not an instrument file"; |
673 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
689 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
690 |
try { |
try { |
691 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
692 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
693 |
|
|
694 |
struct stat statBuf; |
File f = File(FsDir); |
695 |
int res = stat(FsDir.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
696 |
std::stringstream ss; |
std::stringstream ss; |
697 |
ss << "Fail to stat `" << FsDir << "`: " << strerror(errno); |
ss << "Fail to stat `" << FsDir << "`: " << f.GetErrorMsg(); |
698 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
699 |
} |
} |
700 |
|
|
701 |
if (!S_ISDIR(statBuf.st_mode)) { |
if (!f.IsDirectory()) { |
702 |
throw Exception("Directory expected"); |
throw Exception("Directory expected: " + FsDir); |
703 |
} |
} |
704 |
|
|
705 |
if (FsDir.at(FsDir.length() - 1) != '/') FsDir.append("/"); |
if (FsDir.at(FsDir.length() - 1) != File::DirSeparator) { |
706 |
|
FsDir.push_back(File::DirSeparator); |
|
DIR* pDir = opendir(FsDir.c_str()); |
|
|
if (pDir == NULL) { |
|
|
std::stringstream ss; |
|
|
ss << "The scanning of directory `" << FsDir << "` failed: "; |
|
|
ss << strerror(errno); |
|
|
std::cerr << ss.str(); |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
return; |
|
707 |
} |
} |
708 |
|
|
709 |
struct dirent* pEnt = readdir(pDir); |
try { |
710 |
while (pEnt != NULL) { |
FileListPtr fileList = File::GetFiles(FsDir); |
711 |
if (pEnt->d_type != DT_REG) { |
for (int i = 0; i < fileList->size(); i++) { |
712 |
pEnt = readdir(pDir); |
AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress); |
|
continue; |
|
713 |
} |
} |
714 |
|
} catch(Exception e) { |
715 |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name), -1, pProgress); |
e.PrintMessage(); |
716 |
pEnt = readdir(pDir); |
DbInstrumentsMutex.Unlock(); |
717 |
} |
return; |
|
|
|
|
if (closedir(pDir)) { |
|
|
std::stringstream ss; |
|
|
ss << "Failed to close directory `" << FsDir << "`: "; |
|
|
ss << strerror(errno); |
|
|
std::cerr << ss.str(); |
|
718 |
} |
} |
719 |
} catch (Exception e) { |
} catch (Exception e) { |
720 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
727 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { |
728 |
dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat)); |
dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat)); |
729 |
if (pProgress != NULL) { |
if (pProgress != NULL) { |
730 |
pProgress->SetTotalFileCount(InstrumentFileCounter::Count(FsDir)); |
InstrumentFileCounter c; |
731 |
|
pProgress->SetTotalFileCount(c.Count(FsDir)); |
732 |
} |
} |
733 |
|
|
734 |
DirectoryScanner::Scan(DbDir, FsDir, Flat, pProgress); |
DirectoryScanner d; |
735 |
|
d.Scan(DbDir, FsDir, Flat, pProgress); |
736 |
} |
} |
737 |
|
|
738 |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
764 |
} |
} |
765 |
EndTransaction(); |
EndTransaction(); |
766 |
|
|
767 |
if (i == -1) throw Exception("Unknown Db directory: " + Dir); |
if (i == -1) throw Exception("Unknown Db directory: " + toEscapedPath(Dir)); |
768 |
return i; |
return i; |
769 |
} |
} |
770 |
|
|
780 |
BeginTransaction(); |
BeginTransaction(); |
781 |
try { |
try { |
782 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
783 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
784 |
|
|
785 |
StringListPtr pInstrs; |
StringListPtr pInstrs; |
786 |
|
|
794 |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
795 |
|
|
796 |
pInstrs = ExecSqlStringList(sql.str()); |
pInstrs = ExecSqlStringList(sql.str()); |
797 |
|
// Converting to abstract names |
798 |
|
for (int i = 0; i < pInstrs->size(); i++) { |
799 |
|
for (int j = 0; j < pInstrs->at(i).length(); j++) { |
800 |
|
if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0'; |
801 |
|
} |
802 |
|
} |
803 |
} |
} |
804 |
EndTransaction(); |
EndTransaction(); |
805 |
return pInstrs; |
return pInstrs; |
824 |
std::stringstream sql; |
std::stringstream sql; |
825 |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
826 |
sql << DirId << " AND instr_name=?"; |
sql << DirId << " AND instr_name=?"; |
827 |
return ExecSqlInt(sql.str(), InstrName); |
return ExecSqlInt(sql.str(), toDbName(InstrName)); |
828 |
} |
} |
829 |
|
|
830 |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
831 |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
832 |
std::stringstream sql; |
std::stringstream sql; |
833 |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
834 |
return ExecSqlString(sql.str()); |
return toAbstractName(ExecSqlString(sql.str())); |
835 |
} |
} |
836 |
|
|
837 |
void InstrumentsDb::RemoveInstrument(String Instr) { |
void InstrumentsDb::RemoveInstrument(String Instr) { |
843 |
try { |
try { |
844 |
int instrId = GetInstrumentId(Instr); |
int instrId = GetInstrumentId(Instr); |
845 |
if(instrId == -1) { |
if(instrId == -1) { |
846 |
throw Exception("The specified instrument does not exist: " + Instr); |
throw Exception("The specified instrument does not exist: " + toEscapedPath(Instr)); |
847 |
} |
} |
848 |
RemoveInstrument(instrId); |
RemoveInstrument(instrId); |
849 |
} catch (Exception e) { |
} catch (Exception e) { |
878 |
BeginTransaction(); |
BeginTransaction(); |
879 |
try { |
try { |
880 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
881 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
882 |
i = GetInstrumentInfo(id); |
i = GetInstrumentInfo(id); |
883 |
} catch (Exception e) { |
} catch (Exception e) { |
884 |
EndTransaction(); |
EndTransaction(); |
937 |
BeginTransaction(); |
BeginTransaction(); |
938 |
try { |
try { |
939 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
940 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
941 |
|
|
942 |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
943 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
944 |
|
|
945 |
if (GetInstrumentId(dirId, Name) != -1) { |
if (GetInstrumentId(dirId, Name) != -1) { |
946 |
throw Exception("Cannot rename. Instrument with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
947 |
|
throw Exception("Cannot rename. Instrument with that name already exists: " + s); |
948 |
} |
} |
949 |
|
|
950 |
if (GetDirectoryId(dirId, Name) != -1) { |
if (GetDirectoryId(dirId, Name) != -1) { |
951 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
952 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
953 |
} |
} |
954 |
|
|
955 |
std::stringstream sql; |
std::stringstream sql; |
956 |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
957 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), toDbName(Name)); |
958 |
} catch (Exception e) { |
} catch (Exception e) { |
959 |
EndTransaction(); |
EndTransaction(); |
960 |
throw e; |
throw e; |
961 |
} |
} |
962 |
EndTransaction(); |
EndTransaction(); |
963 |
FireInstrumentNameChanged(Instr, Name); |
FireInstrumentNameChanged(Instr, toAbstractName(Name)); |
964 |
} |
} |
965 |
|
|
966 |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
970 |
|
|
971 |
BeginTransaction(); |
BeginTransaction(); |
972 |
try { |
try { |
973 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(ParentDir); |
974 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
975 |
|
|
976 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
977 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
978 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
979 |
|
|
980 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
981 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
982 |
if (dirId == dstId) { |
if (dirId == dstId) { |
983 |
EndTransaction(); |
EndTransaction(); |
984 |
return; |
return; |
985 |
} |
} |
986 |
|
|
987 |
if (GetInstrumentId(dstId, instrName) != -1) { |
if (GetInstrumentId(dstId, instrName) != -1) { |
988 |
throw Exception("Cannot move. Instrument with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
989 |
|
throw Exception("Cannot move. Instrument with that name already exists: " + s); |
990 |
} |
} |
991 |
|
|
992 |
if (GetDirectoryId(dstId, instrName) != -1) { |
if (GetDirectoryId(dstId, instrName) != -1) { |
993 |
throw Exception("Cannot move. Directory with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
994 |
|
throw Exception("Cannot move. Directory with that name already exists: " + s); |
995 |
} |
} |
996 |
|
|
997 |
std::stringstream sql; |
std::stringstream sql; |
1015 |
BeginTransaction(); |
BeginTransaction(); |
1016 |
try { |
try { |
1017 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
1018 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1019 |
|
|
1020 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1021 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1022 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1023 |
|
|
1024 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1025 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1026 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1027 |
EndTransaction(); |
EndTransaction(); |
1028 |
return; |
return; |
1029 |
} |
} |
1030 |
|
|
|
if (GetInstrumentId(dstId, instrName) != -1) { |
|
|
throw Exception("Cannot copy. Instrument with that name already exists: " + instrName); |
|
|
} |
|
|
|
|
|
if (GetDirectoryId(dstId, instrName) != -1) { |
|
|
throw Exception("Cannot copy. Directory with that name already exists: " + instrName); |
|
|
} |
|
|
|
|
1031 |
CopyInstrument(instrId, instrName, dstId, Dst); |
CopyInstrument(instrId, instrName, dstId, Dst); |
1032 |
} catch (Exception e) { |
} catch (Exception e) { |
1033 |
EndTransaction(); |
EndTransaction(); |
1038 |
} |
} |
1039 |
|
|
1040 |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
1041 |
|
if (GetInstrumentId(DstDirId, InstrName) != -1) { |
1042 |
|
String s = toEscapedPath(InstrName); |
1043 |
|
throw Exception("Cannot copy. Instrument with that name already exists: " + s); |
1044 |
|
} |
1045 |
|
|
1046 |
|
if (GetDirectoryId(DstDirId, InstrName) != -1) { |
1047 |
|
String s = toEscapedPath(InstrName); |
1048 |
|
throw Exception("Cannot copy. Directory with that name already exists: " + s); |
1049 |
|
} |
1050 |
|
|
1051 |
DbInstrument i = GetInstrumentInfo(InstrId); |
DbInstrument i = GetInstrumentInfo(InstrId); |
1052 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1053 |
std::stringstream sql; |
std::stringstream sql; |
1061 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1062 |
} |
} |
1063 |
|
|
1064 |
BindTextParam(pStmt, 1, InstrName); |
String s = toDbName(InstrName); |
1065 |
|
BindTextParam(pStmt, 1, s); |
1066 |
BindTextParam(pStmt, 2, i.InstrFile); |
BindTextParam(pStmt, 2, i.InstrFile); |
1067 |
BindTextParam(pStmt, 3, i.FormatFamily); |
BindTextParam(pStmt, 3, i.FormatFamily); |
1068 |
BindTextParam(pStmt, 4, i.FormatVersion); |
BindTextParam(pStmt, 4, i.FormatVersion); |
1087 |
BeginTransaction(); |
BeginTransaction(); |
1088 |
try { |
try { |
1089 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
1090 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1091 |
|
|
1092 |
std::stringstream sql; |
std::stringstream sql; |
1093 |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
1121 |
} |
} |
1122 |
} |
} |
1123 |
} catch(Exception e) { |
} catch(Exception e) { |
1124 |
std::cerr << e.Message() << std::endl; |
e.PrintMessage(); |
1125 |
} |
} |
1126 |
} |
} |
1127 |
|
|
1128 |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index, ScanProgress* pProgress) { |
void InstrumentsDb::AddGigInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { |
1129 |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); |
1130 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
1131 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
1132 |
|
|
1133 |
struct stat statBuf; |
File f = File(FilePath); |
1134 |
int res = stat(File.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
1135 |
std::stringstream ss; |
std::stringstream ss; |
1136 |
ss << "Fail to stat `" << File << "`: " << strerror(errno); |
ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg(); |
1137 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1138 |
} |
} |
1139 |
|
|
1140 |
if (!S_ISREG(statBuf.st_mode)) { |
if (!f.IsFile()) { |
1141 |
std::stringstream ss; |
std::stringstream ss; |
1142 |
ss << "`" << File << "` is not a regular file"; |
ss << "`" << FilePath << "` is not a regular file"; |
1143 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1144 |
} |
} |
1145 |
|
|
1146 |
RIFF::File* riff = NULL; |
RIFF::File* riff = NULL; |
1147 |
gig::File* gig = NULL; |
gig::File* gig = NULL; |
1148 |
try { |
try { |
1149 |
riff = new RIFF::File(File); |
riff = new RIFF::File(FilePath); |
1150 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
1151 |
|
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
1152 |
|
|
1153 |
std::stringstream sql; |
std::stringstream sql; |
1154 |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
1155 |
sql << "instr_nr,format_family,format_version,instr_size,"; |
sql << "instr_nr,format_family,format_version,instr_size,"; |
1156 |
sql << "description,is_drum,product,artists,keywords) VALUES ("; |
sql << "description,is_drum,product,artists,keywords) VALUES ("; |
1157 |
sql << dirId << ",?,?,?,'GIG',?," << statBuf.st_size << ",?,?,?,?,?)"; |
sql << dirId << ",?,?,?,'GIG',?," << f.GetSize() << ",?,?,?,?,?)"; |
1158 |
|
|
1159 |
sqlite3_stmt* pStmt = NULL; |
sqlite3_stmt* pStmt = NULL; |
1160 |
|
|
1163 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1164 |
} |
} |
1165 |
|
|
1166 |
BindTextParam(pStmt, 2, File); |
String s = toEscapedFsPath(FilePath); |
1167 |
|
BindTextParam(pStmt, 2, s); |
1168 |
String ver = ""; |
String ver = ""; |
1169 |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
1170 |
BindTextParam(pStmt, 4, ver); |
BindTextParam(pStmt, 4, ver); |
1177 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1178 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1179 |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
1180 |
AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, instrIndex); |
AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, instrIndex); |
1181 |
|
|
1182 |
instrIndex++; |
instrIndex++; |
1183 |
pInstrument = gig->GetNextInstrument(); |
pInstrument = gig->GetNextInstrument(); |
1190 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1191 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1192 |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
1193 |
AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, Index); |
AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, Index); |
1194 |
} |
} |
1195 |
} |
} |
1196 |
|
|
1201 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1202 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1203 |
std::stringstream ss; |
std::stringstream ss; |
1204 |
ss << "Failed to scan `" << File << "`: " << e.Message; |
ss << "Failed to scan `" << FilePath << "`: " << e.Message; |
1205 |
|
|
1206 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1207 |
} catch (Exception e) { |
} catch (Exception e) { |
1211 |
} catch (...) { |
} catch (...) { |
1212 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1213 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1214 |
throw Exception("Failed to scan `" + File + "`"); |
throw Exception("Failed to scan `" + FilePath + "`"); |
1215 |
} |
} |
1216 |
} |
} |
1217 |
|
|
1218 |
void InstrumentsDb::AddGigInstrument(sqlite3_stmt* pStmt, String DbDir, int DirId, String File, gig::Instrument* pInstrument, int Index) { |
void InstrumentsDb::AddGigInstrument(sqlite3_stmt* pStmt, String DbDir, int DirId, String File, gig::Instrument* pInstrument, int Index) { |
1219 |
|
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
1220 |
String name = pInstrument->pInfo->Name; |
String name = pInstrument->pInfo->Name; |
1221 |
if (name == "") return; |
if (name == "") return; |
1222 |
name = GetUniqueInstrumentName(DirId, name); |
name = GetUniqueInstrumentName(DirId, name); |
1224 |
std::stringstream sql2; |
std::stringstream sql2; |
1225 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1226 |
sql2 << "instr_nr=" << Index; |
sql2 << "instr_nr=" << Index; |
1227 |
if (ExecSqlInt(sql2.str(), File) > 0) return; |
String s = toEscapedFsPath(File); |
1228 |
|
if (ExecSqlInt(sql2.str(), s) > 0) return; |
1229 |
|
|
1230 |
BindTextParam(pStmt, 1, name); |
BindTextParam(pStmt, 1, name); |
1231 |
BindIntParam(pStmt, 3, Index); |
BindIntParam(pStmt, 3, Index); |
1254 |
FireInstrumentCountChanged(DbDir); |
FireInstrumentCountChanged(DbDir); |
1255 |
} |
} |
1256 |
|
|
1257 |
void InstrumentsDb::DirectoryTreeWalk(String Path, DirectoryHandler* pHandler) { |
void InstrumentsDb::DirectoryTreeWalk(String AbstractPath, DirectoryHandler* pHandler) { |
1258 |
int DirId = GetDirectoryId(Path); |
int DirId = GetDirectoryId(AbstractPath); |
1259 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Path); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(AbstractPath)); |
1260 |
DirectoryTreeWalk(pHandler, Path, DirId, 0); |
DirectoryTreeWalk(pHandler, AbstractPath, DirId, 0); |
1261 |
} |
} |
1262 |
|
|
1263 |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String Path, int DirId, int Level) { |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String AbstractPath, int DirId, int Level) { |
1264 |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
1265 |
pHandler->ProcessDirectory(Path, DirId); |
pHandler->ProcessDirectory(AbstractPath, DirId); |
1266 |
|
|
1267 |
String s; |
String s; |
1268 |
StringListPtr pDirs = GetDirectories(DirId); |
StringListPtr pDirs = GetDirectories(DirId); |
1269 |
for(int i = 0; i < pDirs->size(); i++) { |
for(int i = 0; i < pDirs->size(); i++) { |
1270 |
if (Path.length() == 1 && Path.at(0) == '/') s = "/" + pDirs->at(i); |
if (AbstractPath.length() == 1 && AbstractPath.at(0) == '/') { |
1271 |
else s = Path + "/" + pDirs->at(i); |
s = "/" + pDirs->at(i); |
1272 |
|
} else { |
1273 |
|
s = AbstractPath + "/" + pDirs->at(i); |
1274 |
|
} |
1275 |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
1276 |
} |
} |
1277 |
} |
} |
1283 |
BeginTransaction(); |
BeginTransaction(); |
1284 |
try { |
try { |
1285 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1286 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1287 |
|
|
1288 |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
1289 |
else directoryFinder.ProcessDirectory(Dir, DirId); |
else directoryFinder.ProcessDirectory(Dir, DirId); |
1303 |
BeginTransaction(); |
BeginTransaction(); |
1304 |
try { |
try { |
1305 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1306 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1307 |
|
|
1308 |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
1309 |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
1605 |
return Dir.substr(0, i); |
return Dir.substr(0, i); |
1606 |
} |
} |
1607 |
|
|
1608 |
|
void InstrumentsDb::Format() { |
1609 |
|
DbInstrumentsMutex.Lock(); |
1610 |
|
if (db != NULL) { |
1611 |
|
sqlite3_close(db); |
1612 |
|
db = NULL; |
1613 |
|
} |
1614 |
|
|
1615 |
|
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
1616 |
|
String bkp = DbFile + ".bkp"; |
1617 |
|
remove(bkp.c_str()); |
1618 |
|
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
1619 |
|
DbInstrumentsMutex.Unlock(); |
1620 |
|
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
1621 |
|
} |
1622 |
|
|
1623 |
|
String f = DbFile; |
1624 |
|
DbFile = ""; |
1625 |
|
try { CreateInstrumentsDb(f); } |
1626 |
|
catch(Exception e) { |
1627 |
|
DbInstrumentsMutex.Unlock(); |
1628 |
|
throw e; |
1629 |
|
} |
1630 |
|
DbInstrumentsMutex.Unlock(); |
1631 |
|
|
1632 |
|
FireDirectoryCountChanged("/"); |
1633 |
|
FireInstrumentCountChanged("/"); |
1634 |
|
} |
1635 |
|
|
1636 |
void InstrumentsDb::CheckFileName(String File) { |
void InstrumentsDb::CheckFileName(String File) { |
1637 |
if (File.empty()) throw Exception("Invalid file name: " + File); |
if (File.empty()) throw Exception("Invalid file name: " + File); |
|
if (File.find('/') != std::string::npos) { |
|
|
throw Exception("Invalid file name: " + File); |
|
|
} |
|
1638 |
} |
} |
1639 |
|
|
1640 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
1653 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1654 |
} |
} |
1655 |
|
|
1656 |
|
String InstrumentsDb::toDbName(String AbstractName) { |
1657 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1658 |
|
if (AbstractName.at(i) == '\0') AbstractName.at(i) = '/'; |
1659 |
|
} |
1660 |
|
return AbstractName; |
1661 |
|
} |
1662 |
|
|
1663 |
|
String InstrumentsDb::toEscapedPath(String AbstractName) { |
1664 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1665 |
|
if (AbstractName.at(i) == '\0') AbstractName.replace(i++, 1, "\\x2f"); |
1666 |
|
else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\"); |
1667 |
|
else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'"); |
1668 |
|
else if (AbstractName.at(i) == '"') AbstractName.replace(i++, 1, "\\\""); |
1669 |
|
else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r"); |
1670 |
|
else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n"); |
1671 |
|
} |
1672 |
|
return AbstractName; |
1673 |
|
} |
1674 |
|
|
1675 |
|
String InstrumentsDb::toEscapedText(String text) { |
1676 |
|
for (int i = 0; i < text.length(); i++) { |
1677 |
|
if (text.at(i) == '\\') text.replace(i++, 1, "\\\\"); |
1678 |
|
else if (text.at(i) == '\'') text.replace(i++, 1, "\\'"); |
1679 |
|
else if (text.at(i) == '"') text.replace(i++, 1, "\\\""); |
1680 |
|
else if (text.at(i) == '\r') text.replace(i++, 1, "\\r"); |
1681 |
|
else if (text.at(i) == '\n') text.replace(i++, 1, "\\n"); |
1682 |
|
} |
1683 |
|
return text; |
1684 |
|
} |
1685 |
|
|
1686 |
|
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
1687 |
|
return toEscapedText(FsPath); |
1688 |
|
} |
1689 |
|
|
1690 |
|
String InstrumentsDb::toAbstractName(String DbName) { |
1691 |
|
for (int i = 0; i < DbName.length(); i++) { |
1692 |
|
if (DbName.at(i) == '/') DbName.at(i) = '\0'; |
1693 |
|
} |
1694 |
|
return DbName; |
1695 |
|
} |
1696 |
|
|
1697 |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
1698 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1699 |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
1737 |
} |
} |
1738 |
|
|
1739 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
|
|
|
|
#endif // HAVE_SQLITE3 |
|