Container.cxx 3.46 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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"
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 "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 "tag/TagHandler.hxx"
32
#include "tag/TagBuilder.hxx"
33
#include "Log.hxx"
34

35 36 37 38
#include <sys/stat.h>

Directory *
UpdateWalk::MakeDirectoryIfModified(Directory &parent, const char *name,
39
				    const FileInfo &info)
40
{
41
	Directory *directory = parent.FindChild(name);
42 43

	// directory exists already
44
	if (directory != nullptr) {
Max Kellermann's avatar
Max Kellermann committed
45 46 47
		if (directory->IsMount())
			return nullptr;

48
		if (directory->mtime == info.mtime && !walk_discard) {
49
			/* not modified */
50
			return nullptr;
51 52
		}

53
		editor.DeleteDirectory(directory);
54 55 56
		modified = true;
	}

57
	directory = parent.MakeChild(name);
58
	directory->mtime = info.mtime;
59 60 61
	return directory;
}

62 63 64 65 66 67 68
static bool
SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix)
{
	return plugin.container_scan != nullptr &&
		plugin.SupportsSuffix(suffix);
}

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

	db_lock();
82
	Directory *contdir = MakeDirectoryIfModified(directory, name, info);
83
	if (contdir == nullptr) {
84 85 86 87 88 89 90 91
		/* not modified */
		db_unlock();
		return true;
	}

	contdir->device = DEVICE_CONTAINER;
	db_unlock();

92 93 94 95 96 97 98
	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;
	}
99 100 101

	char *vtrack;
	unsigned int tnum = 0;
102
	TagBuilder tag_builder;
103
	while ((vtrack = plugin.container_scan(pathname, ++tnum)) != nullptr) {
104
		Song *song = Song::NewFile(vtrack, *contdir);
105 106

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

109 110
		const auto child_path_fs = AllocatedPath::Build(pathname,
								vtrack);
111
		plugin.ScanFile(child_path_fs,
112
				add_tag_handler, &tag_builder);
113

114
		tag_builder.Commit(song->tag);
115 116

		db_lock();
117
		contdir->AddSong(song);
118 119 120 121
		db_unlock();

		modified = true;

122 123
		FormatDefault(update_domain, "added %s/%s",
			      directory.GetPath(), vtrack);
124
		delete[] vtrack;
125 126 127
	}

	if (tnum == 1) {
128
		editor.LockDeleteDirectory(contdir);
129 130 131 132
		return false;
	} else
		return true;
}