Archive.cxx 4.97 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 20
 * 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.
 */

#include "config.h" /* must be first for large file support */
21
#include "Walk.hxx"
22
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabaseLock.hxx"
24 25
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
26
#include "storage/StorageInterface.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "storage/FileInfo.hxx"
29 30 31 32
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx"
33
#include "util/Error.hxx"
34
#include "util/StringCompare.hxx"
35
#include "Log.hxx"
36

37
#include <string>
38

39
#include <sys/stat.h>
40 41
#include <string.h>

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static Directory *
LockFindChild(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.FindChild(name);
}

static Directory *
LockMakeChild(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.MakeChild(name);
}

static Song *
LockFindSong(Directory &directory, const char *name)
{
	const ScopeDatabaseLock protect;
	return directory.FindSong(name);
}

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

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

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

				modified = true;
94 95
				FormatDefault(update_domain, "added %s/%s",
					      directory.GetPath(), name);
96 97 98 99 100
			}
		}
	}
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
	Directory *directory;

 public:
	UpdateArchiveVisitor(UpdateWalk &_walk, Directory *_directory)
		:walk(_walk), directory(_directory) {}

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

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

131
	if (directory != nullptr && directory->mtime == info.mtime &&
132 133 134 135 136
	    !walk_discard)
		/* MPD has already scanned the archive, and it hasn't
		   changed since - don't consider updating it */
		return;

137 138 139 140 141
	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;
142 143

	/* open archive */
144
	Error error;
145
	ArchiveFile *file = archive_file_open(&plugin, path_fs, error);
146
	if (file == nullptr) {
147
		LogError(error);
148 149
		if (directory != nullptr)
			editor.LockDeleteDirectory(directory);
150 151 152
		return;
	}

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

155
	if (directory == nullptr) {
156 157
		FormatDebug(update_domain,
			    "creating archive directory: %s", name);
158 159

		const ScopeDatabaseLock protect;
160
		directory = parent.CreateChild(name);
161 162 163 164 165
		/* mark this directory as archive (we use device for
		   this) */
		directory->device = DEVICE_INARCHIVE;
	}

166
	directory->mtime = info.mtime;
167

168
	UpdateArchiveVisitor visitor(*this, directory);
169 170
	file->Visit(visitor);
	file->Close();
171 172 173
}

bool
174 175
UpdateWalk::UpdateArchiveFile(Directory &directory,
			      const char *name, const char *suffix,
176
			      const StorageFileInfo &info)
177
{
178
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
179
	if (plugin == nullptr)
180 181
		return false;

182
	UpdateArchiveFile(directory, name, info, *plugin);
183 184
	return true;
}