1
0
Fork 0
forked from len0rd/rockbox

playlist: Add helpers for opening/closing .m3u playlist

This helps maintainability and ensures the opening is done
consistently.

Change-Id: I7805716182dfa93f917d54d40f8e420e87da16b0
This commit is contained in:
Aidan MacDonald 2023-03-18 17:00:07 +00:00
parent 5eb24a9582
commit e6534b051e

View file

@ -252,6 +252,40 @@ static void dc_thread_stop(struct playlist_info *playlist)
#endif
}
/*
* Open playlist file and return file descriptor or -1 on error.
* The fd should not be closed manually. Not thread-safe.
*/
static int pl_open_playlist(struct playlist_info *playlist)
{
if (playlist->fd >= 0)
return playlist->fd;
int fd = open_utf8(playlist->filename, O_RDONLY);
if (fd < 0)
return fd;
/* Presence of UTF-8 BOM forces UTF-8 encoding. */
if (lseek(fd, 0, SEEK_CUR) > 0)
playlist->utf8 = true;
playlist->fd = fd;
return fd;
}
/*
* Close any open file descriptor for the playlist file.
* Not thread-safe.
*/
static void pl_close_playlist(struct playlist_info *playlist)
{
if (playlist->fd >= 0)
{
close(playlist->fd);
playlist->fd = -1;
}
}
static void close_playlist_control_file(struct playlist_info *playlist)
{
playlist_write_lock(playlist);
@ -556,10 +590,7 @@ static void update_playlist_filename_unlocked(struct playlist_info* playlist,
*/
static void empty_playlist_unlocked(struct playlist_info* playlist, bool resume)
{
if(playlist->fd >= 0) /* If there is an already open playlist, close it. */
{
close(playlist->fd);
}
pl_close_playlist(playlist);
if(playlist->control_fd >= 0)
{
@ -578,7 +609,6 @@ static void empty_playlist_unlocked(struct playlist_info* playlist, bool resume)
playlist->in_ram = false;
playlist->modified = false;
playlist->fd = -1;
playlist->control_fd = -1;
playlist->index = 0;
@ -868,14 +898,8 @@ static int add_indices_to_playlist(struct playlist_info* playlist,
playlist_write_lock(playlist);
if(-1 == playlist->fd)
{
playlist->fd = open_utf8(playlist->filename, O_RDONLY);
if(playlist->fd >= 0 && lseek(playlist->fd, 0, SEEK_CUR) > 0)
playlist->utf8 = true; /* Override any earlier indication. */
}
if(playlist->fd < 0)
/* Open playlist file for reading */
if (pl_open_playlist(playlist) < 0)
{
result = -1;
goto exit;
@ -1244,10 +1268,7 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see
}
else
{
if(-1 == playlist->fd)
playlist->fd = open(playlist->filename, O_RDONLY);
fd = playlist->fd;
fd = pl_open_playlist(playlist);
}
if(-1 != fd)
@ -2340,12 +2361,7 @@ void playlist_close(struct playlist_info* playlist)
if (!playlist)
return;
if (playlist->fd >= 0)
{
close(playlist->fd);
playlist->fd = -1;
}
pl_close_playlist(playlist);
close_playlist_control_file(playlist);
if (playlist->control_created)