MtpStorage.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #define LOG_TAG "MtpStorage"
  17. #include "MtpDebug.h"
  18. #include "MtpStorage.h"
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/statfs.h>
  22. #include <unistd.h>
  23. #include <dirent.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <limits.h>
  28. namespace android {
  29. MtpStorage::MtpStorage(MtpStorageID id, const char* filePath,
  30. const char* description, bool removable, uint64_t maxFileSize)
  31. : mStorageID(id),
  32. mFilePath(filePath),
  33. mDescription(description),
  34. mMaxCapacity(0),
  35. mMaxFileSize(maxFileSize),
  36. mRemovable(removable)
  37. {
  38. ALOGV("MtpStorage id: %d path: %s\n", id, filePath);
  39. }
  40. MtpStorage::~MtpStorage() {
  41. }
  42. int MtpStorage::getType() const {
  43. return (mRemovable ? MTP_STORAGE_REMOVABLE_RAM : MTP_STORAGE_FIXED_RAM);
  44. }
  45. int MtpStorage::getFileSystemType() const {
  46. return MTP_STORAGE_FILESYSTEM_HIERARCHICAL;
  47. }
  48. int MtpStorage::getAccessCapability() const {
  49. return MTP_STORAGE_READ_WRITE;
  50. }
  51. uint64_t MtpStorage::getMaxCapacity() {
  52. if (mMaxCapacity == 0) {
  53. struct statfs stat;
  54. if (statfs(getPath(), &stat))
  55. return -1;
  56. mMaxCapacity = (uint64_t)stat.f_blocks * (uint64_t)stat.f_bsize;
  57. }
  58. return mMaxCapacity;
  59. }
  60. uint64_t MtpStorage::getFreeSpace() {
  61. struct statfs stat;
  62. if (statfs(getPath(), &stat))
  63. return -1;
  64. return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
  65. }
  66. const char* MtpStorage::getDescription() const {
  67. return (const char *)mDescription;
  68. }
  69. } // namespace android