Fix dhcpcd.conf duplicate entries + trailing newline accumulation

This commit is contained in:
billz
2025-12-21 19:13:33 +01:00
parent 15a680934d
commit 3211735534

View File

@@ -131,15 +131,15 @@ class DhcpcdManager
$dhcp_cfg = $this->removeIface($dhcp_cfg,'uap0'); $dhcp_cfg = $this->removeIface($dhcp_cfg,'uap0');
$dhcp_cfg .= $config; $dhcp_cfg .= $config;
} else { } else {
if (strpos($dhcp_cfg, 'interface '.$ap_iface) !== false &&
strpos($dhcp_cfg, 'nogateway') !== false) {
$config[] = 'nogateway';
}
$config = join(PHP_EOL, $config); $config = join(PHP_EOL, $config);
$dhcp_cfg = $this->removeIface($dhcp_cfg,'br0'); $dhcp_cfg = $this->removeIface($dhcp_cfg,'br0');
$dhcp_cfg = $this->removeIface($dhcp_cfg,'uap0'); $dhcp_cfg = $this->removeIface($dhcp_cfg,'uap0');
if (!strpos($dhcp_cfg, 'metric')) { if (!strpos($dhcp_cfg, 'metric')) {
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$ap_iface.'\s.*?(?:\s*^\s*$|\s*nogateway)/ms', $config, $dhcp_cfg, 1); $pattern = '/^#\sRaspAP\s' . preg_quote($ap_iface, '/') . '\sconfiguration\n' .
'(?:.*\n)*?' .
'(?:\n)*' .
'(?=#\sRaspAP\s|\z)/m';
$dhcp_cfg = preg_replace($pattern, $config . "\n\n", $dhcp_cfg, 1);
} else { } else {
$metrics = true; $metrics = true;
} }
@@ -199,12 +199,11 @@ class DhcpcdManager
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); $status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
} else { } else {
$cfg = join(PHP_EOL, $cfg); $cfg = join(PHP_EOL, $cfg);
$dhcp_cfg = preg_replace( $pattern = '/^#\sRaspAP\s' . preg_quote($iface, '/') . '\sconfiguration\n' .
'/^#\sRaspAP\s'.$iface.'\s.*?(?=\n*(?:^#\sRaspAP|^interface\s(?!'.$iface.'$)|\z))/ms', '(?:.*\n)*?' .
$cfg . PHP_EOL, '(?:\n)*' .
$dhcp_cfg, '(?=#\sRaspAP\s|\z)/m';
1 $dhcp_cfg = preg_replace($pattern, $cfg . "\n\n", $dhcp_cfg, 1);
);
} }
return $dhcp_cfg; return $dhcp_cfg;
@@ -363,18 +362,31 @@ class DhcpcdManager
]; ];
// merge existing settings with updates // merge existing settings with updates
$processed_keys = [];
foreach ($existing_config as $line) { foreach ($existing_config as $line) {
$matched = false; $matched = false;
foreach ($static_settings as $key => $value) { foreach ($static_settings as $key => $value) {
if (strpos($line, $key) === 0) { if (strpos($line, $key) === 0) {
$config[] = "$key=$value"; $config[] = "$key=$value";
$matched = true; $matched = true;
$processed_keys[] = $key;
unset($static_settings[$key]); unset($static_settings[$key]);
break; break;
} }
} }
if (!$matched && !preg_match('/^interface/', $line)) { if (!$matched && !preg_match('/^interface/', $line)) {
$config[] = $line; // check if this line matches a key we've already processed (prevents duplicates)
$is_duplicate = false;
foreach ($processed_keys as $processed_key) {
if (strpos($line, $processed_key) === 0) {
$is_duplicate = true;
break;
}
}
// also check if the line already exists in config (for non-static settings like nogateway)
if (!$is_duplicate && !in_array($line, $config, true)) {
$config[] = $line;
}
} }
} }