Commit 6fd7d819 authored by Max Kellermann's avatar Max Kellermann

db/simple: refactor Check() to throw exception

parent 2ccd1cc9
...@@ -113,8 +113,8 @@ SimpleDatabase::Configure(const ConfigBlock &block, Error &error) ...@@ -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 ...@@ -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: ");
return false;
}
if (!fi.IsDirectory()) { try {
error.Format(simple_db_domain, fi = FileInfo(dirPath);
"Couldn't create db file \"%s\" because the " } catch (...) {
"parent path is not a directory", std::throw_with_nested(std::runtime_error("On parent directory of db file"));
path_utf8.c_str());
return false;
} }
if (!fi.IsDirectory())
throw std::runtime_error("Couldn't create db file \"" +
path_utf8 + "\" because the "
"parent path is not a directory");
#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) ...@@ -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) ...@@ -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) ...@@ -224,8 +212,7 @@ SimpleDatabase::Open(Error &error)
delete root; delete root;
if (!Check(error)) Check();
return false;
root = Directory::NewRoot(); root = Directory::NewRoot();
} }
......
...@@ -136,7 +136,7 @@ public: ...@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment