2014-02-09 08:24:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2014-02-09 08:24:16 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
#define __STDC_FORMAT_MACROS /* for PRIu64 */
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "StorageCommands.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "CommandError.hxx"
|
2014-02-12 21:46:32 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2017-02-11 22:47:05 +01:00
|
|
|
#include "util/ChronoUtil.hxx"
|
2014-12-06 00:08:08 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2014-02-12 21:46:32 +01:00
|
|
|
#include "fs/Traits.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "Partition.hxx"
|
|
|
|
#include "Instance.hxx"
|
|
|
|
#include "storage/Registry.hxx"
|
|
|
|
#include "storage/CompositeStorage.hxx"
|
2014-02-28 19:02:23 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2014-02-26 08:39:44 +01:00
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
|
|
|
#include "db/update/Service.hxx"
|
2014-02-28 19:02:23 +01:00
|
|
|
#include "TimePrint.hxx"
|
2014-02-12 21:46:16 +01:00
|
|
|
#include "Idle.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
|
2016-10-27 08:52:13 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
#include <inttypes.h> /* for PRIu64 */
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
skip_path(const char *name_utf8) noexcept
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
return strchr(name_utf8, '\n') != nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
|
2014-03-14 08:58:43 +01:00
|
|
|
/* PRIu64 causes bogus compiler warning */
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat"
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-extra-args"
|
|
|
|
#endif
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
static void
|
|
|
|
handle_listfiles_storage(Response &r, StorageDirectoryReader &reader)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
const char *name_utf8;
|
|
|
|
while ((name_utf8 = reader.Read()) != nullptr) {
|
|
|
|
if (skip_path(name_utf8))
|
|
|
|
continue;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2016-10-27 08:40:40 +02:00
|
|
|
try {
|
|
|
|
info = reader.GetInfo(false);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2014-02-28 19:02:23 +01:00
|
|
|
continue;
|
2016-10-27 08:40:40 +02:00
|
|
|
}
|
2014-02-28 19:02:23 +01:00
|
|
|
|
|
|
|
switch (info.type) {
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::OTHER:
|
2014-02-28 19:02:23 +01:00
|
|
|
/* ignore */
|
|
|
|
continue;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::REGULAR:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("file: %s\n"
|
|
|
|
"size: %" PRIu64 "\n",
|
|
|
|
name_utf8,
|
|
|
|
info.size);
|
2014-02-28 19:02:23 +01:00
|
|
|
break;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::DIRECTORY:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("directory: %s\n", name_utf8);
|
2014-02-28 19:02:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-11 22:47:05 +01:00
|
|
|
if (!IsNegative(info.mtime))
|
2015-08-06 22:10:25 +02:00
|
|
|
time_print(r, "Last-Modified", info.mtime);
|
2014-02-28 19:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
|
2014-03-14 08:58:43 +01:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
handle_listfiles_storage(Response &r, Storage &storage, const char *uri)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
2016-10-27 08:40:40 +02:00
|
|
|
std::unique_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(uri));
|
|
|
|
handle_listfiles_storage(r, *reader);
|
2014-02-28 19:02:23 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
2017-02-10 21:46:07 +01:00
|
|
|
handle_listfiles_storage(Client &client, Response &r, const char *uri)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
2017-02-25 10:00:05 +01:00
|
|
|
auto &event_loop = client.GetInstance().io_thread.GetEventLoop();
|
2017-02-10 21:46:07 +01:00
|
|
|
std::unique_ptr<Storage> storage(CreateStorageURI(event_loop, uri));
|
2014-02-28 19:02:23 +01:00
|
|
|
if (storage == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
|
2014-02-28 19:02:23 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
return handle_listfiles_storage(r, *storage, "");
|
2014-02-28 19:02:23 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:46:32 +01:00
|
|
|
static void
|
2015-08-06 22:10:25 +02:00
|
|
|
print_storage_uri(Client &client, Response &r, const Storage &storage)
|
2014-02-12 21:46:32 +01:00
|
|
|
{
|
|
|
|
std::string uri = storage.MapUTF8("");
|
|
|
|
if (uri.empty())
|
|
|
|
return;
|
|
|
|
|
2015-03-02 21:22:18 +01:00
|
|
|
if (PathTraitsUTF8::IsAbsolute(uri.c_str())) {
|
2014-02-12 21:46:32 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("storage: %s\n", uri.c_str());
|
2014-02-12 21:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listmounts(Client &client, gcc_unused Request args, Response &r)
|
2014-02-12 21:46:32 +01:00
|
|
|
{
|
2017-02-25 10:00:05 +01:00
|
|
|
Storage *_composite = client.GetInstance().storage;
|
2014-02-12 21:46:32 +01:00
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-12 21:46:32 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
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);
|
2014-02-12 21:46:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
composite.VisitMounts(visitor);
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_mount(Client &client, Request args, Response &r)
|
2014-02-09 08:24:16 +01:00
|
|
|
{
|
2017-02-25 10:00:05 +01:00
|
|
|
auto &instance = client.GetInstance();
|
|
|
|
|
|
|
|
Storage *_composite = instance.storage;
|
2014-02-09 08:24:16 +01:00
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const local_uri = args[0];
|
|
|
|
const char *const remote_uri = args[1];
|
2014-02-09 08:24:16 +01:00
|
|
|
|
|
|
|
if (*local_uri == 0) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
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 */
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-26 08:39:44 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:00:05 +01:00
|
|
|
auto &event_loop = instance.io_thread.GetEventLoop();
|
2018-01-02 16:11:17 +01:00
|
|
|
auto storage = CreateStorageURI(event_loop, remote_uri);
|
2014-02-09 08:24:16 +01:00
|
|
|
if (storage == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2018-01-02 16:18:34 +01:00
|
|
|
composite.Mount(local_uri, std::move(storage));
|
2017-02-25 10:16:51 +01:00
|
|
|
instance.EmitIdle(IDLE_MOUNT);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
2019-02-20 20:48:20 +01:00
|
|
|
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
|
2016-02-28 11:19:28 +01:00
|
|
|
try {
|
2018-11-19 19:19:20 +01:00
|
|
|
db->Mount(local_uri, remote_uri);
|
2016-02-28 11:19:28 +01:00
|
|
|
} catch (...) {
|
2014-02-26 08:39:44 +01:00
|
|
|
composite.Unmount(local_uri);
|
2016-02-28 11:19:28 +01:00
|
|
|
throw;
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: call Instance::OnDatabaseModified()?
|
|
|
|
// TODO: trigger database update?
|
2017-02-25 10:16:51 +01:00
|
|
|
instance.EmitIdle(IDLE_DATABASE);
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-09 08:24:16 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
2014-02-12 21:45:04 +01:00
|
|
|
|
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_unmount(Client &client, Request args, Response &r)
|
2014-02-12 21:45:04 +01:00
|
|
|
{
|
2017-02-25 10:00:05 +01:00
|
|
|
auto &instance = client.GetInstance();
|
|
|
|
|
|
|
|
Storage *_composite = instance.storage;
|
2014-02-12 21:45:04 +01:00
|
|
|
if (_composite == nullptr) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeStorage &composite = *(CompositeStorage *)_composite;
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const local_uri = args.front();
|
2014-02-12 21:45:04 +01:00
|
|
|
|
|
|
|
if (*local_uri == 0) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad mount point");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2017-02-25 10:00:05 +01:00
|
|
|
if (instance.update != nullptr)
|
2014-02-26 08:39:44 +01:00
|
|
|
/* ensure that no database update will attempt to work
|
|
|
|
with the database/storage instances we're about to
|
|
|
|
destroy here */
|
2017-02-25 10:00:05 +01:00
|
|
|
instance.update->CancelMount(local_uri);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2019-02-20 20:48:20 +01:00
|
|
|
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
|
2018-11-19 19:19:20 +01:00
|
|
|
if (db->Unmount(local_uri))
|
2014-02-26 08:39:44 +01:00
|
|
|
// TODO: call Instance::OnDatabaseModified()?
|
2017-02-25 10:16:51 +01:00
|
|
|
instance.EmitIdle(IDLE_DATABASE);
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-12 21:45:04 +01:00
|
|
|
if (!composite.Unmount(local_uri)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Not a mount point");
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:16:51 +01:00
|
|
|
instance.EmitIdle(IDLE_MOUNT);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2014-02-12 21:45:04 +01:00
|
|
|
return CommandResult::OK;
|
|
|
|
}
|