StorageCommands.cxx 7.54 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 21
#define __STDC_FORMAT_MACROS /* for PRIu64 */

22 23
#include "config.h"
#include "StorageCommands.hxx"
24
#include "Request.hxx"
25
#include "CommandError.hxx"
26
#include "util/UriUtil.hxx"
27
#include "util/Error.hxx"
28
#include "util/ConstBuffer.hxx"
29
#include "fs/Traits.hxx"
30
#include "client/Client.hxx"
31
#include "client/Response.hxx"
32 33 34 35
#include "Partition.hxx"
#include "Instance.hxx"
#include "storage/Registry.hxx"
#include "storage/CompositeStorage.hxx"
36
#include "storage/FileInfo.hxx"
Max Kellermann's avatar
Max Kellermann committed
37 38
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
#include "db/update/Service.hxx"
39
#include "TimePrint.hxx"
40
#include "IOThread.hxx"
41
#include "Idle.hxx"
42

43 44 45 46 47 48 49 50 51
#include <inttypes.h> /* for PRIu64 */

gcc_pure
static bool
skip_path(const char *name_utf8)
{
	return strchr(name_utf8, '\n') != nullptr;
}

52 53 54 55 56 57 58
#if defined(WIN32) && GCC_CHECK_VERSION(4,6)
/* PRIu64 causes bogus compiler warning */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif

59
static bool
60
handle_listfiles_storage(Response &r, StorageDirectoryReader &reader,
61 62 63 64 65 66 67
			 Error &error)
{
	const char *name_utf8;
	while ((name_utf8 = reader.Read()) != nullptr) {
		if (skip_path(name_utf8))
			continue;

68
		StorageFileInfo info;
69 70 71 72
		if (!reader.GetInfo(false, info, error))
			continue;

		switch (info.type) {
73
		case StorageFileInfo::Type::OTHER:
74 75 76
			/* ignore */
			continue;

77
		case StorageFileInfo::Type::REGULAR:
78 79 80 81
			r.Format("file: %s\n"
				 "size: %" PRIu64 "\n",
				 name_utf8,
				 info.size);
82 83
			break;

84
		case StorageFileInfo::Type::DIRECTORY:
85
			r.Format("directory: %s\n", name_utf8);
86 87 88 89
			break;
		}

		if (info.mtime != 0)
90
			time_print(r, "Last-Modified", info.mtime);
91 92 93 94 95
	}

	return true;
}

96 97 98 99
#if defined(WIN32) && GCC_CHECK_VERSION(4,6)
#pragma GCC diagnostic pop
#endif

100
static bool
101
handle_listfiles_storage(Response &r, Storage &storage, const char *uri,
102 103 104 105 106 107
			 Error &error)
{
	auto reader = storage.OpenDirectory(uri, error);
	if (reader == nullptr)
		return false;

108
	bool success = handle_listfiles_storage(r, *reader, error);
109 110 111 112 113
	delete reader;
	return success;
}

CommandResult
114
handle_listfiles_storage(Response &r, Storage &storage, const char *uri)
115 116
{
	Error error;
117 118
	if (!handle_listfiles_storage(r, storage, uri, error))
		return print_error(r, error);
119 120 121 122 123

	return CommandResult::OK;
}

CommandResult
124
handle_listfiles_storage(Response &r, const char *uri)
125 126
{
	Error error;
127
	Storage *storage = CreateStorageURI(io_thread_get(), uri, error);
128 129
	if (storage == nullptr) {
		if (error.IsDefined())
130
			return print_error(r, error);
131

132
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
133 134 135
		return CommandResult::ERROR;
	}

136
	bool success = handle_listfiles_storage(r, *storage, "", error);
137 138
	delete storage;
	if (!success)
139
		return print_error(r, error);
140 141 142 143

	return CommandResult::OK;
}

144
static void
145
print_storage_uri(Client &client, Response &r, const Storage &storage)
146 147 148 149 150
{
	std::string uri = storage.MapUTF8("");
	if (uri.empty())
		return;

151
	if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		/* storage points to local directory */

		if (!client.IsLocal())
			/* only "local" clients may see local paths
			   (same policy as with the "config"
			   command) */
			return;
	} else {
		/* hide username/passwords from client */

		std::string allocated = uri_remove_auth(uri.c_str());
		if (!allocated.empty())
			uri = std::move(allocated);
	}

