db/simple: refactor Check() to throw exception

This commit is contained in:
Max Kellermann 2016-03-18 22:21:09 +01:00
parent 2ccd1cc9f0
commit 6fd7d8191e
2 changed files with 23 additions and 36 deletions

View File

@ -113,8 +113,8 @@ SimpleDatabase::Configure(const ConfigBlock &block, Error &error)
return true; return true;
} }
bool void
SimpleDatabase::Check(Error &error) const SimpleDatabase::Check() const
{ {
assert(!path.IsNull()); assert(!path.IsNull());
@ -127,54 +127,43 @@ SimpleDatabase::Check(Error &error) const
/* Check that the parent part of the path is a directory */ /* Check that the parent part of the path is a directory */
FileInfo fi; FileInfo fi;
if (!GetFileInfo(dirPath, fi, error)) {
error.AddPrefix("On parent directory of db file: "); try {
return false; fi = FileInfo(dirPath);
} catch (...) {
std::throw_with_nested(std::runtime_error("On parent directory of db file"));
} }
if (!fi.IsDirectory()) { if (!fi.IsDirectory())
error.Format(simple_db_domain, throw std::runtime_error("Couldn't create db file \"" +
"Couldn't create db file \"%s\" because the " path_utf8 + "\" because the "
"parent path is not a directory", "parent path is not a directory");
path_utf8.c_str());
return false;
}
#ifndef WIN32 #ifndef WIN32
/* Check if we can write to the directory */ /* Check if we can write to the directory */
if (!CheckAccess(dirPath, X_OK | W_OK)) { if (!CheckAccess(dirPath, X_OK | W_OK)) {
const int e = errno; const int e = errno;
const std::string dirPath_utf8 = dirPath.ToUTF8(); const std::string dirPath_utf8 = dirPath.ToUTF8();
error.FormatErrno(e, "Can't create db file in \"%s\"", throw FormatErrno(e, "Can't create db file in \"%s\"",
dirPath_utf8.c_str()); dirPath_utf8.c_str());
return false;
} }
#endif #endif
return true;
return;
} }
/* Path exists, now check if it's a regular file */ /* Path exists, now check if it's a regular file */
FileInfo fi; const FileInfo fi(path);
if (!GetFileInfo(path, fi, error))
return false;
if (!fi.IsRegular()) { if (!fi.IsRegular())
error.Format(simple_db_domain, throw std::runtime_error("db file \"" + path_utf8 + "\" is not a regular file");
"db file \"%s\" is not a regular file",
path_utf8.c_str());
return false;
}
#ifndef WIN32 #ifndef WIN32
/* And check that we can write to it */ /* And check that we can write to it */
if (!CheckAccess(path, R_OK | W_OK)) { if (!CheckAccess(path, R_OK | W_OK))
error.FormatErrno("Can't open db file \"%s\" for reading/writing", throw FormatErrno("Can't open db file \"%s\" for reading/writing",
path_utf8.c_str()); path_utf8.c_str());
return false;
}
#endif #endif
return true;
} }
bool bool
@ -196,7 +185,7 @@ SimpleDatabase::Load(Error &error)
} }
bool bool
SimpleDatabase::Open(Error &error) SimpleDatabase::Open(gcc_unused Error &error)
{ {
assert(prefixed_light_song == nullptr); assert(prefixed_light_song == nullptr);
@ -214,8 +203,7 @@ SimpleDatabase::Open(Error &error)
delete root; delete root;
if (!Check(error)) Check();
return false;
root = Directory::NewRoot(); root = Directory::NewRoot();
} }
@ -224,8 +212,7 @@ SimpleDatabase::Open(Error &error)
delete root; delete root;
if (!Check(error)) Check();
return false;
root = Directory::NewRoot(); root = Directory::NewRoot();
} }

View File

@ -136,7 +136,7 @@ public:
private: private:
bool Configure(const ConfigBlock &block, Error &error); bool Configure(const ConfigBlock &block, Error &error);
bool Check(Error &error) const; void Check() const;
bool Load(Error &error); bool Load(Error &error);