From 8913c777869dc5d7a01f9175f9d75b72d5d0ed76 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 26 Apr 2014 17:53:59 +0200 Subject: [PATCH] browser: properly report errors when deleting items fails --- src/actions.cpp | 6 +++--- src/browser.cpp | 28 +++++++++++++++++++++++----- src/browser.h | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index 0c8d9438..e8624e99 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -672,15 +672,15 @@ void DeleteBrowserItems::run() { const MPD::Item &i = (*it)->value(); std::string iname = i.type == MPD::itSong ? i.song->getName() : i.name; - if (myBrowser->deleteItem(i)) + std::string errmsg; + if (myBrowser->deleteItem(i, errmsg)) { const char msg[] = "\"%ls\" deleted"; Statusbar::msg(msg, wideShorten(ToWString(iname), COLS-const_strlen(msg)).c_str()); } else { - const char msg[] = "Couldn't delete \"%ls\": %s"; - Statusbar::msg(msg, wideShorten(ToWString(iname), COLS-const_strlen(msg)-25).c_str(), strerror(errno)); + Statusbar::msg("%s", errmsg.c_str()); success = false; break; } diff --git a/src/browser.cpp b/src/browser.cpp index 51020914..778720c8 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -541,7 +541,7 @@ void Browser::ChangeBrowseMode() drawHeader(); } -bool Browser::deleteItem(const MPD::Item &item) +bool Browser::deleteItem(const MPD::Item &item, std::string &errmsg) { if (isParentDirectory((item))) FatalError("Parent directory passed to Browser::deleteItem"); @@ -555,10 +555,28 @@ bool Browser::deleteItem(const MPD::Item &item) path = Config.mpd_music_dir; path += item.type == itSong ? item.song->getURI() : item.name; - if (item.type == itDirectory) - ClearDirectory(path); - - return std::remove(path.c_str()) == 0; + bool rv; + try + { + if (item.type == itDirectory) + ClearDirectory(path); + if (!boost::filesystem::exists(path)) + { + errmsg = "No such item: " + path; + rv = false; + } + else + { + boost::filesystem::remove(path); + rv = true; + } + } + catch (boost::filesystem::filesystem_error &err) + { + errmsg = err.what(); + rv = false; + } + return rv; } #endif // !WIN32 diff --git a/src/browser.h b/src/browser.h index 323c7ade..b4c1b704 100644 --- a/src/browser.h +++ b/src/browser.h @@ -74,7 +74,7 @@ struct Browser: Screen>, Filterable, HasSongs, Searchable, T void GetLocalDirectory(MPD::ItemList &, const std::string &, bool) const; void ClearDirectory(const std::string &) const; void ChangeBrowseMode(); - bool deleteItem(const MPD::Item &); + bool deleteItem(const MPD::Item &, std::string &errmsg); # endif // !WIN32 static bool isParentDirectory(const MPD::Item &item) {