playlist: Simplify get_track_filename()

Have get_track_filename() read track seek offset & insert type
itself to avoid duplicating code. Most callers are not interested
in that information, and only use it to call get_track_filename().

Change-Id: I46d50fda10e515f74e27eebdded8e281bbf4456f
This commit is contained in:
Aidan MacDonald 2023-03-30 10:29:04 +01:00 committed by Solomon Peachy
parent 9ba51e3552
commit 3b1e95d480

View file

@ -1100,8 +1100,8 @@ static int get_next_dir(char *dir, int direction)
/*
* gets pathname for track at seek index
*/
static int get_track_filename(struct playlist_info* playlist, int index, int seek,
bool control_file, char *buf, int buf_length)
static int get_track_filename(struct playlist_info* playlist, int index,
char *buf, int buf_length)
{
int fd;
int max = -1;
@ -1109,8 +1109,10 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
char dir_buf[MAX_PATH+1];
bool utf8 = playlist->utf8;
if (buf_length > MAX_PATH+1)
buf_length = MAX_PATH+1;
playlist_write_lock(playlist);
bool control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
unsigned long seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
#ifdef HAVE_DIRCACHE
if (playlist->dcfrefs_handle)
@ -1126,8 +1128,6 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
if (max < 0)
{
playlist_write_lock(playlist);
if (control_file)
{
fd = playlist->control_fd;
@ -1140,7 +1140,7 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
if(-1 != fd)
{
if (lseek(fd, seek, SEEK_SET) != seek)
if (lseek(fd, seek, SEEK_SET) != (off_t)seek)
max = -1;
else
{
@ -1163,10 +1163,10 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
}
}
playlist_write_unlock(playlist);
if (max < 0)
{
playlist_write_unlock(playlist);
if (usb_detect() == USB_INSERTED)
; /* ignore error on usb plug */
else if (control_file)
@ -1179,10 +1179,12 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
}
strmemccpy(dir_buf, playlist->filename, playlist->dirlen);
playlist_write_unlock(playlist);
return format_track_path(buf, tmp_buf, buf_length, dir_buf);
if (format_track_path(buf, tmp_buf, buf_length, dir_buf) < 0)
return -1;
(void)index;
return 0;
}
/*
@ -1811,8 +1813,6 @@ static void dc_thread_playlist(void)
struct playlist_info *playlist = &current_playlist;
struct dircache_fileref *dcfrefs;
int index;
int seek;
bool control_file;
/* Thread starts out stopped */
long sleep_time = TIMEOUT_BLOCK;
@ -1890,12 +1890,8 @@ static void dc_thread_playlist(void)
break;
}
control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
/* Load the filename from playlist file. */
if (get_track_filename(playlist, index, seek,
control_file, tmp, sizeof(tmp)) < 0)
if (get_track_filename(playlist, index, tmp, sizeof(tmp)))
break;
/* Obtain the dircache file entry cookie. */
@ -2416,31 +2412,24 @@ int playlist_get_seed(const struct playlist_info* playlist)
int playlist_get_track_info(struct playlist_info* playlist, int index,
struct playlist_track_info* info)
{
int seek;
bool control_file;
if (!playlist)
playlist = &current_playlist;
if (index < 0 || index >= playlist->amount)
return -1;
control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
if (get_track_filename(playlist, index, seek, control_file,
info->filename, sizeof(info->filename)) < 0)
if (get_track_filename(playlist, index,
info->filename, sizeof(info->filename)))
return -1;
info->attr = 0;
if (control_file)
if (playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK)
{
if (playlist->indices[index] & PLAYLIST_QUEUE_MASK)
info->attr |= PLAYLIST_ATTR_QUEUED;
else
info->attr |= PLAYLIST_ATTR_INSERTED;
}
if (playlist->indices[index] & PLAYLIST_SKIPPED)
@ -2720,8 +2709,6 @@ void playlist_set_modified(struct playlist_info *playlist, bool modified)
int playlist_move(struct playlist_info* playlist, int index, int new_index)
{
int result = -1;
int seek;
bool control_file;
bool queue;
bool current = false;
int r;
@ -2778,15 +2765,10 @@ int playlist_move(struct playlist_info* playlist, int index, int new_index)
}
}
control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
queue = playlist->indices[index] & PLAYLIST_QUEUE_MASK;
seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
if (get_track_filename(playlist, index, seek,
control_file, filename, sizeof(filename)) < 0)
{
if (get_track_filename(playlist, index, filename, sizeof(filename)))
goto out;
}
/* We want to insert the track at the position that was specified by
new_index. This may be different then new_index because of the
@ -2987,10 +2969,8 @@ bool playlist_next_dir(int direction)
const char* playlist_peek(int steps, char* buf, size_t buf_size)
{
struct playlist_info* playlist = &current_playlist;
int seek;
char *temp_ptr;
int index;
bool control_file;
index = get_next_index(playlist, steps, -1);
if (index < 0)
@ -3000,11 +2980,7 @@ const char* playlist_peek(int steps, char* buf, size_t buf_size)
if (!buf || !buf_size)
return "";
control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
if (get_track_filename(playlist, index, seek,
control_file, buf, buf_size) < 0)
if (get_track_filename(playlist, index, buf, buf_size))
return NULL;
temp_ptr = buf;
@ -3620,10 +3596,6 @@ int playlist_save(struct playlist_info* playlist, char *filename,
index = playlist->first_index;
for (i=0; i<playlist->amount; i++)
{
bool control_file;
bool queue;
int seek;
/* user abort */
if (action_userabort(TIMEOUT_NOBLOCK))
{
@ -3631,15 +3603,10 @@ int playlist_save(struct playlist_info* playlist, char *filename,
break;
}
control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK;
queue = playlist->indices[index] & PLAYLIST_QUEUE_MASK;
seek = playlist->indices[index] & PLAYLIST_SEEK_MASK;
/* Don't save queued files */
if (!queue)
if (!(playlist->indices[index] & PLAYLIST_QUEUE_MASK))
{
if (get_track_filename(playlist, index, seek,
control_file, tmp_buf, MAX_PATH+1) < 0)
if (get_track_filename(playlist, index, tmp_buf, sizeof(tmp_buf)))
{
result = -1;
break;