SongUpdate.cxx 4.42 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "config.h" /* must be first for large file support */
21
#include "DetachedSong.hxx"
22 23
#include "db/plugins/simple/Song.hxx"
#include "db/plugins/simple/Directory.hxx"
24 25
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriUtil.hxx"
27
#include "util/Error.hxx"
28
#include "fs/AllocatedPath.hxx"
29
#include "fs/Traits.hxx"
30
#include "fs/FileInfo.hxx"
31
#include "decoder/DecoderList.hxx"
32
#include "tag/Tag.hxx"
33
#include "tag/TagBuilder.hxx"
34
#include "tag/TagHandler.hxx"
35 36
#include "tag/TagId3.hxx"
#include "tag/ApeTag.hxx"
37
#include "TagFile.hxx"
38
#include "TagStream.hxx"
39

40 41 42 43
#ifdef ENABLE_ARCHIVE
#include "TagArchive.hxx"
#endif

44
#include <assert.h>
45
#include <string.h>
46 47
#include <sys/stat.h>

48 49
#ifdef ENABLE_DATABASE

50
Song *
51
Song::LoadFile(Storage &storage, const char *path_utf8, Directory &parent)
52
{
53
	assert(!uri_has_scheme(path_utf8));
54
	assert(strchr(path_utf8, '\n') == nullptr);
55

56
	Song *song = NewFile(path_utf8, parent);
57 58

	//in archive ?
59 60 61
	bool success =
#ifdef ENABLE_ARCHIVE
		parent.device == DEVICE_INARCHIVE
62
		? song->UpdateFileInArchive(storage)
63 64 65
		:
#endif
		song->UpdateFile(storage);
66
	if (!success) {
67
		song->Free();
68
		return nullptr;
69 70 71 72 73
	}

	return song;
}

74 75
#endif

76 77 78
/**
 * Attempts to load APE or ID3 tags from the specified file.
 */
79
static bool
80
tag_scan_fallback(Path path,
81
		  const struct tag_handler *handler, void *handler_ctx)
82
{
83 84
	return tag_ape_scan2(path, handler, handler_ctx) ||
		tag_id3_scan(path, handler, handler_ctx);
85 86
}

87 88
#ifdef ENABLE_DATABASE

89
bool
90
Song::UpdateFile(Storage &storage)
91
{
92
	const auto &relative_uri = GetURI();
93

94
	StorageFileInfo info;
95
	if (!storage.GetInfo(relative_uri.c_str(), true, info, IgnoreError()))
96
		return false;
97

98
	if (!info.IsRegular())
99 100
		return false;

101
	TagBuilder tag_builder;
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	const auto path_fs = storage.MapFS(relative_uri.c_str());
	if (path_fs.IsNull()) {
		const auto absolute_uri =
			storage.MapUTF8(relative_uri.c_str());
		if (!tag_stream_scan(absolute_uri.c_str(),
				     full_tag_handler, &tag_builder))
			return false;
	} else {
		if (!tag_file_scan(path_fs, full_tag_handler, &tag_builder))
			return false;

		if (tag_builder.IsEmpty())
			tag_scan_fallback(path_fs, &full_tag_handler,
					  &tag_builder);
	}
118

119
	mtime = info.mtime;
120
	tag_builder.Commit(tag);
121
	return true;
122 123
}

124 125 126 127
#endif

#ifdef ENABLE_ARCHIVE

128
bool
129
Song::UpdateFileInArchive(const Storage &storage)
130 131 132
{
	/* check if there's a suffix and a plugin */

133
	const char *suffix = uri_get_suffix(uri);
134
	if (suffix == nullptr)
135 136
		return false;

137
	if (!decoder_plugins_supports_suffix(suffix))
138 139
		return false;

140 141 142
	const auto path_fs = parent->IsRoot()
		? storage.MapFS(uri)
		: storage.MapChildFS(parent->GetPath(), uri);
143 144
	if (path_fs.IsNull())
		return false;
145

146
	TagBuilder tag_builder;
147
	if (!tag_archive_scan(path_fs, full_tag_handler, &tag_builder))
148
		return false;
149

150
	tag_builder.Commit(tag);
151 152
	return true;
}
153

154 155
#endif

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
bool
DetachedSong::LoadFile(Path path)
{
	FileInfo fi;
	if (!GetFileInfo(path, fi) || !fi.IsRegular())
		return false;

	TagBuilder tag_builder;
	if (!tag_file_scan(path, full_tag_handler, &tag_builder))
		return false;

	if (tag_builder.IsEmpty())
		tag_scan_fallback(path, &full_tag_handler,
				  &tag_builder);

	mtime = fi.GetModificationTime();
	tag_builder.Commit(tag);
	return true;
}

176 177 178 179 180
bool
DetachedSong::Update()
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
181
			AllocatedPath::FromUTF8(GetRealURI());
182 183
		if (path_fs.IsNull())
			return false;
184

185
		return LoadFile(path_fs);
186 187 188 189 190 191 192 193 194 195 196 197 198
	} else if (IsRemote()) {
		TagBuilder tag_builder;
		if (!tag_stream_scan(uri.c_str(), full_tag_handler,
				     &tag_builder))
			return false;

		mtime = 0;
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}