2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "VolumeBase.h"
19 #include "VolumeManager.h"
20 #include "ResponseCode.h"
22 #include <base/stringprintf.h>
23 #include <base/logging.h>
27 #include <sys/mount.h>
29 #include <sys/types.h>
31 using android::base::StringPrintf;
38 VolumeBase::VolumeBase(Type type) :
39 mType(type), mFlags(0), mUser(-1), mCreated(false), mState(State::kUnmounted) {
42 VolumeBase::~VolumeBase() {
46 void VolumeBase::setState(State state) {
49 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
50 ResponseCode::VolumeStateChanged,
51 StringPrintf("%s %d", getId().c_str(), mState).c_str(), false);
54 status_t VolumeBase::setFlags(int flags) {
55 if (mState != State::kUnmounted) {
56 LOG(WARNING) << getId() << " flags change requires state unmounted";
64 status_t VolumeBase::setUser(userid_t user) {
65 if (mState != State::kUnmounted) {
66 LOG(WARNING) << getId() << " user change requires state unmounted";
74 status_t VolumeBase::setId(const std::string& id) {
76 LOG(WARNING) << getId() << " id change requires not created";
84 status_t VolumeBase::setPath(const std::string& path) {
85 if (mState != State::kMounting) {
86 LOG(WARNING) << getId() << " path change requires state mounting";
91 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
92 ResponseCode::VolumePathChanged,
93 StringPrintf("%s %s", getId().c_str(), mPath.c_str()).c_str(), false);
97 void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
98 mVolumes.push_back(volume);
101 void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
102 mVolumes.remove(volume);
105 std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
106 for (auto vol : mVolumes) {
107 if (vol->getId() == id) {
114 status_t VolumeBase::create() {
117 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
118 ResponseCode::VolumeCreated,
119 StringPrintf("%s %d", getId().c_str(), mType).c_str(), false);
123 status_t VolumeBase::destroy() {
126 if (mState == State::kMounted) {
131 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
132 ResponseCode::VolumeDestroyed, getId().c_str(), false);
136 status_t VolumeBase::mount() {
137 if (mState != State::kUnmounted) {
138 LOG(WARNING) << getId() << " mount requires state unmounted";
142 setState(State::kMounting);
143 status_t res = doMount();
145 setState(State::kMounted);
147 setState(State::kUnmounted);
153 status_t VolumeBase::unmount() {
154 if (mState != State::kMounted) {
155 LOG(WARNING) << getId() << " unmount requires state mounted";
159 setState(State::kUnmounting);
161 for (auto vol : mVolumes) {
162 if (vol->unmount()) {
163 LOG(WARNING) << getId() << " failed to unmount " << vol->getId()
169 status_t res = doUnmount();
170 setState(State::kUnmounted);
174 status_t VolumeBase::format() {
175 if (mState != State::kUnmounted) {
176 LOG(WARNING) << getId() << " format requires state unmounted";
180 setState(State::kFormatting);
181 status_t res = doFormat();
182 setState(State::kUnmounted);
186 status_t VolumeBase::doFormat() {
191 } // namespace android