From patchwork Mon Apr 13 07:45:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 2944 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4910zc09DLz3yBt for ; Mon, 13 Apr 2020 07:46:04 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4910zY2jY8z2jq; Mon, 13 Apr 2020 07:46:01 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4910zX3T49z30FM; Mon, 13 Apr 2020 07:46:00 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4910zW0bFjz2yXg for ; Mon, 13 Apr 2020 07:45:59 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id 4910zV4ld5zgh; Mon, 13 Apr 2020 07:45:58 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1586763958; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gxIBNRuYp4f/rzIhz1zQfJ1BvfCq0JGytFM5yG4b9kQ=; b=fWQrrF3M8OSi+WAjwljtDlKCvjGPTIf9AC9z2A24GLelZaeIZVzrBslpkke3MydDHLWIYd XhawjR2xyG5nAAAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1586763958; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gxIBNRuYp4f/rzIhz1zQfJ1BvfCq0JGytFM5yG4b9kQ=; b=q0Mi7klZeGb0p7ux2HA9WpF4tGOzMyKcSG5TdrKn8bbE/vdLLx1501rSrlknLd8dSvVSkf m4LRERkoRgVteuD7CU7059e0QkKRflgHFQcYjkeJ/n2NMxFyuyP2HGFRnJrI1kGUNjDlGX RubCFnpKdXfFA1O7xjR3dfnoKD+RQs7Mgvk+yODmJWTtKzfwatc3rHn3jzjRYSZhLuMATm zeSfCoysN+iLaIEe9NFFcXToIehdL9ISMv51PtoRwW8QNoVNf7T7OSBBU+HFYmIq3VVVSM r1R1bw5yeYkmskqCDWNmubx7oPyYEHsKmklOskm2b1S4R1CJUm0Ex1y6zwb8kQ== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 04/19] general-functions.pl: Add formatBytes() function. Date: Mon, 13 Apr 2020 09:45:35 +0200 Message-Id: <20200413074550.2735-4-stefan.schantl@ipfire.org> In-Reply-To: <20200413074550.2735-1-stefan.schantl@ipfire.org> References: <20200413074550.2735-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.mailfrom=stefan.schantl@ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This function can be used to convert an amount of bytes to a humand-readable format. For example "3221225472" will become "3MB". Signed-off-by: Stefan Schantl --- config/cfgroot/general-functions.pl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl index 41a0eac2d..692e072c2 100644 --- a/config/cfgroot/general-functions.pl +++ b/config/cfgroot/general-functions.pl @@ -1261,4 +1261,29 @@ sub get_nameservers () { return &uniq(@nameservers); } +# Function to format a string containing the amount of bytes to +# something human-readable. +sub formatBytes { + # Private array which contains the units. + my @units = qw(B KB MB GB TB PB); + + my $bytes = shift; + my $unit; + + # Loop through the array of units. + foreach my $element (@units) { + # Break loop if the bytes are less than the next unit. + last if $bytes < 1024; + + # Divide bytes amount with 1024. + $bytes /= 1024; + + # Assign current processed element to unit. + $unit = $element; + } + + # Return the divided and rounded bytes count and the unit. + return sprintf("%.2f %s", $bytes, $unit); +} + 1;