-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysutil.hxx
67 lines (47 loc) · 1.2 KB
/
sysutil.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef __sysutil_hxx__
#define __sysutil_hxx__
#include <stdexcept>
#include <errno.h>
template <class T, std::size_t N>
constexpr std::size_t array_size(const T (&array)[N]) noexcept
{
return N;
}
class SystemException : public std::runtime_error {
public:
SystemException(const char* msg, int errnum)
: std::runtime_error(msg),
errno_(errnum)
{}
~SystemException() noexcept
{}
virtual const char* what() const noexcept
{
return (
std::string(std::runtime_error::what()) + ": " +
std::string(strerror(errno_))
).c_str();
}
private:
std::string msg_;
const int errno_;
};
std::string get_current_dir()
{
char name[4096] = {};
if ( getcwd(name, sizeof(name)) == NULL )
throw SystemException("getcwd failed", errno);
return std::string(name);
}
void change_dir(const std::string &path)
{
if ( chdir(path.c_str()) != 0 )
throw SystemException("chdir failed", errno);
}
void ensure_mount_point(const char* mount_point)
{
if ( mkdir(mount_point, 0777) == -1 )
if ( errno != EEXIST )
throw SystemException("Invalid mount point", errno);
}
#endif // include guard