PlaylistFile: use DirectoryReader and file system API

This commit is contained in:
Denis Krjuchkov 2013-05-05 16:12:35 +06:00
parent a688745bdc
commit 1d9b84a5af
1 changed files with 24 additions and 27 deletions

View File

@ -32,6 +32,7 @@
#include "Idle.hxx"
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx"
#include "util/UriUtil.hxx"
#include <assert.h>
@ -77,7 +78,7 @@ spl_valid_name(const char *name_utf8)
strchr(name_utf8, '\r') == NULL;
}
static const char *
static const Path &
spl_map(GError **error_r)
{
const Path &path_fs = map_spl_path();
@ -85,8 +86,7 @@ spl_map(GError **error_r)
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_DISABLED,
"Stored playlists are disabled");
return path_fs.c_str();
return path_fs;
}
static bool
@ -105,8 +105,7 @@ spl_check_name(const char *name_utf8, GError **error_r)
static Path
spl_map_to_fs(const char *name_utf8, GError **error_r)
{
if (spl_map(error_r) == NULL ||
!spl_check_name(name_utf8, error_r))
if (spl_map(error_r).IsNull() || !spl_check_name(name_utf8, error_r))
return Path::Null();
Path path_fs = map_spl_utf8_to_fs(name_utf8);
@ -139,25 +138,24 @@ playlist_errno(GError **error_r)
static bool
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) ||
memchr(name_fs, '\n', name_length) != NULL)
memchr(name_fs_str, '\n', name_length) != NULL)
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;
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;
int ret = stat(path_fs, &st);
g_free(path_fs);
if (ret < 0 || !S_ISREG(st.st_mode))
if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode))
return false;
char *name = g_strndup(name_fs,
char *name = g_strndup(name_fs_str,
name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX));
std::string name_utf8 = Path::ToUTF8(name);
g_free(name);
@ -174,24 +172,23 @@ ListPlaylistFiles(GError **error_r)
{
PlaylistVector list;
const char *parent_path_fs = spl_map(error_r);
if (parent_path_fs == NULL)
const Path &parent_path_fs = spl_map(error_r);
if (parent_path_fs.IsNull())
return list;
DIR *dir = opendir(parent_path_fs);
if (dir == NULL) {
DirectoryReader reader(parent_path_fs);
if (reader.HasFailed()) {
set_error_errno(error_r);
return list;
}
PlaylistInfo info;
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
if (LoadPlaylistFileInfo(info, parent_path_fs, ent->d_name))
while (reader.ReadEntry()) {
const Path entry = reader.GetEntry();
if (LoadPlaylistFileInfo(info, parent_path_fs, entry))
list.push_back(std::move(info));
}
closedir(dir);
return list;
}
@ -201,7 +198,7 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
{
assert(utf8path != NULL);
if (spl_map(error_r) == NULL)
if (spl_map(error_r).IsNull())
return false;
const Path path_fs = spl_map_to_fs(utf8path, error_r);
@ -226,7 +223,7 @@ LoadPlaylistFile(const char *utf8path, GError **error_r)
{
PlaylistFileContents contents;
if (spl_map(error_r) == NULL)
if (spl_map(error_r).IsNull())
return contents;
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
spl_clear(const char *utf8path, GError **error_r)
{
if (spl_map(error_r) == NULL)
if (spl_map(error_r).IsNull())
return false;
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
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;
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
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;
Path from_path_fs = spl_map_to_fs(utf8from, error_r);