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" |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statfs.h> |
| 23 | #include <unistd.h> |
| 24 | #include <dirent.h> |
| 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #include <stdio.h> |
| 28 | #include <limits.h> |
| 29 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 30 | namespace android { |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | |
| 32 | MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, MtpDatabase* db) |
| 33 | : mStorageID(id), |
| 34 | mFilePath(filePath), |
| 35 | mDatabase(db), |
| 36 | mMaxCapacity(0) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | MtpStorage::~MtpStorage() { |
| 41 | } |
| 42 | |
| 43 | int MtpStorage::getType() const { |
| 44 | return MTP_STORAGE_FIXED_RAM; |
| 45 | } |
| 46 | |
| 47 | int MtpStorage::getFileSystemType() const { |
| 48 | return MTP_STORAGE_FILESYSTEM_HIERARCHICAL; |
| 49 | } |
| 50 | |
| 51 | int MtpStorage::getAccessCapability() const { |
| 52 | return MTP_STORAGE_READ_WRITE; |
| 53 | } |
| 54 | |
| 55 | uint64_t MtpStorage::getMaxCapacity() { |
| 56 | if (mMaxCapacity == 0) { |
| 57 | struct statfs stat; |
| 58 | if (statfs(mFilePath, &stat)) |
| 59 | return -1; |
| 60 | mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize; |
| 61 | } |
| 62 | return mMaxCapacity; |
| 63 | } |
| 64 | |
| 65 | uint64_t MtpStorage::getFreeSpace() { |
| 66 | struct statfs stat; |
| 67 | if (statfs(mFilePath, &stat)) |
| 68 | return -1; |
| 69 | return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize; |
| 70 | } |
| 71 | |
| 72 | const char* MtpStorage::getDescription() const { |
| 73 | return "Phone Storage"; |
| 74 | } |
| 75 | |
| 76 | bool MtpStorage::scanFiles() { |
| 77 | mDatabase->beginTransaction(); |
| 78 | int ret = scanDirectory(mFilePath, MTP_PARENT_ROOT); |
| 79 | mDatabase->commitTransaction(); |
| 80 | return (ret == 0); |
| 81 | } |
| 82 | |
| 83 | int MtpStorage::scanDirectory(const char* path, MtpObjectHandle parent) |
| 84 | { |
| 85 | char buffer[PATH_MAX]; |
| 86 | struct dirent* entry; |
| 87 | |
| 88 | int length = strlen(path); |
| 89 | if (length > sizeof(buffer) + 2) { |
| 90 | fprintf(stderr, "path too long: %s\n", path); |
| 91 | } |
| 92 | |
| 93 | DIR* dir = opendir(path); |
| 94 | if (!dir) { |
| 95 | fprintf(stderr, "opendir %s failed, errno: %d", path, errno); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | strncpy(buffer, path, sizeof(buffer)); |
| 100 | char* fileStart = buffer + length; |
| 101 | // make sure we have a trailing slash |
| 102 | if (fileStart[-1] != '/') { |
| 103 | *(fileStart++) = '/'; |
| 104 | } |
| 105 | int fileNameLength = sizeof(buffer) + fileStart - buffer; |
| 106 | |
| 107 | while ((entry = readdir(dir))) { |
| 108 | const char* name = entry->d_name; |
| 109 | |
| 110 | // ignore "." and "..", as well as any files or directories staring with dot |
| 111 | if (name[0] == '.') { |
| 112 | continue; |
| 113 | } |
| 114 | if (strlen(name) + 1 > fileNameLength) { |
| 115 | fprintf(stderr, "path too long for %s\n", name); |
| 116 | continue; |
| 117 | } |
| 118 | strcpy(fileStart, name); |
| 119 | |
| 120 | struct stat statbuf; |
| 121 | memset(&statbuf, 0, sizeof(statbuf)); |
| 122 | stat(buffer, &statbuf); |
| 123 | |
| 124 | if (entry->d_type == DT_DIR) { |
| 125 | MtpObjectHandle handle = mDatabase->addFile(buffer, MTP_FORMAT_ASSOCIATION, |
| 126 | parent, mStorageID, 0, 0, statbuf.st_mtime); |
| 127 | scanDirectory(buffer, handle); |
| 128 | } else if (entry->d_type == DT_REG) { |
| 129 | mDatabase->addFile(buffer, MTP_FORMAT_UNDEFINED, parent, mStorageID, |
| 130 | statbuf.st_size, 0, statbuf.st_mtime); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | closedir(dir); |
| 135 | return 0; |
| 136 | } |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 137 | |
| 138 | } // namespace android |