fs/DirectoryReader: use C++ exceptions instead of class Error
This commit is contained in:
parent
826a654c95
commit
f3503e0026
@ -600,7 +600,7 @@ libfs_a_SOURCES = \
|
|||||||
src/fs/Glob.hxx \
|
src/fs/Glob.hxx \
|
||||||
src/fs/StandardDirectory.cxx src/fs/StandardDirectory.hxx \
|
src/fs/StandardDirectory.cxx src/fs/StandardDirectory.hxx \
|
||||||
src/fs/CheckFile.cxx src/fs/CheckFile.hxx \
|
src/fs/CheckFile.cxx src/fs/CheckFile.hxx \
|
||||||
src/fs/DirectoryReader.hxx
|
src/fs/DirectoryReader.cxx src/fs/DirectoryReader.hxx
|
||||||
libfs_a_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS)
|
libfs_a_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS)
|
||||||
|
|
||||||
if ENABLE_ZLIB
|
if ENABLE_ZLIB
|
||||||
|
@ -174,7 +174,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
|
|||||||
}
|
}
|
||||||
|
|
||||||
PlaylistVector
|
PlaylistVector
|
||||||
ListPlaylistFiles(Error &error)
|
ListPlaylistFiles()
|
||||||
{
|
{
|
||||||
PlaylistVector list;
|
PlaylistVector list;
|
||||||
|
|
||||||
@ -182,10 +182,6 @@ ListPlaylistFiles(Error &error)
|
|||||||
assert(!parent_path_fs.IsNull());
|
assert(!parent_path_fs.IsNull());
|
||||||
|
|
||||||
DirectoryReader reader(parent_path_fs);
|
DirectoryReader reader(parent_path_fs);
|
||||||
if (reader.HasFailed()) {
|
|
||||||
error.SetErrno();
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
PlaylistInfo info;
|
PlaylistInfo info;
|
||||||
while (reader.ReadEntry()) {
|
while (reader.ReadEntry()) {
|
||||||
|
@ -50,11 +50,10 @@ AllocatedPath
|
|||||||
spl_map_to_fs(const char *name_utf8);
|
spl_map_to_fs(const char *name_utf8);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of stored_playlist_info struct pointers. Returns
|
* Returns a list of stored_playlist_info struct pointers.
|
||||||
* nullptr if an error occurred.
|
|
||||||
*/
|
*/
|
||||||
PlaylistVector
|
PlaylistVector
|
||||||
ListPlaylistFiles(Error &error);
|
ListPlaylistFiles();
|
||||||
|
|
||||||
PlaylistFileContents
|
PlaylistFileContents
|
||||||
LoadPlaylistFile(const char *utf8path);
|
LoadPlaylistFile(const char *utf8path);
|
||||||
|
@ -70,15 +70,9 @@ skip_path(Path name_fs)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
CommandResult
|
CommandResult
|
||||||
handle_listfiles_local(Response &r,
|
handle_listfiles_local(Response &r, Path path_fs)
|
||||||
const char *path_utf8, Path path_fs)
|
|
||||||
{
|
{
|
||||||
DirectoryReader reader(path_fs);
|
DirectoryReader reader(path_fs);
|
||||||
if (reader.HasFailed()) {
|
|
||||||
Error error;
|
|
||||||
error.FormatErrno("Failed to open '%s'", path_utf8);
|
|
||||||
return print_error(r, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (reader.ReadEntry()) {
|
while (reader.ReadEntry()) {
|
||||||
const Path name_fs = reader.GetEntry();
|
const Path name_fs = reader.GetEntry();
|
||||||
|
@ -28,8 +28,7 @@ class Response;
|
|||||||
class Path;
|
class Path;
|
||||||
|
|
||||||
CommandResult
|
CommandResult
|
||||||
handle_listfiles_local(Response &response,
|
handle_listfiles_local(Response &response, Path path_fs);
|
||||||
const char *path_utf8, Path path_fs);
|
|
||||||
|
|
||||||
CommandResult
|
CommandResult
|
||||||
handle_read_comments(Client &client, Request request, Response &response);
|
handle_read_comments(Client &client, Request request, Response &response);
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
#include "Partition.hxx"
|
#include "Partition.hxx"
|
||||||
#include "Instance.hxx"
|
#include "Instance.hxx"
|
||||||
#include "Idle.hxx"
|
#include "Idle.hxx"
|
||||||
|
#include "Log.hxx"
|
||||||
|
|
||||||
#ifdef ENABLE_DATABASE
|
#ifdef ENABLE_DATABASE
|
||||||
#include "DatabaseCommands.hxx"
|
#include "DatabaseCommands.hxx"
|
||||||
@ -161,8 +162,7 @@ handle_listfiles(Client &client, Request args, Response &r)
|
|||||||
|
|
||||||
case LocatedUri::Type::PATH:
|
case LocatedUri::Type::PATH:
|
||||||
/* list local directory */
|
/* list local directory */
|
||||||
return handle_listfiles_local(r, located_uri.canonical_uri,
|
return handle_listfiles_local(r, located_uri.path);
|
||||||
located_uri.path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gcc_unreachable();
|
gcc_unreachable();
|
||||||
@ -197,9 +197,11 @@ handle_lsinfo_relative(Client &client, Response &r, const char *uri)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (isRootDirectory(uri)) {
|
if (isRootDirectory(uri)) {
|
||||||
Error error;
|
try {
|
||||||
const auto &list = ListPlaylistFiles(error);
|
print_spl_list(r, ListPlaylistFiles());
|
||||||
print_spl_list(r, list);
|
} catch (const std::exception &e) {
|
||||||
|
LogError(e);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
#ifndef ENABLE_DATABASE
|
#ifndef ENABLE_DATABASE
|
||||||
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
||||||
|
@ -198,11 +198,6 @@ CommandResult
|
|||||||
handle_listplaylists(gcc_unused Client &client, gcc_unused Request args,
|
handle_listplaylists(gcc_unused Client &client, gcc_unused Request args,
|
||||||
Response &r)
|
Response &r)
|
||||||
{
|
{
|
||||||
Error error;
|
print_spl_list(r, ListPlaylistFiles());
|
||||||
const auto list = ListPlaylistFiles(error);
|
|
||||||
if (list.empty() && error.IsDefined())
|
|
||||||
return print_error(r, error);
|
|
||||||
|
|
||||||
print_spl_list(r, list);
|
|
||||||
return CommandResult::OK;
|
return CommandResult::OK;
|
||||||
}
|
}
|
||||||
|
@ -218,7 +218,7 @@ void
|
|||||||
UpdateWalk::UpdateDirectoryChild(Directory &directory,
|
UpdateWalk::UpdateDirectoryChild(Directory &directory,
|
||||||
const ExcludeList &exclude_list,
|
const ExcludeList &exclude_list,
|
||||||
const char *name, const StorageFileInfo &info)
|
const char *name, const StorageFileInfo &info)
|
||||||
{
|
try {
|
||||||
assert(strchr(name, '/') == nullptr);
|
assert(strchr(name, '/') == nullptr);
|
||||||
|
|
||||||
if (info.IsRegular()) {
|
if (info.IsRegular()) {
|
||||||
@ -242,6 +242,8 @@ UpdateWalk::UpdateDirectoryChild(Directory &directory,
|
|||||||
FormatDebug(update_domain,
|
FormatDebug(update_domain,
|
||||||
"%s is not a directory, archive or music", name);
|
"%s is not a directory, archive or music", name);
|
||||||
}
|
}
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
LogError(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* we don't look at "." / ".." nor files with newlines in their name */
|
/* we don't look at "." / ".." nor files with newlines in their name */
|
||||||
@ -455,7 +457,7 @@ UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
|
|||||||
|
|
||||||
inline void
|
inline void
|
||||||
UpdateWalk::UpdateUri(Directory &root, const char *uri)
|
UpdateWalk::UpdateUri(Directory &root, const char *uri)
|
||||||
{
|
try {
|
||||||
Directory *parent = DirectoryMakeUriParentChecked(root, uri);
|
Directory *parent = DirectoryMakeUriParentChecked(root, uri);
|
||||||
if (parent == nullptr)
|
if (parent == nullptr)
|
||||||
return;
|
return;
|
||||||
@ -476,6 +478,8 @@ UpdateWalk::UpdateUri(Directory &root, const char *uri)
|
|||||||
ExcludeList exclude_list;
|
ExcludeList exclude_list;
|
||||||
|
|
||||||
UpdateDirectoryChild(*parent, exclude_list, name, info);
|
UpdateDirectoryChild(*parent, exclude_list, name, info);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
LogError(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "Path.hxx"
|
#include "Path.hxx"
|
||||||
#include "AllocatedPath.hxx"
|
#include "AllocatedPath.hxx"
|
||||||
#include "DirectoryReader.hxx"
|
#include "DirectoryReader.hxx"
|
||||||
|
#include "system/Error.hxx"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -58,11 +59,12 @@ CheckDirectoryReadable(Path path_fs)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
try {
|
||||||
const DirectoryReader reader(path_fs);
|
const DirectoryReader reader(path_fs);
|
||||||
if (reader.HasFailed() && errno == EACCES) {
|
} catch (const std::system_error &e) {
|
||||||
const auto path_utf8 = path_fs.ToUTF8();
|
if (IsAccessDenied(e))
|
||||||
FormatError(config_domain,
|
FormatError(config_domain,
|
||||||
"No permission to read directory: %s",
|
"No permission to read directory: %s",
|
||||||
path_utf8.c_str());
|
path_fs.ToUTF8().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
42
src/fs/DirectoryReader.cxx
Normal file
42
src/fs/DirectoryReader.cxx
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "DirectoryReader.hxx"
|
||||||
|
#include "system/Error.hxx"
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
|
||||||
|
DirectoryReader::DirectoryReader(Path dir)
|
||||||
|
:handle(FindFirstFile(MakeWildcardPath(dir.c_str()), &data))
|
||||||
|
{
|
||||||
|
if (handle == INVALID_HANDLE_VALUE)
|
||||||
|
throw FormatLastError("Failed to open %s", dir.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
DirectoryReader::DirectoryReader(Path dir)
|
||||||
|
:dirp(opendir(dir.c_str()))
|
||||||
|
{
|
||||||
|
if (dirp == nullptr)
|
||||||
|
throw FormatErrno("Failed to open %s", dir.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -61,10 +61,10 @@ class DirectoryReader {
|
|||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Creates new directory reader for the specified #dir.
|
* Creates new directory reader for the specified #dir.
|
||||||
|
*
|
||||||
|
* Throws std::system_error on error.
|
||||||
*/
|
*/
|
||||||
explicit DirectoryReader(Path dir)
|
explicit DirectoryReader(Path dir);
|
||||||
:handle(FindFirstFile(MakeWildcardPath(dir.c_str()), &data)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectoryReader(const DirectoryReader &other) = delete;
|
DirectoryReader(const DirectoryReader &other) = delete;
|
||||||
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
||||||
@ -73,17 +73,9 @@ public:
|
|||||||
* Destroys this instance.
|
* Destroys this instance.
|
||||||
*/
|
*/
|
||||||
~DirectoryReader() {
|
~DirectoryReader() {
|
||||||
if (!HasFailed())
|
|
||||||
FindClose(handle);
|
FindClose(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if directory failed to open.
|
|
||||||
*/
|
|
||||||
bool HasFailed() const {
|
|
||||||
return handle == INVALID_HANDLE_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads next directory entry.
|
* Reads next directory entry.
|
||||||
*/
|
*/
|
||||||
@ -118,10 +110,10 @@ class DirectoryReader {
|
|||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Creates new directory reader for the specified #dir.
|
* Creates new directory reader for the specified #dir.
|
||||||
|
*
|
||||||
|
* Throws std::system_error on error.
|
||||||
*/
|
*/
|
||||||
explicit DirectoryReader(Path dir)
|
explicit DirectoryReader(Path dir);
|
||||||
:dirp(opendir(dir.c_str())) {
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectoryReader(const DirectoryReader &other) = delete;
|
DirectoryReader(const DirectoryReader &other) = delete;
|
||||||
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
||||||
@ -130,22 +122,13 @@ public:
|
|||||||
* Destroys this instance.
|
* Destroys this instance.
|
||||||
*/
|
*/
|
||||||
~DirectoryReader() {
|
~DirectoryReader() {
|
||||||
if (!HasFailed())
|
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if directory failed to open.
|
|
||||||
*/
|
|
||||||
bool HasFailed() const {
|
|
||||||
return dirp == nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if directory entry is available.
|
* Checks if directory entry is available.
|
||||||
*/
|
*/
|
||||||
bool HasEntry() const {
|
bool HasEntry() const {
|
||||||
assert(!HasFailed());
|
|
||||||
return ent != nullptr;
|
return ent != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +136,6 @@ public:
|
|||||||
* Reads next directory entry.
|
* Reads next directory entry.
|
||||||
*/
|
*/
|
||||||
bool ReadEntry() {
|
bool ReadEntry() {
|
||||||
assert(!HasFailed());
|
|
||||||
ent = readdir(dirp);
|
ent = readdir(dirp);
|
||||||
return HasEntry();
|
return HasEntry();
|
||||||
}
|
}
|
||||||
|
@ -41,10 +41,6 @@ public:
|
|||||||
LocalDirectoryReader(AllocatedPath &&_base_fs)
|
LocalDirectoryReader(AllocatedPath &&_base_fs)
|
||||||
:base_fs(std::move(_base_fs)), reader(base_fs) {}
|
:base_fs(std::move(_base_fs)), reader(base_fs) {}
|
||||||
|
|
||||||
bool HasFailed() {
|
|
||||||
return reader.HasFailed();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual methods from class StorageDirectoryReader */
|
/* virtual methods from class StorageDirectoryReader */
|
||||||
const char *Read() override;
|
const char *Read() override;
|
||||||
bool GetInfo(bool follow, StorageFileInfo &info,
|
bool GetInfo(bool follow, StorageFileInfo &info,
|
||||||
@ -160,15 +156,7 @@ LocalStorage::OpenDirectory(const char *uri_utf8, Error &error)
|
|||||||
if (path_fs.IsNull())
|
if (path_fs.IsNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
LocalDirectoryReader *reader =
|
return new LocalDirectoryReader(std::move(path_fs));
|
||||||
new LocalDirectoryReader(std::move(path_fs));
|
|
||||||
if (reader->HasFailed()) {
|
|
||||||
error.FormatErrno("Failed to open '%s'", uri_utf8);
|
|
||||||
delete reader;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return reader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
|
Loading…
Reference in New Issue
Block a user