Container.cxx 2.64 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"
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

bool
34
UpdateWalk::UpdateContainerFile(Directory &directory,
35
				std::string_view name, const char *suffix,
36
				const StorageFileInfo &info) noexcept
37
{
38
	const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){
39
			return plugin.SupportsContainerSuffix(suffix);
40 41
		});
	if (_plugin == nullptr)
42
		return false;
43
	const DecoderPlugin &plugin = *_plugin;
44

45 46 47
	Directory *contdir;
	{
		const ScopeDatabaseLock protect;
48
		contdir = MakeVirtualDirectoryIfModified(directory, name,
49 50
							 info,
							 DEVICE_CONTAINER);
51 52 53 54
		if (contdir == nullptr)
			/* not modified */
			return true;
	}
55

56 57 58 59 60 61 62
	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;
	}
63

64
	try {
65
		auto v = plugin.container_scan(pathname);
66 67 68 69
		if (v.empty()) {
			editor.LockDeleteDirectory(contdir);
			return false;
		}
70

71
		for (auto &vtrack : v) {
72 73
			auto song = std::make_unique<Song>(std::move(vtrack),
							   *contdir);
74

75
			// shouldn't be necessary but it's there..
76
			song->mtime = info.mtime;
77

78
			FormatDefault(update_domain, "added %s/%s",
79 80
				      contdir->GetPath(),
				      song->filename.c_str());
81

82 83
			{
				const ScopeDatabaseLock protect;
84
				contdir->AddSong(std::move(song));
85
			}
86

87 88
			modified = true;
		}
89 90
	} catch (...) {
		LogError(std::current_exception());
91 92
		editor.LockDeleteDirectory(contdir);
		return false;
93 94
	}

95
	return true;
96
}