1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <stdint.h>
18 #include <string.h>
19 
20 #include <libfiemap/fiemap_status.h>
21 
22 namespace android::snapshot {
23 
24 // SnapshotManager functions return either bool or Return objects. "Return" types provides
25 // more information about the reason of the failure.
26 class Return {
27     using FiemapStatus = android::fiemap::FiemapStatus;
28 
29   public:
30     enum class ErrorCode : int32_t {
31         SUCCESS = static_cast<int32_t>(FiemapStatus::ErrorCode::SUCCESS),
32         ERROR = static_cast<int32_t>(FiemapStatus::ErrorCode::ERROR),
33         NO_SPACE = static_cast<int32_t>(FiemapStatus::ErrorCode::NO_SPACE),
34     };
error_code()35     ErrorCode error_code() const { return error_code_; }
is_ok()36     bool is_ok() const { return error_code() == ErrorCode::SUCCESS; }
37     operator bool() const { return is_ok(); }
38     // Total required size on /userdata.
required_size()39     uint64_t required_size() const { return required_size_; }
40     std::string string() const;
41 
Ok()42     static Return Ok() { return Return(ErrorCode::SUCCESS); }
Error()43     static Return Error() { return Return(ErrorCode::ERROR); }
NoSpace(uint64_t size)44     static Return NoSpace(uint64_t size) { return Return(ErrorCode::NO_SPACE, size); }
45     // Does not set required_size_ properly even when status.error_code() == NO_SPACE.
Return(const FiemapStatus & status)46     explicit Return(const FiemapStatus& status)
47         : error_code_(FromFiemapStatusErrorCode(status.error_code())), required_size_(0) {}
48 
49   private:
50     ErrorCode error_code_;
51     uint64_t required_size_;
52     Return(ErrorCode error_code, uint64_t required_size = 0)
error_code_(error_code)53         : error_code_(error_code), required_size_(required_size) {}
54 
55     // FiemapStatus::ErrorCode -> ErrorCode
56     static ErrorCode FromFiemapStatusErrorCode(FiemapStatus::ErrorCode error_code);
57 };
58 
59 }  // namespace android::snapshot
60