SongUpdate.cxx 4.17 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "song/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 "fs/AllocatedPath.hxx"
28
#include "fs/FileInfo.hxx"
29
#include "tag/Builder.hxx"
30
#include "TagFile.hxx"
31
#include "TagStream.hxx"
32

33 34 35 36
#ifdef ENABLE_ARCHIVE
#include "TagArchive.hxx"
#endif

37
#include <exception>
38

39
#include <assert.h>
40
#include <string.h>
41

42 43
#ifdef ENABLE_DATABASE

44
Song *
45 46
Song::LoadFile(Storage &storage, const char *path_utf8,
	       Directory &parent) noexcept
47
{
48
	assert(!uri_has_scheme(path_utf8));
49
	assert(strchr(path_utf8, '\n') == nullptr);
50

51
	Song *song = NewFile(path_utf8, parent);
52
	if (!song->UpdateFile(storage)) {
53
		song->Free();
54
		return nullptr;
55 56 57 58 59
	}

	return song;
}

60 61 62 63
#endif

#ifdef ENABLE_DATABASE

64
bool
65
Song::UpdateFile(Storage &storage) noexcept
66
{
67
	const auto &relative_uri = GetURI();
68

69
	StorageFileInfo info;
70
	try {
71
		info = storage.GetInfo(relative_uri.c_str(), true);
72
	} catch (...) {
73
		return false;
74
	}
75

76
	if (!info.IsRegular())
77 78
		return false;

79
	TagBuilder tag_builder;
80
	auto new_audio_format = AudioFormat::Undefined();
81

82 83 84 85
	const auto path_fs = storage.MapFS(relative_uri.c_str());
	if (path_fs.IsNull()) {
		const auto absolute_uri =
			storage.MapUTF8(relative_uri.c_str());
86 87
		if (!tag_stream_scan(absolute_uri.c_str(), tag_builder,
				     &new_audio_format))
88 89
			return false;
	} else {
90 91
		if (!ScanFileTagsWithGeneric(path_fs, tag_builder,
					     &new_audio_format))
92 93
			return false;
	}
94

95
	mtime = info.mtime;
96
	audio_format = new_audio_format;
97
	tag_builder.Commit(tag);
98
	return true;
99 100
}

101 102
#ifdef ENABLE_ARCHIVE

103 104
Song *
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
105
		      Directory &parent) noexcept
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
	assert(!uri_has_scheme(name_utf8));
	assert(strchr(name_utf8, '\n') == nullptr);

	Song *song = NewFile(name_utf8, parent);

	if (!song->UpdateFileInArchive(archive)) {
		song->Free();
		return nullptr;
	}

	return song;
}

bool
121
Song::UpdateFileInArchive(ArchiveFile &archive) noexcept
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
	assert(parent != nullptr);
	assert(parent->device == DEVICE_INARCHIVE);

	std::string path_utf8(uri);

	for (const Directory *directory = parent;
	     directory->parent != nullptr &&
		     directory->parent->device == DEVICE_INARCHIVE;
	     directory = directory->parent) {
		path_utf8.insert(path_utf8.begin(), '/');
		path_utf8.insert(0, directory->GetName());
	}

	TagBuilder tag_builder;
	if (!tag_archive_scan(archive, path_utf8.c_str(), tag_builder))
		return false;

	tag_builder.Commit(tag);
	return true;
}

144 145
#endif

146 147
#endif /* ENABLE_DATABASE */

148
bool
149
DetachedSong::LoadFile(Path path) noexcept
150 151 152 153 154 155
{
	FileInfo fi;
	if (!GetFileInfo(path, fi) || !fi.IsRegular())
		return false;

	TagBuilder tag_builder;
156
	if (!ScanFileTagsWithGeneric(path, tag_builder))
157 158
		return false;

159
	mtime = fi.GetModificationTime();
160 161 162 163
	tag_builder.Commit(tag);
	return true;
}

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

173
		return LoadFile(path_fs);
174 175
	} else if (IsRemote()) {
		TagBuilder tag_builder;
176
		if (!tag_stream_scan(uri.c_str(), tag_builder))
177 178
			return false;

179
		mtime = std::chrono::system_clock::time_point::min();
180 181 182 183 184 185
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}