| 1 | package File::Create::Layout; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $DATE = '2019-04-16'; # DATE | |||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.060'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::chdir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 14 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | 3 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 16 | create_files_using_layout | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | our %SPEC; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | my %arg_layout = ( | |||||||||||||||||||||||||||
| 22 | layout => { | ||||||||||||||||||||||||||||
| 23 | summary => 'Layout', | ||||||||||||||||||||||||||||
| 24 | description => <<'_', | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | See the module documentation for the format/specification of layout. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | _ | ||||||||||||||||||||||||||||
| 29 | schema => 'str*', | ||||||||||||||||||||||||||||
| 30 | req => 1, | ||||||||||||||||||||||||||||
| 31 | pos => 0, | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | sub _decode_json { | ||||||||||||||||||||||||||
|
Subroutine
_decode_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 7 | state $json = do { | |||||||||||||||||||||||||||
| 37 | 1 | require JSON::MaybeXS; | |||||||||||||||||||||||||||
| 38 | 1 | JSON::MaybeXS->new->allow_nonref; | |||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | 7 | $json->decode(shift); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 30 | ▶ | sub _parse_layout { | ||||||||||||||||||||||||||
|
Subroutine
_parse_layout: called CC=33 SCAR=35.0
| |||||||||||||||||||||||||||||
| 44 | 30 | my $layout = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 30 | my @indents; | |||||||||||||||||||||||||||
| 47 | my @res; | ||||||||||||||||||||||||||||
| 48 | 30 | my $linum = 0; | |||||||||||||||||||||||||||
| 49 | 30 | my $prev_is_dir = 0; | |||||||||||||||||||||||||||
| 50 | 30 | for my $line (split /^/, $layout) { | |||||||||||||||||||||||||||
| 51 | 73 | chomp $line; | |||||||||||||||||||||||||||
| 52 | 73 | my $orig_line = $line; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 73 | $linum++; | |||||||||||||||||||||||||||
| 55 | 73 | ▶ | next if $line =~ /\A\s*\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 70 | ▶ | next if $line =~ /\A\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 68 | $line =~ s/\A(\s*)//; | |||||||||||||||||||||||||||
| 59 | 68 | my $cur_indent = $1; | |||||||||||||||||||||||||||
| 60 | 68 | ▶ | die "(layout):$linum: Tabs are not allowed: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | if $cur_indent =~ /\t/; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 68 | $cur_indent = length($cur_indent); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 68 | ▶ | if (!@indents) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 28 | push @indents, $cur_indent; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 68 | ▶ | if ($cur_indent > $indents[-1]) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | # indent is deeper than previous spec-line, we require that the | ||||||||||||||||||||||||||||
| 71 | # previous spec-line is directory | ||||||||||||||||||||||||||||
| 72 | 13 | ▶ | die "(layout):$linum: More indented than previous spec-line, but ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | "previous spec-line is not a directory: $orig_line" | ||||||||||||||||||||||||||||
| 74 | unless $prev_is_dir; | ||||||||||||||||||||||||||||
| 75 | 12 | push @indents, $cur_indent; | |||||||||||||||||||||||||||
| 76 | } elsif ($cur_indent < $indents[-1]) { | ||||||||||||||||||||||||||||
| 77 | # indent is shallower than previous spec-line, find previous level | ||||||||||||||||||||||||||||
| 78 | 8 | my $found = 0; | |||||||||||||||||||||||||||
| 79 | 8 | for my $i (reverse 0..$#indents) { | |||||||||||||||||||||||||||
| 80 | 17 | ▶ | if ($cur_indent == $indents[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 6 | $found++; | |||||||||||||||||||||||||||
| 82 | 6 | splice @indents, $i+1; | |||||||||||||||||||||||||||
| 83 | 6 | last; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 8 | ▶ | die "(layout):$linum: Invalid indent, must return to one of ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | "previous levels' indent: $orig_line" unless $found; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # parse filename | ||||||||||||||||||||||||||||
| 91 | 65 | my $name; | |||||||||||||||||||||||||||
| 92 | 65 | ▶ | if ($line =~ /\A"/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 5 | ▶ | $line =~ s#\A(".*?(?<!\\)(?:\\\\)*")## | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | or die "(layout):$linum: Invalid quoted filename: $orig_line"; | ||||||||||||||||||||||||||||
| 95 | 3 | $name = $1; | |||||||||||||||||||||||||||
| 96 | 3 | eval { $name = _decode_json($name) }; | |||||||||||||||||||||||||||
| 97 | 3 | ▶ | die "(layout):$linum: Invalid JSON string in filename: $@: $1" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | if $@; | ||||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 60 | $line =~ s!\A([^\s\(/]*)!!; | |||||||||||||||||||||||||||
| 101 | 60 | $name = $1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 63 | ▶ | die "(layout):$linum: Filename cannot be empty: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | unless length($name); | ||||||||||||||||||||||||||||
| 105 | 62 | ▶ | die "(layout):$linum: Filename cannot contain slashes: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | if $name =~ m!/!; | ||||||||||||||||||||||||||||
| 107 | 61 | ▶ | die "(layout):$linum: Filename cannot be . or ..: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | if $name =~ m!\A\.\.?\z!; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 59 | my $is_dir; | |||||||||||||||||||||||||||
| 111 | 59 | ▶ | if ($line =~ s!\A/!!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 15 | $is_dir = 1; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 44 | $is_dir = 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 59 | my ($orig_perm, $perm, $user, $group); | |||||||||||||||||||||||||||
| 118 | 59 | ▶ | if ($line =~ /\A\(/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 6 | ▶ | $line =~ s/\A\((?:([^,]*),([^,]*),)?([0-7]{3,4})\)// | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | or die "(layout):$linum: Invalid syntax in permission/owner: $orig_line"; | ||||||||||||||||||||||||||||
| 121 | 2 | $user = $1; | |||||||||||||||||||||||||||
| 122 | 2 | $group = $2; | |||||||||||||||||||||||||||
| 123 | 2 | $orig_perm = $3; | |||||||||||||||||||||||||||
| 124 | 2 | $perm = oct($3); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 55 | my $sym_target; | |||||||||||||||||||||||||||
| 128 | 55 | ▶ | if ($line =~ s/\s+->\s*//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 5 | ▶ | die "(layout):$linum: Symlink cannot be a directory: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | if $is_dir; | ||||||||||||||||||||||||||||
| 131 | # parse symlink target | ||||||||||||||||||||||||||||
| 132 | 4 | ▶ | if ($line =~ /\A"/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 3 | ▶ | $line =~ s#\A(".*?(?<!\\)(?:\\\\)*")## | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | or die "(layout):$linum: Invalid quoted symlink target: $orig_line"; | ||||||||||||||||||||||||||||
| 135 | 2 | $sym_target = $1; | |||||||||||||||||||||||||||
| 136 | 2 | eval { $sym_target = _decode_json($sym_target) }; | |||||||||||||||||||||||||||
| 137 | 2 | ▶ | die "(layout):$linum: Invalid JSON string in symlink target: $@: $1" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | if $@; | ||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | 1 | $line =~ s!\A([^\s]*)!!; | |||||||||||||||||||||||||||
| 141 | 1 | $sym_target = $1; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 3 | ▶ | die "(layout):$linum: Symlink target cannot be empty: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | unless length($sym_target); | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 52 | my $extras; | |||||||||||||||||||||||||||
| 148 | 52 | ▶ | if ($line =~ s/\s+(\S.*)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 2 | $extras = $1; | |||||||||||||||||||||||||||
| 150 | 2 | eval { $extras = _decode_json("{$extras}") }; | |||||||||||||||||||||||||||
| 151 | 2 | ▶ | die "(layout):$linum: Invalid unquoted JSON hash in extras: $@: $extras" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | if $@; | ||||||||||||||||||||||||||||
| 153 | 2 | ▶ | if (defined $extras->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | die "(layout):$linum: Directory must not have 'content': $@: $orig_line" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | if $is_dir; | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | push @res, { | ||||||||||||||||||||||||||||
| 160 | name => $name, | ||||||||||||||||||||||||||||
| 161 | is_dir => $is_dir, | ||||||||||||||||||||||||||||
| 162 | is_symlink => defined($sym_target) ? 1:0, | ||||||||||||||||||||||||||||
| 163 | (symlink_target => $sym_target) x !!(defined $sym_target), | ||||||||||||||||||||||||||||
| 164 | level => $#indents >= 0 ? $#indents : 0, | ||||||||||||||||||||||||||||
| 165 | _linum => $linum, | ||||||||||||||||||||||||||||
| 166 | perm => $perm, | ||||||||||||||||||||||||||||
| 167 | perm_octal => $orig_perm, | ||||||||||||||||||||||||||||
| 168 | user => $user, | ||||||||||||||||||||||||||||
| 169 | group => $group, | ||||||||||||||||||||||||||||
| 170 | 51 | ▶ | (content => $extras->{content}) x !!(defined $extras->{content}), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 51 | $prev_is_dir = $is_dir; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 13 | \@res; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $SPEC{create_files_using_layout} = { | ||||||||||||||||||||||||||||
| 180 | 3 | v => 1.1, | |||||||||||||||||||||||||||
| 181 | summary => 'Create files/directories according to a layout', | ||||||||||||||||||||||||||||
| 182 | description => <<'_', | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This routine can be used to quickly create several files/directories according | ||||||||||||||||||||||||||||
| 185 | to a layout which you specify. The layout uses a few simple rules and common | ||||||||||||||||||||||||||||
| 186 | conventions usually found in Linux/Unix environment. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | You can use this routine e.g. in a test script. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | _ | ||||||||||||||||||||||||||||
| 191 | args => { | ||||||||||||||||||||||||||||
| 192 | %arg_layout, | ||||||||||||||||||||||||||||
| 193 | prefix => { | ||||||||||||||||||||||||||||
| 194 | summary => 'Root directory to create the files/directories in', | ||||||||||||||||||||||||||||
| 195 | description => <<'_', | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Directory must already exist. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | If unspecified, will simply create starting from current directory. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | _ | ||||||||||||||||||||||||||||
| 202 | schema => 'str*', | ||||||||||||||||||||||||||||
| 203 | }, | ||||||||||||||||||||||||||||
| 204 | }, | ||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||
| 206 | 1 | ▶ | sub create_files_using_layout { | ||||||||||||||||||||||||||
|
Subroutine
create_files_using_layout: called CC=25 SCAR=39.1
Pod
documented
| |||||||||||||||||||||||||||||
| 207 | 1 | require File::chown; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1 | my $parse_res; | |||||||||||||||||||||||||||
| 212 | 1 | eval { $parse_res = _parse_layout($args{layout}) }; | |||||||||||||||||||||||||||
| 213 | 1 | ▶ | return [400, "Syntax error in layout: $@"] if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | my $prefix = $args{prefix}; | |||||||||||||||||||||||||||
| 216 | 1 | ▶ | local $CWD = $prefix // $CWD; | ||||||||||||||||||||||||||
|
Condition33%1/3
$prefix // $File::Create::Layout::CWD
MC/DC0%0/2
$prefix // $File::Create::Layout::CWD
$prefix
$File::Create::Layout::CWD
Truth table33%1/3
A // B
A= $prefixB= $File::Create::Layout::CWD
| |||||||||||||||||||||||||||||
| 217 | 1 | ▶ | $prefix //= "."; | ||||||||||||||||||||||||||
|
Condition50%1/2
$prefix //= "."
MC/DC0%0/1
$prefix //= "."
$prefix
Truth table50%1/2
A
A= $prefix
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | my $prev_level; | |||||||||||||||||||||||||||
| 220 | my @dirs; | ||||||||||||||||||||||||||||
| 221 | 1 | for my $e (@$parse_res) { | |||||||||||||||||||||||||||
| 222 | 33 | my $p = $prefix . join("", map {"/$_"} @dirs); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 33 | ▶ | if (defined $prev_level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 32 | ▶ | if ($e->{level} > $prev_level) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 226 | 8 | log_trace("chdir %s ...", $dirs[-1]); | |||||||||||||||||||||||||||
| 227 | 8 | eval { $CWD = $dirs[-1] }; | |||||||||||||||||||||||||||
| 228 | 8 | ▶ | return [500, "Can't chdir to $p/$e->{name}: $! (cwd=$CWD)"] if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | } elsif ($e->{level} < $prev_level) { | ||||||||||||||||||||||||||||
| 230 | 5 | my $dir = join("/", (("..") x ($prev_level - $e->{level}))); | |||||||||||||||||||||||||||
| 231 | 5 | splice @dirs, $e->{level}; | |||||||||||||||||||||||||||
| 232 | 5 | $p = $prefix . join("", map {"/$_"} @dirs); | |||||||||||||||||||||||||||
| 233 | 5 | log_trace("chdir back %s ...", $dir); | |||||||||||||||||||||||||||
| 234 | 5 | eval { $CWD = $dir }; | |||||||||||||||||||||||||||
| 235 | 5 | ▶ | return [500, "Can't chdir back to $dir: $! (cwd=$CWD)"] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | if $@; | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | log_trace("Creating %s/%s%s ...", | ||||||||||||||||||||||||||||
| 241 | 33 | ▶ | $p, $e->{name}, $e->{is_dir} ? "/":""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 33 | ▶ | if ($e->{is_dir}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 243 | 8 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 8 | ▶ | if (defined $e->{perm}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | mkdir($e->{name}, $e->{perm}); | |||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||
| 247 | 8 | mkdir($e->{name}); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } or return [500, "Can't create directory $p/$e->{name}: $!"]; | ||||||||||||||||||||||||||||
| 250 | 8 | $dirs[$e->{level}] = $e->{name}; | |||||||||||||||||||||||||||
| 251 | } elsif ($e->{is_symlink}) { | ||||||||||||||||||||||||||||
| 252 | symlink($e->{symlink_target}, $e->{name}) | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | or return [500, "Can't create symlink $p/$e->{name} -> ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | "$e->{symlink_target}: $!"]; | ||||||||||||||||||||||||||||
| 255 | } else { | ||||||||||||||||||||||||||||
| 256 | open my($fh), ">", $e->{name} | ||||||||||||||||||||||||||||
| 257 | 25 | ▶ | or return [500, "Can't create file $p/$e->{name}: $!"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 25 | ▶ | if (defined $e->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | print $fh $e->{content} | ||||||||||||||||||||||||||||
| 260 | 0 | ▶ | or return [500, "Can't write content to file ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | "$p/$e->{name}: $!"]; | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 25 | ▶ | if (defined $e->{perm}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | chmod($e->{perm}, $e->{name}) | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | or return [500, "Can't chmod file $p/$e->{name}: $!"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 33 | ▶ | if (defined($e->{user}) || defined($e->{group})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $e->{"user"} or defined $e->{"group"}
MC/DC0%0/2
defined $e->{"user"} or defined $e->{"group"}
defined $e->{"user"}
defined $e->{"group"}
Truth table33%1/3
A or B
A= defined $e->{"user"}B= defined $e->{"group"}
| |||||||||||||||||||||||||||||
| 270 | 0 | my %opts; | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | $opts{deref} = 0 if $e->{is_symlink}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | File::chown::chown(\%opts, $e->{user}, $e->{group}, $e->{name}) | ||||||||||||||||||||||||||||
| 273 | 0 | ▶ | or return [500, "Can't chown file $p/$e->{name}: $!"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 33 | $prev_level = $e->{level}; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 1 | [200, "OK"]; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | $SPEC{check_layout} = { | ||||||||||||||||||||||||||||
| 283 | 3 | v => 1.1, | |||||||||||||||||||||||||||
| 284 | summary => 'Check whether layout has syntax errors', | ||||||||||||||||||||||||||||
| 285 | args => { | ||||||||||||||||||||||||||||
| 286 | %arg_layout, | ||||||||||||||||||||||||||||
| 287 | }, | ||||||||||||||||||||||||||||
| 288 | }; | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | sub check_layout { | ||||||||||||||||||||||||||
|
Subroutine
check_layout: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | eval { _parse_layout($args{layout}) }; | |||||||||||||||||||||||||||
| 293 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 294 | 0 | ▶ | [200, "OK", $err ? 0:1, {'func.error' => $err}]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | $SPEC{parse_layout} = { | ||||||||||||||||||||||||||||
| 298 | 3 | v => 1.1, | |||||||||||||||||||||||||||
| 299 | summary => 'Parse layout string into a data structure '. | ||||||||||||||||||||||||||||
| 300 | 'suitable for processing', | ||||||||||||||||||||||||||||
| 301 | args => { | ||||||||||||||||||||||||||||
| 302 | %arg_layout, | ||||||||||||||||||||||||||||
| 303 | }, | ||||||||||||||||||||||||||||
| 304 | }; | ||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub parse_layout { | ||||||||||||||||||||||||||
|
Subroutine
parse_layout: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 0 | my $res; | |||||||||||||||||||||||||||
| 309 | 0 | eval { $res = _parse_layout($args{layout}) }; | |||||||||||||||||||||||||||
| 310 | 0 | ▶ | return [400, "Layout has error(s): $@"] if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | [200, "OK", $res]; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 3 | 1; | |||||||||||||||||||||||||||
| 315 | # ABSTRACT: Quickly create files/directories according to a layout | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | __END__ | ||||||||||||||||||||||||||||