Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5e3e7652 authored by Yifan Hong's avatar Yifan Hong Committed by Gerrit Code Review
Browse files

Merge "SnapshotManager::Return -> Return"

parents adfb1c6d b37311d4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ filegroup {
        "snapshot.cpp",
        "snapshot_metadata_updater.cpp",
        "partition_cow_creator.cpp",
        "return.cpp",
        "utility.cpp",
    ],
}
+59 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <stdint.h>
#include <string.h>

#include <libfiemap/fiemap_status.h>

namespace android::snapshot {

// SnapshotManager functions return either bool or Return objects. "Return" types provides
// more information about the reason of the failure.
class Return {
    using FiemapStatus = android::fiemap::FiemapStatus;

  public:
    enum class ErrorCode : int32_t {
        SUCCESS = static_cast<int32_t>(FiemapStatus::ErrorCode::SUCCESS),
        ERROR = static_cast<int32_t>(FiemapStatus::ErrorCode::ERROR),
        NO_SPACE = static_cast<int32_t>(FiemapStatus::ErrorCode::NO_SPACE),
    };
    ErrorCode error_code() const { return error_code_; }
    bool is_ok() const { return error_code() == ErrorCode::SUCCESS; }
    operator bool() const { return is_ok(); }
    // Total required size on /userdata.
    uint64_t required_size() const { return required_size_; }
    std::string string() const;

    static Return Ok() { return Return(ErrorCode::SUCCESS); }
    static Return Error() { return Return(ErrorCode::ERROR); }
    static Return NoSpace(uint64_t size) { return Return(ErrorCode::NO_SPACE, size); }
    // Does not set required_size_ properly even when status.error_code() == NO_SPACE.
    explicit Return(const FiemapStatus& status)
        : error_code_(FromFiemapStatusErrorCode(status.error_code())), required_size_(0) {}

  private:
    ErrorCode error_code_;
    uint64_t required_size_;
    Return(ErrorCode error_code, uint64_t required_size = 0)
        : error_code_(error_code), required_size_(required_size) {}

    // FiemapStatus::ErrorCode -> ErrorCode
    static ErrorCode FromFiemapStatusErrorCode(FiemapStatus::ErrorCode error_code);
};

}  // namespace android::snapshot
+1 −21
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <update_engine/update_metadata.pb.h>

#include <libsnapshot/auto_device.h>
#include <libsnapshot/return.h>

#ifndef FRIEND_TEST
#define FRIEND_TEST(test_set_name, individual_test) \
@@ -91,27 +92,6 @@ class SnapshotManager final {
    using FiemapStatus = android::fiemap::FiemapStatus;

  public:
    // SnapshotManager functions return either bool or Return objects. "Return" types provides
    // more information about the reason of the failure.
    class Return : public FiemapStatus {
      public:
        // Total required size on /userdata.
        uint64_t required_size() const { return required_size_; }

        static Return Ok() { return Return(FiemapStatus::ErrorCode::SUCCESS); }
        static Return Error() { return Return(FiemapStatus::ErrorCode::ERROR); }
        static Return NoSpace(uint64_t size) {
            return Return(FiemapStatus::ErrorCode::NO_SPACE, size);
        }
        // Does not set required_size_ properly even when status.error_code() == NO_SPACE.
        explicit Return(const FiemapStatus& status) : Return(status.error_code()) {}

      private:
        uint64_t required_size_;
        Return(FiemapStatus::ErrorCode code, uint64_t required_size = 0)
            : FiemapStatus(code), required_size_(required_size) {}
    };

    // Dependency injection for testing.
    class IDeviceInfo {
      public:
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ using chromeos_update_engine::PartitionUpdate;
using testing::_;
using testing::AssertionResult;
using testing::NiceMock;
using testing::Return;

using namespace android::storage_literals;
using namespace std::string_literals;
@@ -117,6 +116,7 @@ class TestDeviceInfo : public SnapshotManager::IDeviceInfo {
class SnapshotTestPropertyFetcher : public android::fs_mgr::testing::MockPropertyFetcher {
  public:
    SnapshotTestPropertyFetcher(const std::string& slot_suffix) {
        using testing::Return;
        ON_CALL(*this, GetProperty("ro.boot.slot_suffix", _)).WillByDefault(Return(slot_suffix));
        ON_CALL(*this, GetBoolProperty("ro.boot.dynamic_partitions", _))
                .WillByDefault(Return(true));
+44 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libsnapshot/return.h>

#include <string.h>

using android::fiemap::FiemapStatus;

namespace android::snapshot {

std::string Return::string() const {
    switch (error_code()) {
        case ErrorCode::ERROR:
            return "Error";
        case ErrorCode::SUCCESS:
            [[fallthrough]];
        case ErrorCode::NO_SPACE:
            return strerror(-static_cast<int>(error_code()));
    }
}

Return::ErrorCode Return::FromFiemapStatusErrorCode(FiemapStatus::ErrorCode error_code) {
    switch (error_code) {
        case FiemapStatus::ErrorCode::SUCCESS:
        case FiemapStatus::ErrorCode::ERROR:
        case FiemapStatus::ErrorCode::NO_SPACE:
            return static_cast<ErrorCode>(error_code);
        default:
            return ErrorCode::ERROR;
    }
}
}  // namespace android::snapshot
Loading