From 32290d5eb848a22e7b16b29e4be7651c002ca14f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 18 Jul 2018 16:30:46 +0200 Subject: [PATCH] fs/Path: add method ToUTF8Throw() --- src/PlaylistFile.cxx | 10 ++++++---- src/PlaylistSave.cxx | 5 ++++- src/fs/AllocatedPath.hxx | 4 ++++ src/fs/Path.cxx | 8 +++++++- src/fs/Path.hxx | 5 +++++ src/input/plugins/FileInputPlugin.cxx | 2 +- src/playlist/PlaylistStream.cxx | 8 +++----- src/storage/plugins/LocalStorage.cxx | 12 ++++++------ src/storage/plugins/NfsStorage.cxx | 11 +++++------ test/ContainerScan.cxx | 7 ++----- 10 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 4de73df30..1ae65eb3f 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -143,11 +143,13 @@ LoadPlaylistFileInfo(PlaylistInfo &info, return false; const auto name = AllocatedPath::FromFS(name_fs_str, name_fs_end); - std::string name_utf8 = name.ToUTF8(); - if (name_utf8.empty()) - return false; - info.name = std::move(name_utf8); + try { + info.name = name.ToUTF8Throw(); + } catch (...) { + return false; + } + info.mtime = fi.GetModificationTime(); return true; } diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index ba61c7286..21e329d75 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -41,7 +41,10 @@ playlist_print_path(BufferedOutputStream &os, const Path path) /* on Windows, playlists always contain UTF-8, because its "narrow" charset (i.e. CP_ACP) is incapable of storing all Unicode paths */ - os.Format("%s\n", path.ToUTF8().c_str()); + try { + os.Format("%s\n", path.ToUTF8Throw().c_str()); + } catch (...) { + } #else os.Format("%s\n", path.c_str()); #endif diff --git a/src/fs/AllocatedPath.hxx b/src/fs/AllocatedPath.hxx index d9af53bfd..5df893928 100644 --- a/src/fs/AllocatedPath.hxx +++ b/src/fs/AllocatedPath.hxx @@ -253,6 +253,10 @@ public: return ((Path)*this).ToUTF8(); } + std::string ToUTF8Throw() const { + return ((Path)*this).ToUTF8Throw(); + } + /** * Gets directory name of this path. * Returns a "nulled" instance on error. diff --git a/src/fs/Path.cxx b/src/fs/Path.cxx index 640c74dbe..2d6ec897d 100644 --- a/src/fs/Path.cxx +++ b/src/fs/Path.cxx @@ -25,12 +25,18 @@ std::string Path::ToUTF8() const noexcept { try { - return ::PathToUTF8(c_str()); + return ToUTF8Throw(); } catch (...) { return std::string(); } } +std::string +Path::ToUTF8Throw() const +{ + return ::PathToUTF8(c_str()); +} + Path::const_pointer_type Path::GetSuffix() const noexcept { diff --git a/src/fs/Path.hxx b/src/fs/Path.hxx index d43527254..eac017389 100644 --- a/src/fs/Path.hxx +++ b/src/fs/Path.hxx @@ -134,6 +134,11 @@ public: gcc_pure std::string ToUTF8() const noexcept; + /** + * Like ToUTF8(), but throws on error. + */ + std::string ToUTF8Throw() const; + /** * Determine the "base" file name. * The return value points inside this object. diff --git a/src/input/plugins/FileInputPlugin.cxx b/src/input/plugins/FileInputPlugin.cxx index f757c9a93..380cacfce 100644 --- a/src/input/plugins/FileInputPlugin.cxx +++ b/src/input/plugins/FileInputPlugin.cxx @@ -72,7 +72,7 @@ OpenFileInputStream(Path path, Mutex &mutex) #endif #endif - return std::make_unique(path.ToUTF8().c_str(), + return std::make_unique(path.ToUTF8Throw().c_str(), std::move(reader), info.GetSize(), mutex); } diff --git a/src/playlist/PlaylistStream.cxx b/src/playlist/PlaylistStream.cxx index 5c2403a8f..689630f21 100644 --- a/src/playlist/PlaylistStream.cxx +++ b/src/playlist/PlaylistStream.cxx @@ -40,7 +40,7 @@ try { if (suffix == nullptr) return nullptr; - const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8(); + const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8Throw(); if (!playlist_suffix_supported(suffix_utf8.c_str())) return nullptr; @@ -57,10 +57,8 @@ playlist_open_path(Path path, Mutex &mutex) try { assert(!path.IsNull()); - const std::string uri_utf8 = path.ToUTF8(); - auto playlist = !uri_utf8.empty() - ? playlist_list_open_uri(uri_utf8.c_str(), mutex) - : nullptr; + const std::string uri_utf8 = path.ToUTF8Throw(); + auto playlist = playlist_list_open_uri(uri_utf8.c_str(), mutex); if (playlist == nullptr) playlist = playlist_open_path_suffix(path, mutex); diff --git a/src/storage/plugins/LocalStorage.cxx b/src/storage/plugins/LocalStorage.cxx index 78e5df2e4..92c6c99ff 100644 --- a/src/storage/plugins/LocalStorage.cxx +++ b/src/storage/plugins/LocalStorage.cxx @@ -51,7 +51,7 @@ class LocalStorage final : public Storage { public: explicit LocalStorage(Path _base_fs) - :base_fs(_base_fs), base_utf8(base_fs.ToUTF8()) { + :base_fs(_base_fs), base_utf8(base_fs.ToUTF8Throw()) { assert(!base_fs.IsNull()); assert(!base_utf8.empty()); } @@ -162,11 +162,11 @@ LocalDirectoryReader::Read() noexcept if (SkipNameFS(name_fs.c_str())) continue; - name_utf8 = name_fs.ToUTF8(); - if (name_utf8.empty()) - continue; - - return name_utf8.c_str(); + try { + name_utf8 = name_fs.ToUTF8Throw(); + return name_utf8.c_str(); + } catch (...) { + } } return nullptr; diff --git a/src/storage/plugins/NfsStorage.cxx b/src/storage/plugins/NfsStorage.cxx index b615ee200..d43ffb416 100644 --- a/src/storage/plugins/NfsStorage.cxx +++ b/src/storage/plugins/NfsStorage.cxx @@ -378,14 +378,13 @@ NfsListDirectoryOperation::CollectEntries(struct nfsdir *dir) if (SkipNameFS(name_fs.c_str())) continue; - std::string name_utf8 = name_fs.ToUTF8(); - if (name_utf8.empty()) + try { + entries.emplace_front(name_fs.ToUTF8Throw()); + Copy(entries.front().info, *ent); + } catch (...) { /* ignore files whose name cannot be converted to UTF-8 */ - continue; - - entries.emplace_front(std::move(name_utf8)); - Copy(entries.front().info, *ent); + } } } diff --git a/test/ContainerScan.cxx b/test/ContainerScan.cxx index 7c92f1a74..ac4ad55a4 100644 --- a/test/ContainerScan.cxx +++ b/test/ContainerScan.cxx @@ -48,12 +48,9 @@ FindContainerDecoderPlugin(const char *suffix) static const DecoderPlugin * FindContainerDecoderPlugin(Path path) { - const auto utf8 = path.ToUTF8(); - if (utf8.empty()) - return nullptr; - UriSuffixBuffer suffix_buffer; - const char *const suffix = uri_get_suffix(utf8.c_str(), suffix_buffer); + const char *const suffix = uri_get_suffix(path.ToUTF8Throw().c_str(), + suffix_buffer); if (suffix == nullptr) return nullptr;