00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 storage.cc - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: storage.cc 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifdef HAVE_CONFIG_H 00033 #include "autoconfig.h" 00034 #endif 00035 00036 #include "config_manager.h" 00037 00038 #if ! defined(HAVE_MYSQL) && ! defined(HAVE_SQLITE3) 00039 #error "need at least one storage (mysql or sqlite3)" 00040 #endif 00041 00042 #include "storage/sqlite3/sqlite3_storage.h" 00043 #include "storage/mysql/mysql_storage.h" 00044 00045 #include "tools.h" 00046 00047 using namespace zmm; 00048 00049 SINGLETON_MUTEX(Storage, false); 00050 00051 Ref<Storage> Storage::getInstance() 00052 { 00053 if (! instance->singletonActive) 00054 throw _Exception(_("singleton is currently inactive!")); 00055 if(instance == nil) 00056 { 00057 AUTOLOCK(mutex); 00058 if (! instance->singletonActive) 00059 throw _Exception(_("singleton is currently inactive!")); 00060 if (instance == nil) 00061 { 00062 Ref<Storage> tmpInstance = createInstance(); 00063 tmpInstance->init(); 00064 tmpInstance->registerSingleton(); 00065 instance = tmpInstance; 00066 } 00067 } 00068 return instance; 00069 } 00070 00071 00072 Ref<Storage> Storage::createInstance() 00073 { 00074 String type; 00075 Ref<Storage> storage; 00076 00077 Ref<ConfigManager> config = ConfigManager::getInstance(); 00078 type = config->getOption(CFG_SERVER_STORAGE_DRIVER); 00079 00080 do 00081 { 00082 #ifdef HAVE_SQLITE3 00083 if (type == "sqlite3") 00084 { 00085 storage = Ref<Storage>(new Sqlite3Storage()); 00086 break; 00087 } 00088 #endif 00089 00090 #ifdef HAVE_MYSQL 00091 if (type == "mysql") 00092 { 00093 storage = Ref<Storage>(new MysqlStorage()); 00094 break; 00095 } 00096 #endif 00097 // other database types... 00098 throw _Exception(_("Unknown storage type: ") + type); 00099 } 00100 while (false); 00101 00102 return storage; 00103 } 00104 00105 void Storage::stripAndUnescapeVirtualContainerFromPath(String path, String &first, String &last) 00106 { 00107 if (path.charAt(0) != VIRTUAL_CONTAINER_SEPARATOR) 00108 { 00109 throw _Exception(_("got non-absolute virtual path; needs to start with: ") + VIRTUAL_CONTAINER_SEPARATOR); 00110 } 00111 int sep = path.rindex(VIRTUAL_CONTAINER_SEPARATOR); 00112 if (sep == 0) 00113 { 00114 first = _("/"); 00115 last = path.substring(1); 00116 } 00117 else 00118 { 00119 while (sep > 0) 00120 { 00121 char beforeSep = path.charAt(sep - 1); 00122 if (beforeSep != VIRTUAL_CONTAINER_ESCAPE) 00123 { 00124 break; 00125 } 00126 sep = path.rindex(sep - 1, VIRTUAL_CONTAINER_SEPARATOR); 00127 } 00128 if (sep == 0) 00129 { 00130 first = _("/"); 00131 last = unescape(path.substring(sep + 1), VIRTUAL_CONTAINER_ESCAPE); 00132 } 00133 else 00134 { 00135 first = path.substring(0, sep); 00136 last = unescape(path.substring(sep + 1), VIRTUAL_CONTAINER_ESCAPE); 00137 } 00138 } 00139 }
1.6.1