0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-10 05:58:56 +02:00

ircd::fs: Propagate error_code through exception system; improve stack.

This commit is contained in:
Jason Volk 2018-08-23 03:31:36 -07:00
parent ed6dc56d35
commit 1a1002c96d
4 changed files with 209 additions and 50 deletions

120
include/ircd/fs/error.h Normal file
View file

@ -0,0 +1,120 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_FS_ERROR_H
// Forward declarations for boost
namespace boost::filesystem
{
struct filesystem_error;
}
namespace ircd
{
struct error;
std::error_code make_error_code(const boost::filesystem::filesystem_error &);
}
struct ircd::fs::error
:ircd::error
{
std::error_code code;
template<class... args>
error(const char *const &fmt = " ",
args&&...);
template<class... args>
error(const std::error_code &,
const char *const &fmt,
args&&...);
template<class... args>
error(const std::system_error &,
const char *const &fmt,
args&&...);
template<class... args>
error(const boost::filesystem::filesystem_error &,
const char *const &fmt,
args&&...);
error(const std::error_code &e);
error(const std::system_error &);
error(const boost::filesystem::filesystem_error &);
};
inline
ircd::fs::error::error(const boost::filesystem::filesystem_error &code)
:error{make_error_code(code)}
{}
inline
ircd::fs::error::error(const std::system_error &code)
:code{make_error_code(code)}
{
string(this->buf, code);
}
inline
ircd::fs::error::error(const std::error_code &code)
:code{code}
{
string(this->buf, code);
}
template<class... args>
ircd::fs::error::error(const boost::filesystem::filesystem_error &e,
const char *const &fmt,
args&&... a)
:error
{
make_error_code(e), fmt, std::forward<args>(a)...
}
{
}
template<class... args>
ircd::fs::error::error(const std::system_error &e,
const char *const &fmt,
args&&... a)
:error
{
make_error_code(e), fmt, std::forward<args>(a)...
}
{
}
template<class... args>
ircd::fs::error::error(const std::error_code &e,
const char *const &fmt,
args&&... a)
:ircd::error
{
fmt, std::forward<args>(a)...
}
,code
{
make_error_code(e)
}
{
}
template<class... args>
ircd::fs::error::error(const char *const &fmt,
args&&... a)
:ircd::error
{
fmt, std::forward<args>(a)...
}
{
}

View file

@ -37,9 +37,7 @@ namespace ircd::fs
struct aio;
struct init;
enum index :int;
IRCD_EXCEPTION(ircd::error, error)
IRCD_EXCEPTION(error, filesystem_error)
struct error; // custom exception; still inherits from ircd::error
constexpr size_t PATH_MAX { 2048 };
@ -83,6 +81,7 @@ enum ircd::fs::index
_NUM_
};
#include "error.h"
#include "fd.h"
#include "read.h"
#include "write.h"

View file

@ -410,7 +410,10 @@ try
while(retval == std::numeric_limits<ssize_t>::min());
if(retval == -1)
throw_system_error(errcode);
throw fs::error
{
make_error_code(errcode)
};
return size_t(retval);
}

View file

@ -184,9 +184,17 @@ try
syscall(::fsync, fd);
}
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -204,9 +212,17 @@ try
syscall(::fdatasync, fd);
}
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -269,13 +285,17 @@ try
return read(fd, opts);
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -305,13 +325,17 @@ try
return read(fd, buf, opts);
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -334,13 +358,17 @@ try
size_t(syscall(::pread, fd, data(buf), size(buf), opts.offset))
};
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -367,13 +395,17 @@ try
opts.offset = syscall(::lseek, fd, 0, SEEK_END);
return write(fd, buf, opts);
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -392,13 +424,17 @@ try
return write(fd, buf, opts);
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -421,13 +457,17 @@ try
size_t(syscall(::pwrite, fd, data(buf), size(buf), opts.offset))
};
}
catch(const filesystem_error &)
catch(const error &e)
{
throw;
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
catch(const std::exception &e)
{
throw filesystem_error
throw error
{
"%s", e.what()
};
@ -598,10 +638,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
void
@ -612,9 +649,9 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
throw error
{
"`%s': %s", path, e.what()
e, "`%s' :%s", path, e.what()
};
}
@ -626,9 +663,9 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
throw error
{
"`%s': %s", path, e.what()
e, "`%s' :%s", path, e.what()
};
}
@ -640,9 +677,9 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
throw error
{
"`%s': %s", path, e.what()
e, "`%s' :%s", path, e.what()
};
}
@ -663,9 +700,9 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
throw error
{
"`%s' -> `%s': %s", old, new_, e.what()
e, "`%s' -> `%s' :%s", old, new_, e.what()
};
}
@ -700,10 +737,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
std::vector<std::string>
@ -727,10 +761,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
size_t
@ -747,10 +778,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
bool
@ -761,10 +789,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
bool
@ -775,10 +800,7 @@ try
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
throw error{e};
}
std::string
@ -822,3 +844,18 @@ ircd::fs::path(std::string s)
{
return filesystem::path(std::move(s));
}
//
// fs/error.h
//
std::error_code
ircd::make_error_code(const boost::filesystem::filesystem_error &e)
{
const boost::system::error_code &ec
{
e.code()
};
return make_error_code(ec);
}