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 #define LOG_TAG "MtpStorage"
18 
19 #include "MtpDebug.h"
20 #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 
32 namespace android {
33 
MtpStorage(MtpStorageID id,const char * filePath,const char * description,bool removable,uint64_t maxFileSize)34 MtpStorage::MtpStorage(MtpStorageID id, const char* filePath,
35         const char* description, bool removable, uint64_t maxFileSize)
36     :   mStorageID(id),
37         mFilePath(filePath),
38         mDescription(description),
39         mMaxCapacity(0),
40         mMaxFileSize(maxFileSize),
41         mRemovable(removable)
42 {
43     ALOGV("MtpStorage id: %d path: %s\n", id, filePath);
44 }
45 
~MtpStorage()46 MtpStorage::~MtpStorage() {
47 }
48 
getType() const49 int MtpStorage::getType() const {
50     return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM :  MTP_STORAGE_FIXED_RAM);
51 }
52 
getFileSystemType() const53 int MtpStorage::getFileSystemType() const {
54     return MTP_STORAGE_FILESYSTEM_HIERARCHICAL;
55 }
56 
getAccessCapability() const57 int MtpStorage::getAccessCapability() const {
58     return MTP_STORAGE_READ_WRITE;
59 }
60 
getMaxCapacity()61 uint64_t MtpStorage::getMaxCapacity() {
62     if (mMaxCapacity == 0) {
63         struct statfs   stat;
64         if (statfs(getPath(), &stat))
65             return -1;
66         mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
67     }
68     return mMaxCapacity;
69 }
70 
getFreeSpace()71 uint64_t MtpStorage::getFreeSpace() {
72     struct statfs   stat;
73     if (statfs(getPath(), &stat))
74         return -1;
75     return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
76 }
77 
getDescription() const78 const char* MtpStorage::getDescription() const {
79     return (const char *)mDescription;
80 }
81 
82 }  // namespace android
83