Skip to content

Commit

Permalink
Merge pull request #3115: support relative paths when loading DBPL pl…
Browse files Browse the repository at this point in the history
…aylists
  • Loading branch information
Oleksiy-Yakovenko committed Sep 7, 2024
2 parents b2f8c99 + 3d0460b commit 6ef4bad
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions Tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ STATIC_DEPS:=$(ORIGIN)/static-deps
STATIC_ROOT:=$(STATIC_DEPS)/lib-x86-64
INCLUDE=-I external/googletest/googletest \
-I external/googletest/googletest/include \
-I external/googletest/googlemock/include \
-I external/mp4p/include \
-I plugins/libparser \
-I shared \
Expand Down
1 change: 1 addition & 0 deletions Tests/AlbumNavigationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class AlbumNavigationTests : public ::testing::Test {
messagepump_free ();
conf_free ();
ddb_logger_free ();
plug_remove_plugin((DB_plugin_t *)_fakeout);
}
playlist_t *plt;
int srs[128];
Expand Down
29 changes: 29 additions & 0 deletions Tests/PlaylistTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "plmeta.h"
#include "plugins.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>

TEST(PlaylistTests, test_SearchForValueInSingleValueItems_FindsTheItem) {
playlist_t *plt = plt_alloc("test");
Expand All @@ -25,6 +26,7 @@ TEST(PlaylistTests, test_SearchForValueInSingleValueItems_FindsTheItem) {
EXPECT_TRUE(plt->head[PL_SEARCH] != NULL);
EXPECT_TRUE(plt->head[PL_MAIN]->selected);

pl_item_unref(it);
plt_unref (plt);
}

Expand All @@ -43,9 +45,36 @@ TEST(PlaylistTests, test_SearchFor2ndValueInMultiValueItems_FindsTheItem) {
EXPECT_TRUE(plt->head[PL_SEARCH] != NULL);
EXPECT_TRUE(plt->head[PL_MAIN]->selected);

pl_item_unref(it);
plt_unref (plt);
}

TEST (PlaylistTests, test_LoadDBPLWithRelativepaths) {
using ::testing::StartsWith;
ddb_playlist_t *plt = deadbeef->plt_alloc ("test");

char dname[PATH_MAX];
snprintf (dname, sizeof (dname), "%s/TestData", dbplugindir);
char fname[] = "RelativePaths.dbpl";
char plt_path[PATH_MAX];
snprintf (plt_path, sizeof (plt_path), "%s/%s", dname, fname);

ddb_playItem_t *after = deadbeef->plt_load2 (-1, plt, NULL, plt_path, NULL, NULL, NULL);

ASSERT_TRUE (after);
EXPECT_EQ (deadbeef->plt_get_item_count (plt, PL_MAIN), 8);

ddb_playItem_t **its;
size_t n_its = deadbeef->plt_get_items (plt, &its);
for (size_t k = 0; k < n_its; k++) {
EXPECT_THAT (deadbeef->pl_find_meta (its[k], ":URI"), StartsWith (dname));
deadbeef->pl_item_unref (its[k]);
}
free (its);

deadbeef->plt_unref (plt);
}

#pragma mark - IsRelativePathPosix

TEST(PlaylistTests, test_IsRelativePathPosix_AbsolutePath_False) {
Expand Down
1 change: 1 addition & 0 deletions Tests/StreamerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class StreamerTests: public ::testing::Test {
streamer_free ();
conf_free();
ddb_logger_free();
plug_remove_plugin((DB_plugin_t *)_fakeout);
}

protected:
Expand Down
Binary file added Tests/TestData/RelativePaths.dbpl
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/TrackSwitchingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class TrackSwitchingTests: public ::testing::Test {
messagepump_free();
conf_free();
ddb_logger_free();
plug_remove_plugin((DB_plugin_t *)_fakeout);
}
DB_output_t *_fakeout;
uintptr_t _mainloop_tid;
Expand Down
2 changes: 2 additions & 0 deletions osx/deadbeef.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22930,6 +22930,7 @@
"$(inherited)",
"$(SRCROOT)/deps/jansson-2.13.1/src",
"$(SRCROOT)/../external/googletest/googletest/include",
"$(SRCROOT)/../external/googletest/googlemock/include",
"\"$(SRCROOT)/../include\"",
"\"$(SRCROOT)/..\"",
"\"$(SRCROOT)/../shared\"",
Expand Down Expand Up @@ -22977,6 +22978,7 @@
"$(inherited)",
"$(SRCROOT)/deps/jansson-2.13.1/src",
"$(SRCROOT)/../external/googletest/googletest/include",
"$(SRCROOT)/../external/googletest/googlemock/include",
"\"$(SRCROOT)/../include\"",
"\"$(SRCROOT)/..\"",
"\"$(SRCROOT)/../shared\"",
Expand Down
39 changes: 33 additions & 6 deletions src/playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2398,12 +2398,26 @@ pl_save_all (void) {
}

static int
_plt_load_from_file (playlist_t *plt, ddb_file_handle_t *fp, playItem_t **last_added) {
_interpret_relative_path(const char *dname, const char *uri, char *true_uri, size_t count) {
if (dname && is_relative_path (uri)) {
return snprintf (true_uri, count, "%s/%s", dname, uri);
} else {
return snprintf (true_uri, count, "%s", uri);
}
}

static int
_plt_load_from_file (playlist_t *plt, const char *fname, ddb_file_handle_t *fp, playItem_t **last_added) {
int result = -1;
playItem_t *it = NULL;
uint8_t majorver;
uint8_t minorver;
char magic[4];

char *dname = NULL;
char *true_uri = calloc(PATH_MAX, sizeof(char));
// must be allocated here to be unconditionally free() at the end of the function

if (ddb_file_read (magic, 1, 4, fp) != 4) {
// trace ("failed to read magic\n");
goto load_fail;
Expand All @@ -2430,7 +2444,12 @@ _plt_load_from_file (playlist_t *plt, ddb_file_handle_t *fp, playItem_t **last_a
if (ddb_file_read (&cnt, 1, 4, fp) != 4) {
goto load_fail;
}


char *slash = strrchr (fname, '/');
if (slash && fname) {
dname = strndup (fname, slash - fname);
}

for (uint32_t i = 0; i < cnt; i++) {
it = pl_item_alloc ();
if (!it) {
Expand All @@ -2448,7 +2467,8 @@ _plt_load_from_file (playlist_t *plt, ddb_file_handle_t *fp, playItem_t **last_a
goto load_fail;
}
uri[l] = 0;
pl_add_meta (it, ":URI", uri);
_interpret_relative_path(dname, uri, true_uri, PATH_MAX);
pl_add_meta (it, ":URI", true_uri);
// decoder
uint8_t ll;
if (ddb_file_read (&ll, 1, 1, fp) != 1) {
Expand Down Expand Up @@ -2594,7 +2614,12 @@ _plt_load_from_file (playlist_t *plt, ddb_file_handle_t *fp, playItem_t **last_a
// some values are stored twice:
// once in legacy format, and once in metadata format
// here, we delete what was set from legacy, and overwrite with metadata
pl_replace_meta (it, key, value);
if (strcmp (key, ":URI") != 0) {
pl_replace_meta (it, key, value);
} else {
_interpret_relative_path(dname, value, true_uri, PATH_MAX);
pl_replace_meta (it, key, true_uri);
}
}
}
else {
Expand Down Expand Up @@ -2655,6 +2680,8 @@ _plt_load_from_file (playlist_t *plt, ddb_file_handle_t *fp, playItem_t **last_a
pl_item_unref (it);
it = NULL;
}
free (dname);
free (true_uri);
return result;
}

Expand Down Expand Up @@ -2739,7 +2766,7 @@ plt_load_int (
ddb_file_handle_t fh;
ddb_file_init_stdio(&fh, fp);

if (0 != _plt_load_from_file(plt, &fh, &last_added)) {
if (0 != _plt_load_from_file(plt, fname, &fh, &last_added)) {
goto load_fail;
}

Expand Down Expand Up @@ -2769,7 +2796,7 @@ plt_load_from_buffer (playlist_t *plt, const uint8_t *buffer, size_t size) {
ddb_file_init_buffer(&fh, buffer, size);

playItem_t *last_added = NULL;
int res = _plt_load_from_file(plt, &fh, &last_added);
int res = _plt_load_from_file(plt, NULL, &fh, &last_added);
if (last_added) {
pl_item_unref (last_added);
}
Expand Down
4 changes: 4 additions & 0 deletions src/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1883,10 +1883,12 @@ void
plug_register_in (DB_plugin_t *inplug) {
int i;
for (i = 0; g_plugins[i]; i++);
assert(i < MAX_PLUGINS);
g_plugins[i++] = inplug;
g_plugins[i] = NULL;

for (i = 0; g_decoder_plugins[i]; i++);
assert(i < MAX_DECODER_PLUGINS);
g_decoder_plugins[i++] = (DB_decoder_t *)inplug;
g_decoder_plugins[i] = NULL;
}
Expand All @@ -1896,10 +1898,12 @@ void
plug_register_out (DB_plugin_t *outplug) {
int i;
for (i = 0; g_plugins[i]; i++);
assert(i < MAX_PLUGINS);
g_plugins[i++] = outplug;
g_plugins[i] = NULL;

for (i = 0; g_output_plugins[i]; i++);
assert(i < MAX_OUTPUT_PLUGINS);
g_output_plugins[i++] = (DB_output_t *)outplug;
g_output_plugins[i] = NULL;
}
Expand Down
3 changes: 3 additions & 0 deletions src/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ plug_init_plugin (DB_plugin_t* (*loadfunc)(DB_functions_t *), void *handle);
const char *
plug_get_path_for_plugin_ptr (DB_plugin_t *plugin_ptr);

void
plug_remove_plugin (void *p);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 6ef4bad

Please sign in to comment.