summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2023-03-18 17:00:07 +0000
committerAidan MacDonald <amachronic@protonmail.com>2023-03-24 06:36:23 -0400
commite6534b051ec78475548d0392ad1b76c7391b5b6f (patch)
tree48f89ca01ee3faad8b399011f04fa8a172d269d5
parent5eb24a9582eba2b9036e3f849472bfa0950c7277 (diff)
downloadrockbox-e6534b051e.tar.gz
rockbox-e6534b051e.zip
playlist: Add helpers for opening/closing .m3u playlist
This helps maintainability and ensures the opening is done consistently. Change-Id: I7805716182dfa93f917d54d40f8e420e87da16b0
-rw-r--r--apps/playlist.c62
1 files changed, 39 insertions, 23 deletions
diff --git a/apps/playlist.c b/apps/playlist.c
index eef6321f19..988925b8fd 100644
--- a/apps/playlist.c
+++ b/apps/playlist.c
@@ -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)