blob: a147325bee5c2b09905d58f39c8f4681da06b3f7 [file] [log] [blame]
Mike Lockwood16864ba2010-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 Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "MtpStorage"
18
19#include "MtpDebug.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040020#include "MtpStorage.h"
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <sys/statfs.h>
25#include <unistd.h>
26#include <dirent.h>
27#include <errno.h>
28#include <string.h>
29#include <stdio.h>
30#include <limits.h>
31
Mike Lockwood7850ef92010-05-14 10:10:36 -040032namespace android {
Mike Lockwood16864ba2010-05-11 17:16:59 -040033
Mike Lockwood0241cac2011-04-05 10:21:27 -040034MtpStorage::MtpStorage(MtpStorageID id, const char* filePath,
Jerry Zhange5aa05d2017-10-13 12:14:42 -070035 const char* description, bool removable, uint64_t maxFileSize)
Mike Lockwood16864ba2010-05-11 17:16:59 -040036 : mStorageID(id),
37 mFilePath(filePath),
Mike Lockwood0241cac2011-04-05 10:21:27 -040038 mDescription(description),
Mike Lockwood20c3be02010-12-12 12:17:43 -080039 mMaxCapacity(0),
Mike Lockwood9b88b722011-07-11 09:18:03 -040040 mMaxFileSize(maxFileSize),
Mike Lockwood7efab422011-05-09 20:16:05 -070041 mRemovable(removable)
Mike Lockwood16864ba2010-05-11 17:16:59 -040042{
Steve Block3856b092011-10-20 11:56:00 +010043 ALOGV("MtpStorage id: %d path: %s\n", id, filePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -040044}
45
46MtpStorage::~MtpStorage() {
47}
48
49int MtpStorage::getType() const {
Mike Lockwood7efab422011-05-09 20:16:05 -070050 return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM : MTP_STORAGE_FIXED_RAM);
Mike Lockwood16864ba2010-05-11 17:16:59 -040051}
52
53int MtpStorage::getFileSystemType() const {
54 return MTP_STORAGE_FILESYSTEM_HIERARCHICAL;
55}
56
57int MtpStorage::getAccessCapability() const {
58 return MTP_STORAGE_READ_WRITE;
59}
60
61uint64_t MtpStorage::getMaxCapacity() {
62 if (mMaxCapacity == 0) {
63 struct statfs stat;
Mike Lockwooda8494402011-02-18 09:07:14 -050064 if (statfs(getPath(), &stat))
Mike Lockwood16864ba2010-05-11 17:16:59 -040065 return -1;
66 mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
67 }
68 return mMaxCapacity;
69}
70
71uint64_t MtpStorage::getFreeSpace() {
72 struct statfs stat;
Mike Lockwooda8494402011-02-18 09:07:14 -050073 if (statfs(getPath(), &stat))
Mike Lockwood16864ba2010-05-11 17:16:59 -040074 return -1;
Jerry Zhange5aa05d2017-10-13 12:14:42 -070075 return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
Mike Lockwood16864ba2010-05-11 17:16:59 -040076}
77
78const char* MtpStorage::getDescription() const {
Mike Lockwood0241cac2011-04-05 10:21:27 -040079 return (const char *)mDescription;
Mike Lockwood16864ba2010-05-11 17:16:59 -040080}
81
Mike Lockwood7850ef92010-05-14 10:10:36 -040082} // namespace android