/[svn]/linuxsampler/trunk/src/db/InstrumentsDb.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/db/InstrumentsDb.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1944 by persson, Tue Jul 14 18:54:08 2009 UTC revision 2427 by persson, Sat Mar 2 07:03:04 2013 UTC
# Line 1  Line 1 
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  *
# Line 111  namespace LinuxSampler { Line 111  namespace LinuxSampler {
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() {
# Line 464  namespace LinuxSampler { Line 462  namespace LinuxSampler {
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) {
# Line 714  namespace LinuxSampler { Line 705  namespace LinuxSampler {
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    
# Line 734  namespace LinuxSampler { Line 726  namespace LinuxSampler {
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    
# Line 774  namespace LinuxSampler { Line 762  namespace LinuxSampler {
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) {
# Line 1466  namespace LinuxSampler { Line 1447  namespace LinuxSampler {
1447          }          }
1448          InTransaction = false;          InTransaction = false;
1449                    
1450          if(db == NULL) {          if (db == NULL) {
1451              DbInstrumentsMutex.Unlock();              DbInstrumentsMutex.Unlock();
1452              return;              return;
1453          }          }
# Line 1741  namespace LinuxSampler { Line 1722  namespace LinuxSampler {
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      }      }

Legend:
Removed from v.1944  
changed lines
  Added in v.2427

  ViewVC Help
Powered by ViewVC