1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ObbVolume.h"
18 #include "Loop.h"
19 #include "Utils.h"
20 #include "VoldUtil.h"
21 #include "fs/Vfat.h"
22
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25 #include <cutils/fs.h>
26 #include <private/android_filesystem_config.h>
27
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <sys/mount.h>
31 #include <sys/stat.h>
32 #include <sys/sysmacros.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35
36 using android::base::StringPrintf;
37
38 namespace android {
39 namespace vold {
40
ObbVolume(int id,const std::string & sourcePath,gid_t ownerGid)41 ObbVolume::ObbVolume(int id, const std::string& sourcePath, gid_t ownerGid)
42 : VolumeBase(Type::kObb) {
43 setId(StringPrintf("obb:%d", id));
44 mSourcePath = sourcePath;
45 mOwnerGid = ownerGid;
46 }
47
~ObbVolume()48 ObbVolume::~ObbVolume() {}
49
doCreate()50 status_t ObbVolume::doCreate() {
51 if (Loop::create(mSourcePath, mLoopPath)) {
52 PLOG(ERROR) << getId() << " failed to create loop";
53 return -1;
54 }
55 return OK;
56 }
57
doDestroy()58 status_t ObbVolume::doDestroy() {
59 if (!mLoopPath.empty() && Loop::destroyByDevice(mLoopPath.c_str())) {
60 PLOG(WARNING) << getId() << " failed to destroy loop";
61 }
62 mLoopPath.clear();
63 return OK;
64 }
65
doMount()66 status_t ObbVolume::doMount() {
67 auto path = StringPrintf("/mnt/obb/%s", getId().c_str());
68 setPath(path);
69
70 if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT)) {
71 PLOG(ERROR) << getId() << " failed to create mount point";
72 return -1;
73 }
74 // clang-format off
75 if (android::vold::vfat::Mount(mLoopPath, path, true, false, true,
76 0, mOwnerGid, 0227, false)) {
77 // clang-format on
78 PLOG(ERROR) << getId() << " failed to mount";
79 return -1;
80 }
81 return OK;
82 }
83
doUnmount()84 status_t ObbVolume::doUnmount() {
85 auto path = getPath();
86
87 KillProcessesUsingPath(path);
88 ForceUnmount(path);
89 rmdir(path.c_str());
90
91 return OK;
92 }
93
94 } // namespace vold
95 } // namespace android
96