blob: 941e3038d2d9ed5e90d060a4d214d47201f99396 [file] [log] [blame]
Mike Lockwood56118b52010-05-11 17:16:59 -04001/*
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 Lockwood3e6616d2010-06-29 18:11:52 -040017#define LOG_TAG "MtpStorage"
18
19#include "MtpDebug.h"
Mike Lockwood56118b52010-05-11 17:16:59 -040020#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 Lockwood8d3257a2010-05-14 10:10:36 -040033namespace android {
Mike Lockwood56118b52010-05-11 17:16:59 -040034
Mike Lockwood7ae938be2011-04-05 10:21:27 -040035MtpStorage::MtpStorage(MtpStorageID id, const char* filePath,
Mike Lockwood7a59dd22011-07-11 09:18:03 -040036 const char* description, uint64_t reserveSpace,
37 bool removable, uint64_t maxFileSize)
Mike Lockwood56118b52010-05-11 17:16:59 -040038 : mStorageID(id),
39 mFilePath(filePath),
Mike Lockwood7ae938be2011-04-05 10:21:27 -040040 mDescription(description),
Mike Lockwood7f36b192010-12-12 12:17:43 -080041 mMaxCapacity(0),
Mike Lockwood7a59dd22011-07-11 09:18:03 -040042 mMaxFileSize(maxFileSize),
Mike Lockwood51690542011-05-09 20:16:05 -070043 mReserveSpace(reserveSpace),
44 mRemovable(removable)
Mike Lockwood56118b52010-05-11 17:16:59 -040045{
Mike Lockwoodf26a5862011-01-21 21:00:54 -080046 LOGV("MtpStorage id: %d path: %s\n", id, filePath);
Mike Lockwood56118b52010-05-11 17:16:59 -040047}
48
49MtpStorage::~MtpStorage() {
50}
51
52int MtpStorage::getType() const {
Mike Lockwood51690542011-05-09 20:16:05 -070053 return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM : MTP_STORAGE_FIXED_RAM);
Mike Lockwood56118b52010-05-11 17:16:59 -040054}
55
56int MtpStorage::getFileSystemType() const {
57 return MTP_STORAGE_FILESYSTEM_HIERARCHICAL;
58}
59
60int MtpStorage::getAccessCapability() const {
61 return MTP_STORAGE_READ_WRITE;
62}
63
64uint64_t MtpStorage::getMaxCapacity() {
65 if (mMaxCapacity == 0) {
66 struct statfs stat;
Mike Lockwood467ca0d2011-02-18 09:07:14 -050067 if (statfs(getPath(), &stat))
Mike Lockwood56118b52010-05-11 17:16:59 -040068 return -1;
69 mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
70 }
71 return mMaxCapacity;
72}
73
74uint64_t MtpStorage::getFreeSpace() {
75 struct statfs stat;
Mike Lockwood467ca0d2011-02-18 09:07:14 -050076 if (statfs(getPath(), &stat))
Mike Lockwood56118b52010-05-11 17:16:59 -040077 return -1;
Mike Lockwood7f36b192010-12-12 12:17:43 -080078 uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
79 return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0);
Mike Lockwood56118b52010-05-11 17:16:59 -040080}
81
82const char* MtpStorage::getDescription() const {
Mike Lockwood7ae938be2011-04-05 10:21:27 -040083 return (const char *)mDescription;
Mike Lockwood56118b52010-05-11 17:16:59 -040084}
85
Mike Lockwood8d3257a2010-05-14 10:10:36 -040086} // namespace android