Path: blob/main/sys/contrib/openzfs/cmd/zstream/zstream_util.c
178703 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*17* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.18* Copyright (c) 2011, 2020 by Delphix. All rights reserved.19* Copyright (c) 2012, Joyent, Inc. All rights reserved.20* Copyright (c) 2012 Pawel Jakub Dawidek <[email protected]>.21* All rights reserved22* Copyright (c) 2013 Steven Hartland. All rights reserved.23* Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.24* Copyright 2016 Igor Kozhukhov <[email protected]>25* Copyright (c) 2018, loli10K <[email protected]>. All rights reserved.26* Copyright (c) 2019 Datto Inc.27* Copyright (c) 2024, Klara, Inc.28*/2930#include <sys/debug.h>31#include <stddef.h>32#include <errno.h>33#include <unistd.h>34#include <stdlib.h>35#include <stdio.h>36#include <string.h>37#include <zfs_fletcher.h>38#include "zstream_util.h"3940/*41* From libzfs_sendrecv.c42*/43int44dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,45zio_cksum_t *zc, int outfd)46{47ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),48==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));49fletcher_4_incremental_native(drr,50offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);51if (drr->drr_type != DRR_BEGIN) {52ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.53drr_checksum.drr_checksum));54drr->drr_u.drr_checksum.drr_checksum = *zc;55}56fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,57sizeof (zio_cksum_t), zc);58if (write(outfd, drr, sizeof (*drr)) == -1)59return (errno);60if (payload_len != 0) {61fletcher_4_incremental_native(payload, payload_len, zc);62if (write(outfd, payload, payload_len) == -1)63return (errno);64}65return (0);66}6768void *69safe_malloc(size_t size)70{71void *rv = malloc(size);72if (rv == NULL) {73(void) fprintf(stderr, "Error: failed to allocate %zu bytes\n",74size);75exit(1);76}77return (rv);78}7980void *81safe_calloc(size_t size)82{83void *rv = calloc(1, size);84if (rv == NULL) {85(void) fprintf(stderr,86"Error: failed to allocate %zu bytes\n", size);87exit(1);88}89return (rv);90}9192/*93* Safe version of fread(), exits on error.94*/95int96sfread(void *buf, size_t size, FILE *fp)97{98int rv = fread(buf, size, 1, fp);99if (rv == 0 && ferror(fp)) {100(void) fprintf(stderr, "Error while reading file: %s\n",101strerror(errno));102exit(1);103}104return (rv);105}106107108