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.
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.
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]
23 * Copyright (c) 2024 by Pawel Jakub Dawidek
43 copy_file_range(int, loff_t
*, int, loff_t
*, size_t, unsigned int)
44 __attribute__((weak
));
47 mmap_file(int fd
, size_t size
)
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
));
61 usage(const char *progname
)
65 * -i cache input before copy_file_range(2).
66 * -o cache input before copy_file_range(2).
68 (void) fprintf(stderr
, "usage: %s [-io] <input> <output>\n", progname
);
73 main(int argc
, char *argv
[])
77 void *dmem
, *smem
, *ptr
;
80 bool cache_input
, cache_output
;
85 cache_input
= cache_output
= false;
87 while ((c
= getopt(argc
, argv
, "io")) != -1) {
106 sfd
= open(argv
[0], O_RDONLY
);
107 if (fstat(sfd
, &sb
) == -1) {
108 (void) fprintf(stderr
, "fstat failed: %s\n", strerror(errno
));
112 smem
= mmap_file(sfd
, ssize
);
114 dfd
= open(argv
[1], O_RDWR
);
115 if (fstat(dfd
, &sb
) == -1) {
116 (void) fprintf(stderr
, "fstat failed: %s\n", strerror(errno
));
120 dmem
= mmap_file(dfd
, dsize
);
123 * Hopefully it won't be compiled out.
128 memcpy(ptr
, smem
, ssize
);
134 memcpy(ptr
, dmem
, dsize
);
139 if (copy_file_range(sfd
, &soff
, dfd
, &doff
, ssize
, 0) < 0) {
140 (void) fprintf(stderr
, "copy_file_range failed: %s\n",
145 exit(memcmp(smem
, dmem
, ssize
) == 0 ? 0 : 1);