Andrew de los Reyes | 926d209 | 2009-12-15 15:59:52 -0800 | [diff] [blame^] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <gflags/gflags.h> |
| 14 | #include <glib.h> |
| 15 | #include "chromeos/obsolete_logging.h" |
| 16 | |
| 17 | #include "update_engine/delta_diff_generator.h" |
| 18 | #include "update_engine/delta_diff_parser.h" |
| 19 | #include "update_engine/filesystem_copier_action.h" |
| 20 | #include "update_engine/install_action.h" |
| 21 | #include "update_engine/install_plan.h" |
| 22 | #include "update_engine/test_utils.h" |
| 23 | #include "update_engine/update_metadata.pb.h" |
| 24 | #include "update_engine/utils.h" |
| 25 | |
| 26 | // This file contains a simple program that unpacks a delta diff into a |
| 27 | // directory. |
| 28 | |
| 29 | using std::set; |
| 30 | using std::string; |
| 31 | using std::vector; |
| 32 | using std::tr1::shared_ptr; |
| 33 | |
| 34 | DEFINE_string(delta, "", "the delta file"); |
| 35 | DEFINE_string(output_dev, "", "the output device"); |
| 36 | DEFINE_string(root, "", "the fake system root"); |
| 37 | DEFINE_string(dump_delta, "", "path to delta file to dump"); |
| 38 | |
| 39 | namespace chromeos_update_engine { |
| 40 | |
| 41 | class TestInstaller : public ActionProcessorDelegate { |
| 42 | public: |
| 43 | TestInstaller(GMainLoop *loop, |
| 44 | const string& sys_root, |
| 45 | const string& delta_path, |
| 46 | const string& install_path) |
| 47 | : loop_(loop), |
| 48 | sys_root_(sys_root), |
| 49 | delta_path_(delta_path), |
| 50 | install_path_(install_path) {} |
| 51 | void Update(); |
| 52 | |
| 53 | // Delegate method: |
| 54 | void ProcessingDone(const ActionProcessor* processor, bool success); |
| 55 | private: |
| 56 | vector<shared_ptr<AbstractAction> > actions_; |
| 57 | ActionProcessor processor_; |
| 58 | GMainLoop *loop_; |
| 59 | string sys_root_; |
| 60 | string delta_path_; |
| 61 | string install_path_; |
| 62 | }; |
| 63 | |
| 64 | // Returns true on success. If there was no update available, that's still |
| 65 | // success. |
| 66 | // If force_full is true, try to force a full update. |
| 67 | void TestInstaller::Update() { |
| 68 | CHECK(!processor_.IsRunning()); |
| 69 | processor_.set_delegate(this); |
| 70 | |
| 71 | // Actions: |
| 72 | shared_ptr<ObjectFeederAction<InstallPlan> > object_feeder_action( |
| 73 | new ObjectFeederAction<InstallPlan>); |
| 74 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
| 75 | new FilesystemCopierAction); |
| 76 | shared_ptr<InstallAction> install_action( |
| 77 | new InstallAction); |
| 78 | |
| 79 | actions_.push_back(object_feeder_action); |
| 80 | actions_.push_back(filesystem_copier_action); |
| 81 | actions_.push_back(install_action); |
| 82 | |
| 83 | // Enqueue the actions |
| 84 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 85 | it != actions_.end(); ++it) { |
| 86 | processor_.EnqueueAction(it->get()); |
| 87 | } |
| 88 | |
| 89 | // Bond them together. We have to use the leaf-types when calling |
| 90 | // BondActions(). |
| 91 | BondActions(object_feeder_action.get(), filesystem_copier_action.get()); |
| 92 | BondActions(filesystem_copier_action.get(), install_action.get()); |
| 93 | |
| 94 | InstallPlan install_plan(false, |
| 95 | "", |
| 96 | "", |
| 97 | delta_path_, |
| 98 | install_path_); |
| 99 | filesystem_copier_action->set_copy_source(sys_root_); |
| 100 | object_feeder_action->set_obj(install_plan); |
| 101 | processor_.StartProcessing(); |
| 102 | } |
| 103 | |
| 104 | void TestInstaller::ProcessingDone(const ActionProcessor* processor, |
| 105 | bool success) { |
| 106 | LOG(INFO) << "install " << (success ? "succeeded" : "failed"); |
| 107 | actions_.clear(); |
| 108 | g_main_loop_quit(loop_); |
| 109 | } |
| 110 | |
| 111 | gboolean TestInstallInMainLoop(void* arg) { |
| 112 | TestInstaller* test_installer = reinterpret_cast<TestInstaller*>(arg); |
| 113 | test_installer->Update(); |
| 114 | return FALSE; // Don't call this callback function again |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void usage(const char* argv0) { |
| 119 | printf("usage: %s --root=system_root --delta=delta_file " |
| 120 | "--output_dev=output_dev\n", argv0); |
| 121 | exit(1); |
| 122 | } |
| 123 | |
| 124 | bool Main(int argc, char** argv) { |
| 125 | g_thread_init(NULL); |
| 126 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 127 | logging::InitLogging("", |
| 128 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 129 | logging::DONT_LOCK_LOG_FILE, |
| 130 | logging::APPEND_TO_OLD_LOG_FILE); |
| 131 | |
| 132 | LOG(INFO) << "starting"; |
| 133 | |
| 134 | if (!FLAGS_dump_delta.empty()) { |
| 135 | CHECK(FLAGS_delta.empty()); |
| 136 | CHECK(FLAGS_root.empty()); |
| 137 | CHECK(FLAGS_output_dev.empty()); |
| 138 | |
| 139 | DeltaDiffParser parser(FLAGS_dump_delta); |
| 140 | for (DeltaDiffParser::Iterator it = parser.Begin(); |
| 141 | it != parser.End(); it.Increment()) { |
| 142 | DeltaArchiveManifest_File file = it.GetFile(); |
| 143 | const char* format = "---"; |
| 144 | ssize_t offset = -1; |
| 145 | ssize_t length = -1; |
| 146 | if (file.has_data_format()) { |
| 147 | switch (file.data_format()) { |
| 148 | case DeltaArchiveManifest_File_DataFormat_FULL: |
| 149 | format = "FULL"; |
| 150 | break; |
| 151 | case DeltaArchiveManifest_File_DataFormat_FULL_GZ: |
| 152 | format = "FULL_GZ"; |
| 153 | break; |
| 154 | case DeltaArchiveManifest_File_DataFormat_BSDIFF: |
| 155 | format = "BSDIFF"; |
| 156 | break; |
| 157 | case DeltaArchiveManifest_File_DataFormat_COURGETTE: |
| 158 | format = "COURGETTE"; |
| 159 | break; |
| 160 | default: |
| 161 | format = "???"; |
| 162 | } |
| 163 | if (file.has_data_offset()) |
| 164 | offset = file.data_offset(); |
| 165 | if (file.has_data_length()) |
| 166 | length = file.data_length(); |
| 167 | } |
| 168 | printf("%s %o %s %d %d\n", it.path().c_str(), file.mode(), format, offset, |
| 169 | length); |
| 170 | } |
| 171 | exit(0); |
| 172 | } |
| 173 | |
| 174 | CHECK(!FLAGS_delta.empty()); |
| 175 | CHECK(!FLAGS_root.empty()); |
| 176 | CHECK(!FLAGS_output_dev.empty()); |
| 177 | |
| 178 | // Create the single GMainLoop |
| 179 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 180 | |
| 181 | chromeos_update_engine::TestInstaller test_installer(loop, |
| 182 | FLAGS_root, |
| 183 | FLAGS_delta, |
| 184 | FLAGS_output_dev); |
| 185 | |
| 186 | g_timeout_add(0, &chromeos_update_engine::TestInstallInMainLoop, |
| 187 | &test_installer); |
| 188 | |
| 189 | g_main_loop_run(loop); |
| 190 | g_main_loop_unref(loop); |
| 191 | |
| 192 | LOG(INFO) << "Done."; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | } // namespace chromeos_update_engine |
| 197 | |
| 198 | int main(int argc, char** argv) { |
| 199 | return chromeos_update_engine::Main(argc, argv) ? 0 : 1; |
| 200 | } |