Container.cxx 3.3 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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"
21
#include "UpdateDomain.hxx"
22
#include "song/DetachedSong.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 "storage/StorageInterface.hxx"
27 28
#include "decoder/DecoderPlugin.hxx"
#include "decoder/DecoderList.hxx"
29
#include "fs/AllocatedPath.hxx"
30
#include "storage/FileInfo.hxx"
31
#include "Log.hxx"
32

33 34
Directory *
UpdateWalk::MakeDirectoryIfModified(Directory &parent, const char *name,
35
				    const StorageFileInfo &info) noexcept
36
{
37
	Directory *directory = parent.FindChild(name);
38 39

	// directory exists already
40
	if (directory != nullptr) {
Max Kellermann's avatar
Max Kellermann committed
41 42 43
		if (directory->IsMount())
			return nullptr;

44
		if (directory->mtime == info.mtime && !walk_discard) {
45
			/* not modified */
46
			return nullptr;
47 48
		}

49
		editor.DeleteDirectory(directory);
50 51 52
		modified = true;
	}

53
	directory = parent.MakeChild(name);
54
	directory->mtime = info.mtime;
55 56 57
	return directory;
}

58
static bool
59 60
SupportsContainerSuffix(const DecoderPlugin &plugin,
			const char *suffix) noexcept
61 62 63 64 65
{
	return plugin.container_scan != nullptr &&
		plugin.SupportsSuffix(suffix);
}

66
bool
67 68
UpdateWalk::UpdateContainerFile(Directory &directory,
				const char *name, const char *suffix,
69
				const StorageFileInfo &info) noexcept
70
{
71 72 73 74
	const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){
			return SupportsContainerSuffix(plugin, suffix);
		});
	if (_plugin == nullptr)
75
		return false;
76
	const DecoderPlugin &plugin = *_plugin;
77

78 79 80 81 82 83 84
	Directory *contdir;
	{
		const ScopeDatabaseLock protect;
		contdir = MakeDirectoryIfModified(directory, name, info);
		if (contdir == nullptr)
			/* not modified */
			return true;
85

86 87
		contdir->device = DEVICE_CONTAINER;
	}
88

89 90 91 92 93 94 95
	const auto pathname = storage.MapFS(contdir->GetPath());
	if (pathname.IsNull()) {
		/* not a local file: skip, because the container API
		   supports only local files */
		editor.LockDeleteDirectory(contdir);
		return false;
	}
96

97
	try {
98
		auto v = plugin.container_scan(pathname);
99 100 101 102
		if (v.empty()) {
			editor.LockDeleteDirectory(contdir);
			return false;
		}
103

104 105 106
		for (auto &vtrack : v) {
			Song *song = Song::NewFrom(std::move(vtrack),
						   *contdir);
107

108
			// shouldn't be necessary but it's there..
109
			song->mtime = info.mtime;
110

111 112
			FormatDefault(update_domain, "added %s/%s",
				      contdir->GetPath(), song->uri);
113

114 115 116 117
			{
				const ScopeDatabaseLock protect;
				contdir->AddSong(song);
			}
118

119 120
			modified = true;
		}
121 122
	} catch (...) {
		LogError(std::current_exception());
123 124
		editor.LockDeleteDirectory(contdir);
		return false;
125 126
	}

127
	return true;
128
}