Home | History | Annotate | Download | only in payload_consumer
      1 //
      2 // Copyright (C) 2012 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 
     17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
     18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
     19 
     20 #include <sys/stat.h>
     21 #include <sys/types.h>
     22 
     23 #include <string>
     24 #include <vector>
     25 
     26 #include <brillo/streams/stream.h>
     27 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
     28 
     29 #include "update_engine/common/action.h"
     30 #include "update_engine/common/hash_calculator.h"
     31 #include "update_engine/payload_consumer/install_plan.h"
     32 
     33 // This action will hash all the partitions of a single slot involved in the
     34 // update (either source or target slot). The hashes are then either stored in
     35 // the InstallPlan (for source partitions) or verified against it (for target
     36 // partitions).
     37 
     38 namespace chromeos_update_engine {
     39 
     40 // The mode we are running the FilesystemVerifier on. On kComputeSourceHash mode
     41 // it computes the source_hash of all the partitions in the InstallPlan, based
     42 // on the already populated source_size values. On kVerifyTargetHash it computes
     43 // the hash on the target partitions based on the already populated size and
     44 // verifies it matches the one in the target_hash in the InstallPlan.
     45 enum class VerifierMode {
     46   kComputeSourceHash,
     47   kVerifyTargetHash,
     48   kVerifySourceHash,
     49 };
     50 
     51 class FilesystemVerifierAction : public InstallPlanAction {
     52  public:
     53   FilesystemVerifierAction(const BootControlInterface* boot_control,
     54                            VerifierMode verifier_mode);
     55 
     56   void PerformAction() override;
     57   void TerminateProcessing() override;
     58 
     59   // Used for testing. Return true if Cleanup() has not yet been called due
     60   // to a callback upon the completion or cancellation of the verifier action.
     61   // A test should wait until IsCleanupPending() returns false before
     62   // terminating the main loop.
     63   bool IsCleanupPending() const;
     64 
     65   // Debugging/logging
     66   static std::string StaticType() { return "FilesystemVerifierAction"; }
     67   std::string Type() const override { return StaticType(); }
     68 
     69  private:
     70   friend class FilesystemVerifierActionTest;
     71   FRIEND_TEST(FilesystemVerifierActionTest,
     72               RunAsRootDetermineFilesystemSizeTest);
     73 
     74   // Starts the hashing of the current partition. If there aren't any partitions
     75   // remaining to be hashed, if finishes the action.
     76   void StartPartitionHashing();
     77 
     78   // Schedules the asynchronous read of the filesystem.
     79   void ScheduleRead();
     80 
     81   // Called from the main loop when a single read from |src_stream_| succeeds or
     82   // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
     83   void OnReadDoneCallback(size_t bytes_read);
     84   void OnReadErrorCallback(const brillo::Error* error);
     85 
     86   // When the read is done, finalize the hash checking of the current partition
     87   // and continue checking the next one.
     88   void FinishPartitionHashing();
     89 
     90   // Cleans up all the variables we use for async operations and tells the
     91   // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
     92   // true if TerminateProcessing() was called.
     93   void Cleanup(ErrorCode code);
     94 
     95   // The type of the partition that we are verifying.
     96   VerifierMode verifier_mode_;
     97 
     98   // The BootControlInterface used to get the partitions based on the slots.
     99   const BootControlInterface* const boot_control_;
    100 
    101   // The index in the install_plan_.partitions vector of the partition currently
    102   // being hashed.
    103   size_t partition_index_{0};
    104 
    105   // If not null, the FileStream used to read from the device.
    106   brillo::StreamPtr src_stream_;
    107 
    108   // Buffer for storing data we read.
    109   brillo::Blob buffer_;
    110 
    111   bool read_done_{false};  // true if reached EOF on the input stream.
    112   bool cancelled_{false};  // true if the action has been cancelled.
    113 
    114   // The install plan we're passed in via the input pipe.
    115   InstallPlan install_plan_;
    116 
    117   // Calculates the hash of the data.
    118   std::unique_ptr<HashCalculator> hasher_;
    119 
    120   // Reads and hashes this many bytes from the head of the input stream. This
    121   // field is initialized from the corresponding InstallPlan::Partition size,
    122   // when the partition starts to be hashed.
    123   int64_t remaining_size_{0};
    124 
    125   DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
    126 };
    127 
    128 }  // namespace chromeos_update_engine
    129 
    130 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
    131