No Description

mounts.cpp 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2007 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 "mounts.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <mntent.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/mount.h>
  24. #include <string>
  25. #include <vector>
  26. #include <android-base/logging.h>
  27. struct MountedVolume {
  28. std::string device;
  29. std::string mount_point;
  30. std::string filesystem;
  31. std::string flags;
  32. };
  33. std::vector<MountedVolume*> g_mounts_state;
  34. bool scan_mounted_volumes() {
  35. for (size_t i = 0; i < g_mounts_state.size(); ++i) {
  36. delete g_mounts_state[i];
  37. }
  38. g_mounts_state.clear();
  39. // Open and read mount table entries.
  40. FILE* fp = setmntent("/proc/mounts", "re");
  41. if (fp == NULL) {
  42. return false;
  43. }
  44. mntent* e;
  45. while ((e = getmntent(fp)) != NULL) {
  46. MountedVolume* v = new MountedVolume;
  47. v->device = e->mnt_fsname;
  48. v->mount_point = e->mnt_dir;
  49. v->filesystem = e->mnt_type;
  50. v->flags = e->mnt_opts;
  51. g_mounts_state.push_back(v);
  52. }
  53. endmntent(fp);
  54. return true;
  55. }
  56. MountedVolume* find_mounted_volume_by_mount_point(const char* mount_point) {
  57. for (size_t i = 0; i < g_mounts_state.size(); ++i) {
  58. if (g_mounts_state[i]->mount_point == mount_point) return g_mounts_state[i];
  59. }
  60. return nullptr;
  61. }
  62. int unmount_mounted_volume(MountedVolume* volume) {
  63. // Intentionally pass the empty string to umount if the caller tries to unmount a volume they
  64. // already unmounted using this function.
  65. std::string mount_point = volume->mount_point;
  66. volume->mount_point.clear();
  67. int result = umount(mount_point.c_str());
  68. if (result == -1) {
  69. PLOG(WARNING) << "Failed to umount " << mount_point;
  70. }
  71. return result;
  72. }
  73. int unmount_mounted_volume_detach(MountedVolume* volume) {
  74. // Intentionally pass the empty string to umount if the caller tries to unmount a volume they
  75. // already unmounted using this function.
  76. std::string mount_point = volume->mount_point;
  77. volume->mount_point.clear();
  78. int result = umount2(mount_point.c_str(), MNT_DETACH);
  79. if (result == -1) {
  80. PLOG(WARNING) << "Failed to umount " << mount_point;
  81. }
  82. return result;
  83. }