blob: a996fb88e58f4e3a43f4cba7ee90583143e68f7d [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"
adlr@google.com3defe6a2009-12-04 20:57:17 +000016#include "chromeos/obsolete_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 Reyes09e56d62010-04-23 13:45:53 -070030DEFINE_string(apply_delta, "",
31 "If set, apply delta over old_image. (For debugging.)");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032
adlr@google.com3defe6a2009-12-04 20:57:17 +000033// This file contains a simple program that takes an old path, a new path,
34// and an output file as arguments and the path to an output file and
35// generates a delta that can be sent to Chrome OS clients.
36
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080037using std::set;
38using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070039using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080040
adlr@google.com3defe6a2009-12-04 20:57:17 +000041namespace chromeos_update_engine {
42
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080043namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080044
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080045bool IsDir(const char* path) {
46 struct stat stbuf;
47 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
48 return S_ISDIR(stbuf.st_mode);
49}
50
51int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000052 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070053 google::ParseCommandLineFlags(&argc, &argv, true);
54 CommandLine::Init(argc, argv);
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070056 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080057 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
58 logging::DONT_LOCK_LOG_FILE,
59 logging::APPEND_TO_OLD_LOG_FILE);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070060 if (!FLAGS_apply_delta.empty()) {
61 if (FLAGS_old_image.empty()) {
62 LOG(FATAL) << "Must pass --old_image with --apply_delta.";
63 }
64 DeltaPerformer performer;
65 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
66 vector<char> buf(1024 * 1024);
67 int fd = open(FLAGS_apply_delta.c_str(), O_RDONLY, 0);
68 CHECK_GE(fd, 0);
69 ScopedFdCloser fd_closer(&fd);
70 for (off_t offset = 0;; offset += buf.size()) {
71 ssize_t bytes_read;
72 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
73 if (bytes_read == 0)
74 break;
75 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
76 }
77 CHECK_EQ(performer.Close(), 0);
78 LOG(INFO) << "done applying delta.";
79 return 0;
80 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070081 if (FLAGS_old_dir.empty() || FLAGS_new_dir.empty() ||
82 FLAGS_old_image.empty() || FLAGS_new_image.empty() ||
83 FLAGS_out_file.empty()) {
84 LOG(FATAL) << "Missing required argument(s)";
85 }
86 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
87 LOG(FATAL) << "old_dir or new_dir not directory";
adlr@google.com3defe6a2009-12-04 20:57:17 +000088 }
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080089
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070090 DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
91 FLAGS_old_image,
92 FLAGS_new_dir,
93 FLAGS_new_image,
94 FLAGS_out_file);
Andrew de los Reyes1e338b82010-01-22 14:57:27 -080095
adlr@google.com3defe6a2009-12-04 20:57:17 +000096 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080097}
98
99} // namespace {}
100
101} // namespace chromeos_update_engine
102
103int main(int argc, char** argv) {
104 return chromeos_update_engine::Main(argc, argv);
105}