forked from flexibity-team/boost-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmountUtil.hpp
More file actions
45 lines (32 loc) · 1.13 KB
/
mountUtil.hpp
File metadata and controls
45 lines (32 loc) · 1.13 KB
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
#ifndef INCLUDE_FLEXIBITY_MOUNTUTIL_HPP_
#define INCLUDE_FLEXIBITY_MOUNTUTIL_HPP_
#include <flexibity/log.h>
#include <sys/mount.h>
namespace Flexibity {
namespace mountUtil {
int safe_mount(const char* fromDev, const char* toPath) {
int mount_result = ::mount(fromDev, toPath, "ext2", MS_MGC_VAL | MS_RDONLY , "" );
GWARN("Mount errno: " << "(" << mount_result << ")" << errno << " " << strerror(errno));
return mount_result;
}
int safe_remount(const char* toPath, bool rw) {
int flags = MS_REMOUNT | MS_RDONLY;
if (rw)
flags = MS_REMOUNT & ~MS_RDONLY;
int mount_result = ::mount(NULL, toPath, NULL, flags, "" );
GWARN("Mount errno: " << "(" << mount_result << ")" << errno << " " << strerror(errno));
return mount_result;
}
void mount(const char* fromDev, const char* toPath) {
int mount_result = safe_mount(fromDev, toPath);
if(mount_result < 0 ) {
mount_result = safe_remount(toPath, false);
if( mount_result < 0 ) {
system((std::string("mkfs.ext2 ") + std::string(fromDev)).c_str());
safe_mount(fromDev, toPath);
}
}
}
}
}
#endif // INCLUDE_FLEXIBITY_MOUNTUTIL_HPP_