Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
4f120f37
Commit
4f120f37
authored
Jan 20, 2014
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PlaylistSong: modify the given song object in-place
Reduce bloat.
parent
dd20a3ce
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
122 deletions
+86
-122
PlaylistPrint.cxx
src/PlaylistPrint.cxx
+7
-9
PlaylistQueue.cxx
src/PlaylistQueue.cxx
+4
-3
PlaylistSong.cxx
src/PlaylistSong.cxx
+39
-57
PlaylistSong.hxx
src/PlaylistSong.hxx
+5
-5
test_translate_song.cxx
test/test_translate_song.cxx
+31
-48
No files found.
src/PlaylistPrint.cxx
View file @
4f120f37
...
@@ -156,15 +156,13 @@ playlist_provider_print(Client &client, const char *uri,
...
@@ -156,15 +156,13 @@ playlist_provider_print(Client &client, const char *uri,
DetachedSong
*
song
;
DetachedSong
*
song
;
while
((
song
=
e
.
NextSong
())
!=
nullptr
)
{
while
((
song
=
e
.
NextSong
())
!=
nullptr
)
{
song
=
playlist_check_translate_song
(
song
,
base_uri
.
c_str
(),
if
(
playlist_check_translate_song
(
*
song
,
base_uri
.
c_str
(),
false
);
false
))
{
if
(
song
==
nullptr
)
if
(
detail
)
continue
;
song_print_info
(
client
,
*
song
);
else
if
(
detail
)
song_print_uri
(
client
,
*
song
);
song_print_info
(
client
,
*
song
);
}
else
song_print_uri
(
client
,
*
song
);
delete
song
;
delete
song
;
}
}
...
...
src/PlaylistQueue.cxx
View file @
4f120f37
...
@@ -48,10 +48,11 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
...
@@ -48,10 +48,11 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
continue
;
continue
;
}
}
song
=
playlist_check_translate_song
(
song
,
base_uri
.
c_str
(),
if
(
!
playlist_check_translate_song
(
*
song
,
base_uri
.
c_str
(),
secure
);
secure
))
{
if
(
song
==
nullptr
)
delete
song
;
continue
;
continue
;
}
PlaylistResult
result
=
dest
.
AppendSong
(
pc
,
std
::
move
(
*
song
));
PlaylistResult
result
=
dest
.
AppendSong
(
pc
,
std
::
move
(
*
song
));
delete
song
;
delete
song
;
...
...
src/PlaylistSong.cxx
View file @
4f120f37
...
@@ -34,83 +34,70 @@
...
@@ -34,83 +34,70 @@
#include <string.h>
#include <string.h>
static
void
static
void
merge_song_metadata
(
DetachedSong
&
dest
,
const
DetachedSong
&
base
,
merge_song_metadata
(
DetachedSong
&
add
,
const
DetachedSong
&
base
)
const
DetachedSong
&
add
)
{
{
{
{
TagBuilder
builder
(
add
.
GetTag
());
TagBuilder
builder
(
add
.
GetTag
());
builder
.
Complement
(
base
.
GetTag
());
builder
.
Complement
(
base
.
GetTag
());
dest
.
SetTag
(
builder
.
Commit
());
add
.
SetTag
(
builder
.
Commit
());
}
}
dest
.
SetLastModified
(
base
.
GetLastModified
());
add
.
SetLastModified
(
base
.
GetLastModified
());
dest
.
SetStartMS
(
add
.
GetStartMS
());
dest
.
SetEndMS
(
add
.
GetEndMS
());
}
}
static
DetachedSong
*
static
void
apply_song_metadata
(
DetachedSong
*
dest
,
const
DetachedSong
&
src
)
apply_song_metadata
(
DetachedSong
&
dest
,
const
DetachedSong
&
src
)
{
{
assert
(
dest
!=
nullptr
);
if
(
!
src
.
GetTag
().
IsDefined
()
&&
if
(
!
src
.
GetTag
().
IsDefined
()
&&
src
.
GetStartMS
()
==
0
&&
src
.
GetEndMS
()
==
0
)
src
.
GetStartMS
()
==
0
&&
src
.
GetEndMS
()
==
0
)
return
dest
;
return
;
DetachedSong
*
tmp
=
new
DetachedSong
(
dest
->
GetURI
());
merge_song_metadata
(
dest
,
src
);
merge_song_metadata
(
*
tmp
,
*
dest
,
src
);
if
(
dest
->
GetTag
().
IsDefined
()
&&
dest
->
GetTag
().
time
>
0
&&
if
(
dest
.
GetTag
().
IsDefined
()
&&
dest
.
GetTag
().
time
>
0
&&
src
.
GetStartMS
()
>
0
&&
src
.
GetEndMS
()
==
0
&&
src
.
GetStartMS
()
>
0
&&
src
.
GetEndMS
()
==
0
&&
src
.
GetStartMS
()
/
1000
<
(
unsigned
)
dest
->
GetTag
().
time
)
src
.
GetStartMS
()
/
1000
<
(
unsigned
)
dest
.
GetTag
().
time
)
/* the range is open-ended, and the playlist plugin
/* the range is open-ended, and the playlist plugin
did not know the total length of the song file
did not know the total length of the song file
(e.g. last track on a CUE file); fix it up here */
(e.g. last track on a CUE file); fix it up here */
tmp
->
WritableTag
().
time
=
dest
.
WritableTag
().
time
=
dest
->
GetTag
().
time
-
src
.
GetStartMS
()
/
1000
;
dest
.
GetTag
().
time
-
src
.
GetStartMS
()
/
1000
;
delete
dest
;
return
tmp
;
}
}
static
DetachedSong
*
static
bool
playlist_check_load_song
(
const
DetachedSong
*
song
,
const
char
*
uri
)
playlist_check_load_song
(
DetachedSong
&
song
)
{
{
DetachedSong
*
dest
;
const
char
*
const
uri
=
song
.
GetURI
()
;
if
(
uri_has_scheme
(
uri
))
{
if
(
uri_has_scheme
(
uri
))
{
dest
=
new
DetachedSong
(
uri
)
;
return
true
;
}
else
if
(
PathTraitsUTF8
::
IsAbsolute
(
uri
))
{
}
else
if
(
PathTraitsUTF8
::
IsAbsolute
(
uri
))
{
dest
=
new
DetachedSong
(
uri
);
DetachedSong
tmp
(
uri
);
if
(
!
dest
->
Update
())
{
if
(
!
tmp
.
Update
())
delete
dest
;
return
false
;
return
nullptr
;
}
apply_song_metadata
(
song
,
tmp
);
return
true
;
}
else
{
}
else
{
dest
=
DatabaseDetachSong
(
uri
,
IgnoreError
());
DetachedSong
*
tmp
=
DatabaseDetachSong
(
uri
,
IgnoreError
());
if
(
dest
==
nullptr
)
if
(
tmp
==
nullptr
)
return
nullptr
;
return
false
;
}
return
apply_song_metadata
(
dest
,
*
song
);
apply_song_metadata
(
song
,
*
tmp
);
delete
tmp
;
return
true
;
}
}
}
DetachedSong
*
bool
playlist_check_translate_song
(
DetachedSong
*
song
,
const
char
*
base_uri
,
playlist_check_translate_song
(
DetachedSong
&
song
,
const
char
*
base_uri
,
bool
secure
)
bool
secure
)
{
{
const
char
*
uri
=
song
->
GetURI
();
const
char
*
const
uri
=
song
.
GetURI
();
if
(
uri_has_scheme
(
uri
))
{
if
(
uri_has_scheme
(
uri
))
if
(
uri_supported_scheme
(
uri
))
/* valid remote song? */
/* valid remote song */
return
uri_supported_scheme
(
uri
);
return
song
;
else
{
/* unsupported remote song */
delete
song
;
return
nullptr
;
}
}
if
(
base_uri
!=
nullptr
&&
strcmp
(
base_uri
,
"."
)
==
0
)
if
(
base_uri
!=
nullptr
&&
strcmp
(
base_uri
,
"."
)
==
0
)
/* PathTraitsUTF8::GetParent() returns "." when there
/* PathTraitsUTF8::GetParent() returns "." when there
...
@@ -125,25 +112,20 @@ playlist_check_translate_song(DetachedSong *song, const char *base_uri,
...
@@ -125,25 +112,20 @@ playlist_check_translate_song(DetachedSong *song, const char *base_uri,
assert
(
suffix
!=
nullptr
);
assert
(
suffix
!=
nullptr
);
if
(
suffix
!=
uri
)
if
(
suffix
!=
uri
)
uri
=
suffix
;
song
.
SetURI
(
std
::
string
(
suffix
))
;
else
if
(
!
secure
)
{
else
if
(
!
secure
)
/* local files must be relative to the music
/* local files must be relative to the music
directory when "secure" is enabled */
directory when "secure" is enabled */
delete
song
;
return
false
;
return
nullptr
;
}
base_uri
=
nullptr
;
base_uri
=
nullptr
;
}
}
if
(
base_uri
!=
nullptr
)
{
if
(
base_uri
!=
nullptr
)
{
song
->
SetURI
(
PathTraitsUTF8
::
Build
(
base_uri
,
uri
));
song
.
SetURI
(
PathTraitsUTF8
::
Build
(
base_uri
,
uri
));
/* repeat the above checks */
/* repeat the above checks */
return
playlist_check_translate_song
(
song
,
nullptr
,
secure
);
return
playlist_check_translate_song
(
song
,
nullptr
,
secure
);
}
}
DetachedSong
*
dest
=
playlist_check_load_song
(
song
,
uri
);
return
playlist_check_load_song
(
song
);
delete
song
;
return
dest
;
}
}
src/PlaylistSong.hxx
View file @
4f120f37
...
@@ -23,15 +23,15 @@
...
@@ -23,15 +23,15 @@
class
DetachedSong
;
class
DetachedSong
;
/**
/**
* Verifies the song, returns nullptr if it is unsafe. Translate the
* Verifies the song, returns false if it is unsafe. Translate the
* song to a new song object within the database, if it is a local
* song to a song within the database, if it is a local file.
* file. The old song object is freed.
*
*
* @param secure if true, then local files are only allowed if they
* @param secure if true, then local files are only allowed if they
* are relative to base_uri
* are relative to base_uri
* @return true on success, false if the song should not be used
*/
*/
DetachedSong
*
bool
playlist_check_translate_song
(
DetachedSong
*
song
,
const
char
*
base_uri
,
playlist_check_translate_song
(
DetachedSong
&
song
,
const
char
*
base_uri
,
bool
secure
);
bool
secure
);
#endif
#endif
test/test_translate_song.cxx
View file @
4f120f37
...
@@ -186,15 +186,6 @@ ToString(const DetachedSong &song)
...
@@ -186,15 +186,6 @@ ToString(const DetachedSong &song)
return
result
;
return
result
;
}
}
static
std
::
string
ToString
(
const
DetachedSong
*
song
)
{
if
(
song
==
nullptr
)
return
"nullptr"
;
return
ToString
(
*
song
);
}
class
TranslateSongTest
:
public
CppUnit
::
TestFixture
{
class
TranslateSongTest
:
public
CppUnit
::
TestFixture
{
CPPUNIT_TEST_SUITE
(
TranslateSongTest
);
CPPUNIT_TEST_SUITE
(
TranslateSongTest
);
CPPUNIT_TEST
(
TestAbsoluteURI
);
CPPUNIT_TEST
(
TestAbsoluteURI
);
...
@@ -205,76 +196,68 @@ class TranslateSongTest : public CppUnit::TestFixture {
...
@@ -205,76 +196,68 @@ class TranslateSongTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE_END
();
CPPUNIT_TEST_SUITE_END
();
void
TestAbsoluteURI
()
{
void
TestAbsoluteURI
()
{
auto
song1
=
new
DetachedSong
(
"http://example.com/foo.ogg"
);
DetachedSong
song1
(
"http://example.com/foo.ogg"
);
auto
song2
=
playlist_check_translate_song
(
song1
,
"/ignored"
,
false
);
auto
se
=
ToString
(
song1
);
CPPUNIT_ASSERT_EQUAL
(
song1
,
song2
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song1
,
"/ignored"
,
false
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song1
));
}
}
void
TestInsecure
()
{
void
TestInsecure
()
{
/* illegal because secure=false */
/* illegal because secure=false */
auto
song1
=
new
DetachedSong
(
uri1
);
DetachedSong
song1
(
uri1
);
auto
song2
=
playlist_check_translate_song
(
song1
,
nullptr
,
false
);
CPPUNIT_ASSERT
(
!
playlist_check_translate_song
(
song1
,
nullptr
,
false
));
CPPUNIT_ASSERT_EQUAL
((
DetachedSong
*
)
nullptr
,
song2
);
}
}
void
TestSecure
()
{
void
TestSecure
()
{
auto
song1
=
new
DetachedSong
(
uri1
,
MakeTag1b
());
DetachedSong
song1
(
uri1
,
MakeTag1b
());
auto
s1
=
ToString
(
song1
);
auto
s1
=
ToString
(
song1
);
auto
se
=
ToString
(
DetachedSong
(
uri1
,
MakeTag1c
()));
auto
se
=
ToString
(
DetachedSong
(
uri1
,
MakeTag1c
()));
auto
song2
=
playlist_check_translate_song
(
song1
,
"/ignored"
,
true
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song1
,
"/ignored"
,
true
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song1
));
delete
song2
;
}
}
void
TestInDatabase
()
{
void
TestInDatabase
()
{
auto
song1
=
new
DetachedSong
(
"doesntexist"
);
DetachedSong
song1
(
"doesntexist"
);
auto
song2
=
playlist_check_translate_song
(
song1
,
nullptr
,
false
);
CPPUNIT_ASSERT
(
!
playlist_check_translate_song
(
song1
,
nullptr
,
false
));
CPPUNIT_ASSERT_EQUAL
((
DetachedSong
*
)
nullptr
,
song2
);
song1
=
new
DetachedSong
(
uri2
,
MakeTag2b
());
DetachedSong
song2
(
uri2
,
MakeTag2b
());
auto
s1
=
ToString
(
song
1
);
auto
s1
=
ToString
(
song
2
);
auto
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
auto
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
song2
=
playlist_check_translate_song
(
song1
,
nullptr
,
false
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song2
,
nullptr
,
false
)
);
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
delete
song2
;
song1
=
new
DetachedSong
(
"/music/foo/bar.ogg"
,
MakeTag2b
());
DetachedSong
song3
(
"/music/foo/bar.ogg"
,
MakeTag2b
());
s1
=
ToString
(
song
1
);
s1
=
ToString
(
song
3
);
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
song2
=
playlist_check_translate_song
(
song1
,
nullptr
,
false
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song3
,
nullptr
,
false
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song3
));
delete
song2
;
}
}
void
TestRelative
()
{
void
TestRelative
()
{
/* map to music_directory */
/* map to music_directory */
auto
song1
=
new
DetachedSong
(
"bar.ogg"
,
MakeTag2b
());
DetachedSong
song1
(
"bar.ogg"
,
MakeTag2b
());
auto
s1
=
ToString
(
song1
);
auto
s1
=
ToString
(
song1
);
auto
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
auto
se
=
ToString
(
DetachedSong
(
uri2
,
MakeTag2c
()));
auto
song2
=
playlist_check_translate_song
(
song1
,
"/music/foo"
,
false
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song1
,
"/music/foo"
,
false
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song1
));
delete
song2
;
/* illegal because secure=false */
/* illegal because secure=false */
song1
=
new
DetachedSong
(
"bar.ogg"
,
MakeTag2b
());
DetachedSong
song2
(
"bar.ogg"
,
MakeTag2b
());
song2
=
playlist_check_translate_song
(
song1
,
"/foo"
,
false
);
CPPUNIT_ASSERT
(
!
playlist_check_translate_song
(
song1
,
"/foo"
,
false
));
CPPUNIT_ASSERT_EQUAL
((
DetachedSong
*
)
nullptr
,
song2
);
/* legal because secure=true */
/* legal because secure=true */
song1
=
new
DetachedSong
(
"bar.ogg"
,
MakeTag1b
());
DetachedSong
song3
(
"bar.ogg"
,
MakeTag1b
());
s1
=
ToString
(
song
2
);
s1
=
ToString
(
song
3
);
se
=
ToString
(
DetachedSong
(
uri1
,
MakeTag1c
()));
se
=
ToString
(
DetachedSong
(
uri1
,
MakeTag1c
()));
song2
=
playlist_check_translate_song
(
song1
,
"/foo"
,
true
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song3
,
"/foo"
,
true
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song3
));
delete
song2
;
/* relative to http:// */
/* relative to http:// */
song1
=
new
DetachedSong
(
"bar.ogg"
,
MakeTag2a
());
DetachedSong
song4
(
"bar.ogg"
,
MakeTag2a
());
s1
=
ToString
(
song
1
);
s1
=
ToString
(
song
4
);
se
=
ToString
(
DetachedSong
(
"http://example.com/foo/bar.ogg"
,
MakeTag2a
()));
se
=
ToString
(
DetachedSong
(
"http://example.com/foo/bar.ogg"
,
MakeTag2a
()));
song2
=
playlist_check_translate_song
(
song1
,
"http://example.com/foo"
,
false
);
CPPUNIT_ASSERT
(
playlist_check_translate_song
(
song4
,
"http://example.com/foo"
,
false
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song2
));
CPPUNIT_ASSERT_EQUAL
(
se
,
ToString
(
song4
));
delete
song2
;
}
}
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment