From 4fdcc0496fbfce7b1ad4f46b249e4c9ee0b0e997 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sun, 12 Feb 2012 17:06:34 +0100
Subject: update_walk: move code to update_db.c

---
 Makefile.am       |   1 +
 src/update_db.c   | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/update_db.h   |  52 +++++++++++++++++++++++++++
 src/update_walk.c |  89 +++-------------------------------------------
 4 files changed, 160 insertions(+), 85 deletions(-)
 create mode 100644 src/update_db.c
 create mode 100644 src/update_db.h

diff --git a/Makefile.am b/Makefile.am
index 21df3e554..7602fc674 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/src/update_db.c b/src/update_db.c
new file mode 100644
index 000000000..f2126c559
--- /dev/null
+++ b/src/update_db.c
@@ -0,0 +1,103 @@
+/*
+ * 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;
+}
diff --git a/src/update_db.h b/src/update_db.h
new file mode 100644
index 000000000..0a9e46b05
--- /dev/null
+++ b/src/update_db.h
@@ -0,0 +1,52 @@
+/*
+ * 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
diff --git a/src/update_walk.c b/src/update_walk.c
index cc0749e6d..e2f7d3315 100644
--- a/src/update_walk.c
+++ b/src/update_walk.c
@@ -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);
 }
-- 
cgit v1.2.3