MtpStorage.cpp revision 7efab4282a7179b2f53a3dbccee3e70d7fce6a7f
1/* 2 * Copyright (C) 2010 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#define LOG_TAG "MtpStorage" 18 19#include "MtpDebug.h" 20#include "MtpDatabase.h" 21#include "MtpStorage.h" 22 23#include <sys/types.h> 24#include <sys/stat.h> 25#include <sys/statfs.h> 26#include <unistd.h> 27#include <dirent.h> 28#include <errno.h> 29#include <string.h> 30#include <stdio.h> 31#include <limits.h> 32 33namespace android { 34 35MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, 36 const char* description, uint64_t reserveSpace, bool removable) 37 : mStorageID(id), 38 mFilePath(filePath), 39 mDescription(description), 40 mMaxCapacity(0), 41 mReserveSpace(reserveSpace), 42 mRemovable(removable) 43{ 44 LOGV("MtpStorage id: %d path: %s\n", id, filePath); 45} 46 47MtpStorage::~MtpStorage() { 48} 49 50int MtpStorage::getType() const { 51 return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM : MTP_STORAGE_FIXED_RAM); 52} 53 54int MtpStorage::getFileSystemType() const { 55 return MTP_STORAGE_FILESYSTEM_HIERARCHICAL; 56} 57 58int MtpStorage::getAccessCapability() const { 59 return MTP_STORAGE_READ_WRITE; 60} 61 62uint64_t MtpStorage::getMaxCapacity() { 63 if (mMaxCapacity == 0) { 64 struct statfs stat; 65 if (statfs(getPath(), &stat)) 66 return -1; 67 mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize; 68 } 69 return mMaxCapacity; 70} 71 72uint64_t MtpStorage::getFreeSpace() { 73 struct statfs stat; 74 if (statfs(getPath(), &stat)) 75 return -1; 76 uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize; 77 return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0); 78} 79 80const char* MtpStorage::getDescription() const { 81 return (const char *)mDescription; 82} 83 84} // namespace android 85