UpdateSong.cxx 2.92 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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) noexcept
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 && !walk_discard) &&
55
	    UpdateContainerFile(directory, name, suffix, info)) {
56 57
		if (song != nullptr)
			editor.LockDeleteSong(directory, song);
58 59 60 61

		return;
	}

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

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

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

		modified = true;
	}
}

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

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