From 51148279373fd903c87536362540f89097bef7f3 Mon Sep 17 00:00:00 2001 From: Christian Soffke Date: Tue, 14 Nov 2023 01:18:39 +0100 Subject: Fix: Rotate indices after saving playlist w/ first_index > 0 When saving the current playlist, entries written out to disk were rotated, but its indices were not, resulting in first_index being out of sync between the two. Thus, an incorrect index was used for any playlist commands, from the moment you saved the playlist until the next time you resumed, which didn't produce the right playlist. Change-Id: Ie4b02ce9e07e565b1b15c938cc4b820db08e8f1f --- apps/playlist.c | 40 ++++++++++++++++++++++++++++++++++------ apps/root_menu.c | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/apps/playlist.c b/apps/playlist.c index 503e4a28d1..67d59d1aac 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -3826,6 +3826,26 @@ error: return err; } +static void pl_reverse(struct playlist_info *playlist, int start, int end) +{ + for (; start < end; start++, end--) + { + unsigned long index_swap = playlist->indices[start]; + playlist->indices[start] = playlist->indices[end]; + playlist->indices[end] = index_swap; + +#ifdef HAVE_DIRCACHE + if (playlist->dcfrefs_handle) + { + struct dircache_fileref *dcfrefs = core_get_data(playlist->dcfrefs_handle); + struct dircache_fileref dcf_swap = dcfrefs[start]; + dcfrefs[start] = dcfrefs[end]; + dcfrefs[end] = dcf_swap; + } +#endif + } +} + /* * Update the control file after saving the playlist under a new name. * A new control file is generated, containing the new playlist filename. @@ -3838,7 +3858,7 @@ error: static int pl_save_update_control(struct playlist_info* playlist, char *tmpbuf, size_t tmpsize) { - int old_fd, index; + int old_fd; int err; char c; bool any_queued = false; @@ -3878,12 +3898,20 @@ static int pl_save_update_control(struct playlist_info* playlist, if (err <= 0) return -4; - index = playlist->first_index; - for (int i = 0; i < playlist->amount; ++i, ++index) + if (playlist->first_index > 0) { - if (index == playlist->amount) - index = 0; + /* rotate indices so they'll be in sync with new control file */ + pl_reverse(playlist, 0, playlist->amount - 1); + pl_reverse(playlist, 0, playlist->amount - playlist->first_index - 1); + pl_reverse(playlist, playlist->amount - playlist->first_index, playlist->amount - 1); + playlist->index = rotate_index(playlist, playlist->index); + playlist->last_insert_pos = rotate_index(playlist, playlist->last_insert_pos); + playlist->first_index = 0; + } + + for (int index = 0; index < playlist->amount; ++index) + { /* We only need to update queued files */ if (!(playlist->indices[index] & PLAYLIST_QUEUED)) continue; @@ -3895,7 +3923,7 @@ static int pl_save_update_control(struct playlist_info* playlist, /* Write it out to the new control file */ int seekpos; err = update_control_unlocked(playlist, PLAYLIST_COMMAND_QUEUE, - i, playlist->last_insert_pos, + index, playlist->last_insert_pos, tmpbuf, NULL, &seekpos); if (err <= 0) return -5; diff --git a/apps/root_menu.c b/apps/root_menu.c index 71753f27c4..762f5b2961 100644 --- a/apps/root_menu.c +++ b/apps/root_menu.c @@ -330,7 +330,7 @@ static int wpsscrn(void* param) } else if ( global_status.resume_index != -1 ) { - DEBUGF("Resume index %X crc32 %lX offset %lX\n", + DEBUGF("Resume index %d crc32 %lX offset %lX\n", global_status.resume_index, (unsigned long)global_status.resume_crc32, (unsigned long)global_status.resume_offset); -- cgit v1.2.3