1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2024 by Pawel Jakub Dawidek 24 */ 25 26 #include <sys/mman.h> 27 #include <sys/stat.h> 28 29 #include <assert.h> 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <stdbool.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #ifdef __FreeBSD__ 39 #define loff_t off_t 40 #endif 41 42 ssize_t 43 copy_file_range(int, loff_t *, int, loff_t *, size_t, unsigned int) 44 __attribute__((weak)); 45 46 static void * 47 mmap_file(int fd, size_t size) 48 { 49 void *p; 50 51 p = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); 52 if (p == MAP_FAILED) { 53 (void) fprintf(stderr, "mmap failed: %s\n", strerror(errno)); 54 exit(2); 55 } 56 57 return (p); 58 } 59 60 static void 61 usage(const char *progname) 62 { 63 64 /* 65 * -i cache input before copy_file_range(2). 66 * -o cache input before copy_file_range(2). 67 */ 68 (void) fprintf(stderr, "usage: %s [-io] <input> <output>\n", progname); 69 exit(3); 70 } 71 72 int 73 main(int argc, char *argv[]) 74 { 75 int dfd, sfd; 76 size_t dsize, ssize; 77 void *dmem, *smem, *ptr; 78 off_t doff, soff; 79 struct stat sb; 80 bool cache_input, cache_output; 81 const char *progname; 82 int c; 83 84 progname = argv[0]; 85 cache_input = cache_output = false; 86 87 while ((c = getopt(argc, argv, "io")) != -1) { 88 switch (c) { 89 case 'i': 90 cache_input = true; 91 break; 92 case 'o': 93 cache_output = true; 94 break; 95 default: 96 usage(progname); 97 } 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (argc != 2) { 103 usage(progname); 104 } 105 106 sfd = open(argv[0], O_RDONLY); 107 if (fstat(sfd, &sb) == -1) { 108 (void) fprintf(stderr, "fstat failed: %s\n", strerror(errno)); 109 exit(2); 110 } 111 ssize = sb.st_size; 112 smem = mmap_file(sfd, ssize); 113 114 dfd = open(argv[1], O_RDWR); 115 if (fstat(dfd, &sb) == -1) { 116 (void) fprintf(stderr, "fstat failed: %s\n", strerror(errno)); 117 exit(2); 118 } 119 dsize = sb.st_size; 120 dmem = mmap_file(dfd, dsize); 121 122 /* 123 * Hopefully it won't be compiled out. 124 */ 125 if (cache_input) { 126 ptr = malloc(ssize); 127 assert(ptr != NULL); 128 memcpy(ptr, smem, ssize); 129 free(ptr); 130 } 131 if (cache_output) { 132 ptr = malloc(ssize); 133 assert(ptr != NULL); 134 memcpy(ptr, dmem, dsize); 135 free(ptr); 136 } 137 138 soff = doff = 0; 139 if (copy_file_range(sfd, &soff, dfd, &doff, ssize, 0) < 0) { 140 (void) fprintf(stderr, "copy_file_range failed: %s\n", 141 strerror(errno)); 142 exit(2); 143 } 144 145 exit(memcmp(smem, dmem, ssize) == 0 ? 0 : 1); 146 } 147