db/update/Editor: add locking method variants

This commit is contained in:
Max Kellermann 2014-01-31 22:17:49 +01:00
parent 04b4f53488
commit 26970579b8
5 changed files with 36 additions and 27 deletions

View File

@ -118,9 +118,7 @@ UpdateWalk::UpdateContainerFile(Directory &directory,
} }
if (tnum == 1) { if (tnum == 1) {
db_lock(); editor.LockDeleteDirectory(contdir);
editor.DeleteDirectory(contdir);
db_unlock();
return false; return false;
} else } else
return true; return true;

View File

@ -47,6 +47,14 @@ DatabaseEditor::DeleteSong(Directory &dir, Song *del)
db_lock(); db_lock();
} }
void
DatabaseEditor::LockDeleteSong(Directory &parent, Song *song)
{
db_lock();
DeleteSong(parent, song);
db_unlock();
}
/** /**
* Recursively remove all sub directories and songs from a directory, * Recursively remove all sub directories and songs from a directory,
* leaving an empty directory. * leaving an empty directory.
@ -77,6 +85,14 @@ DatabaseEditor::DeleteDirectory(Directory *directory)
directory->Delete(); directory->Delete();
} }
void
DatabaseEditor::LockDeleteDirectory(Directory *directory)
{
db_lock();
DeleteDirectory(directory);
db_unlock();
}
bool bool
DatabaseEditor::DeleteNameIn(Directory &parent, const char *name) DatabaseEditor::DeleteNameIn(Directory &parent, const char *name)
{ {

View File

@ -39,6 +39,11 @@ public:
*/ */
void DeleteSong(Directory &parent, Song *song); void DeleteSong(Directory &parent, Song *song);
/**
* DeleteSong() with automatic locking.
*/
void LockDeleteSong(Directory &parent, Song *song);
/** /**
* Recursively free a directory and all its contents. * Recursively free a directory and all its contents.
* *
@ -46,6 +51,11 @@ public:
*/ */
void DeleteDirectory(Directory *directory); void DeleteDirectory(Directory *directory);
/**
* DeleteDirectory() with automatic locking.
*/
void LockDeleteDirectory(Directory *directory);
/** /**
* Caller must NOT lock the #db_mutex. * Caller must NOT lock the #db_mutex.
* *

View File

@ -42,11 +42,8 @@ UpdateWalk::UpdateSongFile2(Directory &directory,
FormatError(update_domain, FormatError(update_domain,
"no read permissions on %s/%s", "no read permissions on %s/%s",
directory.GetPath(), name); directory.GetPath(), name);
if (song != nullptr) { if (song != nullptr)
db_lock(); editor.LockDeleteSong(directory, song);
editor.DeleteSong(directory, song);
db_unlock();
}
return; return;
} }
@ -54,11 +51,8 @@ UpdateWalk::UpdateSongFile2(Directory &directory,
if (!(song != nullptr && st->st_mtime == song->mtime && if (!(song != nullptr && st->st_mtime == song->mtime &&
!walk_discard) && !walk_discard) &&
UpdateContainerFile(directory, name, suffix, st)) { UpdateContainerFile(directory, name, suffix, st)) {
if (song != nullptr) { if (song != nullptr)
db_lock(); editor.LockDeleteSong(directory, song);
editor.DeleteSong(directory, song);
db_unlock();
}
return; return;
} }
@ -88,9 +82,7 @@ UpdateWalk::UpdateSongFile2(Directory &directory,
FormatDebug(update_domain, FormatDebug(update_domain,
"deleting unrecognized file %s/%s", "deleting unrecognized file %s/%s",
directory.GetPath(), name); directory.GetPath(), name);
db_lock(); editor.LockDeleteSong(directory, song);
editor.DeleteSong(directory, song);
db_unlock();
} }
modified = true; modified = true;

View File

@ -107,9 +107,7 @@ UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
if (directory_exists(*child)) if (directory_exists(*child))
continue; continue;
db_lock(); editor.LockDeleteDirectory(child);
editor.DeleteDirectory(child);
db_unlock();
modified = true; modified = true;
} }
@ -118,9 +116,7 @@ UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
directory_for_each_song_safe(song, ns, directory) { directory_for_each_song_safe(song, ns, directory) {
const auto path = map_song_fs(*song); const auto path = map_song_fs(*song);
if (path.IsNull() || !FileExists(path)) { if (path.IsNull() || !FileExists(path)) {
db_lock(); editor.LockDeleteSong(directory, song);
editor.DeleteSong(directory, song);
db_unlock();
modified = true; modified = true;
} }
@ -223,11 +219,8 @@ UpdateWalk::UpdateDirectoryChild(Directory &directory,
assert(&directory == subdir->parent); assert(&directory == subdir->parent);
if (!UpdateDirectory(*subdir, st)) { if (!UpdateDirectory(*subdir, st))
db_lock(); editor.LockDeleteDirectory(subdir);
editor.DeleteDirectory(subdir);
db_unlock();
}
} else { } else {
FormatDebug(update_domain, FormatDebug(update_domain,
"%s is not a directory, archive or music", name); "%s is not a directory, archive or music", name);