Container.cxx 3.4 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 24 25
#include "db/DatabaseLock.hxx"
#include "db/Directory.hxx"
#include "db/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
#include <sys/stat.h>

37 38
#include <glib.h>

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

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

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

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

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

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

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

	contdir->device = DEVICE_CONTAINER;
	db_unlock();

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

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

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

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

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

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

		modified = true;

121 122
		FormatDefault(update_domain, "added %s/%s",
			      directory.GetPath(), vtrack);
123 124 125 126
		g_free(vtrack);
	}

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