167
	r.Format("storage: %s\n", uri.c_str());
168 169 170
}

CommandResult
171
handle_listmounts(Client &client, gcc_unused Request args, Response &r)
172 173 174
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
175
		r.Error(ACK_ERROR_NO_EXIST, "No database");
176 177 178 179 180
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

181 182 183 184
	const auto visitor = [&client, &r](const char *mount_uri,
					   const Storage &storage){
		r.Format("mount: %s\n", mount_uri);
		print_storage_uri(client, r, storage);
185 186 187 188 189 190 191
	};

	composite.VisitMounts(visitor);

	return CommandResult::OK;
}

192
CommandResult
193
handle_mount(Client &client, Request args, Response &r)
194 195 196
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
197
		r.Error(ACK_ERROR_NO_EXIST, "No database");
198 199 200 201 202
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

203 204
	const char *const local_uri = args[0];
	const char *const remote_uri = args[1];
205 206

	if (*local_uri == 0) {
207
		r.Error(ACK_ERROR_ARG, "Bad mount point");
208 209 210
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
211 212 213 214 215 216
	if (strchr(local_uri, '/') != nullptr) {
		/* allow only top-level mounts for now */
		/* TODO: eliminate this limitation after ensuring that
		   UpdateQueue::Erase() really gets called for every
		   unmount, and no Directory disappears recursively
		   during database update */
217
		r.Error(ACK_ERROR_ARG, "Bad mount point");
Max Kellermann's avatar
Max Kellermann committed
218 219 220
		return CommandResult::ERROR;
	}

221
	Error error;
222 223
	Storage *storage = CreateStorageURI(io_thread_get(), remote_uri,
					    error);
224 225
	if (storage == nullptr) {
		if (error.IsDefined())
226
			return print_error(r, error);
227

228
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
229 230 231 232
		return CommandResult::ERROR;
	}

	composite.Mount(local_uri, storage);
233
	client.partition.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
234 235 236 237 238 239

#ifdef ENABLE_DATABASE
	Database *_db = client.partition.instance.database;
	if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
		SimpleDatabase &db = *(SimpleDatabase *)_db;

240 241 242 243 244 245
		try {
			if (!db.Mount(local_uri, remote_uri, error)) {
				composite.Unmount(local_uri);
				return print_error(r, error);
			}
		} catch (...) {
Max Kellermann's avatar
Max Kellermann committed
246
			composite.Unmount(local_uri);
247
			throw;
Max Kellermann's avatar
Max Kellermann committed
248 249 250 251
		}

		// TODO: call Instance::OnDatabaseModified()?
		// TODO: trigger database update?
252
		client.partition.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
253 254 255
	}
#endif

256 257
	return CommandResult::OK;
}
258 259

CommandResult
260
handle_unmount(Client &client, Request args, Response &r)
261 262 263
{
	Storage *_composite = client.partition.instance.storage;
	if (_composite == nullptr) {
264
		r.Error(ACK_ERROR_NO_EXIST, "No database");
265 266 267 268 269
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

270
	const char *const local_uri = args.front();
271 272

	if (*local_uri == 0) {
273
		r.Error(ACK_ERROR_ARG, "Bad mount point");
274 275 276
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
277 278 279 280 281 282 283 284 285 286 287 288 289
#ifdef ENABLE_DATABASE
	if (client.partition.instance.update != nullptr)
		/* ensure that no database update will attempt to work
		   with the database/storage instances we're about to
		   destroy here */
		client.partition.instance.update->CancelMount(local_uri);

	Database *_db = client.partition.instance.database;
	if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
		SimpleDatabase &db = *(SimpleDatabase *)_db;

		if (db.Unmount(local_uri))
			// TODO: call Instance::OnDatabaseModified()?
290
			client.partition.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
291 292 293
	}
#endif

294
	if (!composite.Unmount(local_uri)) {
295
		r.Error(ACK_ERROR_ARG, "Not a mount point");
296 297 298
		return CommandResult::ERROR;
	}

299
	client.partition.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
300

301 302
	return CommandResult::OK;
}