SongUpdate.cxx 3.94 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "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 38
#include <stdexcept>

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

42 43
#ifdef ENABLE_DATABASE

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

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

	return song;
}

59 60 61 62
#endif

#ifdef ENABLE_DATABASE

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

68
	StorageFileInfo info;
69
	try {
70
		info = storage.GetInfo(relative_uri.c_str(), true);
71
	} catch (const std::runtime_error &) {
72
		return false;
73
	}
74

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

78
	TagBuilder tag_builder;
79

80 81 82 83
	const auto path_fs = storage.MapFS(relative_uri.c_str());
	if (path_fs.IsNull()) {
		const auto absolute_uri =
			storage.MapUTF8(relative_uri.c_str());
84
		if (!tag_stream_scan(absolute_uri.c_str(), tag_builder))
85 86
			return false;
	} else {
87
		if (!tag_file_scan(path_fs, tag_builder))
88 89
			return false;
	}
90

91
	mtime = info.mtime;
92
	tag_builder.Commit(tag);
93
	return true;
94 95
}

96 97 98 99
#endif

#ifdef ENABLE_ARCHIVE

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
Song *
Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8,
		      Directory &parent)
{
	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
Song::UpdateFileInArchive(ArchiveFile &archive)
{
	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;
}

141 142
#endif

143 144 145 146 147 148 149 150
bool
DetachedSong::LoadFile(Path path)
{
	FileInfo fi;
	if (!GetFileInfo(path, fi) || !fi.IsRegular())
		return false;

	TagBuilder tag_builder;
151
	if (!tag_file_scan(path, tag_builder))
152 153
		return false;

154
	mtime = fi.GetModificationTime();
155 156 157 158
	tag_builder.Commit(tag);
	return true;
}

159 160 161 162 163
bool
DetachedSong::Update()
{
	if (IsAbsoluteFile()) {
		const AllocatedPath path_fs =
164
			AllocatedPath::FromUTF8(GetRealURI());
165 166
		if (path_fs.IsNull())
			return false;
167

168
		return LoadFile(path_fs);
169 170
	} else if (IsRemote()) {
		TagBuilder tag_builder;
171
		if (!tag_stream_scan(uri.c_str(), tag_builder))
172 173
			return false;

174
		mtime = std::chrono::system_clock::time_point::min();
175 176 177 178 179 180
		tag_builder.Commit(tag);
		return true;
	} else
		// TODO: implement
		return false;
}