summaryrefslogtreecommitdiffstats
path: root/mtp/MtpServer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mtp/MtpServer.cpp')
-rwxr-xr-xmtp/MtpServer.cpp29
1 files changed, 27 insertions, 2 deletions
diff --git a/mtp/MtpServer.cpp b/mtp/MtpServer.cpp
index f4af2b948..f99554b03 100755
--- a/mtp/MtpServer.cpp
+++ b/mtp/MtpServer.cpp
@@ -116,9 +116,13 @@ MtpServer::~MtpServer() {
}
void MtpServer::addStorage(MtpStorage* storage) {
+ android::Mutex::Autolock autoLock(mMutex);
MTPD("addStorage(): storage: %x\n", storage);
+ if (getStorage(storage->getStorageID()) != NULL) {
+ MTPE("MtpServer::addStorage Storage for storage ID %i already exists.\n", storage->getStorageID());
+ return;
+ }
mDatabase->createDB(storage, storage->getStorageID());
- android::Mutex::Autolock autoLock(mMutex);
mStorages.push(storage);
sendStoreAdded(storage->getStorageID());
}
@@ -128,11 +132,31 @@ void MtpServer::removeStorage(MtpStorage* storage) {
for (size_t i = 0; i < mStorages.size(); i++) {
if (mStorages[i] == storage) {
+ MTPD("MtpServer::removeStorage calling sendStoreRemoved\n");
+ // First lock the mutex so that the inotify thread and main
+ // thread do not do anything while we remove the storage
+ // item, and to make sure we don't remove the item while an
+ // operation is in progress
+ mDatabase->lockMutex();
+ // Grab the storage ID before we delete the item from the
+ // database
+ MtpStorageID storageID = storage->getStorageID();
+ // Remove the item from the mStorages from the vector. At
+ // this point the main thread will no longer be able to find
+ // this storage item anymore.
mStorages.removeAt(i);
- sendStoreRemoved(storage->getStorageID());
+ // Destroy the storage item, free up all the memory, kill
+ // the inotify thread.
+ mDatabase->destroyDB(storageID);
+ // Tell the host OS that the storage item is gone.
+ sendStoreRemoved(storageID);
+ // Unlock any remaining mutexes on other storage devices.
+ // If no storage devices exist anymore this will do nothing.
+ mDatabase->unlockMutex();
break;
}
}
+ MTPD("MtpServer::removeStorage DONE\n");
}
MtpStorage* MtpServer::getStorage(MtpStorageID id) {
@@ -275,6 +299,7 @@ void MtpServer::sendStoreAdded(MtpStorageID id) {
void MtpServer::sendStoreRemoved(MtpStorageID id) {
MTPD("sendStoreRemoved %08X\n", id);
sendEvent(MTP_EVENT_STORE_REMOVED, id);
+ MTPD("MtpServer::sendStoreRemoved done\n");
}
void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {