UpdateSong.cxx 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 */
Max Kellermann's avatar
Max Kellermann committed
21 22 23 24 25
#include "UpdateSong.hxx"
#include "UpdateInternal.hxx"
#include "UpdateIO.hxx"
#include "UpdateDatabase.hxx"
#include "UpdateContainer.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "DatabaseLock.hxx"
27
#include "Directory.hxx"
28 29
#include "song.h"
#include "decoder_plugin.h"
30
#include "DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
31

32 33 34 35 36
#include <glib.h>

#include <unistd.h>

static void
37
update_song_file2(Directory *directory,
38 39 40 41
		  const char *name, const struct stat *st,
		  const struct decoder_plugin *plugin)
{
	db_lock();
42
	struct song *song = directory->FindSong(name);
43 44 45 46
	db_unlock();

	if (!directory_child_access(directory, name, R_OK)) {
		g_warning("no read permissions on %s/%s",
47
			  directory->GetPath(), name);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		if (song != NULL) {
			db_lock();
			delete_song(directory, song);
			db_unlock();
		}

		return;
	}

	if (!(song != NULL && st->st_mtime == song->mtime &&
	      !walk_discard) &&
	    update_container_file(directory, name, st, plugin)) {
		if (song != NULL) {
			db_lock();
			delete_song(directory, song);
			db_unlock();
		}

		return;
	}

	if (song == NULL) {
70
		g_debug("reading %s/%s", directory->GetPath(), name);
71 72 73
		song = song_file_load(name, directory);
		if (song == NULL) {
			g_debug("ignoring unrecognized file %s/%s",
74
				directory->GetPath(), name);
75 76 77 78
			return;
		}

		db_lock();
79
		directory->AddSong(song);
80 81 82 83
		db_unlock();

		modified = true;
		g_message("added %s/%s",
84
			  directory->GetPath(), name);
85 86
	} else if (st->st_mtime != song->mtime || walk_discard) {
		g_message("updating %s/%s",
87
			  directory->GetPath(), name);
88 89
		if (!song_file_update(song)) {
			g_debug("deleting unrecognized file %s/%s",
90
				directory->GetPath(), name);
91 92 93 94 95 96 97 98 99 100
			db_lock();
			delete_song(directory, song);
			db_unlock();
		}

		modified = true;
	}
}

bool
101
update_song_file(Directory *directory,
102 103 104 105
		 const char *name, const char *suffix,
		 const struct stat *st)
{
	const struct decoder_plugin *plugin =
Max Kellermann's avatar
Max Kellermann committed
106
		decoder_plugin_from_suffix(suffix, nullptr);
107 108 109 110 111 112
	if (plugin == NULL)
		return false;

	update_song_file2(directory, name, st, plugin);
	return true;
}