|
| 1 | +// SPDX-License-Identifier: CDDL-1.0 |
| 2 | +/* |
| 3 | + * CDDL HEADER START |
| 4 | + * |
| 5 | + * This file and its contents are supplied under the terms of the |
| 6 | + * Common Development and Distribution License ("CDDL"), version 1.0. |
| 7 | + * You may only use this file in accordance with the terms of version |
| 8 | + * 1.0 of the CDDL. |
| 9 | + * |
| 10 | + * A full copy of the text of the CDDL should have accompanied this |
| 11 | + * source. A copy of the CDDL is also available via the Internet at |
| 12 | + * http://www.illumos.org/license/CDDL. |
| 13 | + * |
| 14 | + * CDDL HEADER END |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
| 19 | + * Copyright (c) 2011, 2020 by Delphix. All rights reserved. |
| 20 | + * Copyright (c) 2012, Joyent, Inc. All rights reserved. |
| 21 | + * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. |
| 22 | + * All rights reserved |
| 23 | + * Copyright (c) 2013 Steven Hartland. All rights reserved. |
| 24 | + * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. |
| 25 | + * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> |
| 26 | + * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. |
| 27 | + * Copyright (c) 2019 Datto Inc. |
| 28 | + * Copyright (c) 2024, Klara, Inc. |
| 29 | +*/ |
| 30 | + |
| 31 | +#include "zstream_util.h" |
| 32 | + |
| 33 | +/* |
| 34 | + * From libzfs_sendrecv.c |
| 35 | + */ |
| 36 | +int |
| 37 | +dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len, |
| 38 | + zio_cksum_t *zc, int outfd) |
| 39 | +{ |
| 40 | + ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), |
| 41 | + ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); |
| 42 | + fletcher_4_incremental_native(drr, |
| 43 | + offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc); |
| 44 | + if (drr->drr_type != DRR_BEGIN) { |
| 45 | + ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u. |
| 46 | + drr_checksum.drr_checksum)); |
| 47 | + drr->drr_u.drr_checksum.drr_checksum = *zc; |
| 48 | + } |
| 49 | + fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum, |
| 50 | + sizeof (zio_cksum_t), zc); |
| 51 | + if (write(outfd, drr, sizeof (*drr)) == -1) |
| 52 | + return (errno); |
| 53 | + if (payload_len != 0) { |
| 54 | + fletcher_4_incremental_native(payload, payload_len, zc); |
| 55 | + if (write(outfd, payload, payload_len) == -1) |
| 56 | + return (errno); |
| 57 | + } |
| 58 | + return (0); |
| 59 | +} |
| 60 | + |
| 61 | +void * |
| 62 | +safe_malloc(size_t size) |
| 63 | +{ |
| 64 | + void *rv = malloc(size); |
| 65 | + if (rv == NULL) { |
| 66 | + (void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n", |
| 67 | + size); |
| 68 | + abort(); |
| 69 | + } |
| 70 | + return (rv); |
| 71 | +} |
| 72 | + |
| 73 | +void * |
| 74 | +safe_calloc(size_t n) |
| 75 | +{ |
| 76 | + void *rv = calloc(1, n); |
| 77 | + if (rv == NULL) { |
| 78 | + fprintf(stderr, |
| 79 | + "Error: could not allocate %u bytes of memory\n", |
| 80 | + (int)n); |
| 81 | + exit(1); |
| 82 | + } |
| 83 | + return (rv); |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * Safe version of fread(), exits on error. |
| 88 | + */ |
| 89 | +int |
| 90 | +sfread(void *buf, size_t size, FILE *fp) |
| 91 | +{ |
| 92 | + int rv = fread(buf, size, 1, fp); |
| 93 | + if (rv == 0 && ferror(fp)) { |
| 94 | + (void) fprintf(stderr, "Error while reading file: %s\n", |
| 95 | + strerror(errno)); |
| 96 | + exit(1); |
| 97 | + } |
| 98 | + return (rv); |
| 99 | +} |
| 100 | + |
0 commit comments