Exfat.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2018 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. #include <sys/mount.h>
  17. #include <android-base/logging.h>
  18. #include <android-base/stringprintf.h>
  19. #include <logwrap/logwrap.h>
  20. #include "Exfat.h"
  21. #include "Utils.h"
  22. using android::base::StringPrintf;
  23. namespace android {
  24. namespace vold {
  25. namespace exfat {
  26. static const char* kMkfsPath = "/system/bin/mkfs.exfat";
  27. static const char* kFsckPath = "/system/bin/fsck.exfat";
  28. bool IsSupported() {
  29. return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
  30. IsFilesystemSupported("exfat");
  31. }
  32. status_t Check(const std::string& source) {
  33. std::vector<std::string> cmd;
  34. cmd.push_back(kFsckPath);
  35. cmd.push_back(source);
  36. int rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
  37. if (rc == 0) {
  38. LOG(INFO) << "Check OK";
  39. return 0;
  40. } else {
  41. LOG(ERROR) << "Check failed (code " << rc << ")";
  42. errno = EIO;
  43. return -1;
  44. }
  45. }
  46. status_t Mount(const std::string& source, const std::string& target, int ownerUid, int ownerGid,
  47. int permMask) {
  48. int mountFlags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME | MS_NOEXEC;
  49. auto mountData = android::base::StringPrintf("uid=%d,gid=%d,fmask=%o,dmask=%o", ownerUid,
  50. ownerGid, permMask, permMask);
  51. if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
  52. return 0;
  53. }
  54. PLOG(ERROR) << "Mount failed; attempting read-only";
  55. mountFlags |= MS_RDONLY;
  56. if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
  57. return 0;
  58. }
  59. return -1;
  60. }
  61. status_t Format(const std::string& source) {
  62. std::vector<std::string> cmd;
  63. cmd.push_back(kMkfsPath);
  64. cmd.push_back("-n");
  65. cmd.push_back("android");
  66. cmd.push_back(source);
  67. int rc = ForkExecvp(cmd);
  68. if (rc == 0) {
  69. LOG(INFO) << "Format OK";
  70. return 0;
  71. } else {
  72. LOG(ERROR) << "Format failed (code " << rc << ")";
  73. errno = EIO;
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. } // namespace exfat
  79. } // namespace vold
  80. } // namespace android