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 <sys/debug.h>
32 #include <stddef.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <zfs_fletcher.h>
39 #include "zstream_util.h"
40
41 /*
42 * From libzfs_sendrecv.c
43 */
44 int
dump_record(dmu_replay_record_t * drr,void * payload,size_t payload_len,zio_cksum_t * zc,int outfd)45 dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
46 zio_cksum_t *zc, int outfd)
47 {
48 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
49 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
50 fletcher_4_incremental_native(drr,
51 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
52 if (drr->drr_type != DRR_BEGIN) {
53 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
54 drr_checksum.drr_checksum));
55 drr->drr_u.drr_checksum.drr_checksum = *zc;
56 }
57 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
58 sizeof (zio_cksum_t), zc);
59 if (write(outfd, drr, sizeof (*drr)) == -1)
60 return (errno);
61 if (payload_len != 0) {
62 fletcher_4_incremental_native(payload, payload_len, zc);
63 if (write(outfd, payload, payload_len) == -1)
64 return (errno);
65 }
66 return (0);
67 }
68
69 void *
safe_malloc(size_t size)70 safe_malloc(size_t size)
71 {
72 void *rv = malloc(size);
73 if (rv == NULL) {
74 (void) fprintf(stderr, "Error: failed to allocate %zu bytes\n",
75 size);
76 exit(1);
77 }
78 return (rv);
79 }
80
81 void *
safe_calloc(size_t size)82 safe_calloc(size_t size)
83 {
84 void *rv = calloc(1, size);
85 if (rv == NULL) {
86 (void) fprintf(stderr,
87 "Error: failed to allocate %zu bytes\n", size);
88 exit(1);
89 }
90 return (rv);
91 }
92
93 /*
94 * Safe version of fread(), exits on error.
95 */
96 int
sfread(void * buf,size_t size,FILE * fp)97 sfread(void *buf, size_t size, FILE *fp)
98 {
99 int rv = fread(buf, size, 1, fp);
100 if (rv == 0 && ferror(fp)) {
101 (void) fprintf(stderr, "Error while reading file: %s\n",
102 strerror(errno));
103 exit(1);
104 }
105 return (rv);
106 }
107