| 1 | # Copyright © 2011 Raphaël Hertzog <hertzog@debian.org> | ||||||||||
| 2 | # Copyright © 2012 Guillem Jover <guillem@debian.org> | ||||||||||
| 3 | # | ||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||
| 7 | # (at your option) any later version. | ||||||||||
| 8 | # | ||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||
| 13 | # | ||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||
| 16 | |||||||||||
| 17 | =encoding utf8 | ||||||||||
| 18 | |||||||||||
| 19 | =head1 NAME | ||||||||||
| 20 | |||||||||||
| 21 | Dpkg::File - file handling | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | This module provides file handling support functions. | ||||||||||
| 26 | |||||||||||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | package Dpkg::File 0.01; | ||||||||||
| 32 | |||||||||||
| 33 | 25 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 23 | our @EXPORT = qw( | |||||||||
| 36 | file_slurp | ||||||||||
| 37 | file_dump | ||||||||||
| 38 | file_touch | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 25 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 25 | ▶ | use Scalar::Util qw(openhandle); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 25 | ▶ | use Dpkg::ErrorHandling; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 25 | ▶ | use Dpkg::Gettext; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 40 | ▶ | sub file_slurp { | ||||||||
|
Subroutine
file_slurp: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 48 | 40 | my $file = shift; | |||||||||
| 49 | 40 | my $fh; | |||||||||
| 50 | 40 | my $doclose = 0; | |||||||||
| 51 | |||||||||||
| 52 | 40 | ▶ | if (openhandle($file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 5 | $fh = $file; | |||||||||
| 54 | } else { | ||||||||||
| 55 | 35 | ▶ | open $fh, '<', $file or syserr(g_('cannot read %s'), $fh); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 34 | $doclose = 1; | |||||||||
| 57 | } | ||||||||||
| 58 | 39 | local $/; | |||||||||
| 59 | 39 | my $data = <$fh>; | |||||||||
| 60 | 39 | ▶ | close $fh if $doclose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 39 | return $data; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 38 | ▶ | sub file_dump { | ||||||||
|
Subroutine
file_dump: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 66 | 38 | my ($file, $data) = @_; | |||||||||
| 67 | 38 | my $fh; | |||||||||
| 68 | 38 | my $doclose = 0; | |||||||||
| 69 | |||||||||||
| 70 | 38 | ▶ | if (openhandle($file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 15 | $fh = $file; | |||||||||
| 72 | } else { | ||||||||||
| 73 | 23 | ▶ | open $fh, '>', $file or syserr(g_('cannot create file %s'), $file); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 23 | $doclose = 1; | |||||||||
| 75 | } | ||||||||||
| 76 | 38 | print { $fh } $data; | |||||||||
| 77 | 38 | ▶ | if ($doclose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 23 | ▶ | close $fh or syserr(g_('cannot write %s'), $file); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 38 | return; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 75 | ▶ | sub file_touch { | ||||||||
|
Subroutine
file_touch: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||
| 85 | 75 | my $file = shift; | |||||||||
| 86 | |||||||||||
| 87 | 75 | ▶ | open my $fh, '>>', $file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | or syserr(g_('cannot touch file %s'), $file); | ||||||||||
| 89 | 75 | ▶ | close $fh or syserr(g_('cannot write %s'), $file); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 75 | ▶ | utime undef, undef, $file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | or syserr(g_('cannot change timestamp for %s'), $file); | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head1 CHANGES | ||||||||||
| 95 | |||||||||||
| 96 | =head2 Version 0.xx | ||||||||||
| 97 | |||||||||||
| 98 | This is a private module. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 23 | 1; | |||||||||