Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 1 | // 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 Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame^] | 6 | #include <fcntl.h> |
| 7 | #include <sys/mman.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <limits> |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 17 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 18 | #include "bsdiff/bsdiff.h" |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame^] | 19 | #include "bsdiff/patch_writer_factory.h" |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // mmap() the passed |filename| to read-only memory and store in |filesize| the |
| 24 | // size of the file. To release the memory, call munmap with the returned |
| 25 | // pointer and filesize. In case of error returns nullptr. |
| 26 | void* MapFile(const char* filename, size_t* filesize) { |
| 27 | int fd = open(filename, O_RDONLY); |
| 28 | if (fd < 0) { |
| 29 | perror("open()"); |
| 30 | return nullptr; |
| 31 | } |
| 32 | |
| 33 | struct stat st; |
| 34 | fstat(fd, &st); |
| 35 | if (static_cast<uint64_t>(st.st_size) > std::numeric_limits<size_t>::max()) { |
| 36 | fprintf(stderr, "File too big\n"); |
| 37 | close(fd); |
| 38 | return nullptr; |
| 39 | } |
| 40 | *filesize = st.st_size; |
| 41 | |
| 42 | void* ret = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 43 | if (ret == MAP_FAILED) { |
| 44 | perror("mmap()"); |
| 45 | close(fd); |
| 46 | return nullptr; |
| 47 | } |
| 48 | close(fd); |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | // Generate bsdiff patch from the |old_filename| file to the |new_filename| |
| 53 | // file storing the resulting patch in a new |patch_filename| file. |
| 54 | int GenerateBsdiffFromFiles(const char* old_filename, |
| 55 | const char* new_filename, |
| 56 | const char* patch_filename) { |
| 57 | size_t oldsize, newsize; |
| 58 | int ret = 0; |
| 59 | |
| 60 | uint8_t* old_buf = static_cast<uint8_t*>(MapFile(old_filename, &oldsize)); |
| 61 | uint8_t* new_buf = static_cast<uint8_t*>(MapFile(new_filename, &newsize)); |
| 62 | |
| 63 | if (old_buf && new_buf) { |
| 64 | auto patch_writer = bsdiff::CreateBsdiffPatchWriter(patch_filename); |
| 65 | |
| 66 | ret = bsdiff::bsdiff(old_buf, oldsize, new_buf, newsize, patch_writer.get(), |
| 67 | nullptr); |
| 68 | } else { |
| 69 | ret = 1; |
| 70 | } |
| 71 | |
| 72 | if (old_buf) |
| 73 | munmap(old_buf, oldsize); |
| 74 | if (new_buf) |
| 75 | munmap(new_buf, newsize); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | } // namespace |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 81 | |
| 82 | int main(int argc, char* argv[]) { |
| 83 | if (argc != 4) |
| 84 | errx(1, "usage: %s oldfile newfile patchfile\n", argv[0]); |
| 85 | |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame^] | 86 | return GenerateBsdiffFromFiles(argv[1], argv[2], argv[3]); |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 87 | } |