TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / common / mount_entry.cpp
1 /*
2  * Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include "anbox/common/mount_entry.h"
19 #include "anbox/common/loop_device.h"
20 #include "anbox/logger.h"
21
22 #include <sys/mount.h>
23
24 namespace anbox {
25 namespace common {
26 std::shared_ptr<MountEntry> MountEntry::create(const boost::filesystem::path &src, const boost::filesystem::path &target,
27                                                const std::string &fs_type, unsigned long flags, const std::string &data) {
28   auto entry = std::shared_ptr<MountEntry>(new MountEntry(target));
29   if (!entry)
30     return nullptr;
31
32   const void *mount_data = nullptr;
33   if (!data.empty())
34     mount_data = reinterpret_cast<const void*>(data.c_str());
35
36   DEBUG("Mounting %s on %s ...", src, target);
37
38   if (::mount(src.c_str(), target.c_str(), !fs_type.empty() ? fs_type.c_str() : nullptr, flags, mount_data) < 0) {
39     ERROR("Failed to mount %s: %s", target, strerror(errno));
40     return nullptr;
41   }
42
43   entry->active_ = true;
44
45   return entry;
46 }
47
48 std::shared_ptr<MountEntry> MountEntry::create(const std::shared_ptr<LoopDevice> &loop, const boost::filesystem::path &target,
49                                                const std::string &fs_type, unsigned long flags, const std::string &data) {
50   auto entry = create(loop->path(), target, fs_type, flags, data);
51   if (!entry)
52     return nullptr;
53
54   entry->loop_ = loop;
55   return entry;
56 }
57
58 std::shared_ptr<MountEntry> MountEntry::create(const boost::filesystem::path &target) {
59   auto entry = std::shared_ptr<MountEntry>(new MountEntry(target));
60   if (!entry)
61     return nullptr;
62
63   entry->active_ = true;
64
65   return entry;
66 }
67
68 MountEntry::MountEntry(const boost::filesystem::path &target) :
69   active_{false}, target_{target} {}
70
71 MountEntry::~MountEntry() {
72   if (!active_)
73     return;
74
75   ::umount(target_.c_str());
76 }
77 } // namespace common
78 } // namespace anbox