From patchwork Fri Mar 19 08:55:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1455718 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::c38; helo=mail-oo1-xc38.google.com; envelope-from=swupdate+bncbcwivbv7sugrbdwo2gbamgqevuy7qgq@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=A2+JMeTM; dkim-atps=neutral Received: from mail-oo1-xc38.google.com (mail-oo1-xc38.google.com [IPv6:2607:f8b0:4864:20::c38]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F1yR76XjYz9sW1 for ; Fri, 19 Mar 2021 19:55:45 +1100 (AEDT) Received: by mail-oo1-xc38.google.com with SMTP id w12sf22348191ooe.17 for ; Fri, 19 Mar 2021 01:55:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1616144143; cv=pass; d=google.com; s=arc-20160816; b=oUkI1G1SpfiLAVJoSotwHw5X8mf3ZWYvKnla/E7roAutcDIjhU6YtZK2yLqrzS15PI mTREGB5dyoMaYhUxwlDPS59KeGIGAtlfDUFnYGRnwWUbRLuM+rRmEG8CXVROSZuH3/f0 SDEXC+ite4igRya8WZG+nQtkhFKY8+l6k1ji0gOJUWc3y+WJR5LIB+hjn0ls5FWnrCXB 1GSVIMr/Y+LEx/Oa+Bv8yNXsg/OSkSuiW6SnjnautT4wAbpGj6rLiEvRLJCRrBsj+ac1 CPFpaBe7xRbL1NDiCLT4nVpYI1f1FLkO6v6lm09Nwhgs8nGdGqcFpxMFxkF/h/GUTAl7 bgKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=Whzd/oZCQZAICivcgFDuRjyxJFPleY4JE2A6rMksEPc=; b=sDxWJmODiAakpv3xDHlxKWLU46NIv2lU7XNNJvGUDUqUEImDbQA8d7zIf/XMy7ChN4 DPy9dEpjODhgwUuUIRr34XwcpkX0jQAWmE11+wD6edjAR1dZW4M3pNuGjhvrQcKeeUqx I/UPv9ebidxPpErKZNXgBdS4M1QTEtjx98ZMMX1P4RQSH7A3iRr8iat95rd3y0T0u/uO D+yG2kL5JbaTbOYGkOsDRNNIQWIGJcSfQFXDDxDLECDQ+whZ8Ooshxujjbacnf+bje5/ Oo8IC+JklwUNXlqM+ovQ30Lg+YkiAJiv+CHZqsikXkO32Wd/GdIuaHb7R3Z3DNJzlpPl pD3Q== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Whzd/oZCQZAICivcgFDuRjyxJFPleY4JE2A6rMksEPc=; b=A2+JMeTMC7fyw3gsql6+2wk+0xONIgmLvP+C3bZZsI7T+ksYQEKJRsC/zxB2BUHsWh xzN705CFQHcY0QvrCGLDo3o2PAPoPLW8oW1C9PiZyEOpcYebpADXlAZghQGmmh/0tvfr 5g9z+aBN4gyu1Cz6dJ2jLy6UoO9XqpcZfcAeHecDDXO4upomfp5aNaUW78ULhuLO+GPd 6+N87GRjKZC0Q6j/mqQlQflv0krc/PTPqubp9pKsbD3XUq3FECANxtJsHSOnouvAyD6W mtQn2iAdzVnXPIt6mX9r5uV6vdjpJLcYTaorcXp+IwhMUDvhDNRgtBI+LfluFgmamVgu q+Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=Whzd/oZCQZAICivcgFDuRjyxJFPleY4JE2A6rMksEPc=; b=aYIdBVfcGcAFbCDLzMsAW8xQhq9kvsskZziz/ZCJBGk6kwRknvqAzwx4C5chvWupqg wbKWp0HzlI1eKFYCEGEBQq+pdHcv8BHbkquUvJVYsfrAfjTS7s5UnATyQqagr7AhonIB bXBxk7pRZVAv4+ueNJwgKfZneZG9cb22PDKYDMYbR7ypblgb9aHD02g9yM9VN0t+bW1x HGaOOcKP0lADwYZ+bPYZOa17AMhO1wcZeCGmmcOWjTgso/vrbQgYwzEQcfBPJMZUhf9Z 6M6lvkyEuq6swkRN6KiGQsi5RSKsx2GHlQUJD2F4nJYtfTMcac89+XrBshPf7z5oyvV2 ZETw== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532pTNgTRgHrZC1j7ToxpQRtP0pQAcQdQdTUNHyaGMTQ+AAyV1nj 3ISy0nWm+Q5suCjuvDhxM9U= X-Google-Smtp-Source: ABdhPJxqmaF9bC/6isasVoKIJvt5He94ATzKu+HxhPg1QAi0Fh5VDWpsKjG/jdb/zBMg50g1/NUVbg== X-Received: by 2002:a9d:3b86:: with SMTP id k6mr272024otc.194.1616144142799; Fri, 19 Mar 2021 01:55:42 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6808:f0d:: with SMTP id m13ls1253337oiw.4.gmail; Fri, 19 Mar 2021 01:55:42 -0700 (PDT) X-Received: by 2002:a05:6808:ab0:: with SMTP id r16mr226277oij.34.1616144142280; Fri, 19 Mar 2021 01:55:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616144142; cv=none; d=google.com; s=arc-20160816; b=Q4Zaqu8nJCo+pVev1Dijv9poFvVcJ8ZbnTPV++OA+VFTMz10At3m/qg+uD5JPWJAw4 /V9MPD8AXzg7bmCrPv1D5xlxC9d38wPn06yn8+A/LlaIaNlsddqr2Q2ZaUA/KcKc+mzU PtCgnNFMCzq76uvaI+ySJA9z3reL+M9cTkOvGQwi+20vZ6uKKZugNMu2Ym+ZJGIsWc65 XeL4WMJbz80OHa5VHpjPk0AiaMXBc4yYdagPpk7dDrsufrmsloHGg9ArkItBVSCgv3Db tVR6nPDX5uMBYKDLPIu+dwCvykCVXaCGVYU5l39PjBK1NUbQcBjkXtjsVps3nyj1lMOy xt3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from; bh=N8PrviaK/w+diy2OWPx8dn8yuIEWI/qXgF6o6HWqj7k=; b=SyqoEWsgZS8jyjs1MujK0hkD/f8ZeIn1etdq39W73YW7UilXd6oujQ34nbiZ8+APM4 KhUYBw+uiO6ilh81BeU73G1dMNjPvHlsjoYm9rOHRf55fYAs+SX8U8CHrRWEGLQoVqVj PcnGJ5pybfLZAKv/hoF+cEbpnfTxnVtWi41mWuIKOPQRYsw3FyAgkOs5DmWrqworhyem iVaD6wE/Suohek74lKIZU2s3PGSFDTlx65k0j2i7OQfTifiQoLXiQMp/IACxTzodZXHg P5Mt0b/FbAaj92kb0cUEnTYobG0ekbN0atC5Vq9o6qZ66Kccy5mtyGVsqT/1XnDZtdL7 +/4w== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw.atmark-techno.com (gw.atmark-techno.com. [13.115.124.170]) by gmr-mx.google.com with ESMTP id w16si523511oov.0.2021.03.19.01.55.41 for ; Fri, 19 Mar 2021 01:55:41 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) client-ip=13.115.124.170; Received: from gw.atmark-techno.com (localhost [127.0.0.1]) by gw.atmark-techno.com (Postfix) with ESMTP id 1A5E280448 for ; Fri, 19 Mar 2021 17:55:40 +0900 (JST) Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by gw.atmark-techno.com (Postfix) with ESMTPS id CA18180448 for ; Fri, 19 Mar 2021 17:55:39 +0900 (JST) Received: by mail-pg1-f200.google.com with SMTP id e4so24205280pgt.17 for ; Fri, 19 Mar 2021 01:55:39 -0700 (PDT) X-Received: by 2002:a17:90b:4c0a:: with SMTP id na10mr9083155pjb.227.1616144138529; Fri, 19 Mar 2021 01:55:38 -0700 (PDT) X-Received: by 2002:a17:90b:4c0a:: with SMTP id na10mr9083137pjb.227.1616144138257; Fri, 19 Mar 2021 01:55:38 -0700 (PDT) Received: from pc-0115 (178.101.200.35.bc.googleusercontent.com. [35.200.101.178]) by smtp.gmail.com with ESMTPSA id 10sm4716455pfp.4.2021.03.19.01.55.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Mar 2021 01:55:37 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94) (envelope-from ) id 1lNAuu-004USV-18; Fri, 19 Mar 2021 17:55:36 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH 1/3] utils: move get_output_size from ubivol handler Date: Fri, 19 Mar 2021 17:55:29 +0900 Message-Id: <20210319085531.1070347-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Signed-off-by: Dominique Martinet Acked-by: Stefano Babic --- Note this patch applies on top of my utils: add read_lines_notify helper patch. It's simple enough to rebase if required though but that would explain if it doesn't apply cleanly for you, I'm not sure which is preferable -- unless you have more remarks the run_system_cmd fixes are probably going to get in first so I left it like this. core/util.c | 40 +++++++++++++++++++++++++++++++++++++++ handlers/ubivol_handler.c | 40 --------------------------------------- include/util.h | 1 + 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/core/util.c b/core/util.c index a9353ca8cb27..30c7e0b7aece 100644 --- a/core/util.c +++ b/core/util.c @@ -894,3 +894,43 @@ int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, return n; } + +long long get_output_size(struct img_type *img) +{ + char *output_size_str = NULL; + long long bytes = img->size; + + if (img->compressed) { + output_size_str = dict_get_value(&img->properties, "decompressed-size"); + + bytes = ustrtoull(output_size_str, 0); + if (errno) { + ERROR("decompressed-size argument: ustrtoull failed"); + return -1; + } + + if (bytes == 0) { + ERROR("UBIFS to be decompressed, but decompressed-size not valid"); + return -1; + } + TRACE("Image is compressed, decompressed size %lld bytes", bytes); + + } else if (img->is_encrypted) { + + output_size_str = dict_get_value(&img->properties, "decrypted-size"); + + bytes = ustrtoull(output_size_str, 0); + if (errno){ + ERROR("decrypted-size argument: ustrtoull failed"); + return -1; + } + + if (bytes < AES_BLK_SIZE) { + ERROR("Encrypted image size (%lld) too small", bytes); + return -1; + } + TRACE("Image is crypted, decrypted size %lld bytes", bytes); + } + + return bytes; +} diff --git a/handlers/ubivol_handler.c b/handlers/ubivol_handler.c index 80db4994c8d9..bf8c035e1b81 100644 --- a/handlers/ubivol_handler.c +++ b/handlers/ubivol_handler.c @@ -147,46 +147,6 @@ static int check_ubi_alwaysremove(struct img_type *img) return ret; } -static long long get_output_size(struct img_type *img) -{ - char *output_size_str = NULL; - long long bytes = img->size; - - if (img->compressed) { - output_size_str = dict_get_value(&img->properties, "decompressed-size"); - - bytes = ustrtoull(output_size_str, 0); - if (errno) { - ERROR("decompressed-size argument: ustrtoull failed"); - return -1; - } - - if (bytes == 0) { - ERROR("UBIFS to be decompressed, but decompressed-size not valid"); - return -1; - } - TRACE("Image is compressed, decompressed size %lld bytes", bytes); - - } else if (img->is_encrypted) { - - output_size_str = dict_get_value(&img->properties, "decrypted-size"); - - bytes = ustrtoull(output_size_str, 0); - if (errno){ - ERROR("decrypted-size argument: ustrtoull failed"); - return -1; - } - - if (bytes < AES_BLK_SIZE) { - ERROR("Encrypted image size (%lld) too small", bytes); - return -1; - } - TRACE("Image is crypted, decrypted size %lld bytes", bytes); - } - - return bytes; -} - static int update_volume(libubi_t libubi, struct img_type *img, struct ubi_vol_info *vol) { diff --git a/include/util.h b/include/util.h index 76448504f303..1fed441192bd 100644 --- a/include/util.h +++ b/include/util.h @@ -218,6 +218,7 @@ unsigned int count_string_array(const char **nodes); void free_string_array(char **nodes); int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, LOGLEVEL level); +long long get_output_size(struct img_type *img); /* Decryption key functions */ int load_decryption_key(char *fname); From patchwork Fri Mar 19 08:55:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1455719 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::c3f; helo=mail-oo1-xc3f.google.com; envelope-from=swupdate+bncbcwivbv7sugrbego2gbamgqeuuo5mni@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=OrmlXjDw; dkim-atps=neutral Received: from mail-oo1-xc3f.google.com (mail-oo1-xc3f.google.com [IPv6:2607:f8b0:4864:20::c3f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F1yR76RJhz9sRR for ; Fri, 19 Mar 2021 19:55:46 +1100 (AEDT) Received: by mail-oo1-xc3f.google.com with SMTP id u30sf14914005ooj.22 for ; Fri, 19 Mar 2021 01:55:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1616144144; cv=pass; d=google.com; s=arc-20160816; b=O4NNg0+IVjHZ9+HJFInk+RuD+T/yN+4ndG61ONokydwMVaOXPqQUBJfFww9Jj8ueKl yIADgFSiOgYQOY2g3LO9V0j7neb6sfk8atKsItdUXn7QGWnQWLTR6Tzx0bAfhuqfajPi jmgQl7fpetR6S1ehxG8kjl+FVGvPcjuSTrqOHUyEUXtObztNfyN5uBp5X8aV4rtj5DLz sOlIKEdc1/sQpzK6Y5WspJ4M89Pcm7gA7XTRrXLTfxM4O7Ov3OH3Zl3QCh4VXNAfa3Fp XPPMgc+TBE0xfqGPG2a/tEbwwY1/d7lUrA+HdSB866nK06C70qvNoQXHU9Lmb6O0NM0y r7Cg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=OYWkF9GEvksqYqQvPCkKHNAkxUs0+wQiEZR+v9T+AC8=; b=xlijLBuAZUkyEYYyp/BcaRPIMohK50Uhq8BYgzKFPP7+QlijSn8fI0qJjlpoE4hCe9 eYrRzyTCB3LujOOl95ul5DegcNOyTMoEPYI9/NF1u/15A7LO39GS5vbwo6vhcdcWgvCu JH1CyUOk0ieLoHVBo8sxdN/Qhq/rfi7g3wruH1pp0wg6FTyoA+fsVl+021PgfTWnhD7S ciOoxO48sqE8RWLaxpz+qiEcynEl8oiJvSeSjTVCr1TAh7KfzQW0WJzr4gMZ3MjPPJKC xkBr6d6qgHAjFHsgUwTzUcxg+o7g2X1eJAJK2L9QmR6kDIKPAoL+MQEAme1Nks+Wbnti lzJw== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=OYWkF9GEvksqYqQvPCkKHNAkxUs0+wQiEZR+v9T+AC8=; b=OrmlXjDwa1hCSfopIoJLuMsfiMtiARMfmTQFgyLSWcfa5+PvOhUNAhyEmR1rlcTA3b nntXtEUXx/VCFYx3ghEJYx2CR5gvlAFQxMfiDIsjyUYM0eupn8amMH/f560+z6SJikoV m16Ukt1z4541BzYcS7lEGb59BmtWoL0Toy87Ngoj0xKMe3riWIpwQF6dXfZP6yFhg7OB 523sHlxTMcpSVkwwWCPzM4pJhx0qii5vKAcLLWQUKPWkDonXBxyS1Z/xt2Ijj3W/DRLt uAvuJtTGS9OfdTV2iK44t80hGKQLsvdcVsd+hGGcN7njPxmlmUBWlMUiUDmxzFWcwGvn jGkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=OYWkF9GEvksqYqQvPCkKHNAkxUs0+wQiEZR+v9T+AC8=; b=L1A0TmhIMNw1bxFsRHhzELsONSLpg+5j1GRp2H1Tl5cLnJcrw1LtOboGbPGnbCXl3z qAO/HEWtEu+7lr+ifUgiq2XOY2YNEe8ZWJpmNyiEbFD+ssMr9x2st8iub7+L1SCGpzTC DKbCjnX1EelLEPOquwL+0miS4nEQyQ0fv+j6jgK8dEVlzGRiIt0SCfOUVdhn63GZUb+k ByumSeCmsBoY5DUvHMc7sC+3L1h/MMxBxy+cITuRHel+wiVFW/HKMABYQF3XiTpS2kLq 0ApqtmICDJYcoFpECIaLjD69AgXW4fXO3vrGn4prvS7BsMSq/0S5Ix8ecCQLIGgqZKR4 LMMA== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM5301oaEw79LyJkAPoHZNYQ93GUY+uIAbMqN1kk9xUR3ppKN7hUaQ 7t1ELg840xVyS732kg7qoxc= X-Google-Smtp-Source: ABdhPJxduq2tCveU34G7DE4p2xKksIq/cJRxS4vTi13VrrF1tAyLlT9I0/zbDv1evBa5C7SxkxNRWQ== X-Received: by 2002:aca:a958:: with SMTP id s85mr199035oie.117.1616144144765; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a4a:2256:: with SMTP id z22ls320838ooe.8.gmail; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) X-Received: by 2002:a4a:e8d1:: with SMTP id h17mr457492ooe.20.1616144144368; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616144144; cv=none; d=google.com; s=arc-20160816; b=1FdPmWC+/zEV7VwXMBTXZcRYKSHWIfX7NgLN+34FzCt6LIcxghO/q1CwJBnJWa+b/M fJ75uF6MRs4bmmPqeB8uAlBkoNCZ5H0nESlm2jS8uRjstCjtPVzZeWoctGiLrxfZekhb FitJU8wzgXuewjCf50IvFP4RWTj+iE5QtrLa+MtSs6tHwQTDh5Gg4oLSCvyhc7likHK0 wuTNzNiPcmSo77VxUDcnP3EmYSUZQtxJH8Bd51CIE2tNcd38vJTopdb+Y26k9SSio4zm nbpZIXkg91eu3cTBbjSAW7WJPfWScEKi4/+HEs1F2CiC7zf7ttVw3FIa2qsyYmza4dNc 2wsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=5rHI++5L701O7OTX4k0X7d4+BF26QBqlD1S5ijnqGUg=; b=rg/qI2TMO5EruB3FTlh6yXziOABUCsFcv1d7j81xfJzRT15hdibhjzcSZu2agyGNBM XJBFSWOyCwE4JQQ1r8DR4vDpdQP0IgkD/5U0o0Jjj8dm78nLRhCnYYJxnOcIB0R2GYik STRSjViCJvsCwiI/YHxcKh7f/y88Jmw9HPr3HRJx0YDAvvvKOcBWh9OtxHTGG8uMpJn1 TK8bt30engeRQauWEh1/09qE/KzWDZ03jqfjzKZModqCQacskFp4c5vwVRSgwvcP3ZtN RyH0UY00RFu2JWU9Jpyt5R8F4jdJmMaUZx+8ILynTpLjsU8+So/HFNYfHeFMk9HbAXU7 pafg== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw.atmark-techno.com (gw.atmark-techno.com. [13.115.124.170]) by gmr-mx.google.com with ESMTP id w4si298780oiv.4.2021.03.19.01.55.44 for ; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) client-ip=13.115.124.170; Received: from gw.atmark-techno.com (localhost [127.0.0.1]) by gw.atmark-techno.com (Postfix) with ESMTP id E0E2F80448 for ; Fri, 19 Mar 2021 17:55:42 +0900 (JST) Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by gw.atmark-techno.com (Postfix) with ESMTPS id 9C10B80448 for ; Fri, 19 Mar 2021 17:55:42 +0900 (JST) Received: by mail-pf1-f197.google.com with SMTP id f65so8851767pfa.13 for ; Fri, 19 Mar 2021 01:55:42 -0700 (PDT) X-Received: by 2002:a17:902:7592:b029:e2:e80f:6893 with SMTP id j18-20020a1709027592b02900e2e80f6893mr13951947pll.61.1616144141137; Fri, 19 Mar 2021 01:55:41 -0700 (PDT) X-Received: by 2002:a17:902:7592:b029:e2:e80f:6893 with SMTP id j18-20020a1709027592b02900e2e80f6893mr13951936pll.61.1616144140926; Fri, 19 Mar 2021 01:55:40 -0700 (PDT) Received: from pc-0115 (35.112.198.104.bc.googleusercontent.com. [104.198.112.35]) by smtp.gmail.com with ESMTPSA id v134sm4715901pfc.182.2021.03.19.01.55.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Mar 2021 01:55:40 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94) (envelope-from ) id 1lNAuw-004USb-PC; Fri, 19 Mar 2021 17:55:38 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH 2/3] utils: add 'warn' flag to get_output_size to silence non-errors Date: Fri, 19 Mar 2021 17:55:30 +0900 Message-Id: <20210319085531.1070347-2-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210319085531.1070347-1-dominique.martinet@atmark-techno.com> References: <20210319085531.1070347-1-dominique.martinet@atmark-techno.com> MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Also make error a bit more explicit, and check if no parameter was found (would segfault otherwise) Signed-off-by: Dominique Martinet --- Note I don't really understand the 'bytes < AES_BLK_SIZE' check -- the encrypted size has to be a multiple of the AES block size, but the decrypted size should be free to be anything -- it's just truncated as appropriate? Anyway I left this untouched as it shouldn't be a problem for me, worst that could happen here is a free space check of a few KB is skipped if someone bothers specifying such a small size in the config... For free space check I would normally just return the uncompressed size instead of an error but it's probably better to keep things simple here. core/util.c | 29 +++++++++++++++++++++++------ handlers/ubivol_handler.c | 4 ++-- include/util.h | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/core/util.c b/core/util.c index 30c7e0b7aece..0e9ad14d5020 100644 --- a/core/util.c +++ b/core/util.c @@ -895,22 +895,31 @@ int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, return n; } -long long get_output_size(struct img_type *img) +long long get_output_size(struct img_type *img, bool warn) { char *output_size_str = NULL; long long bytes = img->size; if (img->compressed) { output_size_str = dict_get_value(&img->properties, "decompressed-size"); + if (!output_size_str) { + if (warn) + ERROR("compressed file but no parameter 'decompressed-size'"); + return -1; + } bytes = ustrtoull(output_size_str, 0); if (errno) { - ERROR("decompressed-size argument: ustrtoull failed"); + if (warn) + ERROR("decompressed-size argument %s: ustrtoull failed", + output_size_str); return -1; } if (bytes == 0) { - ERROR("UBIFS to be decompressed, but decompressed-size not valid"); + if (warn) + ERROR("image to be decompressed, but decompressed-size %s not valid", + output_size_str); return -1; } TRACE("Image is compressed, decompressed size %lld bytes", bytes); @@ -918,15 +927,23 @@ long long get_output_size(struct img_type *img) } else if (img->is_encrypted) { output_size_str = dict_get_value(&img->properties, "decrypted-size"); + if (!output_size_str) { + if (warn) + ERROR("encrypted file but no parameter 'decrypted-size'"); + return -1; + } bytes = ustrtoull(output_size_str, 0); - if (errno){ - ERROR("decrypted-size argument: ustrtoull failed"); + if (errno) { + if (warn) + ERROR("decrypted-size argument %s: ustrtoull failed", + output_size_str); return -1; } if (bytes < AES_BLK_SIZE) { - ERROR("Encrypted image size (%lld) too small", bytes); + if (warn) + ERROR("Encrypted image size (%lld) too small", bytes); return -1; } TRACE("Image is crypted, decrypted size %lld bytes", bytes); diff --git a/handlers/ubivol_handler.c b/handlers/ubivol_handler.c index bf8c035e1b81..4730b8c02fbb 100644 --- a/handlers/ubivol_handler.c +++ b/handlers/ubivol_handler.c @@ -157,7 +157,7 @@ static int update_volume(libubi_t libubi, struct img_type *img, char sbuf[128]; struct ubi_vol_info *repl_vol; - bytes = get_output_size(img); + bytes = get_output_size(img, true); if (bytes <= 0) return -1; @@ -411,7 +411,7 @@ static int install_ubivol_image(struct img_type *img, int ret; if (check_ubi_autoresize(img)) { - long long bytes = get_output_size(img); + long long bytes = get_output_size(img, true); if (bytes <= 0) return -1; diff --git a/include/util.h b/include/util.h index 1fed441192bd..ef487fbed21c 100644 --- a/include/util.h +++ b/include/util.h @@ -218,7 +218,7 @@ unsigned int count_string_array(const char **nodes); void free_string_array(char **nodes); int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, LOGLEVEL level); -long long get_output_size(struct img_type *img); +long long get_output_size(struct img_type *img, bool warn); /* Decryption key functions */ int load_decryption_key(char *fname); From patchwork Fri Mar 19 08:55:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1455720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::538; helo=mail-pg1-x538.google.com; envelope-from=swupdate+bncbcwivbv7sugrbfgo2gbamgqeakvxp7i@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=rfcxaGI5; dkim-atps=neutral Received: from mail-pg1-x538.google.com (mail-pg1-x538.google.com [IPv6:2607:f8b0:4864:20::538]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F1yRB5fBpz9sRR for ; Fri, 19 Mar 2021 19:55:50 +1100 (AEDT) Received: by mail-pg1-x538.google.com with SMTP id y26sf24220551pga.10 for ; Fri, 19 Mar 2021 01:55:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1616144148; cv=pass; d=google.com; s=arc-20160816; b=kNr8sT0eU7rDtStMZpkrWCv/gQeONfK/LERvfZ+eUSFs1k2wY4KYoaupmhh/NfZSPh lVrmxtXRmrZlbaomDU45LH4e/U7lzPd2/WJOIR+giFDhaWpRw5L8jgATnHNN1yJyxP0S oqg5Oth8icmezhgJhm2KjCMOhBuXPcyBL8UBTfa/WwIoQggvN9wEfMZ+s2pWPPEJj7qI awfXH/+/WQOhazvLxz3/uq927HE0bWo0BNJwsz4O74zgzeANFkQbzKS0v9o3qM7PDeqt ET91EDCFh08h9Ae09aPswm3DI8kIOPtu2eTcbgnD+e+3v0H7vd+p6dCoRhAZAgn1GOkz bFBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=BGUh5xFmWqefdqnTQgwys73H4VTKCR8uOY+8LH4WeQQ=; b=rDpXZUbip4yOtSg00Cg2dZvbv8t7Vkov2o+WblupJm4bk1v9NAhDpJG2Fc5xxUEN0m Txf3to4cOOACqWrXVXOiv15GA8XUWuY2ykL21PPbFtAoOkIeDDBvCZR2PG3sH5Xl+OYR i4F5+koLoSVsSZgPlaYuPLe3GKj8DD9EyJfYu5fPdlkzeSA8dVhETsAlttcCXFLBbUN/ NW0AgyDMc6hYE6pttNe3mj1Kk+fTN4RihBNUCepmTUPh9b81+mz7BaB0BxPYgzcE5ya4 MjT3nKkgQKQPy3gmgwTxZmH5vYlmdz0/jM/s+CL08lu5BZPuYTCSSPeXpQFIDCvYCxrS rqsg== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=BGUh5xFmWqefdqnTQgwys73H4VTKCR8uOY+8LH4WeQQ=; b=rfcxaGI5KnNQv9zm7tfrN/0APT6skbIP1n6LZgTN99FByR8NlR5WR1EXSgE4te0Mb/ Nc8vHw7cczfSgZRIxKFh3Y+Dkm5KeOuC5efitDOH7cI4FGExZdL27iAIPfIYQTdx8cPF C0+tY0S623TYSwH7Jj7mSRSsWVVJHv1wGboU7mf9TnMHcsSLfIYzrr79Ma4fTSaQkzLR 5Ns4LvLPt7IleCA/o75a0tV6KDV/JRVFM28BFhVH4PW8rUtx2sLIgWCQpzFoLvWxi9eL dw8+tmbkcm/G//13TBK4m2n6DKBQCfo2NqrmuHkGFc9IFOFyhjW08iV9/g6nlJw1/dlK +/8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=BGUh5xFmWqefdqnTQgwys73H4VTKCR8uOY+8LH4WeQQ=; b=qI8gV9tUkhOKw2tP32PzdZqOk9AIjl8sGb3S4lrfK45mdsennEO4A9ZMSZrRLAHkcd wR4lbDFCbevt1FenQbz+ehB7blTxdsIWDypMUKGka+ERzORdjGD1JZ6p4UHQ0xiklkAM l4tGrAqDWff6uFJikdtg+xH05RWohPNDlpFBJiQwYZJnYWNiblROvaVNQ4CRx9ckQxgy rVN49GBOgjcxZm1zMJxu7ES/dl5x4K5Ds9lyD1eDvsWklJdiABvlSq5KnPegb2/fhv43 0WPyYX20Bguz70c/XE2gOf4lYBG4Hg/HqHd4HsnCe1eDnwM0I+4yp4yhaCmbnaRCcc+C MnOQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532BoOPgFfN5s/j3zMXQ4jJF+ORqqRxLUzAzHSSVszMSBof5zruJ 9PWn1xvYgMFuPOecHGq71RM= X-Google-Smtp-Source: ABdhPJzhC4tI1kzJLvgnLwydfdXkM18JjSKvLLL4TfeP0w1VaZy0XmpHrK5pL+ohwEWqwzhFHJFLsA== X-Received: by 2002:a17:90a:e60b:: with SMTP id j11mr8357881pjy.42.1616144148284; Fri, 19 Mar 2021 01:55:48 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a63:ef0b:: with SMTP id u11ls1772186pgh.4.gmail; Fri, 19 Mar 2021 01:55:47 -0700 (PDT) X-Received: by 2002:a63:3d2:: with SMTP id 201mr10180091pgd.359.1616144147340; Fri, 19 Mar 2021 01:55:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616144147; cv=none; d=google.com; s=arc-20160816; b=ZYFQr3As4P57ry51MGKtFDjbEOSuE173/0xPDzVU8xFcrMWAWgxbPeBM7HV3NxYDrL 5ILzP8Ni5qnzWiuGwbnHH54h7tB5J9LEnXctPsYVZfikb4kkhwAqNV1gWmP2aVS5VmpN Y9M1KoPvCQWd3LuTv+/nwUjmgQhutrZ7IaZnbEwxUH0GQgzoB2BmT+uaElclEv/UADdx MqtjJrOCUaqddbZTPpXK41uwh0EspSkSfH1QwfB1hizuqtWiGkoOesdg+ibCoHoUF7vB r2MfOwQsXei76EhX6emcpaWEmspVTJomNOOloe9pSl9fpitg4tStDVXQhIFPhJFTmi16 v3mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=uo2NSiJK/h2UKKc3xYnefrAFMLW/9xrzKlYNhW0/92E=; b=u61nm4fhbJuWyDf0xJrWCawh3hDBNgCaLW4j0mECBv1NrcREtnPYaaRpF4RhAaTXLt xW93hUkVeXcNu2idgEW4w/rac4OLhraBVyVEm2k8MPAhkTm3G4LM+QJAhopxsrWrKIkC 4jmb13gDk9JcY6wrZdlA/7IC586xpDBD7xSmi+A+6ngbshoEOTuRdN9F8wYQuD7+yQT0 Mxwxm3yjzTRoKuPGlru81zeNktg9Eq4Lm7vV6crvkCtjISLYsS8cR1DDjUMNuekkoWXk toLnLwb7YPbaKlzc/1ZlQxBVMalVsLKDNxlXbcNMR5+p/ZuXfDYVNw1pdoAoDUoGQbCr JL2Q== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw.atmark-techno.com (gw.atmark-techno.com. [13.115.124.170]) by gmr-mx.google.com with ESMTP id k3si293255pll.1.2021.03.19.01.55.47 for ; Fri, 19 Mar 2021 01:55:47 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) client-ip=13.115.124.170; Received: from gw.atmark-techno.com (localhost [127.0.0.1]) by gw.atmark-techno.com (Postfix) with ESMTP id 21A3B8046A for ; Fri, 19 Mar 2021 17:55:46 +0900 (JST) Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by gw.atmark-techno.com (Postfix) with ESMTPS id D30D88046A for ; Fri, 19 Mar 2021 17:55:45 +0900 (JST) Received: by mail-pg1-f197.google.com with SMTP id j4so24193559pgs.18 for ; Fri, 19 Mar 2021 01:55:45 -0700 (PDT) X-Received: by 2002:a17:90a:f28e:: with SMTP id fs14mr8609322pjb.100.1616144144482; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) X-Received: by 2002:a17:90a:f28e:: with SMTP id fs14mr8609310pjb.100.1616144144322; Fri, 19 Mar 2021 01:55:44 -0700 (PDT) Received: from pc-0115 (117.209.187.35.bc.googleusercontent.com. [35.187.209.117]) by smtp.gmail.com with ESMTPSA id g12sm4535194pjd.57.2021.03.19.01.55.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Mar 2021 01:55:43 -0700 (PDT) Received: from martinet by pc-0115 with local (Exim 4.94) (envelope-from ) id 1lNAv0-004USe-5N; Fri, 19 Mar 2021 17:55:42 +0900 From: Dominique Martinet To: swupdate@googlegroups.com Cc: Dominique Martinet , Christian Storm Subject: [swupdate] [PATCH v2 3/3] core/stream_interface: add free space checks before writing file copies Date: Fri, 19 Mar 2021 17:55:31 +0900 Message-Id: <20210319085531.1070347-3-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210319085531.1070347-1-dominique.martinet@atmark-techno.com> References: <20210319085531.1070347-1-dominique.martinet@atmark-techno.com> MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 13.115.124.170 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , copyfile would normally fail with ENOSPC on write, but if we can know beforehand that the file will not fit it is better to error early and not disrupt whatever is running. The check reuses the existing get_output_size() helper which reads the decompressed-size or decrypted-size properties depending on the file type, and is skipped altogether if no size is set (if the file is uncompressed, the size is always its size in the cpio header) No actual check that the decompressed size matches the actual size is made unless the handler requires it later, and this only checks files that are not installed directly. Signed-off-by: Dominique Martinet Cc: Christian Storm Acked-by: Stefano Babic --- Changelog v1 -> v2: - use decompressed/decrypted size properties with the ubivol helper for compatibility - make freebsd use statfs instead of statvfs; looking at linux man page linux is better off with statvfs so I kept the linux code identical to v1. I downloaded a freeBSD image so I will at least compile test this early next week, didn't have time to yet -- I've only gone as far as linux tests (size not set, uncompressed file, compressed file with size set small enough to fit into partition incorrectly or big enough to be caught by statvfs) core/stream_interface.c | 2 ++ core/util.c | 47 +++++++++++++++++++++++++++++++++++++++++ include/util.h | 1 + 3 files changed, 50 insertions(+) diff --git a/core/stream_interface.c b/core/stream_interface.c index d459010886b5..574ed47634cb 100644 --- a/core/stream_interface.c +++ b/core/stream_interface.c @@ -222,6 +222,8 @@ static int extract_files(int fd, struct swupdate_cfg *software) fdout = openfileoutput(img->extract_file); if (fdout < 0) return -1; + if (!img_check_free_space(img, fdout)) + return -1; if (copyfile(fd, &fdout, fdh.size, &offset, 0, 0, 0, &checksum, img->sha256, false, NULL, NULL) < 0) { close(fdout); return -1; diff --git a/core/util.c b/core/util.c index 0e9ad14d5020..db34d0307699 100644 --- a/core/util.c +++ b/core/util.c @@ -24,6 +24,10 @@ #include #include +#if defined(__linux__) +#include +#endif + #include "swupdate.h" #include "util.h" #include "generated/autoconf.h" @@ -951,3 +955,46 @@ long long get_output_size(struct img_type *img, bool warn) return bytes; } + +static bool check_free_space(int fd, long long size, char *fname) +{ + /* This needs OS-specific implementation because + * linux's statfs f_bsize is optimal IO size vs. statvfs f_bsize fs block size, + * and freeBSD is the opposite... + * As everything else is the same down to field names work around + * this by just defining an alias + */ +#if defined(__FreeBSD__) +#define statvfs statfs +#define fstatvfs fstatfs +#endif +#if defined(__linux__) || defined(__FreeBSD__) + struct statvfs statvfs; + + if (fstatvfs(fd, &statvfs)) { + ERROR("Statfs failed on %s, skipping free space check", fname); + return true; + } + + if (statvfs.f_bfree * statvfs.f_bsize < size) { + ERROR("Not enough free space to extract %s (needed %llu, got %lu)", + fname, size, statvfs.f_bfree * statvfs.f_bsize); + return false; + } +#endif + + return true; +} + +bool img_check_free_space(struct img_type *img, int fd) +{ + long long size; + + size = get_output_size(img, true); + + if (size <= 0) + /* Skip check if no size found */ + return true; + + return check_free_space(fd, size, img->fname); +} diff --git a/include/util.h b/include/util.h index ef487fbed21c..7b5b2a85655d 100644 --- a/include/util.h +++ b/include/util.h @@ -219,6 +219,7 @@ void free_string_array(char **nodes); int read_lines_notify(int fd, char *buf, int buf_size, int *buf_offset, LOGLEVEL level); long long get_output_size(struct img_type *img, bool warn); +bool img_check_free_space(struct img_type *img, int fd); /* Decryption key functions */ int load_decryption_key(char *fname);