Archive.cxx 4.71 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
LockMakeChild(Directory &directory, const char *name) noexcept
42 43 44 45 46 47
{
	const ScopeDatabaseLock protect;
	return directory.MakeChild(name);
}

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

54
void
55
UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
56
			      const char *name) noexcept
57
{
58
	const char *tmp = strchr(name, '/');
59
	if (tmp) {
60
		const std::string child_name(name, tmp);
61
		//add dir is not there already
62 63
		Directory *subdir = LockMakeChild(directory,
						  child_name.c_str());
64
		subdir->device = DEVICE_INARCHIVE;
65

66
		//create directories first
67
		UpdateArchiveTree(archive, *subdir, tmp + 1);
68
	} else {
69
		if (StringIsEmpty(name)) {
70 71
			LogWarning(update_domain,
				   "archive returned directory only");
72 73 74 75
			return;
		}

		//add file
76
		Song *song = LockFindSong(directory, name);
77
		if (song == nullptr) {
78
			auto new_song = Song::LoadFromArchive(archive, name, directory);
79
			if (new_song) {
80 81
				{
					const ScopeDatabaseLock protect;
82
					directory.AddSong(std::move(new_song));
83
				}
84 85

				modified = true;
86 87
				FormatDefault(update_domain, "added %s/%s",
					      directory.GetPath(), name);
88
			}
89 90 91 92 93 94 95
		} else {
			if (!song->UpdateFileInArchive(archive)) {
				FormatDebug(update_domain,
					    "deleting unrecognized file %s/%s",
					    directory.GetPath(), name);
				editor.LockDeleteSong(directory, song);
			}
96 97 98 99
		}
	}
}

100 101
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
102
	ArchiveFile &archive;
103
	Directory &directory;
104 105

 public:
106
	UpdateArchiveVisitor(UpdateWalk &_walk, ArchiveFile &_archive,
107
			     Directory &_directory) noexcept
108
		:walk(_walk), archive(_archive), directory(_directory) {}
109

110
	void VisitArchiveEntry(const char *path_utf8) override {
111 112
		FormatDebug(update_domain,
			    "adding archive file: %s", path_utf8);
113
		walk.UpdateArchiveTree(archive, directory, path_utf8);
114 115 116
	}
};

117 118 119 120 121 122 123 124
/**
 * 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
 */
125 126
void
UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
127
			      const StorageFileInfo &info,
128
			      const ArchivePlugin &plugin) noexcept
129
{
130 131 132 133 134
	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;
135

136 137 138 139 140 141 142
	Directory *directory =
		LockMakeVirtualDirectoryIfModified(parent, name, info,
						   DEVICE_INARCHIVE);
	if (directory == nullptr)
		/* not modified */
		return;

143
	/* open archive */
144
	std::unique_ptr<ArchiveFile> file;
145 146
	try {
		file = archive_file_open(&plugin, path_fs);
147 148
	} catch (...) {
		LogError(std::current_exception());
149
		editor.LockDeleteDirectory(directory);
150 151 152
		return;
	}

153
	FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
154

155
	UpdateArchiveVisitor visitor(*this, *file, *directory);
156
	file->Visit(visitor);
157 158 159
}

bool
160 161
UpdateWalk::UpdateArchiveFile(Directory &directory,
			      const char *name, const char *suffix,
162
			      const StorageFileInfo &info) noexcept
163
{
164
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
165
	if (plugin == nullptr)
166 167
		return false;

168
	UpdateArchiveFile(directory, name, info, *plugin);
169 170
	return true;
}