| 1 | # | ||||||||||||||||||
| 2 | # This file is part of Config-Model | ||||||||||||||||||
| 3 | # | ||||||||||||||||||
| 4 | # This software is Copyright (c) 2005-2022 by Dominique Dumont. | ||||||||||||||||||
| 5 | # | ||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||
| 7 | # | ||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||
| 9 | # | ||||||||||||||||||
| 10 | package Config::Model::Backend::Fstab 2.169; | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 1 | ▶ | use Mouse; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | 1 | ▶ | use 5.020; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 1 | ▶ | use feature qw/postderef signatures/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | 1 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 1 | extends 'Config::Model::Backend::Any'; | |||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 1 | my $logger = get_logger("Backend::Fstab"); | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 4 | ▶ | sub annotation { return 1; } | ||||||||||||||||
|
Subroutine
annotation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 1 | my %opt_r_translate = ( | |||||||||||||||||
| 27 | ro => 'rw=0', | ||||||||||||||||||
| 28 | rw => 'rw=1', | ||||||||||||||||||
| 29 | bsddf => 'statfs_behavior=bsddf', | ||||||||||||||||||
| 30 | minixdf => 'statfs_behavior=minixdf', | ||||||||||||||||||
| 31 | noatime => 'atime=0', | ||||||||||||||||||
| 32 | noauto => 'auto=0', | ||||||||||||||||||
| 33 | nodev => 'dev=0', | ||||||||||||||||||
| 34 | nodiratime => 'diratime=0', | ||||||||||||||||||
| 35 | noexec => 'exec=0', | ||||||||||||||||||
| 36 | noiversion => 'iversion=0', | ||||||||||||||||||
| 37 | nomand => 'mand=0', | ||||||||||||||||||
| 38 | norelatime => 'relatime=0', | ||||||||||||||||||
| 39 | nostrictatime => 'strictatime=0', | ||||||||||||||||||
| 40 | nolazytime => 'lazytime=0', | ||||||||||||||||||
| 41 | nosuid => 'suid=0', | ||||||||||||||||||
| 42 | nouser => 'user=0', | ||||||||||||||||||
| 43 | ); | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||||||||||
| 46 | 4 | ▶ | sub read ($self, %args) { | ||||||||||||||||
|
Subroutine
read: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||
| 47 | # args are: | ||||||||||||||||||
| 48 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||||||||
| 49 | # config_dir => /etc/foo', # absolute path | ||||||||||||||||||
| 50 | # file => 'foo.conf', # file name | ||||||||||||||||||
| 51 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||||||||
| 52 | # check => yes|no|skip | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 4 | ▶ | return 0 unless $args{file_path}->exists; # no file to read | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 4 | ▶ | my $check = $args{check} || 'yes'; | ||||||||||||||||
|
Condition50%1/2
$args{'check'} || 'yes'
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 4 | my @lines = $args{file_path}->lines_utf8; | |||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | # try to get global comments (comments before a blank line) | ||||||||||||||||||
| 60 | 4 | $self->read_global_comments( \@lines, '#' ); | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 4 | my @assoc = $self->associates_comments_with_data( \@lines, '#' ); | |||||||||||||||||
| 63 | 4 | foreach my $item (@assoc) { | |||||||||||||||||
| 64 | 62 | my ( $data, $comment ) = @$item; | |||||||||||||||||
| 65 | 62 | $logger->trace("fstab read data '$data' comment '$comment'"); | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 62 | my ( $device, $mount_point, $type, $options, $dump, $pass ) = | |||||||||||||||||
| 68 | split /\s+/, $data; | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 62 | my $swap_idx = 0; | |||||||||||||||||
| 71 | 62 | ▶ | my $label = | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 72 | $device =~ /LABEL=(\w+)$/ ? $1 | ||||||||||||||||||
| 73 | : $type eq 'swap' ? "swap-" . $swap_idx++ | ||||||||||||||||||
| 74 | : $mount_point; | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 62 | my $fs_obj = $self->node->fetch_element('fs')->fetch_with_id($label); | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 62 | ▶ | if ($comment) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 26 | $logger->trace("Annotation: $comment\n"); | |||||||||||||||||
| 80 | 26 | $fs_obj->annotation($comment); | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 62 | my $load_line = "fs_vfstype=$type fs_spec=$device fs_file=$mount_point " | |||||||||||||||||
| 84 | . "fs_freq=$dump fs_passno=$pass"; | ||||||||||||||||||
| 85 | 62 | $logger->debug("Loading:$load_line\n"); | |||||||||||||||||
| 86 | 62 | $fs_obj->load( step => $load_line, check => $check ); | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | # now load fs options | ||||||||||||||||||
| 89 | 62 | $logger->trace("fs_type $type options is $options"); | |||||||||||||||||
| 90 | # options can contain quoted values like | ||||||||||||||||||
| 91 | # fscontext="system_u:object_r:tmp_t:s0:c127,c456" | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 62 | my @options = $options =~ m/("[^"]+"|[^,]+)(?:,\s*)?/g; | |||||||||||||||||
| 94 | 62 | my @steps; | |||||||||||||||||
| 95 | 62 | foreach my $opt ( @options ) { | |||||||||||||||||
| 96 | # $v contains the quotes coming from /etc/fstab | ||||||||||||||||||
| 97 | 136 | my ($k,$v) = split /=/, $opt; | |||||||||||||||||
| 98 | 136 | ▶ | $v //= 1; | ||||||||||||||||
|
Condition100%2/2
$v //= 1
MC/DC100%1/1
$v //= 1
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||
| 99 | 136 | ▶ | push @steps, $opt_r_translate{$k} // sprintf('"%s"=%s', $k, $v); | ||||||||||||||||
|
Condition100%2/2
$opt_r_translate{$k} // sprintf('"%s"=%s', $k, $v)
MC/DC100%1/1
$opt_r_translate{$k} // sprintf('"%s"=%s', $k, $v)
$opt_r_translate{$k}
Truth table100%2/2
A
A= $opt_r_translate{$k}
| |||||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 62 | $logger->debug("Loading:@steps"); | |||||||||||||||||
| 103 | 62 | $fs_obj->fetch_element('fs_mntopts')->load( step => "@steps", check => $check ); | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | 4 | return 1; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 2 | ▶ | sub write ($self, %args) { | ||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 109 | # args are: | ||||||||||||||||||
| 110 | # object => $obj, # Config::Model::Node object | ||||||||||||||||||
| 111 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||||||||
| 112 | # config_dir => /etc/foo', # absolute path | ||||||||||||||||||
| 113 | # file => 'foo.conf', # file name | ||||||||||||||||||
| 114 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||||||||
| 115 | # check => yes|no|skip | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 2 | my $node = $args{object}; | |||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 2 | ▶ | croak "Undefined file handle to write" unless defined $args{file_path}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | 2 | my $res = $self->write_global_comment( '#' ); | |||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | # Using Config::Model::ObjTreeScanner would be overkill | ||||||||||||||||||
| 124 | 2 | foreach my $line_obj ( $node->fetch_element('fs')->fetch_all ) { | |||||||||||||||||
| 125 | my $d = sprintf( | ||||||||||||||||||
| 126 | "%-30s %-25s %-6s %-10s %d %d\n", | ||||||||||||||||||
| 127 | 93 | (map { $line_obj->fetch_element_value($_); } qw/fs_spec fs_file fs_vfstype/), | |||||||||||||||||
| 128 | $self->option_string( $line_obj->fetch_element('fs_mntopts') ), | ||||||||||||||||||
| 129 | 31 | (map { $line_obj->fetch_element_value($_); } qw/fs_freq fs_passno/), | |||||||||||||||||
| 130 | ); | ||||||||||||||||||
| 131 | 31 | $res .= $self->write_data_and_comments( '#', $d, $line_obj->annotation ); | |||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | 2 | $args{file_path}->spew_utf8($res); | |||||||||||||||||
| 136 | 2 | return 1; | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | 1 | my %rev_opt_r_translate = reverse %opt_r_translate; | |||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 31 | ▶ | sub option_string ($self, $obj) { | ||||||||||||||||
|
Subroutine
option_string: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 142 | 31 | my @options; | |||||||||||||||||
| 143 | 31 | foreach my $opt ( $obj->get_element_name ) { | |||||||||||||||||
| 144 | 699 | my $v = $obj->fetch_element_value($opt); | |||||||||||||||||
| 145 | 699 | ▶ | next unless defined $v; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 146 | 68 | my $key = "$opt=$v"; | |||||||||||||||||
| 147 | my $str = | ||||||||||||||||||
| 148 | 68 | ▶ | defined $rev_opt_r_translate{$key} ? $rev_opt_r_translate{$key} | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 149 | : "$v" eq '0' ? 'no' . $opt | ||||||||||||||||||
| 150 | : "$v" eq '1' ? $opt | ||||||||||||||||||
| 151 | : $key; | ||||||||||||||||||
| 152 | 68 | push @options, $str; | |||||||||||||||||
| 153 | } | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | 31 | return join ',', @options; | |||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 1 | ▶ | no Mouse; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 159 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | # ABSTRACT: Read and write config from fstab file | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||