SongUpdate.cxx 4.11 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
#include "tag/Generic.hxx"
36
#include "TagFile.hxx"
37
#include "TagStream.hxx"
38

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

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

47 48
#ifdef ENABLE_DATABASE

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

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

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

	return song;
}

73 74 75 76
#endif

#ifdef ENABLE_DATABASE

77
bool
78
Song::UpdateFile(Storage &storage)
79
{
80
	const auto &relative_uri = GetURI();
81

82
	StorageFileInfo info;
83
	if (!storage.GetInfo(relative_uri.c_str(), true, info, IgnoreError()))
84
		return false;
85

86
	if (!info.IsRegular())
87 88
		return false;

89
	TagBuilder tag_builder;
90

91 92 93 94 95 96 97 98 99 100 101 102
	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())
103 104
			ScanGenericTags(path_fs, full_tag_handler,
					&tag_builder);
105
	}
106

107
	mtime = info.mtime;
108
	tag_builder.Commit(tag);
109
	return true;
110 111
}

112 113 114 115
#endif

#ifdef ENABLE_ARCHIVE

116
bool
117
Song::UpdateFileInArchive(const Storage &storage)
118 119 120
{
	/* check if there's a suffix and a plugin */

121
	const char *suffix = uri_get_suffix(uri);
122
	if (suffix == nullptr)
123 124
		return false;

125
	if (!decoder_plugins_supports_suffix(suffix))
126 127
		return false;

128 129 130
	const auto path_fs = parent->IsRoot()
		? storage.MapFS(uri)
		: storage.MapChildFS(parent->GetPath(), uri);
131 132
	if (path_fs.IsNull())
		return false;
133

134
	TagBuilder tag_builder;
135
	if (!tag_archive_scan(path_fs, full_tag_handler, &tag_builder))
136
		return false;
137

138
	tag_builder.Commit(tag);
139 140
	return true;
}
141

142 143
#endif

144 145 146 147 148 149 150 151 152 153 154 155
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())
156
		ScanGenericTags(path, full_tag_handler, &tag_builder);
157 158 159 160 161 162

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

163 164 165 166 167
bool
DetachedSong::Update()
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
168
			AllocatedPath::FromUTF8(GetRealURI());
169 170
		if (path_fs.IsNull())
			return false;
171

172
		return LoadFile(path_fs);
173 174 175 176 177 178 179 180 181 182 183 184 185
	} 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;
}