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