2017-11-25 11:20:32 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2017-11-25 11:20:32 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save and load mounts of the compound storage to/from the state file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StorageState.hxx"
|
|
|
|
#include "fs/io/TextFile.hxx"
|
|
|
|
#include "fs/io/BufferedOutputStream.hxx"
|
|
|
|
#include "storage/Registry.hxx"
|
|
|
|
#include "storage/CompositeStorage.hxx"
|
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
|
|
|
#include "util/StringCompare.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Instance.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
|
2020-05-28 13:28:09 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
/* ignore -Wcomma due to strange code in boost/array.hpp (in Boost
|
|
|
|
1.72) */
|
|
|
|
#pragma GCC diagnostic ignored "-Wcomma"
|
|
|
|
#endif
|
|
|
|
|
2017-11-25 11:20:32 +01:00
|
|
|
#include <boost/crc.hpp>
|
|
|
|
|
2020-05-28 13:28:09 +02:00
|
|
|
#include <set>
|
|
|
|
|
2017-11-25 11:20:32 +01:00
|
|
|
#define MOUNT_STATE_BEGIN "mount_begin"
|
|
|
|
#define MOUNT_STATE_END "mount_end"
|
|
|
|
#define MOUNT_STATE_STORAGE_URI "uri: "
|
|
|
|
#define MOUNT_STATE_MOUNTED_URL "mounted_url: "
|
|
|
|
|
|
|
|
static constexpr Domain storage_domain("storage");
|
|
|
|
|
|
|
|
void
|
|
|
|
storage_state_save(BufferedOutputStream &os, const Instance &instance)
|
|
|
|
{
|
2018-01-02 14:13:26 +01:00
|
|
|
if (instance.storage == nullptr)
|
|
|
|
return;
|
|
|
|
|
2017-11-25 11:20:32 +01:00
|
|
|
const auto visitor = [&os](const char *mount_uri, const Storage &storage) {
|
|
|
|
std::string uri = storage.MapUTF8("");
|
|
|
|
if (uri.empty() || StringIsEmpty(mount_uri))
|
|
|
|
return;
|
|
|
|
|
|
|
|
os.Format(
|
|
|
|
MOUNT_STATE_BEGIN "\n"
|
|
|
|
MOUNT_STATE_STORAGE_URI "%s\n"
|
|
|
|
MOUNT_STATE_MOUNTED_URL "%s\n"
|
|
|
|
MOUNT_STATE_END "\n", mount_uri, uri.c_str());
|
|
|
|
};
|
|
|
|
|
|
|
|
((CompositeStorage*)instance.storage)->VisitMounts(visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
storage_state_restore(const char *line, TextFile &file, Instance &instance)
|
|
|
|
{
|
|
|
|
if (!StringStartsWith(line, MOUNT_STATE_BEGIN))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string url;
|
|
|
|
std::string uri;
|
|
|
|
const char* value;
|
|
|
|
|
|
|
|
while ((line = file.ReadLine()) != nullptr) {
|
|
|
|
if (StringStartsWith(line, MOUNT_STATE_END))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((value = StringAfterPrefix(line, MOUNT_STATE_MOUNTED_URL)))
|
|
|
|
url = value;
|
|
|
|
else if ((value = StringAfterPrefix(line, MOUNT_STATE_STORAGE_URI)))
|
|
|
|
uri = value;
|
|
|
|
else
|
|
|
|
FormatError(storage_domain, "Unrecognized line in mountpoint state: %s", line);
|
|
|
|
}
|
|
|
|
|
2018-01-02 14:13:26 +01:00
|
|
|
if (instance.storage == nullptr)
|
|
|
|
/* without storage (a CompositeStorage instance), we
|
|
|
|
cannot mount, and therefore we silently ignore the
|
|
|
|
state file */
|
|
|
|
return true;
|
|
|
|
|
2017-11-25 11:20:32 +01:00
|
|
|
if (url.empty() || uri.empty()) {
|
|
|
|
LogError(storage_domain, "Missing value in mountpoint state.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatDebug(storage_domain, "Restoring mount %s => %s", uri.c_str(), url.c_str());
|
|
|
|
|
2020-07-06 17:45:59 +02:00
|
|
|
auto &composite_storage = *(CompositeStorage *)instance.storage;
|
|
|
|
if (composite_storage.IsMountPoint(uri.c_str())) {
|
|
|
|
LogError(storage_domain, "Mount point busy");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-06 18:00:57 +02:00
|
|
|
if (composite_storage.IsMounted(url.c_str())) {
|
|
|
|
LogError(storage_domain, "This storage is already mounted");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:48:14 +01:00
|
|
|
auto &event_loop = instance.io_thread.GetEventLoop();
|
2018-01-02 16:11:17 +01:00
|
|
|
auto storage = CreateStorageURI(event_loop, url.c_str());
|
2017-11-25 11:20:32 +01:00
|
|
|
if (storage == nullptr) {
|
|
|
|
FormatError(storage_domain, "Unrecognized storage URI: %s", url.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:48:20 +01:00
|
|
|
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
|
2017-11-25 11:20:32 +01:00
|
|
|
try {
|
2018-11-19 19:19:20 +01:00
|
|
|
db->Mount(uri.c_str(), url.c_str());
|
2017-11-25 11:20:32 +01:00
|
|
|
} catch (...) {
|
2018-01-02 14:07:23 +01:00
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to restore mount to %s",
|
|
|
|
url.c_str());
|
|
|
|
return true;
|
2017-11-25 11:20:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:45:59 +02:00
|
|
|
composite_storage.Mount(uri.c_str(), std::move(storage));
|
2017-11-25 11:20:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
storage_state_get_hash(const Instance &instance)
|
|
|
|
{
|
2018-01-02 14:13:26 +01:00
|
|
|
if (instance.storage == nullptr)
|
|
|
|
return 0;
|
|
|
|
|
2017-12-18 23:50:31 +01:00
|
|
|
std::set<std::string> mounts;
|
2017-11-25 11:20:32 +01:00
|
|
|
|
|
|
|
const auto visitor = [&mounts](const char *mount_uri, const Storage &storage) {
|
2017-12-18 23:50:31 +01:00
|
|
|
mounts.emplace(std::string(mount_uri) + ":" + storage.MapUTF8(""));
|
2017-11-25 11:20:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
((CompositeStorage*)instance.storage)->VisitMounts(visitor);
|
|
|
|
|
|
|
|
boost::crc_32_type result;
|
|
|
|
|
2020-02-01 06:03:57 +01:00
|
|
|
for (const auto& mount : mounts) {
|
2017-11-25 11:20:32 +01:00
|
|
|
result.process_bytes(mount.c_str(), mount.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.checksum();
|
|
|
|
}
|