From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <t.lamprecht@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id BAF6167797
 for <pmg-devel@pve.proxmox.com>; Tue, 10 Nov 2020 09:35:29 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A525B1D6C0
 for <pmg-devel@pve.proxmox.com>; Tue, 10 Nov 2020 09:34:59 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 29B181D6B2
 for <pmg-devel@pve.proxmox.com>; Tue, 10 Nov 2020 09:34:59 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id EE0EB45F8A
 for <pmg-devel@pve.proxmox.com>; Tue, 10 Nov 2020 09:34:58 +0100 (CET)
To: Stoiko Ivanov <s.ivanov@proxmox.com>, pmg-devel@pve.proxmox.com
References: <20201028185432.23067-1-s.ivanov@proxmox.com>
 <20201028185432.23067-2-s.ivanov@proxmox.com>
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
Message-ID: <e843ea9c-3913-7ead-2c70-ac9e70e630e9@proxmox.com>
Date: Tue, 10 Nov 2020 09:34:58 +0100
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101
 Thunderbird/83.0
MIME-Version: 1.0
In-Reply-To: <20201028185432.23067-2-s.ivanov@proxmox.com>
Content-Type: text/plain; charset=UTF-8
Content-Language: en-US
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.104 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 NICE_REPLY_A           -0.001 Looks like a legit reply (A)
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [directory.pm]
Subject: Re: [pmg-devel] [PATCH pve-common 1/2] Systemd: add helpers for
 parsing unit files
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 10 Nov 2020 08:35:29 -0000

On 28.10.20 19:54, Stoiko Ivanov wrote:
> taken from pve-storage/PVE/API2/Disks/Directory.pm (and made available as
> public sub)

we could now replace those usages over there? Not urgent, just as reminder. 

> 
> Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
> ---
> refactoring pve-storage to use this (and drop the now duplicated code) will
> be done separately
> 
>  src/PVE/Systemd.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 72 insertions(+)
> 
> diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm
> index 85b35a3..bcba5eb 100644
> --- a/src/PVE/Systemd.pm
> +++ b/src/PVE/Systemd.pm
> @@ -7,6 +7,8 @@ use Net::DBus qw(dbus_uint32 dbus_uint64);
>  use Net::DBus::Callback;
>  use Net::DBus::Reactor;
>  
> +use PVE::Tools qw(file_set_contents file_get_contents trim);
> +
>  sub escape_unit {
>      my ($val, $is_path) = @_;
>  
> @@ -163,4 +165,74 @@ sub wait_for_unit_removed($;$) {
>      }, $timeout);
>  }
>  
> +sub read_ini {
> +    my ($filename) = @_;
> +
> +    my $content = file_get_contents($filename);
> +    my @lines = split /\n/, $content;
> +
> +    my $result = {};
> +    my $section;
> +
> +    foreach my $line (@lines) {
> +	$line = trim($line);
> +	if ($line =~ m/^\[([^\]]+)\]/) {
> +	    $section = $1;
> +	    if (!defined($result->{$section})) {
> +		$result->{$section} = {};
> +	    }
> +	} elsif ($line =~ m/^(.*?)=(.*)$/) {
> +	    my ($key, $val) = ($1, $2);
> +	    if (!$section) {
> +		warn "key value pair found without section, skipping\n";
> +		next;
> +	    }
> +
> +	    if ($result->{$section}->{$key}) {
> +		# make duplicate properties to arrays to keep the order
> +		my $prop = $result->{$section}->{$key};
> +		if (ref($prop) eq 'ARRAY') {
> +		    push @$prop, $val;
> +		} else {
> +		    $result->{$section}->{$key} = [$prop, $val];
> +		}
> +	    } else {
> +		$result->{$section}->{$key} = $val;
> +	    }
> +	}
> +	# ignore everything else
> +    }
> +
> +    return $result;
> +};
> +
> +sub write_ini {
> +    my ($ini, $filename) = @_;
> +
> +    my $content = "";
> +
> +    foreach my $sname (sort keys %$ini) {
> +	my $section = $ini->{$sname};
> +
> +	$content .= "[$sname]\n";
> +
> +	foreach my $pname (sort keys %$section) {
> +	    my $prop = $section->{$pname};
> +
> +	    if (!ref($prop)) {
> +		$content .= "$pname=$prop\n";
> +	    } elsif (ref($prop) eq 'ARRAY') {
> +		foreach my $val (@$prop) {
> +		    $content .= "$pname=$val\n";
> +		}
> +	    } else {
> +		die "invalid property '$pname'\n";
> +	    }
> +	}
> +	$content .= "\n";
> +    }
> +
> +    file_set_contents($filename, $content);
> +};
> +
>  1;
>