This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mount_entry.cpp
74 lines (59 loc) · 2.29 KB
/
mount_entry.cpp
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
68
69
70
71
72
73
74
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "anbox/common/mount_entry.h"
#include "anbox/common/loop_device.h"
#include "anbox/logger.h"
#include <sys/mount.h>
namespace anbox {
namespace common {
std::shared_ptr<MountEntry> MountEntry::create(const boost::filesystem::path &src, const boost::filesystem::path &target,
const std::string &fs_type, unsigned long flags, const std::string &data) {
auto entry = std::shared_ptr<MountEntry>(new MountEntry(target));
if (!entry)
return nullptr;
const void *mount_data = nullptr;
if (!data.empty())
mount_data = reinterpret_cast<const void*>(data.c_str());
if (::mount(src.c_str(), target.c_str(), !fs_type.empty() ? fs_type.c_str() : nullptr, flags, mount_data) != 0)
return nullptr;
entry->active_ = true;
return entry;
}
std::shared_ptr<MountEntry> MountEntry::create(const std::shared_ptr<LoopDevice> &loop, const boost::filesystem::path &target,
const std::string &fs_type, unsigned long flags, const std::string &data) {
auto entry = create(loop->path(), target, fs_type, flags, data);
if (!entry)
return nullptr;
entry->loop_ = loop;
return entry;
}
std::shared_ptr<MountEntry> MountEntry::create(const boost::filesystem::path &target) {
auto entry = std::shared_ptr<MountEntry>(new MountEntry(target));
if (!entry)
return nullptr;
entry->active_ = true;
return entry;
}
MountEntry::MountEntry(const boost::filesystem::path &target) :
active_{false}, target_{target} {}
MountEntry::~MountEntry() {
if (!active_)
return;
::umount(target_.c_str());
}
} // namespace common
} // namespace anbox