Skip to content

Commit

Permalink
systemd::escape: Also escape - (dash)
Browse files Browse the repository at this point in the history
We also need to escape dashes in unit names, for instance for .swap
services on device nodes with dashes in them.

systemd-escape escapes dashes:
  # systemd-escape -p /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-ua-swap
  dev-disk-by\x2did-scsi\x2d0QEMU_QEMU_HARDDISK_drive\x2dua\x2dswap
but this function does not (yet).

In the long term, we may want to more closely match the behaviour of
systemd's unit-name.c, but this is all I need for now.

(Also cf. voxpupuli#242.)
  • Loading branch information
Peter Palfrader committed Feb 8, 2022
1 parent b1bdb2b commit 5c04ec1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions functions/escape.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function systemd::escape(String[1] $input, Boolean $path = false) >> String {
}
'/': { $_escaped = '-' }
',': { $_escaped = '\x2c' }
'-': { $_escaped = '\x2d' }
default: { $_escaped = $_token }
}
$_escaped
Expand Down
2 changes: 2 additions & 0 deletions spec/functions/escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it { is_expected.to run.with_params('//foo//bar//', false).and_return('--foo--bar--') }
it { is_expected.to run.with_params('//foo:bar,foo_bar.//', false).and_return('--foo:bar\x2cfoo_bar.--') }
it { is_expected.to run.with_params('.foo', false).and_return('\x2efoo') }
it { is_expected.to run.with_params('/foo/bar-baz/qux-quux', false).and_return('-foo-bar\x2dbaz-qux\x2dquux') }
end

context 'with path true' do
Expand All @@ -18,5 +19,6 @@
it { is_expected.to run.with_params('//foo//bar//', true).and_return('foo-bar') }
it { is_expected.to run.with_params('//foo:bar,foo_bar.//', true).and_return('foo:bar\x2cfoo_bar.') }
it { is_expected.to run.with_params('.foo', true).and_return('\x2efoo') }
it { is_expected.to run.with_params('/foo/bar-baz/qux-quux', true).and_return('foo-bar\x2dbaz-qux\x2dquux') }
end
end

0 comments on commit 5c04ec1

Please sign in to comment.