Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 17 | #define LOG_TAG "MtpStorage" |
| 18 | |
| 19 | #include "MtpDebug.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 33 | namespace android { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 34 | |
Mike Lockwood | 7f36b19 | 2010-12-12 12:17:43 -0800 | [diff] [blame] | 35 | MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, uint64_t reserveSpace) |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 36 | : mStorageID(id), |
| 37 | mFilePath(filePath), |
Mike Lockwood | 7f36b19 | 2010-12-12 12:17:43 -0800 | [diff] [blame] | 38 | mMaxCapacity(0), |
| 39 | mReserveSpace(reserveSpace) |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 40 | { |
Mike Lockwood | f26a586 | 2011-01-21 21:00:54 -0800 | [diff] [blame] | 41 | LOGV("MtpStorage id: %d path: %s\n", id, filePath); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | MtpStorage::~MtpStorage() { |
| 45 | } |
| 46 | |
| 47 | int MtpStorage::getType() const { |
| 48 | return MTP_STORAGE_FIXED_RAM; |
| 49 | } |
| 50 | |
| 51 | int MtpStorage::getFileSystemType() const { |
| 52 | return MTP_STORAGE_FILESYSTEM_HIERARCHICAL; |
| 53 | } |
| 54 | |
| 55 | int MtpStorage::getAccessCapability() const { |
| 56 | return MTP_STORAGE_READ_WRITE; |
| 57 | } |
| 58 | |
| 59 | uint64_t MtpStorage::getMaxCapacity() { |
| 60 | if (mMaxCapacity == 0) { |
| 61 | struct statfs stat; |
| 62 | if (statfs(mFilePath, &stat)) |
| 63 | return -1; |
| 64 | mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize; |
| 65 | } |
| 66 | return mMaxCapacity; |
| 67 | } |
| 68 | |
| 69 | uint64_t MtpStorage::getFreeSpace() { |
| 70 | struct statfs stat; |
| 71 | if (statfs(mFilePath, &stat)) |
| 72 | return -1; |
Mike Lockwood | 7f36b19 | 2010-12-12 12:17:43 -0800 | [diff] [blame] | 73 | uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize; |
| 74 | return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const char* MtpStorage::getDescription() const { |
Mike Lockwood | 622ccdc | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 78 | return "Device Storage"; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 81 | } // namespace android |