dm_table.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "libdm/dm_table.h"
  17. #include <android-base/logging.h>
  18. #include <android-base/macros.h>
  19. namespace android {
  20. namespace dm {
  21. bool DmTable::AddTarget(std::unique_ptr<DmTarget>&& target) {
  22. if (!target->Valid()) {
  23. return false;
  24. }
  25. targets_.push_back(std::move(target));
  26. return true;
  27. }
  28. bool DmTable::RemoveTarget(std::unique_ptr<DmTarget>&& /* target */) {
  29. return true;
  30. }
  31. bool DmTable::valid() const {
  32. if (targets_.empty()) {
  33. LOG(ERROR) << "Device-mapper table must have at least one target.";
  34. return "";
  35. }
  36. if (targets_[0]->start() != 0) {
  37. LOG(ERROR) << "Device-mapper table must start at logical sector 0.";
  38. return "";
  39. }
  40. return true;
  41. }
  42. uint64_t DmTable::num_sectors() const {
  43. return valid() ? num_sectors_ : 0;
  44. }
  45. // Returns a string representation of the table that is ready to be passed
  46. // down to the kernel for loading.
  47. //
  48. // Implementation must verify there are no gaps in the table, table starts
  49. // with sector == 0, and iterate over each target to get its table
  50. // serialized.
  51. std::string DmTable::Serialize() const {
  52. if (!valid()) {
  53. return "";
  54. }
  55. std::string table;
  56. for (const auto& target : targets_) {
  57. table += target->Serialize();
  58. }
  59. return table;
  60. }
  61. } // namespace dm
  62. } // namespace android