StorageState.cxx 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright 2003-2017 The Music Player Daemon Project
 * 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 "config.h"
#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"

37
#include <set>
38 39 40 41 42 43 44 45 46 47 48 49
#include <boost/crc.hpp>

#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)
{
50 51 52
	if (instance.storage == nullptr)
		return;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	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);
	}

90 91 92 93 94 95
	if (instance.storage == nullptr)
		/* without storage (a CompositeStorage instance), we
		   cannot mount, and therefore we silently ignore the
		   state file */
		return true;

96 97 98 99 100 101 102
	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());

Max Kellermann's avatar
Max Kellermann committed
103
	auto &event_loop = instance.io_thread.GetEventLoop();
104
	auto storage = CreateStorageURI(event_loop, url.c_str());
105 106 107 108 109 110 111 112 113 114
	if (storage == nullptr) {
		FormatError(storage_domain, "Unrecognized storage URI: %s", url.c_str());
		return true;
	}

	Database *db = instance.database;
	if (db != nullptr && db->IsPlugin(simple_db_plugin)) {
		try {
			((SimpleDatabase *)db)->Mount(uri.c_str(), url.c_str());
		} catch (...) {
115 116 117 118
			FormatError(std::current_exception(),
				    "Failed to restore mount to %s",
				    url.c_str());
			return true;
119 120 121
		}
	}

122
	((CompositeStorage*)instance.storage)->Mount(uri.c_str(),
123
						     std::move(storage));
124 125 126 127 128 129 130

	return true;
}

unsigned
storage_state_get_hash(const Instance &instance)
{
131 132 133
	if (instance.storage == nullptr)
		return 0;

134
	std::set<std::string> mounts;
135 136

	const auto visitor = [&mounts](const char *mount_uri, const Storage &storage) {
137
		mounts.emplace(std::string(mount_uri) + ":" + storage.MapUTF8(""));
138 139 140 141 142 143 144 145 146 147 148 149
	};

	((CompositeStorage*)instance.storage)->VisitMounts(visitor);

	boost::crc_32_type result;

	for (auto mount: mounts) {
		result.process_bytes(mount.c_str(), mount.length());
	}

	return result.checksum();
}