blob: 674705550c0aae24569d08139d77206fc8a4fd28 [file] [log] [blame]
Alex Deymo20891f92015-10-12 17:28:04 -07001// Copyright 2015 The Chromium OS 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 <err.h>
Alex Deymoe4458302017-10-26 16:40:01 +02006#include <fcntl.h>
Tianjie Xu1f1cdb22017-11-20 11:05:55 -08007#include <getopt.h>
Alex Deymoe4458302017-10-26 16:40:01 +02008#include <sys/mman.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <unistd.h>
12
13#include <stddef.h>
14#include <stdio.h>
15#include <stdlib.h>
16
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080017#include <iostream>
Alex Deymoe4458302017-10-26 16:40:01 +020018#include <limits>
Alex Deymo20891f92015-10-12 17:28:04 -070019
Alex Deymoddf9db52017-03-02 16:10:41 -080020#include "bsdiff/bsdiff.h"
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080021#include "bsdiff/bsdiff_arguments.h"
22#include "bsdiff/constants.h"
Alex Deymoe4458302017-10-26 16:40:01 +020023#include "bsdiff/patch_writer_factory.h"
24
25namespace {
26
27// mmap() the passed |filename| to read-only memory and store in |filesize| the
28// size of the file. To release the memory, call munmap with the returned
29// pointer and filesize. In case of error returns nullptr.
30void* MapFile(const char* filename, size_t* filesize) {
31 int fd = open(filename, O_RDONLY);
32 if (fd < 0) {
33 perror("open()");
34 return nullptr;
35 }
36
37 struct stat st;
38 fstat(fd, &st);
39 if (static_cast<uint64_t>(st.st_size) > std::numeric_limits<size_t>::max()) {
40 fprintf(stderr, "File too big\n");
41 close(fd);
42 return nullptr;
43 }
44 *filesize = st.st_size;
45
46 void* ret = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
47 if (ret == MAP_FAILED) {
48 perror("mmap()");
49 close(fd);
50 return nullptr;
51 }
52 close(fd);
53 return ret;
54}
55
56// Generate bsdiff patch from the |old_filename| file to the |new_filename|
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080057// file with options in |arguments|. Store the resulting patch in a new
58// |patch_filename| file. Returns 0 on success.
Alex Deymoe4458302017-10-26 16:40:01 +020059int GenerateBsdiffFromFiles(const char* old_filename,
60 const char* new_filename,
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080061 const char* patch_filename,
62 const bsdiff::BsdiffArguments& arguments) {
63 size_t oldsize;
Alex Deymoe4458302017-10-26 16:40:01 +020064 uint8_t* old_buf = static_cast<uint8_t*>(MapFile(old_filename, &oldsize));
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080065 if (!old_buf) {
66 return 1;
Alex Deymoe4458302017-10-26 16:40:01 +020067 }
68
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080069 size_t newsize;
70 uint8_t* new_buf = static_cast<uint8_t*>(MapFile(new_filename, &newsize));
71 if (!new_buf) {
Alex Deymoe4458302017-10-26 16:40:01 +020072 munmap(old_buf, oldsize);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080073 return 1;
74 }
Alex Deymoe4458302017-10-26 16:40:01 +020075
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080076 std::unique_ptr<bsdiff::PatchWriterInterface> patch_writer;
77 if (arguments.format() == bsdiff::BsdiffFormat::kLegacy) {
78 patch_writer = bsdiff::CreateBsdiffPatchWriter(patch_filename);
79 } else if (arguments.format() == bsdiff::BsdiffFormat::kBsdf2) {
80 patch_writer = bsdiff::CreateBSDF2PatchWriter(
81 patch_filename, arguments.compressor_type(),
82 arguments.compression_quality());
83 } else {
84 std::cerr << "unexpected bsdiff format." << std::endl;
85 return 1;
86 }
87
Alex Deymocdd45ca2018-02-14 19:18:49 +010088 int ret = bsdiff::bsdiff(old_buf, oldsize, new_buf, newsize,
89 arguments.min_length(), patch_writer.get(), nullptr);
90
91 munmap(old_buf, oldsize);
92 munmap(new_buf, newsize);
93
94 return ret;
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080095}
96
97void PrintUsage(const std::string& proc_name) {
98 std::cerr << "usage: " << proc_name
99 << " [options] oldfile newfile patchfile\n";
100 std::cerr << " --format <legacy|bsdiff40|bsdf2> The format of the bsdiff"
101 " patch.\n"
Alex Deymo383f6772018-02-08 15:50:11 +0100102 << " --minlen LEN The minimum match length required "
103 "to consider a match in the algorithm.\n"
Tianjie Xu1f1cdb22017-11-20 11:05:55 -0800104 << " --type <bz2|brotli> The algorithm to compress the "
105 "patch, bsdf2 format only.\n"
106 << " --quality Quality of the patch compression,"
107 " brotli only.\n";
Alex Deymoe4458302017-10-26 16:40:01 +0200108}
109
110} // namespace
Alex Deymo20891f92015-10-12 17:28:04 -0700111
112int main(int argc, char* argv[]) {
Tianjie Xu1f1cdb22017-11-20 11:05:55 -0800113 bsdiff::BsdiffArguments arguments;
Alex Deymo20891f92015-10-12 17:28:04 -0700114
Tianjie Xu1f1cdb22017-11-20 11:05:55 -0800115 if (!arguments.ParseCommandLine(argc, argv)) {
116 PrintUsage(argv[0]);
117 return 1;
118 }
119
120 // The optind will be updated in ParseCommandLine to parse the options; and
121 // we expect the rest of the arguments to be oldfile, newfile, patchfile.
122 if (!arguments.IsValid() || argc - optind != 3) {
123 PrintUsage(argv[0]);
124 return 1;
125 }
126
127 return GenerateBsdiffFromFiles(argv[optind], argv[optind + 1],
128 argv[optind + 2], arguments);
Alex Deymo20891f92015-10-12 17:28:04 -0700129}