storage/Interface: convert URI parameters to std::string_view

This commit is contained in:
Max Kellermann 2020-04-03 16:22:39 +02:00
parent 0080eee857
commit a98d627c0b
12 changed files with 115 additions and 128 deletions

View File

@ -42,11 +42,13 @@ LocateFileUri(const char *uri, const Client *client
#ifdef ENABLE_DATABASE
if (storage != nullptr) {
const char *suffix = storage->MapToRelativeUTF8(uri);
if (suffix != nullptr)
const auto suffix = storage->MapToRelativeUTF8(uri);
if (suffix.data() != nullptr)
/* this path was relative to the music
directory */
return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
// TODO: don't use suffix.data() (ok for now because we know it's null-terminated)
return LocatedUri(LocatedUri::Type::RELATIVE,
suffix.data());
}
#endif
@ -80,9 +82,11 @@ LocateAbsoluteUri(UriPluginKind kind, const char *uri
#ifdef ENABLE_DATABASE
if (storage != nullptr) {
const char *suffix = storage->MapToRelativeUTF8(uri);
if (suffix != nullptr)
return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
const auto suffix = storage->MapToRelativeUTF8(uri);
if (suffix.data() != nullptr)
// TODO: don't use suffix.data() (ok for now because we know it's null-terminated)
return LocatedUri(LocatedUri::Type::RELATIVE,
suffix.data());
}
#endif

View File

@ -54,11 +54,11 @@ SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
{
#ifdef ENABLE_DATABASE
if (storage != nullptr) {
const char *suffix = storage->MapToRelativeUTF8(path_utf8);
if (suffix != nullptr)
const auto suffix = storage->MapToRelativeUTF8(path_utf8);
if (suffix.data() != nullptr)
/* this path was relative to the music
directory - obtain it from the database */
return LoadFromDatabase(suffix);
return LoadFromDatabase(std::string(suffix).c_str());
}
#endif

View File

@ -260,9 +260,9 @@ UpdateWalk::SkipSymlink(const Directory *directory,
if (target_utf8.empty())
return true;
const char *relative =
auto relative =
storage.MapToRelativeUTF8(target_utf8.c_str());
return relative != nullptr
return relative.data() != nullptr
? !config.follow_inside_symlinks
: !config.follow_outside_symlinks;
}

View File

@ -83,24 +83,19 @@ CompositeDirectoryReader::GetInfo(bool follow)
}
static std::string_view
NextSegment(const char *&uri_r)
NextSegment(std::string_view &uri_r) noexcept
{
const char *uri = uri_r;
const char *slash = strchr(uri, '/');
if (slash == nullptr) {
uri_r += strlen(uri);
return uri;
} else {
uri_r = slash + 1;
return std::string_view(uri, slash - uri);
}
StringView uri(uri_r);
auto s = uri.Split('/');
uri_r = s.second;
return s.first;
}
const CompositeStorage::Directory *
CompositeStorage::Directory::Find(const char *uri) const noexcept
CompositeStorage::Directory::Find(std::string_view uri) const noexcept
{
const Directory *directory = this;
while (*uri != 0) {
while (!uri.empty()) {
const auto name = NextSegment(uri);
auto i = directory->children.find(name);
if (i == directory->children.end())
@ -113,10 +108,10 @@ CompositeStorage::Directory::Find(const char *uri) const noexcept
}
CompositeStorage::Directory &
CompositeStorage::Directory::Make(const char *uri)
CompositeStorage::Directory::Make(std::string_view uri)
{
Directory *directory = this;
while (*uri != 0) {
while (!uri.empty()) {
auto name = NextSegment(uri);
auto i = directory->children.emplace(std::move(name),
Directory());
@ -137,9 +132,9 @@ CompositeStorage::Directory::Unmount() noexcept
}
bool
CompositeStorage::Directory::Unmount(const char *uri) noexcept
CompositeStorage::Directory::Unmount(std::string_view uri) noexcept
{
if (StringIsEmpty(uri))
if (uri.empty())
return Unmount();
const auto name = NextSegment(uri);
@ -157,11 +152,11 @@ CompositeStorage::Directory::Unmount(const char *uri) noexcept
bool
CompositeStorage::Directory::MapToRelativeUTF8(std::string &buffer,
const char *uri) const noexcept
std::string_view uri) const noexcept
{
if (storage != nullptr) {
const char *result = storage->MapToRelativeUTF8(uri);
if (result != nullptr) {
auto result = storage->MapToRelativeUTF8(uri);
if (result.data() != nullptr) {
buffer = result;
return true;
}
@ -191,12 +186,12 @@ CompositeStorage::CompositeStorage() noexcept
CompositeStorage::~CompositeStorage() = default;
Storage *
CompositeStorage::GetMount(const char *uri) noexcept
CompositeStorage::GetMount(std::string_view uri) noexcept
{
const std::lock_guard<Mutex> protect(mutex);
auto result = FindStorage(uri);
if (*result.uri != 0)
if (!result.uri.empty())
/* not a mount point */
return nullptr;
@ -221,12 +216,12 @@ CompositeStorage::Unmount(const char *uri)
}
CompositeStorage::FindResult
CompositeStorage::FindStorage(const char *uri) const noexcept
CompositeStorage::FindStorage(std::string_view uri) const noexcept
{
FindResult result{&root, uri};
const Directory *directory = &root;
while (*uri != 0) {
while (!uri.empty()) {
const auto name = NextSegment(uri);
auto i = directory->children.find(name);
@ -242,7 +237,7 @@ CompositeStorage::FindStorage(const char *uri) const noexcept
}
StorageFileInfo
CompositeStorage::GetInfo(const char *uri, bool follow)
CompositeStorage::GetInfo(std::string_view uri, bool follow)
{
const std::lock_guard<Mutex> protect(mutex);
@ -268,7 +263,7 @@ CompositeStorage::GetInfo(const char *uri, bool follow)
}
std::unique_ptr<StorageDirectoryReader>
CompositeStorage::OpenDirectory(const char *uri)
CompositeStorage::OpenDirectory(std::string_view uri)
{
const std::lock_guard<Mutex> protect(mutex);
@ -295,7 +290,7 @@ CompositeStorage::OpenDirectory(const char *uri)
}
std::string
CompositeStorage::MapUTF8(const char *uri) const noexcept
CompositeStorage::MapUTF8(std::string_view uri) const noexcept
{
const std::lock_guard<Mutex> protect(mutex);
@ -307,7 +302,7 @@ CompositeStorage::MapUTF8(const char *uri) const noexcept
}
AllocatedPath
CompositeStorage::MapFS(const char *uri) const noexcept
CompositeStorage::MapFS(std::string_view uri) const noexcept
{
const std::lock_guard<Mutex> protect(mutex);
@ -318,19 +313,19 @@ CompositeStorage::MapFS(const char *uri) const noexcept
return f.directory->storage->MapFS(f.uri);
}
const char *
CompositeStorage::MapToRelativeUTF8(const char *uri) const noexcept
std::string_view
CompositeStorage::MapToRelativeUTF8(std::string_view uri) const noexcept
{
const std::lock_guard<Mutex> protect(mutex);
if (root.storage != nullptr) {
const char *result = root.storage->MapToRelativeUTF8(uri);
if (result != nullptr)
auto result = root.storage->MapToRelativeUTF8(uri);
if (result.data() != nullptr)
return result;
}
if (!root.MapToRelativeUTF8(relative_buffer, uri))
return nullptr;
return relative_buffer.c_str();
return relative_buffer;
}

View File

@ -57,21 +57,21 @@ class CompositeStorage final : public Storage {
}
gcc_pure
const Directory *Find(const char *uri) const noexcept;
const Directory *Find(std::string_view uri) const noexcept;
Directory &Make(const char *uri);
Directory &Make(std::string_view uri);
bool Unmount() noexcept;
bool Unmount(const char *uri) noexcept;
bool Unmount(std::string_view uri) noexcept;
gcc_pure
bool MapToRelativeUTF8(std::string &buffer,
const char *uri) const noexcept;
std::string_view uri) const noexcept;
};
struct FindResult {
const Directory *directory;
const char *uri;
std::string_view uri;
};
/**
@ -98,7 +98,7 @@ public:
* value is being used.
*/
gcc_pure gcc_nonnull_all
Storage *GetMount(const char *uri) noexcept;
Storage *GetMount(std::string_view uri) noexcept;
/**
* Call the given function for each mounted storage, including
@ -116,15 +116,15 @@ public:
bool Unmount(const char *uri);
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri, bool follow) override;
StorageFileInfo GetInfo(std::string_view uri, bool follow) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri) override;
std::string MapUTF8(const char *uri) const noexcept override;
std::string MapUTF8(std::string_view uri) const noexcept override;
AllocatedPath MapFS(const char *uri) const noexcept override;
AllocatedPath MapFS(std::string_view uri) const noexcept override;
const char *MapToRelativeUTF8(const char *uri) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri) const noexcept override;
private:
template<typename T>
@ -155,7 +155,7 @@ private:
* the URI was used).
*/
gcc_pure
FindResult FindStorage(const char *uri) const noexcept;
FindResult FindStorage(std::string_view uri) const noexcept;
const char *MapToRelativeUTF8(const Directory &directory,
const char *uri) const;

View File

@ -22,7 +22,7 @@
#include "fs/Traits.hxx"
AllocatedPath
Storage::MapFS([[maybe_unused]] const char *uri_utf8) const noexcept
Storage::MapFS([[maybe_unused]] std::string_view uri_utf8) const noexcept
{
return nullptr;
}

View File

@ -52,18 +52,18 @@ public:
/**
* Throws #std::runtime_error on error.
*/
virtual StorageFileInfo GetInfo(const char *uri_utf8, bool follow) = 0;
virtual StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) = 0;
/**
* Throws #std::runtime_error on error.
*/
virtual std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) = 0;
virtual std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) = 0;
/**
* Map the given relative URI to an absolute URI.
*/
gcc_pure
virtual std::string MapUTF8(const char *uri_utf8) const noexcept = 0;
virtual std::string MapUTF8(std::string_view uri_utf8) const noexcept = 0;
/**
* Map the given relative URI to a local file path. Returns
@ -71,7 +71,7 @@ public:
* support local files.
*/
gcc_pure
virtual AllocatedPath MapFS(const char *uri_utf8) const noexcept;
virtual AllocatedPath MapFS(std::string_view uri_utf8) const noexcept;
gcc_pure
AllocatedPath MapChildFS(std::string_view uri_utf8,
@ -83,7 +83,7 @@ public:
* string); if not, returns nullptr.
*/
gcc_pure
virtual const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept = 0;
virtual std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept = 0;
};
#endif

View File

@ -57,29 +57,27 @@ public:
curl(_loop) {}
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override;
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
std::string MapUTF8(const char *uri_utf8) const noexcept override;
std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
};
std::string
CurlStorage::MapUTF8(const char *uri_utf8) const noexcept
CurlStorage::MapUTF8(std::string_view uri_utf8) const noexcept
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
return base;
std::string path_esc = CurlEscapeUriPath(uri_utf8);
return PathTraitsUTF8::Build(base, path_esc);
}
const char *
CurlStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
std::string_view
CurlStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
{
return PathTraitsUTF8::Relative(base,
CurlUnescape(uri_utf8).c_str());
@ -447,7 +445,7 @@ protected:
};
StorageFileInfo
CurlStorage::GetInfo(const char *uri_utf8, [[maybe_unused]] bool follow)
CurlStorage::GetInfo(std::string_view uri_utf8, [[maybe_unused]] bool follow)
{
// TODO: escape the given URI
@ -541,7 +539,7 @@ protected:
};
std::unique_ptr<StorageDirectoryReader>
CurlStorage::OpenDirectory(const char *uri_utf8)
CurlStorage::OpenDirectory(std::string_view uri_utf8)
{
std::string uri = MapUTF8(uri_utf8);

View File

@ -56,18 +56,18 @@ public:
}
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override;
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
std::string MapUTF8(const char *uri_utf8) const noexcept override;
std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
AllocatedPath MapFS(const char *uri_utf8) const noexcept override;
AllocatedPath MapFS(std::string_view uri_utf8) const noexcept override;
const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
private:
AllocatedPath MapFSOrThrow(const char *uri_utf8) const;
AllocatedPath MapFSOrThrow(std::string_view uri_utf8) const;
};
static StorageFileInfo
@ -95,29 +95,27 @@ Stat(Path path, bool follow)
}
std::string
LocalStorage::MapUTF8(const char *uri_utf8) const noexcept
LocalStorage::MapUTF8(std::string_view uri_utf8) const noexcept
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
return base_utf8;
return PathTraitsUTF8::Build(base_utf8, uri_utf8);
}
AllocatedPath
LocalStorage::MapFSOrThrow(const char *uri_utf8) const
LocalStorage::MapFSOrThrow(std::string_view uri_utf8) const
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
return base_fs;
return base_fs / AllocatedPath::FromUTF8Throw(uri_utf8);
}
AllocatedPath
LocalStorage::MapFS(const char *uri_utf8) const noexcept
LocalStorage::MapFS(std::string_view uri_utf8) const noexcept
{
try {
return MapFSOrThrow(uri_utf8);
@ -126,20 +124,20 @@ LocalStorage::MapFS(const char *uri_utf8) const noexcept
}
}
const char *
LocalStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
std::string_view
LocalStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
{
return PathTraitsUTF8::Relative(base_utf8, uri_utf8);
}
StorageFileInfo
LocalStorage::GetInfo(const char *uri_utf8, bool follow)
LocalStorage::GetInfo(std::string_view uri_utf8, bool follow)
{
return Stat(MapFSOrThrow(uri_utf8), follow);
}
std::unique_ptr<StorageDirectoryReader>
LocalStorage::OpenDirectory(const char *uri_utf8)
LocalStorage::OpenDirectory(std::string_view uri_utf8)
{
return std::make_unique<LocalDirectoryReader>(MapFSOrThrow(uri_utf8));
}

