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

@@ -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);
}