Mike Lockwood | 16864ba | 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 | |
| 17 | #include "MtpDatabase.h" |
| 18 | #include "MtpStorage.h" |
Mike Lockwood | fceef46 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 19 | #include "MtpMediaScanner.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statfs.h> |
| 24 | #include <unistd.h> |
| 25 | #include <dirent.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include <limits.h> |
| 30 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 31 | namespace android { |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 32 | |
| 33 | MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, MtpDatabase* db) |
| 34 | : mStorageID(id), |
| 35 | mFilePath(filePath), |
| 36 | mDatabase(db), |
| 37 | mMaxCapacity(0) |
| 38 | { |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 39 | printf("MtpStorage id: %d path: %s\n", id, filePath); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | MtpStorage::~MtpStorage() { |
| 43 | } |
| 44 | |
| 45 | int MtpStorage::getType() const { |
| 46 | return MTP_STORAGE_FIXED_RAM; |
| 47 | } |
| 48 | |
| 49 | int MtpStorage::getFileSystemType() const { |
| 50 | return MTP_STORAGE_FILESYSTEM_HIERARCHICAL; |
| 51 | } |
| 52 | |
| 53 | int MtpStorage::getAccessCapability() const { |
| 54 | return MTP_STORAGE_READ_WRITE; |
| 55 | } |
| 56 | |
| 57 | uint64_t MtpStorage::getMaxCapacity() { |
| 58 | if (mMaxCapacity == 0) { |
| 59 | struct statfs stat; |
| 60 | if (statfs(mFilePath, &stat)) |
| 61 | return -1; |
| 62 | mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize; |
| 63 | } |
| 64 | return mMaxCapacity; |
| 65 | } |
| 66 | |
| 67 | uint64_t MtpStorage::getFreeSpace() { |
| 68 | struct statfs stat; |
| 69 | if (statfs(mFilePath, &stat)) |
| 70 | return -1; |
| 71 | return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize; |
| 72 | } |
| 73 | |
| 74 | const char* MtpStorage::getDescription() const { |
Mike Lockwood | c42aa12 | 2010-06-14 17:58:08 -0700 | [diff] [blame] | 75 | return "Device Storage"; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool MtpStorage::scanFiles() { |
Mike Lockwood | fceef46 | 2010-05-14 15:35:17 -0400 | [diff] [blame] | 79 | MtpMediaScanner scanner(mStorageID, mFilePath, mDatabase); |
| 80 | return scanner.scanFiles(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 81 | } |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 82 | |
| 83 | } // namespace android |