blob: 49f5c2ba120a8f0f8799b912280770ec3bdabedf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# Module: File.pm
# File manipulation functions
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****
package Vyatta::File;
use strict;
use warnings;
our @EXPORT = qw(touch mkdir_p rm_rf);
our @EXPORT_OK = qw(show_error);
use base qw(Exporter);
use Fcntl;
use File::Path qw(make_path remove_tree);
# Change file time stamps
# if file does not exist, it is created empty
sub touch {
my $file = shift;
my $t = time;
sysopen (my $f, $file, O_RDWR|O_CREAT)
or die "Can't touch $file: $!";
close $f;
utime $t, $t, $file;
}
# like mkdir -p
# Wrapper of File::Path:make_tree
sub mkdir_p {
my $path = shift;
my $err;
make_path($path, { error => \$err } );
return @$err;
}
# like rm -rf
# returns an array of errors if any (see File::Path)
sub rm_rf {
my $path = shift;
my $err;
remove_tree($path, { error => \$err } );
return @$err;
}
sub show_error {
for my $diag (@_) {
my ($f, $msg) = %$diag;
warn "$f: $msg\n";
}
}
1;
|