CompositeStorage.hxx 4.39 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */

#ifndef MPD_COMPOSITE_STORAGE_HXX
#define MPD_COMPOSITE_STORAGE_HXX

#include "check.h"
#include "StorageInterface.hxx"
#include "thread/Mutex.hxx"
26
#include "util/Compiler.h"
27

28
#include <memory>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <string>
#include <map>

/**
 * A #Storage implementation that combines multiple other #Storage
 * instances in one virtual tree.  It is used to "mount" new #Storage
 * instances into the storage tree.
 *
 * This class is thread-safe: mounts may be added and removed at any
 * time in any thread.
 */
class CompositeStorage final : public Storage {
	/**
	 * A node in the virtual directory tree.
	 */
	struct Directory {
		/**
46
		 * The #Storage mounted in this virtual directory.  All
47 48 49 50
		 * "leaf" Directory instances must have a #Storage.
		 * Other Directory instances may have one, and child
		 * mounts will be "mixed" in.
		 */
51
		std::unique_ptr<Storage> storage;
52 53 54 55

		std::map<std::string, Directory> children;

		gcc_pure
56
		bool IsEmpty() const noexcept {
57 58 59 60
			return storage == nullptr && children.empty();
		}

		gcc_pure
61
		const Directory *Find(const char *uri) const noexcept;
62 63 64

		Directory &Make(const char *uri);

65 66
		bool Unmount() noexcept;
		bool Unmount(const char *uri) noexcept;
67 68 69

		gcc_pure
		bool MapToRelativeUTF8(std::string &buffer,
70
				       const char *uri) const noexcept;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	};

	struct FindResult {
		const Directory *directory;
		const char *uri;
	};

	/**
	 * Protects the virtual #Directory tree.
	 *
	 * TODO: use readers-writer lock
	 */
	mutable Mutex mutex;

	Directory root;

	mutable std::string relative_buffer;

public:
90
	CompositeStorage() noexcept;
91 92
	virtual ~CompositeStorage();

93 94 95 96 97 98 99 100 101
	/**
	 * Get the #Storage at the specified mount point.  Returns
	 * nullptr if the given URI is not a mount point.
	 *
	 * The returned pointer is unprotected.  No other thread is
	 * allowed to unmount the given mount point while the return
	 * value is being used.
	 */
	gcc_pure gcc_nonnull_all
102
	Storage *GetMount(const char *uri) noexcept;
103

104 105 106 107 108 109 110
	/**
	 * Call the given function for each mounted storage, including
	 * the root storage.  Passes mount point URI and the a const
	 * Storage reference to the function.
	 */
	template<typename T>
	void VisitMounts(T t) const {
111
		const std::lock_guard<Mutex> protect(mutex);
112 113 114 115
		std::string uri;
		VisitMounts(uri, root, t);
	}

116
	void Mount(const char *uri, std::unique_ptr<Storage> storage);
117 118 119
	bool Unmount(const char *uri);

	/* virtual methods from class Storage */
120
	StorageFileInfo GetInfo(const char *uri, bool follow) override;
121

122
	std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri) override;
123

124
	std::string MapUTF8(const char *uri) const noexcept override;
125

126
	AllocatedPath MapFS(const char *uri) const noexcept override;
127

128
	const char *MapToRelativeUTF8(const char *uri) const noexcept override;
129 130

private:
131 132 133
	template<typename T>
	void VisitMounts(std::string &uri, const Directory &directory,
			 T t) const {
134 135
		if (directory.storage)
			t(uri.c_str(), *directory.storage);
136 137 138 139 140 141 142 143 144 145 146 147 148 149

		if (!uri.empty())
			uri.push_back('/');

		const size_t uri_length = uri.length();

		for (const auto &i : directory.children) {
			uri.resize(uri_length);
			uri.append(i.first);

			VisitMounts(uri, i.second, t);
		}
	}

150 151 152 153 154 155 156 157
	/**
	 * Follow the given URI path, and find the outermost directory
	 * which is a #Storage mount point.  If there are no mounts,
	 * it returns the root directory (with a nullptr "storage"
	 * attribute, of course).  FindResult::uri contains the
	 * remaining unused part of the URI (may be empty if all of
	 * the URI was used).
	 */
158
	gcc_pure
159
	FindResult FindStorage(const char *uri) const noexcept;
160 161 162 163 164 165

	const char *MapToRelativeUTF8(const Directory &directory,
				      const char *uri) const;
};

#endif