Bug 1084180 - Refine RemoveEntry - Don't remove only the entry but also its children if they exist. r=dhylands

This commit is contained in:
Alphan Chen 2014-10-22 02:43:00 -04:00
parent cbd95cfe18
commit 434dd2043d
2 changed files with 27 additions and 2 deletions

View File

@ -171,9 +171,29 @@ void
MozMtpDatabase::RemoveEntry(MtpObjectHandle aHandle)
{
MutexAutoLock lock(mMutex);
if (!IsValidHandle(aHandle)) {
return;
}
if (aHandle > 0 && aHandle < mDb.Length()) {
mDb[aHandle] = nullptr;
RefPtr<DbEntry> removedEntry = mDb[aHandle];
mDb[aHandle] = nullptr;
MTP_DBG("0x%08x removed", aHandle);
// if the entry is not a folder, just return.
if (removedEntry->mObjectFormat != MTP_FORMAT_ASSOCIATION) {
return;
}
// Find out and remove the children of aHandle.
// Since the index for a directory will always be less than the index of any of its children,
// we can remove the entire subtree in one pass.
ProtectedDbArray::size_type numEntries = mDb.Length();
ProtectedDbArray::index_type entryIndex;
for (entryIndex = aHandle+1; entryIndex < numEntries; entryIndex++) {
RefPtr<DbEntry> entry = mDb[entryIndex];
if (entry && IsValidHandle(entry->mParent) && !mDb[entry->mParent]) {
mDb[entryIndex] = nullptr;
MTP_DBG("0x%08x removed", aHandle);
}
}
}

View File

@ -226,6 +226,11 @@ private:
MatchParentFormat,
};
bool IsValidHandle(MtpObjectHandle aHandle)
{
return aHandle > 0 && aHandle < mDb.Length();
}
void AddEntry(DbEntry* aEntry);
void AddEntryAndNotify(DbEntry* aEntr, RefCountedMtpServer* aMtpServer);
void DumpEntries(const char* aLabel);