StorageCommands.cxx 6.88 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 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 "time/ChronoUtil.hxx"
27
#include "util/UriUtil.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 "Idle.hxx"
41

42 43
#include <memory>

44 45 46 47
#include <inttypes.h> /* for PRIu64 */

gcc_pure
static bool
48
skip_path(const char *name_utf8) noexcept
49 50 51 52
{
	return strchr(name_utf8, '\n') != nullptr;
}

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

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

68
		StorageFileInfo info;
69 70
		try {
			info = reader.GetInfo(false);
71
		} catch (...) {
72
			continue;
73
		}
74 75

		switch (info.type) {
76
		case StorageFileInfo::Type::OTHER:
77 78 79
			/* ignore */
			continue;

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

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

92
		if (!IsNegative(info.mtime))
93
			time_print(r, "Last-Modified", info.mtime);
94 95 96
	}
}

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

101
CommandResult
102
handle_listfiles_storage(Response &r, Storage &storage, const char *uri)
103
{
104 105
	std::unique_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(uri));
	handle_listfiles_storage(r, *reader);
106 107 108 109
	return CommandResult::OK;
}

CommandResult
110
handle_listfiles_storage(Client &client, Response &r, const char *uri)
111
{
112
	auto &event_loop = client.GetInstance().io_thread.GetEventLoop();
113
	std::unique_ptr<Storage> storage(CreateStorageURI(event_loop, uri));
114
	if (storage == nullptr) {
115
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
116 117 118
		return CommandResult::ERROR;
	}

119
	return handle_listfiles_storage(r, *storage, "");
120 121
}

122
static void
123
print_storage_uri(Client &client, Response &r, const Storage &storage)
124 125 126 127 128
{
	std::string uri = storage.MapUTF8("");
	if (uri.empty())
		return;

129
	if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		/* 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);
	}

145
	r.Format("storage: %s\n", uri.c_str());
146 147 148
}

CommandResult
149
handle_listmounts(Client &client, gcc_unused Request args, Response &r)
150
{
151
	Storage *_composite = client.GetInstance().storage;
152
	if (_composite == nullptr) {
153
		r.Error(ACK_ERROR_NO_EXIST, "No database");
154 155 156 157 158
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

159 160 161 162
	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);
163 164 165 166 167 168 169
	};

	composite.VisitMounts(visitor);

	return CommandResult::OK;
}

170
CommandResult
171
handle_mount(Client &client, Request args, Response &r)
172
{
173 174 175
	auto &instance = client.GetInstance();

	Storage *_composite = instance.storage;
176
	if (_composite == nullptr) {
177
		r.Error(ACK_ERROR_NO_EXIST, "No database");
178 179 180 181 182
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

183 184
	const char *const local_uri = args[0];
	const char *const remote_uri = args[1];
185 186

	if (*local_uri == 0) {
187
		r.Error(ACK_ERROR_ARG, "Bad mount point");
188 189 190
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
191 192 193 194 195 196
	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 */
197
		r.Error(ACK_ERROR_ARG, "Bad mount point");
Max Kellermann's avatar
Max Kellermann committed
198 199 200
		return CommandResult::ERROR;
	}

201
	auto &event_loop = instance.io_thread.GetEventLoop();
202
	auto storage = CreateStorageURI(event_loop, remote_uri);
203
	if (storage == nullptr) {
204
		r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
205 206 207
		return CommandResult::ERROR;
	}

208
	composite.Mount(local_uri, std::move(storage));
209
	instance.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
210 211

#ifdef ENABLE_DATABASE
212
	if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
213
		try {
214
			db->Mount(local_uri, remote_uri);
215
		} catch (...) {
Max Kellermann's avatar
Max Kellermann committed
216
			composite.Unmount(local_uri);
217
			throw;
Max Kellermann's avatar
Max Kellermann committed
218 219 220 221
		}

		// TODO: call Instance::OnDatabaseModified()?
		// TODO: trigger database update?
222
		instance.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
223 224 225
	}
#endif

226 227
	return CommandResult::OK;
}
228 229

CommandResult
230
handle_unmount(Client &client, Request args, Response &r)
231
{
232 233 234
	auto &instance = client.GetInstance();

	Storage *_composite = instance.storage;
235
	if (_composite == nullptr) {
236
		r.Error(ACK_ERROR_NO_EXIST, "No database");
237 238 239 240 241
		return CommandResult::ERROR;
	}

	CompositeStorage &composite = *(CompositeStorage *)_composite;

242
	const char *const local_uri = args.front();
243 244

	if (*local_uri == 0) {
245
		r.Error(ACK_ERROR_ARG, "Bad mount point");
246 247 248
		return CommandResult::ERROR;
	}

Max Kellermann's avatar
Max Kellermann committed
249
#ifdef ENABLE_DATABASE
250
	if (instance.update != nullptr)
Max Kellermann's avatar
Max Kellermann committed
251 252 253
		/* ensure that no database update will attempt to work
		   with the database/storage instances we're about to
		   destroy here */
254
		instance.update->CancelMount(local_uri);
Max Kellermann's avatar
Max Kellermann committed
255

256
	if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
257
		if (db->Unmount(local_uri))
Max Kellermann's avatar
Max Kellermann committed
258
			// TODO: call Instance::OnDatabaseModified()?
259
			instance.EmitIdle(IDLE_DATABASE);
Max Kellermann's avatar
Max Kellermann committed
260 261 262
	}
#endif

263
	if (!composite.Unmount(local_uri)) {
264
		r.Error(ACK_ERROR_ARG, "Not a mount point");
265 266 267
		return CommandResult::ERROR;
	}

268
	instance.EmitIdle(IDLE_MOUNT);
Max Kellermann's avatar
Max Kellermann committed
269

270 271
	return CommandResult::OK;
}