blob: 81227e441577a4511d95642d0e8fd83241fafbc9 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// 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>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08007#include <errno.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07008#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <unistd.h>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010#include <set>
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <string>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070012#include <vector>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070013#include <gflags/gflags.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include <glib.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070015#include "base/command_line.h"
Chris Masone790e62e2010-08-12 10:41:18 -070016#include "base/logging.h"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080017#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/delta_performer.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include "update_engine/subprocess.h"
20#include "update_engine/update_metadata.pb.h"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080021#include "update_engine/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000022
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070023DEFINE_string(old_dir, "",
24 "Directory where the old rootfs is loop mounted read-only");
25DEFINE_string(new_dir, "",
26 "Directory where the new rootfs is loop mounted read-only");
27DEFINE_string(old_image, "", "Path to the old rootfs");
28DEFINE_string(new_image, "", "Path to the new rootfs");
29DEFINE_string(out_file, "", "Path to output file");
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070030DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
31DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070032DEFINE_string(private_key, "", "Path to private key in .pem format");
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070033DEFINE_string(apply_delta, "",
34 "If set, apply delta over old_image. (For debugging.)");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070035
adlr@google.com3defe6a2009-12-04 20:57:17 +000036// This file contains a simple program that takes an old path, a new path,
37// and an output file as arguments and the path to an output file and
38// generates a delta that can be sent to Chrome OS clients.
39
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080040using std::set;
41using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080043
adlr@google.com3defe6a2009-12-04 20:57:17 +000044namespace chromeos_update_engine {
45
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080046namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080047
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080048bool IsDir(const char* path) {
49 struct stat stbuf;
50 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
51 return S_ISDIR(stbuf.st_mode);
52}
53
54int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070056 google::ParseCommandLineFlags(&argc, &argv, true);
57 CommandLine::Init(argc, argv);
adlr@google.com3defe6a2009-12-04 20:57:17 +000058 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070059 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080060 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
61 logging::DONT_LOCK_LOG_FILE,
62 logging::APPEND_TO_OLD_LOG_FILE);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070063 if (!FLAGS_apply_delta.empty()) {
64 if (FLAGS_old_image.empty()) {
65 LOG(FATAL) << "Must pass --old_image with --apply_delta.";
66 }
67 DeltaPerformer performer;
68 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070069 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070070 vector<char> buf(1024 * 1024);
71 int fd = open(FLAGS_apply_delta.c_str(), O_RDONLY, 0);
72 CHECK_GE(fd, 0);
73 ScopedFdCloser fd_closer(&fd);
74 for (off_t offset = 0;; offset += buf.size()) {
75 ssize_t bytes_read;
76 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
77 if (bytes_read == 0)
78 break;
79 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
80 }
81 CHECK_EQ(performer.Close(), 0);
82 LOG(INFO) << "done applying delta.";
83 return 0;
84 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070085 CHECK(!FLAGS_old_dir.empty());
86 CHECK(!FLAGS_new_dir.empty());
87 CHECK(!FLAGS_old_image.empty());
88 CHECK(!FLAGS_new_image.empty());
89 CHECK(!FLAGS_out_file.empty());
90 CHECK(!FLAGS_old_kernel.empty());
91 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070092 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
93 LOG(FATAL) << "old_dir or new_dir not directory";
adlr@google.com3defe6a2009-12-04 20:57:17 +000094 }
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080095
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070096 DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
97 FLAGS_old_image,
98 FLAGS_new_dir,
99 FLAGS_new_image,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700100 FLAGS_old_kernel,
101 FLAGS_new_kernel,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700102 FLAGS_out_file,
103 FLAGS_private_key);
Andrew de los Reyes1e338b82010-01-22 14:57:27 -0800104
adlr@google.com3defe6a2009-12-04 20:57:17 +0000105 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800106}
107
108} // namespace {}
109
110} // namespace chromeos_update_engine
111
112int main(int argc, char** argv) {
113 return chromeos_update_engine::Main(argc, argv);
114}