Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4074DC433EF for ; Fri, 12 Nov 2021 19:29:23 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B76E960F41 for ; Fri, 12 Nov 2021 19:29:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B76E960F41 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BBA3F83085; Fri, 12 Nov 2021 20:29:09 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="XzZQQxU2"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A29638304F; Fri, 12 Nov 2021 20:28:58 +0100 (CET) Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0674C83023 for ; Fri, 12 Nov 2021 20:28:52 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x235.google.com with SMTP id q25so14190866oiw.0 for ; Fri, 12 Nov 2021 11:28:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=44h2GXpouUQV8c5lndx9hcMynUT5IEkCj/2OoQ15azM=; b=XzZQQxU2GNsHTCyvQ2/NSxna3i5m/MOJcx1KYpTenry/Zywkcj3NMi3ebD3CXmFDmW V6mQ5tcldQCcut1VNZxQMcf8tMPx7kn2Ui6SjTwrYYj7WlNXiQIL2ekTzXBJLntXywKw gx2N8+nsZT1vwUi/dGGiSNCJGsJzSik9OrceM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=44h2GXpouUQV8c5lndx9hcMynUT5IEkCj/2OoQ15azM=; b=mrI5o9d2w3WV73l6EqT3ZCgrzmgPBhlTtYK0q5G0kj8O4rM5Xnw8dXxZ6//2QbxPgC sw4OMe7SkhrpN0FOvI1k4V9adznLUwQuwvyZkDR53zQ3nmOqrR2kKp4W12zodJ2212lF AMOQCN0jzdcmIEAqedMsXMAyevPFaQDlPJn70bcXtu3qwpsXfZ89B1bWL+jKIV+DFjqd HC1SEfsT+MKS5h/9XG7ALGq3uQMJpLFBiBp7F1KYyEUHwsrsJt5LuDSYxyeVIC7sk0x+ SKwpd+nWH4aShBQiKjQrfLwyDJt36mrNtiQfUlb8R96szpmnTL1f7YlnZVVFpesaQ2Su iDPw== X-Gm-Message-State: AOAM532bo46WFxjn2S36cuWL7G8gwQXsqYZU433/+lBaom4TH45nc7Z0 6lIU/r1YEyfJ2R9CYOS7UmncwB5pzxFmPQ== X-Google-Smtp-Source: ABdhPJwlimKwtPS+JaJc71rV2Q+7sz9dCz7B6H2WjqaxPQnJvf8UbOrI280CITtIO5324fLnaUguRw== X-Received: by 2002:a05:6808:f08:: with SMTP id m8mr9871545oiw.5.1636745330483; Fri, 12 Nov 2021 11:28:50 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id n20sm1283527ooe.7.2021.11.12.11.28.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Nov 2021 11:28:50 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Rob Herring , Tom Rini , Bill Mills , Simon Glass , Alexandru Gagniuc , Sven Roederer Subject: [PATCH 03/16] tools: Move copyfile() into a common file Date: Fri, 12 Nov 2021 12:28:04 -0700 Message-Id: <20211112192817.199075-4-sjg@chromium.org> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog In-Reply-To: <20211112192817.199075-1-sjg@chromium.org> References: <20211112192817.199075-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.35 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean This function is useful in other places. Move it to a common file. Signed-off-by: Simon Glass --- tools/fit_common.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ tools/fit_common.h | 11 +++++++++ tools/fit_image.c | 56 ---------------------------------------------- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/tools/fit_common.c b/tools/fit_common.c index 52b63296f89..c161f23b0f7 100644 --- a/tools/fit_common.c +++ b/tools/fit_common.c @@ -111,3 +111,59 @@ err: return -1; } + +int copyfile(const char *src, const char *dst) +{ + int fd_src = -1, fd_dst = -1; + void *buf = NULL; + ssize_t size; + size_t count; + int ret = -1; + + fd_src = open(src, O_RDONLY); + if (fd_src < 0) { + printf("Can't open file %s (%s)\n", src, strerror(errno)); + goto out; + } + + fd_dst = open(dst, O_WRONLY | O_CREAT, 0666); + if (fd_dst < 0) { + printf("Can't open file %s (%s)\n", dst, strerror(errno)); + goto out; + } + + buf = calloc(1, 512); + if (!buf) { + printf("Can't allocate buffer to copy file\n"); + goto out; + } + + while (1) { + size = read(fd_src, buf, 512); + if (size < 0) { + printf("Can't read file %s\n", src); + goto out; + } + if (!size) + break; + + count = size; + size = write(fd_dst, buf, count); + if (size < 0) { + printf("Can't write file %s\n", dst); + goto out; + } + } + + ret = 0; + + out: + if (fd_src >= 0) + close(fd_src); + if (fd_dst >= 0) + close(fd_dst); + if (buf) + free(buf); + + return ret; +} diff --git a/tools/fit_common.h b/tools/fit_common.h index 1e81d4c68b6..a76c4001c59 100644 --- a/tools/fit_common.h +++ b/tools/fit_common.h @@ -39,4 +39,15 @@ int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc, void **blobp, struct stat *sbuf, bool delete_on_error, bool read_only); +/** + * copyfile() - Copy a file + * + * This uses read()/write() to copy file @src to file @dst + * + * @src: Filename to read from + * @dst: Filename to write to + * @return 0 if OK, -1 on error + */ +int copyfile(const char *src, const char *dst); + #endif /* _FIT_COMMON_H_ */ diff --git a/tools/fit_image.c b/tools/fit_image.c index f4f372ba62f..c4f56bb6967 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -654,62 +654,6 @@ err: return ret; } -static int copyfile(const char *src, const char *dst) -{ - int fd_src = -1, fd_dst = -1; - void *buf = NULL; - ssize_t size; - size_t count; - int ret = -1; - - fd_src = open(src, O_RDONLY); - if (fd_src < 0) { - printf("Can't open file %s (%s)\n", src, strerror(errno)); - goto out; - } - - fd_dst = open(dst, O_WRONLY | O_CREAT, 0666); - if (fd_dst < 0) { - printf("Can't open file %s (%s)\n", dst, strerror(errno)); - goto out; - } - - buf = calloc(1, 512); - if (!buf) { - printf("Can't allocate buffer to copy file\n"); - goto out; - } - - while (1) { - size = read(fd_src, buf, 512); - if (size < 0) { - printf("Can't read file %s\n", src); - goto out; - } - if (!size) - break; - - count = size; - size = write(fd_dst, buf, count); - if (size < 0) { - printf("Can't write file %s\n", dst); - goto out; - } - } - - ret = 0; - - out: - if (fd_src >= 0) - close(fd_src); - if (fd_dst >= 0) - close(fd_dst); - if (buf) - free(buf); - - return ret; -} - /** * fit_handle_file - main FIT file processing function * -- 2.34.0.rc1.387.gb447b232ab-goog