blob: b64c1ba8b57e839aa62ff1c027eec31a507f31cb [file] [log] [blame]
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001/*
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
Paul Crowley14c8c072018-09-18 13:30:21 -070017#include "ObbVolume.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060018#include "Loop.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060019#include "Utils.h"
20#include "VoldUtil.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070021#include "fs/Vfat.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060022
23#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060025#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>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060032#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070033#include <sys/types.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060034#include <sys/wait.h>
35
36using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060037
38namespace android {
39namespace vold {
40
Eric Biggers7e79a432022-03-01 21:19:18 +000041ObbVolume::ObbVolume(int id, const std::string& sourcePath, gid_t ownerGid)
Paul Crowley14c8c072018-09-18 13:30:21 -070042 : VolumeBase(Type::kObb) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060043 setId(StringPrintf("obb:%d", id));
44 mSourcePath = sourcePath;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060045 mOwnerGid = ownerGid;
46}
47
Paul Crowley14c8c072018-09-18 13:30:21 -070048ObbVolume::~ObbVolume() {}
Jeff Sharkey11c2d382017-09-11 10:32:01 -060049
50status_t ObbVolume::doCreate() {
51 if (Loop::create(mSourcePath, mLoopPath)) {
52 PLOG(ERROR) << getId() << " failed to create loop";
53 return -1;
54 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -060055 return OK;
56}
57
58status_t ObbVolume::doDestroy() {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060059 if (!mLoopPath.empty() && Loop::destroyByDevice(mLoopPath.c_str())) {
60 PLOG(WARNING) << getId() << " failed to destroy loop";
61 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -060062 mLoopPath.clear();
63 return OK;
64}
65
66status_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 }
Paul Crowley14c8c072018-09-18 13:30:21 -070074 // clang-format off
Eric Biggers7e79a432022-03-01 21:19:18 +000075 if (android::vold::vfat::Mount(mLoopPath, path, true, false, true,
Paul Crowley14c8c072018-09-18 13:30:21 -070076 0, mOwnerGid, 0227, false)) {
77 // clang-format on
Jeff Sharkey11c2d382017-09-11 10:32:01 -060078 PLOG(ERROR) << getId() << " failed to mount";
79 return -1;
80 }
81 return OK;
82}
83
84status_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