UpdateSong.cxx 3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "Walk.hxx"
Max Kellermann's avatar
Max Kellermann committed
21
#include "UpdateIO.hxx"
22
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabaseLock.hxx"
24 25
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
26
#include "decoder/DecoderList.hxx"
27
#include "storage/FileInfo.hxx"
28
#include "Log.hxx"
29 30 31

#include <unistd.h>

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

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

		return;
	}

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

		return;
	}

61
	if (song == nullptr) {
62
		FormatDebug(update_domain, "reading %s/%s",
63
			    directory.GetPath(), name);
64

65 66
		auto new_song = Song::LoadFile(storage, name, directory);
		if (!new_song) {
67 68
			FormatDebug(update_domain,
				    "ignoring unrecognized file %s/%s",
69
				    directory.GetPath(), name);
70 71 72
			return;
		}

73 74
		{
			const ScopeDatabaseLock protect;
75
			directory.AddSong(std::move(new_song));
76
		}
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
		}

		modified = true;
	}
93 94 95 96
} catch (...) {
	FormatError(std::current_exception(),
		    "error reading file %s/%s",
		    directory.GetPath(), name);
97 98 99
}

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

107
	UpdateSongFile2(directory, name, suffix, info);
108 109
	return true;
}