VintfObjectRecovery.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2017 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 "VintfObjectRecovery.h"
  17. #include <sys/mount.h>
  18. #include <set>
  19. #include <android-base/logging.h>
  20. #include <android-base/strings.h>
  21. #include <fs_mgr.h>
  22. #include <fs_mgr/roots.h>
  23. namespace android {
  24. namespace vintf {
  25. namespace details {
  26. using android::base::StartsWith;
  27. static const char* const kMountImageRootDir = "/mnt";
  28. static const char* const kSystemImageRootDir = "/mnt/system";
  29. class RecoveryPartitionMounter {
  30. public:
  31. RecoveryPartitionMounter() {}
  32. ~RecoveryPartitionMounter() {
  33. for (const auto& pair : mounted_) {
  34. if (umount(pair.second.c_str()) != 0) {
  35. PLOG(ERROR) << "Cannot unmount " << pair.first << " at " << pair.second;
  36. }
  37. }
  38. mounted_.clear();
  39. }
  40. status_t mount(const std::string& path) {
  41. if (path == "/system") {
  42. return mount(android::fs_mgr::GetSystemRoot(), kSystemImageRootDir);
  43. }
  44. return mount(path, kMountImageRootDir + path);
  45. }
  46. private:
  47. std::map<std::string, std::string> mounted_;
  48. status_t mount(const std::string& path, const std::string& mountPoint) {
  49. if (mounted_.find(path) != mounted_.end()) {
  50. return OK;
  51. }
  52. android::fs_mgr::Fstab fstab;
  53. if (!android::fs_mgr::ReadDefaultFstab(&fstab)) {
  54. return errno ? -errno : UNKNOWN_ERROR;
  55. }
  56. if (!android::fs_mgr::EnsurePathMounted(&fstab, path, mountPoint)) {
  57. return errno ? -errno : UNKNOWN_ERROR;
  58. }
  59. mounted_.emplace(path, mountPoint);
  60. return OK;
  61. }
  62. };
  63. class RecoveryFileSystem : public FileSystem {
  64. public:
  65. RecoveryFileSystem() = default;
  66. status_t fetch(const std::string& path, std::string* fetched, std::string* error) const {
  67. const FileSystem* fs = nullptr;
  68. status_t err = getFileSystem(path, &fs, error);
  69. if (err != OK) return err;
  70. return fs->fetch(path, fetched, error);
  71. }
  72. status_t listFiles(const std::string& path, std::vector<std::string>* out,
  73. std::string* error) const {
  74. const FileSystem* fs = nullptr;
  75. status_t err = getFileSystem(path, &fs, error);
  76. if (err != OK) return err;
  77. return fs->listFiles(path, out, error);
  78. }
  79. private:
  80. FileSystemUnderPath mSystemFileSystem{kSystemImageRootDir};
  81. FileSystemUnderPath mMntFileSystem{kMountImageRootDir};
  82. std::unique_ptr<RecoveryPartitionMounter> mMounter =
  83. std::make_unique<RecoveryPartitionMounter>();
  84. status_t getFileSystem(const std::string& path, const FileSystem** fs,
  85. std::string* error) const {
  86. auto partition = GetPartition(path);
  87. if (partition.empty()) {
  88. if (error) *error = "Cannot list or fetch relative path " + path;
  89. return NAME_NOT_FOUND;
  90. }
  91. status_t err = mMounter->mount(partition);
  92. if (err != OK) {
  93. // in recovery, ignore mount errors and assume the file does not exist.
  94. if (error) *error = "Cannot mount for path " + path + ": " + strerror(-err);
  95. return NAME_NOT_FOUND;
  96. }
  97. // /system files are under /mnt/system/system because system.img contains the root dir.
  98. if (partition == "/system") {
  99. *fs = &mSystemFileSystem;
  100. } else {
  101. *fs = &mMntFileSystem;
  102. }
  103. return OK;
  104. }
  105. // /system -> /system
  106. // /system/foo -> /system
  107. static std::string GetPartition(const std::string& path) {
  108. if (path.empty()) return "";
  109. if (path[0] != '/') return "";
  110. auto idx = path.find('/', 1);
  111. if (idx == std::string::npos) return path;
  112. return path.substr(0, idx);
  113. }
  114. };
  115. } // namespace details
  116. // static
  117. int32_t VintfObjectRecovery::CheckCompatibility(const std::vector<std::string>& xmls,
  118. std::string* error) {
  119. auto vintfObject = VintfObject::Builder()
  120. .setFileSystem(std::make_unique<details::RecoveryFileSystem>())
  121. .build();
  122. return vintfObject->checkCompatibility(xmls, error);
  123. }
  124. } // namespace vintf
  125. } // namespace android