[pve-devel] [PATCH v3 pve-storage 06/13] cephconfig: support line-continuations in parser

Max Carrara m.carrara at proxmox.com
Fri Feb 16 15:56:08 CET 2024


Ceph's docs state the following [0]:
> The backslash character `\` is used as the line-continuation marker
> that combines the next line with the current one.

This commit implements the support of such line-continuations in our
parser.

The line following a line ending with a '\' has its whitespace
preserved, which matches the behaviour in Ceph's original
implementation [1]. In other words, leading and trailing whitespace is
not stripped from a continued line.

[0]: https://docs.ceph.com/en/reef/rados/configuration/ceph-conf/#changes-introduced-in-octopus
[1]: https://git.proxmox.com/?p=ceph.git;a=blob;f=ceph/src/common/ConfUtils.cc;h=2f78fd02bf9e27467275752e6f3bca0c5e3946ce;hb=refs/heads/master#l262

Signed-off-by: Max Carrara <m.carrara at proxmox.com>
---
Changes v2 --> v3:
  * new

 src/PVE/CephConfig.pm | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/PVE/CephConfig.pm b/src/PVE/CephConfig.pm
index bbb9d39..2c4a086 100644
--- a/src/PVE/CephConfig.pm
+++ b/src/PVE/CephConfig.pm
@@ -19,13 +19,32 @@ sub parse_ceph_config {
     return $cfg if !defined($raw);
 
     my @lines = split /\n/, $raw;
+    my @lines_normalized;
+
+    my $re_comment_not_escaped = qr/(?<!\\)(#|;).*$/;
+    my $re_leading_ws = qr/^\s+/;
+    my $re_trailing_ws = qr/\s+$/;
+
+    while (scalar(@lines)) {
+	my $line = shift(@lines);
+	$line =~ s/$re_comment_not_escaped//;
+	$line =~ s/$re_leading_ws//;
+	$line =~ s/$re_trailing_ws//;
+	next if !$line;
+
+	# merge lines ending with continuation character '\'
+	while ($line =~ s/\\$//) {
+	    my $next_line = shift(@lines);
+	    $next_line =~ s/$re_comment_not_escaped//;
+	    $line .= $next_line;
+	}
+
+	push(@lines_normalized, $line);
+    }
 
     my $section;
 
-    for my $line (@lines) {
-	$line =~ s/(?<!\\)(#|;).*$//;
-	$line =~ s/^\s+//;
-	$line =~ s/\s+$//;
+    for my $line (@lines_normalized) {
 	next if !$line;
 
 	$section = $1 if $line =~ m/^\[(.+)\]$/;
-- 
2.39.2





More information about the pve-devel mailing list