blob: 4a61298f081650ebe8ea272583290a333d00e6f8 [file] [log] [blame]
Gilad Arnold6dbbd392012-07-10 16:19:11 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__
7
8#include <sys/stat.h>
9#include <sys/types.h>
Darin Petkov698d0412010-10-13 10:59:44 -070010
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <string>
Andrew de los Reyesc7020782010-04-28 10:46:04 -070012#include <vector>
Darin Petkov698d0412010-10-13 10:59:44 -070013
Andrew de los Reyesc7020782010-04-28 10:46:04 -070014#include <gio/gio.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include <glib.h>
Darin Petkov698d0412010-10-13 10:59:44 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
17
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include "update_engine/action.h"
19#include "update_engine/install_plan.h"
Darin Petkov698d0412010-10-13 10:59:44 -070020#include "update_engine/omaha_hash_calculator.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000021
22// This action will only do real work if it's a delta update. It will
Andrew de los Reyesc7020782010-04-28 10:46:04 -070023// copy the root partition to install partition, and then terminate.
adlr@google.com3defe6a2009-12-04 20:57:17 +000024
25namespace chromeos_update_engine {
26
adlr@google.com3defe6a2009-12-04 20:57:17 +000027
Chris Sosad317e402013-06-12 13:47:09 -070028class FilesystemCopierAction : public InstallPlanAction {
adlr@google.com3defe6a2009-12-04 20:57:17 +000029 public:
Gilad Arnold581c2ea2012-07-19 12:33:49 -070030 FilesystemCopierAction(bool copying_kernel_install_path, bool verify_hash);
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080031
adlr@google.com3defe6a2009-12-04 20:57:17 +000032 void PerformAction();
33 void TerminateProcessing();
34
Ben Chan2add7d72012-10-08 19:28:37 -070035 // Used for testing. Return true if Cleanup() has not yet been called due
36 // to a callback upon the completion or cancellation of the copier action.
37 // A test should wait until IsCleanupPending() returns false before
38 // terminating the glib main loop.
39 bool IsCleanupPending() const;
40
adlr@google.com3defe6a2009-12-04 20:57:17 +000041 // Used for testing, so we can copy from somewhere other than root
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080042 void set_copy_source(const std::string& path) { copy_source_ = path; }
adlr@google.com3defe6a2009-12-04 20:57:17 +000043
44 // Debugging/logging
45 static std::string StaticType() { return "FilesystemCopierAction"; }
46 std::string Type() const { return StaticType(); }
47
48 private:
Darin Petkov698d0412010-10-13 10:59:44 -070049 friend class FilesystemCopierActionTest;
50 FRIEND_TEST(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest);
51
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080052 // Ping-pong buffers generally cycle through the following states:
Darin Petkov3aefa862010-12-07 14:45:00 -080053 // Empty->Reading->Full->Writing->Empty. In hash verification mode the state
54 // is never set to Writing.
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080055 enum BufferState {
56 kBufferStateEmpty,
57 kBufferStateReading,
58 kBufferStateFull,
59 kBufferStateWriting
60 };
Darin Petkov698d0412010-10-13 10:59:44 -070061
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080062 // Callbacks from glib when the read/write operation is done.
63 void AsyncReadReadyCallback(GObject *source_object, GAsyncResult *res);
64 static void StaticAsyncReadReadyCallback(GObject *source_object,
65 GAsyncResult *res,
66 gpointer user_data);
67
68 void AsyncWriteReadyCallback(GObject *source_object, GAsyncResult *res);
69 static void StaticAsyncWriteReadyCallback(GObject *source_object,
70 GAsyncResult *res,
71 gpointer user_data);
72
73 // Based on the state of the ping-pong buffers spawns appropriate read/write
74 // actions asynchronously.
75 void SpawnAsyncActions();
76
77 // Cleans up all the variables we use for async operations and tells the
Darin Petkov3aefa862010-12-07 14:45:00 -080078 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
Darin Petkovc2e4a7d2010-12-02 14:47:06 -080079 // true if TerminateProcessing() was called.
David Zeuthena99981f2013-04-29 13:42:47 -070080 void Cleanup(ErrorCode code);
Darin Petkov698d0412010-10-13 10:59:44 -070081
82 // Determine, if possible, the source file system size to avoid copying the
83 // whole partition. Currently this supports only the root file system assuming
84 // it's ext3-compatible.
85 void DetermineFilesystemSize(int fd);
86
Andrew de los Reyesf9185172010-05-03 11:07:05 -070087 // If true, this action is copying to the kernel_install_path from
88 // the install plan, otherwise it's copying just to the install_path.
89 const bool copying_kernel_install_path_;
Darin Petkov698d0412010-10-13 10:59:44 -070090
Darin Petkov3aefa862010-12-07 14:45:00 -080091 // If true, this action is running in applied update hash verification mode --
92 // it computes a hash for the target install path and compares it against the
93 // expected value.
94 const bool verify_hash_;
95
Andrew de los Reyesc7020782010-04-28 10:46:04 -070096 // The path to copy from. If empty (the default), the source is from the
97 // passed in InstallPlan.
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080098 std::string copy_source_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000099
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700100 // If non-NULL, these are GUnixInputStream objects for the opened
101 // source/destination partitions.
102 GInputStream* src_stream_;
103 GOutputStream* dst_stream_;
Darin Petkov698d0412010-10-13 10:59:44 -0700104
Darin Petkovc2e4a7d2010-12-02 14:47:06 -0800105 // Ping-pong buffers for storing data we read/write. Only one buffer is being
106 // read at a time and only one buffer is being written at a time.
107 std::vector<char> buffer_[2];
Darin Petkov698d0412010-10-13 10:59:44 -0700108
Darin Petkovc2e4a7d2010-12-02 14:47:06 -0800109 // The state of each buffer.
110 BufferState buffer_state_[2];
Darin Petkov698d0412010-10-13 10:59:44 -0700111
Darin Petkovc2e4a7d2010-12-02 14:47:06 -0800112 // Number of valid elements in |buffer_| if its state is kBufferStateFull.
113 std::vector<char>::size_type buffer_valid_size_[2];
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700114
Darin Petkovc2e4a7d2010-12-02 14:47:06 -0800115 // The cancellable objects for the in-flight async calls.
116 GCancellable* canceller_[2];
117
118 bool read_done_; // true if reached EOF on the input stream.
119 bool failed_; // true if the action has failed.
120 bool cancelled_; // true if the action has been cancelled.
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700121
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122 // The install plan we're passed in via the input pipe.
123 InstallPlan install_plan_;
Darin Petkov698d0412010-10-13 10:59:44 -0700124
125 // Calculates the hash of the copied data.
126 OmahaHashCalculator hasher_;
127
128 // Copies and hashes this many bytes from the head of the input stream. This
129 // field is initialized when the action is started and decremented as more
130 // bytes get copied.
131 int64_t filesystem_size_;
132
adlr@google.com3defe6a2009-12-04 20:57:17 +0000133 DISALLOW_COPY_AND_ASSIGN(FilesystemCopierAction);
134};
135
136} // namespace chromeos_update_engine
137
138#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__