View File

@ -86,13 +86,13 @@ public:
}
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override;
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
std::string MapUTF8(const char *uri_utf8) const noexcept override;
std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
/* virtual methods from NfsLease */
void OnNfsConnectionReady() noexcept final {
@ -216,10 +216,8 @@ private:
};
static std::string
UriToNfsPath(const char *_uri_utf8)
UriToNfsPath(std::string_view _uri_utf8)
{
assert(_uri_utf8 != nullptr);
/* libnfs paths must begin with a slash */
std::string uri_utf8("/");
uri_utf8.append(_uri_utf8);
@ -233,18 +231,16 @@ UriToNfsPath(const char *_uri_utf8)
}
std::string
NfsStorage::MapUTF8(const char *uri_utf8) const noexcept
NfsStorage::MapUTF8(std::string_view uri_utf8) const noexcept
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
return base;
return PathTraitsUTF8::Build(base, uri_utf8);
}
const char *
NfsStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
std::string_view
NfsStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
{
return PathTraitsUTF8::Relative(base, uri_utf8);
}
@ -294,7 +290,7 @@ protected:
};
StorageFileInfo
NfsStorage::GetInfo(const char *uri_utf8, bool follow)
NfsStorage::GetInfo(std::string_view uri_utf8, bool follow)
{
const std::string path = UriToNfsPath(uri_utf8);
@ -397,7 +393,7 @@ NfsListDirectoryOperation::CollectEntries(struct nfsdir *dir)
}
std::unique_ptr<StorageDirectoryReader>
NfsStorage::OpenDirectory(const char *uri_utf8)
NfsStorage::OpenDirectory(std::string_view uri_utf8)
{
const std::string path = UriToNfsPath(uri_utf8);

View File

@ -64,28 +64,26 @@ public:
}
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override;
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override;
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
std::string MapUTF8(const char *uri_utf8) const noexcept override;
std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
};
std::string
SmbclientStorage::MapUTF8(const char *uri_utf8) const noexcept
SmbclientStorage::MapUTF8(std::string_view uri_utf8) const noexcept
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
return base;
return PathTraitsUTF8::Build(base, uri_utf8);
}
const char *
SmbclientStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
std::string_view
SmbclientStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
{
return PathTraitsUTF8::Relative(base, uri_utf8);
}
@ -117,14 +115,14 @@ GetInfo(const char *path)
}
StorageFileInfo
SmbclientStorage::GetInfo(const char *uri_utf8, [[maybe_unused]] bool follow)
SmbclientStorage::GetInfo(std::string_view uri_utf8, [[maybe_unused]] bool follow)
{
const std::string mapped = MapUTF8(uri_utf8);
return ::GetInfo(mapped.c_str());
}
std::unique_ptr<StorageDirectoryReader>
SmbclientStorage::OpenDirectory(const char *uri_utf8)
SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
{
std::string mapped = MapUTF8(uri_utf8);

View File

@ -98,19 +98,19 @@ public:
}
/* virtual methods from class Storage */
StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override {
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override {
MountWait();
return mounted_storage->GetInfo(uri_utf8, follow);
}
std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override {
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override {
MountWait();
return mounted_storage->OpenDirectory(uri_utf8);
}
std::string MapUTF8(const char *uri_utf8) const noexcept override;
std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
AllocatedPath MapFS(const char *uri_utf8) const noexcept override {
AllocatedPath MapFS(std::string_view uri_utf8) const noexcept override {
try {
const_cast<UdisksStorage *>(this)->MountWait();
} catch (...) {
@ -120,7 +120,7 @@ public:
return mounted_storage->MapFS(uri_utf8);
}
const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
private:
void SetMountPoint(Path mount_point);
@ -324,11 +324,9 @@ try {
}
std::string
UdisksStorage::MapUTF8(const char *uri_utf8) const noexcept
UdisksStorage::MapUTF8(std::string_view uri_utf8) const noexcept
{
assert(uri_utf8 != nullptr);
if (StringIsEmpty(uri_utf8))
if (uri_utf8.empty())
/* kludge for a special case: return the "udisks://"
URI if the parameter is an empty string to fix the
mount URIs in the state file */
@ -344,8 +342,8 @@ UdisksStorage::MapUTF8(const char *uri_utf8) const noexcept
}
}
const char *
UdisksStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
std::string_view
UdisksStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
{
return PathTraitsUTF8::Relative(base_uri, uri_utf8);
}