Commit 4fdcc049 authored by Max Kellermann's avatar Max Kellermann

update_walk: move code to update_db.c

parent 0c4a2bea
......@@ -266,6 +266,7 @@ src_mpd_SOURCES = \
src/update.c \
src/update_queue.c \
src/update_io.c src/update_io.h \
src/update_db.c src/update_db.h \
src/update_walk.c \
src/update_remove.c \
src/client.c \
......
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h" /* must be first for large file support */
#include "update_db.h"
#include "update_internal.h"
#include "directory.h"
#include "song.h"
#include "db_lock.h"
#include <glib.h>
#include <assert.h>
void
delete_song(struct directory *dir, struct song *del)
{
assert(del->parent == dir);
/* first, prevent traversers in main task from getting this */
directory_remove_song(dir, del);
db_unlock(); /* temporary unlock, because update_remove_song() blocks */
/* now take it out of the playlist (in the main_task) */
update_remove_song(del);
/* finally, all possible references gone, free it */
song_free(del);
db_lock();
}
/**
* Recursively remove all sub directories and songs from a directory,
* leaving an empty directory.
*
* Caller must lock the #db_mutex.
*/
static void
clear_directory(struct directory *directory)
{
struct directory *child, *n;
directory_for_each_child_safe(child, n, directory)
delete_directory(child);
struct song *song, *ns;
directory_for_each_song_safe(song, ns, directory) {
assert(song->parent == directory);
delete_song(directory, song);
}
}
void
delete_directory(struct directory *directory)
{
assert(directory->parent != NULL);
clear_directory(directory);
directory_delete(directory);
}
bool
delete_name_in(struct directory *parent, const char *name)
{
bool modified = false;
db_lock();
struct directory *directory = directory_get_child(parent, name);
if (directory != NULL) {
delete_directory(directory);
modified = true;
}
struct song *song = directory_get_song(parent, name);
if (song != NULL) {
delete_song(parent, song);
modified = true;
}
db_unlock();
playlist_vector_remove(&parent->playlists, name);
return modified;
}
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_UPDATE_DB_H
#define MPD_UPDATE_DB_H
#include "check.h"
#include <stdbool.h>
struct directory;
struct song;
/**
* Caller must lock the #db_mutex.
*/
void
delete_song(struct directory *parent, struct song *song);
/**
* Recursively free a directory and all its contents.
*
* Caller must lock the #db_mutex.
*/
void
delete_directory(struct directory *directory);
/**
* Caller must NOT lock the #db_mutex.
*
* @return true if the database was modified
*/
bool
delete_name_in(struct directory *parent, const char *name);
#endif
......@@ -20,6 +20,7 @@
#include "config.h" /* must be first for large file support */
#include "update_internal.h"
#include "update_io.h"
#include "update_db.h"
#include "database.h"
#include "db_lock.h"
#include "exclude.h"
......@@ -93,88 +94,6 @@ directory_set_stat(struct directory *dir, const struct stat *st)
dir->have_stat = true;
}
/**
* Caller must lock the #db_mutex.
*/
static void
delete_song(struct directory *dir, struct song *del)
{
assert(del->parent == dir);
/* first, prevent traversers in main task from getting this */
directory_remove_song(dir, del);
db_unlock(); /* temporary unlock, because update_remove_song() blocks */
/* now take it out of the playlist (in the main_task) */
update_remove_song(del);
/* finally, all possible references gone, free it */
song_free(del);
db_lock();
}
static void
delete_directory(struct directory *directory);
/**
* Recursively remove all sub directories and songs from a directory,
* leaving an empty directory.
*
* Caller must lock the #db_mutex.
*/
static void
clear_directory(struct directory *directory)
{
struct directory *child, *n;
directory_for_each_child_safe(child, n, directory)
delete_directory(child);
struct song *song, *ns;
directory_for_each_song_safe(song, ns, directory) {
assert(song->parent == directory);
delete_song(directory, song);
}
}
/**
* Recursively free a directory and all its contents.
*
* Caller must lock the #db_mutex.
*/
static void
delete_directory(struct directory *directory)
{
assert(directory->parent != NULL);
clear_directory(directory);
directory_delete(directory);
}
static void
delete_name_in(struct directory *parent, const char *name)
{
db_lock();
struct directory *directory = directory_get_child(parent, name);
if (directory != NULL) {
delete_directory(directory);
modified = true;
}
struct song *song = directory_get_song(parent, name);
if (song != NULL) {
delete_song(parent, song);
modified = true;
}
db_unlock();
playlist_vector_remove(&parent->playlists, name);
}
static void
remove_excluded_from_directory(struct directory *directory,
GSList *exclude_list)
......@@ -717,7 +636,7 @@ updateDirectory(struct directory *directory, const struct stat *st)
continue;
if (skip_symlink(directory, utf8)) {
delete_name_in(directory, utf8);
modified |= delete_name_in(directory, utf8);
g_free(utf8);
continue;
}
......@@ -725,7 +644,7 @@ updateDirectory(struct directory *directory, const struct stat *st)
if (stat_directory_child(directory, utf8, &st2) == 0)
updateInDirectory(directory, utf8, &st2);
else
delete_name_in(directory, utf8);
modified |= delete_name_in(directory, utf8);
g_free(utf8);
}
......@@ -810,7 +729,7 @@ updatePath(const char *path)
if (stat_directory_child(parent, name, &st) == 0)
updateInDirectory(parent, name, &st);
else
delete_name_in(parent, name);
modified |= delete_name_in(parent, name);
g_free(name);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment