adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [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> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 7 | #include <errno.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <unistd.h> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 10 | #include <set> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <string> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 12 | #include <vector> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 13 | #include <gflags/gflags.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 14 | #include <glib.h> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 15 | #include "base/command_line.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | #include "chromeos/obsolete_logging.h" |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 17 | #include "update_engine/delta_diff_generator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 18 | #include "update_engine/delta_performer.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | #include "update_engine/subprocess.h" |
| 20 | #include "update_engine/update_metadata.pb.h" |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 21 | #include "update_engine/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 23 | DEFINE_string(old_dir, "", |
| 24 | "Directory where the old rootfs is loop mounted read-only"); |
| 25 | DEFINE_string(new_dir, "", |
| 26 | "Directory where the new rootfs is loop mounted read-only"); |
| 27 | DEFINE_string(old_image, "", "Path to the old rootfs"); |
| 28 | DEFINE_string(new_image, "", "Path to the new rootfs"); |
| 29 | DEFINE_string(out_file, "", "Path to output file"); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 30 | DEFINE_string(apply_delta, "", |
| 31 | "If set, apply delta over old_image. (For debugging.)"); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 32 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 33 | // 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 Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 37 | using std::set; |
| 38 | using std::string; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 39 | using std::vector; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 40 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | namespace chromeos_update_engine { |
| 42 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 43 | namespace { |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 44 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 45 | bool 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 | |
| 51 | int Main(int argc, char** argv) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 52 | g_thread_init(NULL); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 53 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 54 | CommandLine::Init(argc, argv); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | Subprocess::Init(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 56 | logging::InitLogging("delta_generator.log", |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 57 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 58 | logging::DONT_LOCK_LOG_FILE, |
| 59 | logging::APPEND_TO_OLD_LOG_FILE); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 60 | 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 Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 81 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | } |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 89 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 90 | DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir, |
| 91 | FLAGS_old_image, |
| 92 | FLAGS_new_dir, |
| 93 | FLAGS_new_image, |
| 94 | FLAGS_out_file); |
Andrew de los Reyes | 1e338b8 | 2010-01-22 14:57:27 -0800 | [diff] [blame] | 95 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 96 | return 0; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } // namespace {} |
| 100 | |
| 101 | } // namespace chromeos_update_engine |
| 102 | |
| 103 | int main(int argc, char** argv) { |
| 104 | return chromeos_update_engine::Main(argc, argv); |
| 105 | } |