From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 946B2C4338F for ; Thu, 19 Aug 2021 03:42:25 +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 1D1DD6109E for ; Thu, 19 Aug 2021 03:42:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1D1DD6109E 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 751B08311A; Thu, 19 Aug 2021 05:41:34 +0200 (CEST) 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="Oa5xKg9N"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A0BE882DD7; Thu, 19 Aug 2021 05:40:58 +0200 (CEST) Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) (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 B71D382EBB for ; Thu, 19 Aug 2021 05:40:49 +0200 (CEST) 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-oo1-xc2f.google.com with SMTP id t2-20020a4ae9a2000000b0028c7144f106so329480ood.6 for ; Wed, 18 Aug 2021 20:40:49 -0700 (PDT) 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=eze9UiuYvuEaGl6gi9n5RWRC2arbwmfgAi07dFYxbPk=; b=Oa5xKg9NM9KGjJSnEHSOLeHoJwpy9Ie696+5U+p9trzVnUXZhIIwNSCkkCJzFuUXXu 3scrUjAY6devdYyZApc8dhiQnd1wph6VD1qY0Xn3aWcFlsnrnkba3KAe6O5DMlkg5oBy O7cCDvx6vtNf80P1VKv/wtYSea5MgUqH+j3uw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eze9UiuYvuEaGl6gi9n5RWRC2arbwmfgAi07dFYxbPk=; b=JuylxlZmRj6l/QaUaYxkSRU1H+qALAliP7PCi5cJoshlxF2gzFzPdfjNH2KUdIQNQ3 k4b+ofX0iGT/mVeLtJ/o8bBaUSUgR7eCQjISzma8JPYhWUcmkKRNkGs9Cw3oHzLlnjvs TCgy5fDZLRsl7CRq6v7xvsM66DvDvGX67ovpTKDsRRdp+vvehMljiUnjtCfsXAABb9pz ElJUvZaCjB8o/tSXBMIMC/WbrVAlv3WTVQ4XYtDgHKK3DyKb10m4RaaP18JGO7rcv/KC W9HwyZ6Vti3rJBA+Q8oZXEHFDZp8l+ZSczulHybft+QsScUmnurCf0v/bHPnWp0mDC3m Cd+A== X-Gm-Message-State: AOAM533Noj0vyUW69DHHYkqPhCx3LuhczgXR0ksA/8wHEZGi4Jy8mNvk WSifg0qxLbd2VTQeQnjH6jK+4+WQVjFrQw== X-Google-Smtp-Source: ABdhPJwRwFouJhtk9mWhlLlOIUpOs0Nvs2WZmiOAooCEkXq5pmtorzL6NPBN9/0WUd86T76R6tmjHg== X-Received: by 2002:a4a:d498:: with SMTP id o24mr9562963oos.23.1629344448191; Wed, 18 Aug 2021 20:40:48 -0700 (PDT) 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 q27sm415877oof.12.2021.08.18.20.40.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Aug 2021 20:40:47 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass , Heinrich Schuchardt , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Patrick Delaunay Subject: [PATCH 08/11] sandbox: Add a way to find the size of a file Date: Wed, 18 Aug 2021 21:40:30 -0600 Message-Id: <20210818214022.8.I474febf1cd7d0eece13bfe3345b3851b75faf3ee@changeid> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210819034033.1617201-1-sjg@chromium.org> References: <20210819034033.1617201-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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 Add a function to return the size of a file. This is useful in situations where we need to allocate memory for it before reading it. Signed-off-by: Simon Glass --- arch/sandbox/cpu/os.c | 22 ++++++++++++++++------ include/os.h | 8 ++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 151f42a5d6c..a4262881c54 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -133,6 +133,19 @@ int os_write_file(const char *fname, const void *buf, int size) return 0; } +int os_filesize(int fd) +{ + off_t size; + + size = os_lseek(fd, 0, OS_SEEK_END); + if (size < 0) + return -errno; + if (os_lseek(fd, 0, OS_SEEK_SET) < 0) + return -errno; + + return size; +} + int os_read_file(const char *fname, void **bufp, int *sizep) { off_t size; @@ -144,15 +157,12 @@ int os_read_file(const char *fname, void **bufp, int *sizep) printf("Cannot open file '%s'\n", fname); goto err; } - size = os_lseek(fd, 0, OS_SEEK_END); + size = os_filesize(fd); if (size < 0) { - printf("Cannot seek to end of file '%s'\n", fname); - goto err; - } - if (os_lseek(fd, 0, OS_SEEK_SET) < 0) { - printf("Cannot seek to start of file '%s'\n", fname); + printf("Cannot get file size of '%s'\n", fname); goto err; } + *bufp = os_malloc(size); if (!*bufp) { printf("Not enough memory to read file '%s'\n", fname); diff --git a/include/os.h b/include/os.h index 7b20d606dd0..7661078d336 100644 --- a/include/os.h +++ b/include/os.h @@ -51,6 +51,14 @@ off_t os_lseek(int fd, off_t offset, int whence); #define OS_SEEK_CUR 1 #define OS_SEEK_END 2 +/** + * os_filesize() - Calculate the size of a file + * + * @fd: File descriptor as returned by os_open() + * Return: file size or negative error code + */ +int os_filesize(int fd); + /** * Access to the OS open() system call * -- 2.33.0.rc1.237.g0d66db33f3-goog