UpdateSong.cxx 2.91 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 */
21
#include "Walk.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "UpdateIO.hxx"
23
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "db/DatabaseLock.hxx"
25 26
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
27
#include "decoder/DecoderList.hxx"
28
#include "storage/FileInfo.hxx"
29
#include "Log.hxx"
30 31 32

#include <unistd.h>

33 34 35
inline void
UpdateWalk::UpdateSongFile2(Directory &directory,
			    const char *name, const char *suffix,
36
			    const StorageFileInfo &info)
37
{
38 39 40 41 42
	Song *song;
	{
		const ScopeDatabaseLock protect;
		song = directory.FindSong(name);
	}
43

44
	if (!directory_child_access(storage, directory, name, R_OK)) {
45 46
		FormatError(update_domain,
			    "no read permissions on %s/%s",
47
			    directory.GetPath(), name);
48 49
		if (song != nullptr)
			editor.LockDeleteSong(directory, song);
50 51 52 53

		return;
	}

54
	if (!(song != nullptr && info.mtime == song->mtime &&
55
	      !walk_discard) &&
56
	    UpdateContainerFile(directory, name, suffix, info)) {
57 58
		if (song != nullptr)
			editor.LockDeleteSong(directory, song);
59 60 61 62

		return;
	}

63
	if (song == nullptr) {
64
		FormatDebug(update_domain, "reading %s/%s",
65
			    directory.GetPath(), name);
66
		song = Song::LoadFile(storage, name, directory);
67
		if (song == nullptr) {
68 69
			FormatDebug(update_domain,
				    "ignoring unrecognized file %s/%s",
70
				    directory.GetPath(), name);
71 72 73
			return;
		}

74 75 76 77
		{
			const ScopeDatabaseLock protect;
			directory.AddSong(song);
		}
78 79

		modified = true;
80 81
		FormatDefault(update_domain, "added %s/%s",
			      directory.GetPath(), name);
82
	} else if (info.mtime != song->mtime || walk_discard) {
83 84
		FormatDefault(update_domain, "updating %s/%s",
			      directory.GetPath(), name);
85
		if (!song->UpdateFile(storage)) {
86 87
			FormatDebug(update_domain,
				    "deleting unrecognized file %s/%s",
88
				    directory.GetPath(), name);
89
			editor.LockDeleteSong(directory, song);
90 91 92 93 94 95 96
		}

		modified = true;
	}
}

bool
97 98
UpdateWalk::UpdateSongFile(Directory &directory,
			   const char *name, const char *suffix,
99
			   const StorageFileInfo &info)
100
{
101
	if (!decoder_plugins_supports_suffix(suffix))
102 103
		return false;

104
	UpdateSongFile2(directory, name, suffix, info);
105 106
	return true;
}