Bug 411760

Summary: Deleting track deletes whole disc
Product: [Applications] amarok Reporter: Mark Fraser <mfraz74+kde>
Component: Collections/LocalAssignee: Amarok Developers <amarok-bugs-dist>
Status: RESOLVED FIXED    
Severity: critical CC: matej, ralf-engels, wbauer1
Priority: NOR    
Version: kf5   
Target Milestone: kf5   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In:

Description Mark Fraser 2019-09-09 15:05:38 UTC
SUMMARY
If I right click on a track from within Media Sources and select delete, instead of just deleting the one track it deletes the whole disc.

STEPS TO REPRODUCE
1. Select track and delete it.
2. 
3. 

OBSERVED RESULT
Whole disc is deleted.

EXPECTED RESULT
Just the track that isn't required is deleted.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 5.16.5
KDE Frameworks Version: 5.61
Qt Version: 5.12.2

ADDITIONAL INFORMATION
Comment 1 Wolfgang Bauer 2019-09-18 06:54:11 UTC
I can reproduce it here.

It's caused by a bug in the function that generates the name for the mood file (introduced when porting it from KUrl to QUrl):
QUrl 
SqlCollectionLocation::moodFile( const QUrl &track ) const
{
    QUrl moodPath = track;
    moodPath = moodPath.adjusted(QUrl::RemoveFilename);
    moodPath.setPath(moodPath.path() +  '.' + moodPath.fileName().replace( QRegExp( "(\\.\\w{2,5})$" ), ".mood" ) );
    return moodPath;
}

This wants to replace "filename.ext" with ".filename.mood" in the Url, but it already removed the filename from the path in the line before, so the actual result is "/path/to/folder/." (as the fileName() is an empty string) which is the containing folder itself.
And when Amarok tries to delete the mood file corresponding to the music file you delete afterwards, the whole folder gets deleted instead.

Proposed fix:
https://phabricator.kde.org/D24040
Comment 2 Wolfgang Bauer 2019-09-20 09:12:45 UTC
Git commit b986f52d1d53b675a0d21a7507941e368acd3a14 by Wolfgang Bauer.
Committed on 20/09/2019 at 09:09.
Pushed by wbauer into branch 'master'.

Don't delete whole folder when deleting a track

`SqlCollectionLocation::moodFile()` tries to replace the filename's
extension with ".mood" (and prepend a '.') to get the Url of the
moodfile.
But it does that *after* the filename has already been removed from the
Url, so `QUrl::fileName()` gives an empty string and the end result is
actually "/path/to/folder/." which depicts the containing folder.

As a result, when Amarok tries to delete the corresponding moodfile
after a track is deleted, the whole folder gets deleted instead.

To fix this, save the filename in a temporary variable before removing
it, and use that for generating the new filename.
Differential Revision: https://phabricator.kde.org/D24040

M  +2    -1    src/core-impl/collections/db/sql/SqlCollectionLocation.cpp

https://commits.kde.org/amarok/b986f52d1d53b675a0d21a7507941e368acd3a14