blob: 435f5d4825debb9d9f3e4e556209e69d2726e79d [file] [log] [blame]
Andrew de los Reyes926d2092009-12-15 15:59:52 -08001// 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
Andrew de los Reyes1e338b82010-01-22 14:57:27 -08005// TODO(adlr): get rid of commented out lines or comment them back in.
6// Look for "// re-add" next to those comments.
7
Andrew de los Reyes926d2092009-12-15 15:59:52 -08008#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12#include <set>
13#include <string>
14#include <vector>
15
16#include <gflags/gflags.h>
17#include <glib.h>
18#include "chromeos/obsolete_logging.h"
19
20#include "update_engine/delta_diff_generator.h"
21#include "update_engine/delta_diff_parser.h"
22#include "update_engine/filesystem_copier_action.h"
Andrew de los Reyes1e338b82010-01-22 14:57:27 -080023// #include "update_engine/install_action.h" // re-add
Andrew de los Reyes926d2092009-12-15 15:59:52 -080024#include "update_engine/install_plan.h"
25#include "update_engine/test_utils.h"
26#include "update_engine/update_metadata.pb.h"
27#include "update_engine/utils.h"
28
29// This file contains a simple program that unpacks a delta diff into a
30// directory.
31
32using std::set;
33using std::string;
34using std::vector;
35using std::tr1::shared_ptr;
36
37DEFINE_string(delta, "", "the delta file");
38DEFINE_string(output_dev, "", "the output device");
39DEFINE_string(root, "", "the fake system root");
40DEFINE_string(dump_delta, "", "path to delta file to dump");
41
42namespace chromeos_update_engine {
43
44class TestInstaller : public ActionProcessorDelegate {
45 public:
46 TestInstaller(GMainLoop *loop,
47 const string& sys_root,
48 const string& delta_path,
49 const string& install_path)
50 : loop_(loop),
51 sys_root_(sys_root),
Andrew de los Reyes926d2092009-12-15 15:59:52 -080052 install_path_(install_path) {}
53 void Update();
54
55 // Delegate method:
56 void ProcessingDone(const ActionProcessor* processor, bool success);
57 private:
58 vector<shared_ptr<AbstractAction> > actions_;
59 ActionProcessor processor_;
60 GMainLoop *loop_;
61 string sys_root_;
Andrew de los Reyes926d2092009-12-15 15:59:52 -080062 string install_path_;
63};
64
65// Returns true on success. If there was no update available, that's still
66// success.
67// If force_full is true, try to force a full update.
68void TestInstaller::Update() {
69 CHECK(!processor_.IsRunning());
70 processor_.set_delegate(this);
71
72 // Actions:
73 shared_ptr<ObjectFeederAction<InstallPlan> > object_feeder_action(
74 new ObjectFeederAction<InstallPlan>);
75 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
76 new FilesystemCopierAction);
Andrew de los Reyes1e338b82010-01-22 14:57:27 -080077 // shared_ptr<InstallAction> install_action( // re-add
78 // new InstallAction);
Andrew de los Reyes926d2092009-12-15 15:59:52 -080079
80 actions_.push_back(object_feeder_action);
81 actions_.push_back(filesystem_copier_action);
Andrew de los Reyes1e338b82010-01-22 14:57:27 -080082 // actions_.push_back(install_action); // re-add
Andrew de los Reyes926d2092009-12-15 15:59:52 -080083
84 // Enqueue the actions
85 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
86 it != actions_.end(); ++it) {
87 processor_.EnqueueAction(it->get());
88 }
89
90 // Bond them together. We have to use the leaf-types when calling
91 // BondActions().
92 BondActions(object_feeder_action.get(), filesystem_copier_action.get());
Andrew de los Reyes1e338b82010-01-22 14:57:27 -080093 // re-add
94 // BondActions(filesystem_copier_action.get(), install_action.get());
Andrew de los Reyes926d2092009-12-15 15:59:52 -080095
96 InstallPlan install_plan(false,
97 "",
98 "",
Andrew de los Reyes926d2092009-12-15 15:59:52 -080099 install_path_);
100 filesystem_copier_action->set_copy_source(sys_root_);
101 object_feeder_action->set_obj(install_plan);
102 processor_.StartProcessing();
103}
104
105void TestInstaller::ProcessingDone(const ActionProcessor* processor,
106 bool success) {
107 LOG(INFO) << "install " << (success ? "succeeded" : "failed");
108 actions_.clear();
109 g_main_loop_quit(loop_);
110}
111
112gboolean TestInstallInMainLoop(void* arg) {
113 TestInstaller* test_installer = reinterpret_cast<TestInstaller*>(arg);
114 test_installer->Update();
115 return FALSE; // Don't call this callback function again
116}
117
118
119void usage(const char* argv0) {
120 printf("usage: %s --root=system_root --delta=delta_file "
121 "--output_dev=output_dev\n", argv0);
122 exit(1);
123}
124
125bool Main(int argc, char** argv) {
126 g_thread_init(NULL);
127 google::ParseCommandLineFlags(&argc, &argv, true);
128 logging::InitLogging("",
129 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
130 logging::DONT_LOCK_LOG_FILE,
131 logging::APPEND_TO_OLD_LOG_FILE);
132
133 LOG(INFO) << "starting";
134
Andrew de los Reyes926d2092009-12-15 15:59:52 -0800135 // Create the single GMainLoop
136 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
137
138 chromeos_update_engine::TestInstaller test_installer(loop,
139 FLAGS_root,
140 FLAGS_delta,
141 FLAGS_output_dev);
142
143 g_timeout_add(0, &chromeos_update_engine::TestInstallInMainLoop,
144 &test_installer);
145
146 g_main_loop_run(loop);
147 g_main_loop_unref(loop);
148
149 LOG(INFO) << "Done.";
150 return true;
151}
152
153} // namespace chromeos_update_engine
154
155int main(int argc, char** argv) {
156 return chromeos_update_engine::Main(argc, argv) ? 0 : 1;
157}