Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ void EditLibraryAlbum::run()
for (size_t i = 0; i < myLibrary->Songs.size(); ++i)
{
Statusbar::printf("Updating tags in \"%1%\"...", myLibrary->Songs[i].value().getName());
std::string path = Config.mpd_music_dir + myLibrary->Songs[i].value().getURI();
std::string path = Config.mpd_music_dir + normalizePath(myLibrary->Songs[i].value().getURI());
TagLib::FileRef f(path.c_str());
if (f.isNull())
{
Expand Down Expand Up @@ -1566,11 +1566,11 @@ void EditDirectoryName::run()
std::string full_old_dir;
if (!myBrowser->isLocal())
full_old_dir += Config.mpd_music_dir;
full_old_dir += old_dir;
full_old_dir += normalizePath(old_dir);
std::string full_new_dir;
if (!myBrowser->isLocal())
full_new_dir += Config.mpd_music_dir;
full_new_dir += new_dir;
full_new_dir += normalizePath(new_dir);
boost::filesystem::rename(full_old_dir, full_new_dir);
const char msg[] = "Directory renamed to \"%1%\"";
Statusbar::printf(msg, wideShorten(new_dir, COLS-const_strlen(msg)));
Expand All @@ -1590,8 +1590,8 @@ void EditDirectoryName::run()
}
if (!new_dir.empty() && new_dir != old_dir)
{
std::string full_old_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + old_dir;
std::string full_new_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + new_dir;
std::string full_old_dir = Config.mpd_music_dir + normalizePath(myTagEditor->CurrentDir() + "/" + old_dir);
std::string full_new_dir = Config.mpd_music_dir + normalizePath(myTagEditor->CurrentDir() + "/" + new_dir);
if (rename(full_old_dir.c_str(), full_new_dir.c_str()) == 0)
{
const char msg[] = "Directory renamed to \"%1%\"";
Expand Down
2 changes: 1 addition & 1 deletion src/lyrics_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ LyricsFetcher::Result TagsLyricsFetcher::fetch([[maybe_unused]] const std::strin
std::string path;
if (song.isFromDatabase())
path += Config.mpd_music_dir;
path += song.getURI();
path += normalizePath(song.getURI());

TagLib::FileRef f(path.c_str());
if (f.isNull())
Expand Down
3 changes: 2 additions & 1 deletion src/screens/song_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "tags.h"
#include "title.h"
#include "screens/screen_switcher.h"
#include "utility/string.h"

#ifdef HAVE_TAGLIB_H
# include "fileref.h"
Expand Down Expand Up @@ -120,7 +121,7 @@ void SongInfo::PrepareSong(const MPD::Song &s)
std::string path_to_file;
if (s.isFromDatabase())
path_to_file += Config.mpd_music_dir;
path_to_file += s.getURI();
path_to_file += normalizePath(s.getURI());
TagLib::FileRef f(path_to_file.c_str());
if (!f.isNull())
{
Expand Down
2 changes: 1 addition & 1 deletion src/screens/tiny_tag_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool TinyTagEditor::getTags()
std::string path_to_file;
if (itsEdited.isFromDatabase())
path_to_file += Config.mpd_music_dir;
path_to_file += itsEdited.getURI();
path_to_file += normalizePath(itsEdited.getURI());

TagLib::FileRef f(path_to_file.c_str());
if (f.isNull())
Expand Down
6 changes: 3 additions & 3 deletions src/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ bool write(MPD::MutableSong &s)
std::string old_name;
if (s.isFromDatabase())
old_name += Config.mpd_music_dir;
old_name += s.getURI();
old_name += normalizePath(s.getURI());

TagLib::FileRef f(old_name.c_str());
if (f.isNull())
Expand Down Expand Up @@ -330,9 +330,9 @@ bool write(MPD::MutableSong &s)
std::string new_name;
if (s.isFromDatabase())
new_name += Config.mpd_music_dir;
new_name += s.getDirectory();
new_name += normalizePath(s.getDirectory());
new_name += "/";
new_name += s.getNewName();
new_name += normalizePath(s.getNewName());
boost::filesystem::rename(old_name, new_name);
}
return true;
Expand Down
27 changes: 27 additions & 0 deletions src/utility/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <cassert>
#include <cwctype>
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/split.hpp>
#include "utility/string.h"

std::string getBasename(const std::string &path)
Expand Down Expand Up @@ -108,3 +111,27 @@ void escapeSingleQuotes(std::string &filename)
}
}
}

std::string normalizePath(const std::string &path)
{
std::vector<std::string> parts;
boost::split(parts, path, boost::is_any_of("/"));
bool absolute = !path.empty() && path[0] == '/';
std::vector<std::string> kept;
for (auto &part : parts)
{
if (part.empty() || part == ".")
continue;
if (part == "..")
{
if (!kept.empty())
kept.pop_back();
continue;
}
kept.push_back(std::move(part));
}
std::string result = boost::join(kept, "/");
if (absolute)
result = "/" + result;
return result;
}
5 changes: 5 additions & 0 deletions src/utility/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ void removeInvalidCharsFromFilename(std::string &filename, bool win32_compatible

void escapeSingleQuotes(std::string &filename);

// Collapse "." and ".." path segments. Relative paths cannot escape above
// their root via ".."; absolute paths keep their leading slash. Used to
// confine MPD-supplied URIs/directories under mpd_music_dir before joining.
std::string normalizePath(const std::string &path);

#endif // NCMPCPP_UTILITY_STRING_H