Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / external_mount.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/external_mount.h"
6
7 #include <app2ext_interface.h>
8 #include <manifest_parser/utils/logging.h>
9
10 #include <filesystem>
11
12 #include "common/utils/pkgmgr_query.h"
13 #include "common/utils/paths.h"
14
15 namespace fs = std::filesystem;
16
17 namespace common_installer {
18
19 const char kInstalledExternally[] = "installed_external";
20
21 ExternalMount::ExternalMount(const std::string& pkgid, uid_t uid)
22     : pkgid_(pkgid),
23       uid_(uid),
24       mounted_(false) {
25 }
26
27 ExternalMount::~ExternalMount() {
28   if (mounted_) {
29     if (service.DisableExternalPkgForUsr(pkgid_.c_str(), uid_)) {
30       LOG(ERROR) << "app2ext_usr_disable_external_pkg failed";
31     }
32   }
33 }
34
35 bool ExternalMount::IsAvailable() const {
36   fs::path storage_path = GetExternalCardPath();
37   if (!fs::exists(storage_path)) {
38     LOG(WARNING) << "External storage (SD Card) is not mounted.";
39     return false;
40   }
41   PkgQueryInterface pkg_query(pkgid_, uid_);
42   std::string storage = pkg_query.StorageForPkgId();
43   if (storage != kInstalledExternally)
44     return false;
45
46   return true;
47 }
48
49 bool ExternalMount::Mount() {
50   if (mounted_)
51     return true;
52   if (service.EnableExternalPkgForUsr(pkgid_.c_str(), uid_)) {
53     LOG(ERROR) << "app2ext_usr_enable_external_pkg failed";
54     return false;
55   }
56   mounted_ = true;
57   return true;
58 }
59
60 bool ExternalMount::Umount() {
61   if (!mounted_)
62     return true;
63   if (service.DisableExternalPkgForUsr(pkgid_.c_str(), uid_)) {
64     LOG(ERROR) << "app2ext_usr_disable_external_pkg failed";
65     return false;
66   }
67   mounted_ = false;
68   return true;
69 }
70
71 }  // namespace common_installer