PlaylistFile: use DirectoryReader and file system API
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
#include "Idle.hxx"
|
#include "Idle.hxx"
|
||||||
#include "fs/Path.hxx"
|
#include "fs/Path.hxx"
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
|
#include "fs/DirectoryReader.hxx"
|
||||||
#include "util/UriUtil.hxx"
|
#include "util/UriUtil.hxx"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -77,7 +78,7 @@ spl_valid_name(const char *name_utf8)
|
|||||||
strchr(name_utf8, '\r') == NULL;
|
strchr(name_utf8, '\r') == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const Path &
|
||||||
spl_map(GError **error_r)
|
spl_map(GError **error_r)
|
||||||
{
|
{
|
||||||
const Path &path_fs = map_spl_path();
|
const Path &path_fs = map_spl_path();
|
||||||
@@ -85,8 +86,7 @@ spl_map(GError **error_r)
|
|||||||
g_set_error_literal(error_r, playlist_quark(),
|
g_set_error_literal(error_r, playlist_quark(),
|
||||||
PLAYLIST_RESULT_DISABLED,
|
PLAYLIST_RESULT_DISABLED,
|
||||||
"Stored playlists are disabled");
|
"Stored playlists are disabled");
|
||||||
|
return path_fs;
|
||||||
return path_fs.c_str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@@ -105,8 +105,7 @@ spl_check_name(const char *name_utf8, GError **error_r)
|
|||||||
static Path
|
static Path
|
||||||
spl_map_to_fs(const char *name_utf8, GError **error_r)
|
spl_map_to_fs(const char *name_utf8, GError **error_r)
|
||||||
{
|
{
|
||||||
if (spl_map(error_r) == NULL ||
|
if (spl_map(error_r).IsNull() || !spl_check_name(name_utf8, error_r))
|
||||||
!spl_check_name(name_utf8, error_r))
|
|
||||||
return Path::Null();
|
return Path::Null();
|
||||||
|
|
||||||
Path path_fs = map_spl_utf8_to_fs(name_utf8);
|
Path path_fs = map_spl_utf8_to_fs(name_utf8);
|
||||||
@@ -139,25 +138,24 @@ playlist_errno(GError **error_r)
|
|||||||
|
|
||||||
static bool
|
static bool
|
||||||
LoadPlaylistFileInfo(PlaylistInfo &info,
|
LoadPlaylistFileInfo(PlaylistInfo &info,
|
||||||
const char *parent_path_fs, const char *name_fs)
|
const Path &parent_path_fs, const Path &name_fs)
|
||||||
{
|
{
|
||||||
size_t name_length = strlen(name_fs);
|
const char *name_fs_str = name_fs.c_str();
|
||||||
|
size_t name_length = strlen(name_fs_str);
|
||||||
|
|
||||||
if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) ||
|
if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) ||
|
||||||
memchr(name_fs, '\n', name_length) != NULL)
|
memchr(name_fs_str, '\n', name_length) != NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!g_str_has_suffix(name_fs, PLAYLIST_FILE_SUFFIX))
|
if (!g_str_has_suffix(name_fs_str, PLAYLIST_FILE_SUFFIX))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
char *path_fs = g_build_filename(parent_path_fs, name_fs, NULL);
|
Path path_fs = Path::Build(parent_path_fs, name_fs);
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int ret = stat(path_fs, &st);
|
if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode))
|
||||||
g_free(path_fs);
|
|
||||||
if (ret < 0 || !S_ISREG(st.st_mode))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
char *name = g_strndup(name_fs,
|
char *name = g_strndup(name_fs_str,
|
||||||
name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX));
|
name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX));
|
||||||
std::string name_utf8 = Path::ToUTF8(name);
|
std::string name_utf8 = Path::ToUTF8(name);
|
||||||
g_free(name);
|
g_free(name);
|
||||||
@@ -174,24 +172,23 @@ ListPlaylistFiles(GError **error_r)
|
|||||||
{
|
{
|
||||||
PlaylistVector list;
|
PlaylistVector list;
|
||||||
|
|
||||||
const char *parent_path_fs = spl_map(error_r);
|
const Path &parent_path_fs = spl_map(error_r);
|
||||||
if (parent_path_fs == NULL)
|
if (parent_path_fs.IsNull())
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
DIR *dir = opendir(parent_path_fs);
|
DirectoryReader reader(parent_path_fs);
|
||||||
if (dir == NULL) {
|
if (reader.HasFailed()) {
|
||||||
set_error_errno(error_r);
|
set_error_errno(error_r);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlaylistInfo info;
|
PlaylistInfo info;
|
||||||
struct dirent *ent;
|
while (reader.ReadEntry()) {
|
||||||
while ((ent = readdir(dir)) != NULL) {
|
const Path entry = reader.GetEntry();
|
||||||
if (LoadPlaylistFileInfo(info, parent_path_fs, ent->d_name))
|
if (LoadPlaylistFileInfo(info, parent_path_fs, entry))
|
||||||
list.push_back(std::move(info));
|
list.push_back(std::move(info));
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +198,7 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
|
|||||||
{
|
{
|
||||||
assert(utf8path != NULL);
|
assert(utf8path != NULL);
|
||||||
|
|
||||||
if (spl_map(error_r) == NULL)
|
if (spl_map(error_r).IsNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
||||||
@@ -226,7 +223,7 @@ LoadPlaylistFile(const char *utf8path, GError **error_r)
|
|||||||
{
|
{
|
||||||
PlaylistFileContents contents;
|
PlaylistFileContents contents;
|
||||||
|
|
||||||
if (spl_map(error_r) == NULL)
|
if (spl_map(error_r).IsNull())
|
||||||
return contents;
|
return contents;
|
||||||
|
|
||||||
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
||||||
@@ -302,7 +299,7 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest,
|
|||||||
bool
|
bool
|
||||||
spl_clear(const char *utf8path, GError **error_r)
|
spl_clear(const char *utf8path, GError **error_r)
|
||||||
{
|
{
|
||||||
if (spl_map(error_r) == NULL)
|
if (spl_map(error_r).IsNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
||||||
@@ -365,7 +362,7 @@ spl_remove_index(const char *utf8path, unsigned pos, GError **error_r)
|
|||||||
bool
|
bool
|
||||||
spl_append_song(const char *utf8path, struct song *song, GError **error_r)
|
spl_append_song(const char *utf8path, struct song *song, GError **error_r)
|
||||||
{
|
{
|
||||||
if (spl_map(error_r) == NULL)
|
if (spl_map(error_r).IsNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
const Path path_fs = spl_map_to_fs(utf8path, error_r);
|
||||||
@@ -454,7 +451,7 @@ spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs,
|
|||||||
bool
|
bool
|
||||||
spl_rename(const char *utf8from, const char *utf8to, GError **error_r)
|
spl_rename(const char *utf8from, const char *utf8to, GError **error_r)
|
||||||
{
|
{
|
||||||
if (spl_map(error_r) == NULL)
|
if (spl_map(error_r).IsNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Path from_path_fs = spl_map_to_fs(utf8from, error_r);
|
Path from_path_fs = spl_map_to_fs(utf8from, error_r);
|
||||||
|
Reference in New Issue
Block a user