diff --git a/src/LocateUri.cxx b/src/LocateUri.cxx index dbc531f54..ac1bc0b93 100644 --- a/src/LocateUri.cxx +++ b/src/LocateUri.cxx @@ -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 diff --git a/src/SongLoader.cxx b/src/SongLoader.cxx index eda809d46..0e2e06291 100644 --- a/src/SongLoader.cxx +++ b/src/SongLoader.cxx @@ -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 diff --git a/src/db/update/Walk.cxx b/src/db/update/Walk.cxx index 084bb5c37..7cf792c19 100644 --- a/src/db/update/Walk.cxx +++ b/src/db/update/Walk.cxx @@ -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; } diff --git a/src/storage/CompositeStorage.cxx b/src/storage/CompositeStorage.cxx index c124d0d31..2833cab25 100644 --- a/src/storage/CompositeStorage.cxx +++ b/src/storage/CompositeStorage.cxx @@ -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 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 protect(mutex); @@ -268,7 +263,7 @@ CompositeStorage::GetInfo(const char *uri, bool follow) } std::unique_ptr -CompositeStorage::OpenDirectory(const char *uri) +CompositeStorage::OpenDirectory(std::string_view uri) { const std::lock_guard 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 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 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 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; } diff --git a/src/storage/CompositeStorage.hxx b/src/storage/CompositeStorage.hxx index 2646101a7..3ed9ab730 100644 --- a/src/storage/CompositeStorage.hxx +++ b/src/storage/CompositeStorage.hxx @@ -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 OpenDirectory(const char *uri) override; + std::unique_ptr 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 @@ -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; diff --git a/src/storage/StorageInterface.cxx b/src/storage/StorageInterface.cxx index 183b46655..ce729ee5e 100644 --- a/src/storage/StorageInterface.cxx +++ b/src/storage/StorageInterface.cxx @@ -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; } diff --git a/src/storage/StorageInterface.hxx b/src/storage/StorageInterface.hxx index e1a0626a6..3783e0b2e 100644 --- a/src/storage/StorageInterface.hxx +++ b/src/storage/StorageInterface.hxx @@ -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 OpenDirectory(const char *uri_utf8) = 0; + virtual std::unique_ptr 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 diff --git a/src/storage/plugins/CurlStorage.cxx b/src/storage/plugins/CurlStorage.cxx index 9dca427d2..6d0bba16d 100644 --- a/src/storage/plugins/CurlStorage.cxx +++ b/src/storage/plugins/CurlStorage.cxx @@ -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 OpenDirectory(const char *uri_utf8) override; + std::unique_ptr 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 -CurlStorage::OpenDirectory(const char *uri_utf8) +CurlStorage::OpenDirectory(std::string_view uri_utf8) { std::string uri = MapUTF8(uri_utf8); diff --git a/src/storage/plugins/LocalStorage.cxx b/src/storage/plugins/LocalStorage.cxx index a53e19a39..f1882b722 100644 --- a/src/storage/plugins/LocalStorage.cxx +++ b/src/storage/plugins/LocalStorage.cxx @@ -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 OpenDirectory(const char *uri_utf8) override; + std::unique_ptr 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 -LocalStorage::OpenDirectory(const char *uri_utf8) +LocalStorage::OpenDirectory(std::string_view uri_utf8) { return std::make_unique(MapFSOrThrow(uri_utf8)); } diff --git a/src/storage/plugins/NfsStorage.cxx b/src/storage/plugins/NfsStorage.cxx index 0d73c0235..3b8d9f9ea 100644 --- a/src/storage/plugins/NfsStorage.cxx +++ b/src/storage/plugins/NfsStorage.cxx @@ -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 OpenDirectory(const char *uri_utf8) override; + std::unique_ptr 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 -NfsStorage::OpenDirectory(const char *uri_utf8) +NfsStorage::OpenDirectory(std::string_view uri_utf8) { const std::string path = UriToNfsPath(uri_utf8); diff --git a/src/storage/plugins/SmbclientStorage.cxx b/src/storage/plugins/SmbclientStorage.cxx index 6302555e1..899640784 100644 --- a/src/storage/plugins/SmbclientStorage.cxx +++ b/src/storage/plugins/SmbclientStorage.cxx @@ -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 OpenDirectory(const char *uri_utf8) override; + std::unique_ptr 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 -SmbclientStorage::OpenDirectory(const char *uri_utf8) +SmbclientStorage::OpenDirectory(std::string_view uri_utf8) { std::string mapped = MapUTF8(uri_utf8); diff --git a/src/storage/plugins/UdisksStorage.cxx b/src/storage/plugins/UdisksStorage.cxx index c888662bb..8f87a933e 100644 --- a/src/storage/plugins/UdisksStorage.cxx +++ b/src/storage/plugins/UdisksStorage.cxx @@ -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 OpenDirectory(const char *uri_utf8) override { + std::unique_ptr 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(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); }