2009-10-11 02:22:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2009-10-12 20:32:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "Vold"
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
#include <sysutils/NetlinkEvent.h>
|
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
#include "VolumeManager.h"
|
2009-10-12 23:57:05 +02:00
|
|
|
#include "DirectVolume.h"
|
2009-10-11 02:22:08 +02:00
|
|
|
#include "ErrorCode.h"
|
|
|
|
|
|
|
|
VolumeManager *VolumeManager::sInstance = NULL;
|
|
|
|
|
|
|
|
VolumeManager *VolumeManager::Instance() {
|
|
|
|
if (!sInstance)
|
|
|
|
sInstance = new VolumeManager();
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::VolumeManager() {
|
|
|
|
mBlockDevices = new BlockDeviceCollection();
|
|
|
|
mVolumes = new VolumeCollection();
|
|
|
|
mBroadcaster = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::~VolumeManager() {
|
|
|
|
delete mBlockDevices;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::start() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::stop() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::addVolume(Volume *v) {
|
|
|
|
mVolumes->push_back(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
|
|
|
|
const char *devpath = evt->findParam("DEVPATH");
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2009-10-12 20:32:47 +02:00
|
|
|
/* Lookup a volume to handle this device */
|
2009-10-11 02:22:08 +02:00
|
|
|
VolumeCollection::iterator it;
|
|
|
|
bool hit = false;
|
|
|
|
for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
|
2009-10-12 20:32:47 +02:00
|
|
|
if (!(*it)->handleBlockEvent(evt)) {
|
2009-10-11 02:22:08 +02:00
|
|
|
hit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hit) {
|
2009-10-12 20:32:47 +02:00
|
|
|
LOGW("No volumes handled block event for '%s'", devpath);
|
2009-10-11 02:22:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::listVolumes(SocketClient *cli) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s %s %d",
|
|
|
|
(*i)->getLabel(), (*i)->getMountpoint(),
|
|
|
|
(*i)->getState());
|
|
|
|
cli->sendMsg(ErrorCode::VolumeListResult, buffer, false);
|
|
|
|
free(buffer);
|
|
|
|
}
|
2009-10-13 01:29:01 +02:00
|
|
|
cli->sendMsg(ErrorCode::CommandOkay, "Volumes listed.", false);
|
2009-10-11 02:22:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-13 01:29:01 +02:00
|
|
|
|
|
|
|
int VolumeManager::mountVolume(const char *label) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->getState() != Volume::State_Idle) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->mount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int VolumeManager::unmountVolume(const char *label) {
|
|
|
|
Volume *v = lookupVolume(label);
|
|
|
|
|
|
|
|
if (!v) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->getState() != Volume::State_Mounted) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->unmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
Volume *VolumeManager::lookupVolume(const char *label) {
|
|
|
|
VolumeCollection::iterator i;
|
|
|
|
|
|
|
|
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
|
|
|
|
if (!strcmp(label, (*i)->getLabel()))
|
|
|
|
return (*i);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|