fs/DirectoryReader: use C++ exceptions instead of class Error
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include "Path.hxx"
|
||||
#include "AllocatedPath.hxx"
|
||||
#include "DirectoryReader.hxx"
|
||||
#include "system/Error.hxx"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -58,11 +59,12 @@ CheckDirectoryReadable(Path path_fs)
|
||||
}
|
||||
#endif
|
||||
|
||||
const DirectoryReader reader(path_fs);
|
||||
if (reader.HasFailed() && errno == EACCES) {
|
||||
const auto path_utf8 = path_fs.ToUTF8();
|
||||
FormatError(config_domain,
|
||||
"No permission to read directory: %s",
|
||||
path_utf8.c_str());
|
||||
try {
|
||||
const DirectoryReader reader(path_fs);
|
||||
} catch (const std::system_error &e) {
|
||||
if (IsAccessDenied(e))
|
||||
FormatError(config_domain,
|
||||
"No permission to read directory: %s",
|
||||
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:
|
||||
/**
|
||||
* Creates new directory reader for the specified #dir.
|
||||
*
|
||||
* Throws std::system_error on error.
|
||||
*/
|
||||
explicit DirectoryReader(Path dir)
|
||||
:handle(FindFirstFile(MakeWildcardPath(dir.c_str()), &data)) {
|
||||
}
|
||||
explicit DirectoryReader(Path dir);
|
||||
|
||||
DirectoryReader(const DirectoryReader &other) = delete;
|
||||
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
||||
@@ -73,15 +73,7 @@ public:
|
||||
* Destroys this instance.
|
||||
*/
|
||||
~DirectoryReader() {
|
||||
if (!HasFailed())
|
||||
FindClose(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if directory failed to open.
|
||||
*/
|
||||
bool HasFailed() const {
|
||||
return handle == INVALID_HANDLE_VALUE;
|
||||
FindClose(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,10 +110,10 @@ class DirectoryReader {
|
||||
public:
|
||||
/**
|
||||
* Creates new directory reader for the specified #dir.
|
||||
*
|
||||
* Throws std::system_error on error.
|
||||
*/
|
||||
explicit DirectoryReader(Path dir)
|
||||
:dirp(opendir(dir.c_str())) {
|
||||
}
|
||||
explicit DirectoryReader(Path dir);
|
||||
|
||||
DirectoryReader(const DirectoryReader &other) = delete;
|
||||
DirectoryReader &operator=(const DirectoryReader &other) = delete;
|
||||
@@ -130,22 +122,13 @@ public:
|
||||
* Destroys this instance.
|
||||
*/
|
||||
~DirectoryReader() {
|
||||
if (!HasFailed())
|
||||
closedir(dirp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if directory failed to open.
|
||||
*/
|
||||
bool HasFailed() const {
|
||||
return dirp == nullptr;
|
||||
closedir(dirp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if directory entry is available.
|
||||
*/
|
||||
bool HasEntry() const {
|
||||
assert(!HasFailed());
|
||||
return ent != nullptr;
|
||||
}
|
||||
|
||||
@@ -153,7 +136,6 @@ public:
|
||||
* Reads next directory entry.
|
||||
*/
|
||||
bool ReadEntry() {
|
||||
assert(!HasFailed());
|
||||
ent = readdir(dirp);
|
||||
return HasEntry();
|
||||
}
|
||||
|
Reference in New Issue
Block a user