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
This commit is contained in:
Christian Soffke 2023-11-14 01:18:39 +01:00
parent bd93f9f96f
commit 5114827937
2 changed files with 35 additions and 7 deletions

View file

@ -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;

View file

@ -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);