Archive.cxx 5.29 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 "Walk.hxx"
21
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/DatabaseLock.hxx"
23 24
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
25
#include "storage/StorageInterface.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "storage/FileInfo.hxx"
28 29 30 31
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx"
32
#include "util/StringCompare.hxx"
33
#include "Log.hxx"
34

35
#include <string>
36
#include <exception>
37 38 39

#include <string.h>

40
static Directory *
41
LockFindChild(Directory &directory, const char *name) noexcept
42 43 44 45 46 47
{
	const ScopeDatabaseLock protect;
	return directory.FindChild(name);
}

static Directory *
48
LockMakeChild(Directory &directory, const char *name) noexcept
49 50 51 52 53 54
{
	const ScopeDatabaseLock protect;
	return directory.MakeChild(name);
}

static Song *
55
LockFindSong(Directory &directory, const char *name) noexcept
56 57 58 59 60
{
	const ScopeDatabaseLock protect;
	return directory.FindSong(name);
}

61
void
62
UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
63
			      const char *name) noexcept
64
{
65
	const char *tmp = strchr(name, '/');
66
	if (tmp) {
67
		const std::string child_name(name, tmp);
68
		//add dir is not there already
69 70
		Directory *subdir = LockMakeChild(directory,
						  child_name.c_str());
71
		subdir->device = DEVICE_INARCHIVE;
72

73
		//create directories first
74
		UpdateArchiveTree(archive, *subdir, tmp + 1);
75
	} else {
76
		if (StringIsEmpty(name)) {
77 78
			LogWarning(update_domain,
				   "archive returned directory only");
79 80 81 82
			return;
		}

		//add file
83
		Song *song = LockFindSong(directory, name);
84
		if (song == nullptr) {
85
			song = Song::LoadFromArchive(archive, name, directory);
86
			if (song != nullptr) {
87 88 89 90
				{
					const ScopeDatabaseLock protect;
					directory.AddSong(song);
				}
91 92

				modified = true;
93 94
				FormatDefault(update_domain, "added %s/%s",
					      directory.GetPath(), name);
95
			}
96 97 98 99 100 101 102
		} else {
			if (!song->UpdateFileInArchive(archive)) {
				FormatDebug(update_domain,
					    "deleting unrecognized file %s/%s",
					    directory.GetPath(), name);
				editor.LockDeleteSong(directory, song);
			}
103 104 105 106
		}
	}
}

107 108
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
109
	ArchiveFile &archive;
110 111 112
	Directory *directory;

 public:
113
	UpdateArchiveVisitor(UpdateWalk &_walk, ArchiveFile &_archive,
114
			     Directory *_directory) noexcept
115
		:walk(_walk), archive(_archive), directory(_directory) {}
116 117 118 119

	virtual void VisitArchiveEntry(const char *path_utf8) override {
		FormatDebug(update_domain,
			    "adding archive file: %s", path_utf8);
120
		walk.UpdateArchiveTree(archive, *directory, path_utf8);
121 122 123
	}
};

124 125 126 127 128 129 130 131
/**
 * Updates the file listing from an archive file.
 *
 * @param parent the parent directory the archive file resides in
 * @param name the UTF-8 encoded base name of the archive file
 * @param st stat() information on the archive file
 * @param plugin the archive plugin which fits this archive type
 */
132 133
void
UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
134
			      const StorageFileInfo &info,
135
			      const ArchivePlugin &plugin) noexcept
136
{
137
	Directory *directory = LockFindChild(parent, name);
138

139
	if (directory != nullptr && directory->mtime == info.mtime &&
140 141 142 143 144
	    !walk_discard)
		/* MPD has already scanned the archive, and it hasn't
		   changed since - don't consider updating it */
		return;

145 146 147 148 149
	const auto path_fs = storage.MapChildFS(parent.GetPath(), name);
	if (path_fs.IsNull())
		/* not a local file: skip, because the archive API
		   supports only local files */
		return;
150 151

	/* open archive */
152
	std::unique_ptr<ArchiveFile> file;
153 154
	try {
		file = archive_file_open(&plugin, path_fs);
155 156
	} catch (...) {
		LogError(std::current_exception());
157 158
		if (directory != nullptr)
			editor.LockDeleteDirectory(directory);
159 160 161
		return;
	}

162
	FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
163

164
	if (directory == nullptr) {
165 166
		FormatDebug(update_domain,
			    "creating archive directory: %s", name);
167 168

		const ScopeDatabaseLock protect;
169
		directory = parent.CreateChild(name);
170 171 172 173 174
		/* mark this directory as archive (we use device for
		   this) */
		directory->device = DEVICE_INARCHIVE;
	}

175
	directory->mtime = info.mtime;
176

177
	UpdateArchiveVisitor visitor(*this, *file, directory);
178
	file->Visit(visitor);
179 180 181
}

bool
182 183
UpdateWalk::UpdateArchiveFile(Directory &directory,
			      const char *name, const char *suffix,
184
			      const StorageFileInfo &info) noexcept
185
{
186
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
187
	if (plugin == nullptr)
188 189
		return false;

190
	UpdateArchiveFile(directory, name, info, *plugin);
191 192
	return true;
}