Line 0
Link Here
|
|
|
1 |
package Koha::File; |
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Fcntl; |
6 |
|
7 |
=head1 SYNOPSIS |
8 |
|
9 |
This package providers helper methods to work with files |
10 |
Maybe even be a wrapper in Koha for working with files some day? |
11 |
|
12 |
=cut |
13 |
|
14 |
our @filetypes; #Save the translation table for filetypes for global access |
15 |
$filetypes[Fcntl::S_IFDIR] = "d"; |
16 |
$filetypes[Fcntl::S_IFCHR] = "c"; |
17 |
$filetypes[Fcntl::S_IFBLK] = "b"; |
18 |
$filetypes[Fcntl::S_IFREG] = "-"; |
19 |
$filetypes[Fcntl::S_IFIFO] = "p"; |
20 |
$filetypes[Fcntl::S_IFLNK] = "l"; |
21 |
$filetypes[Fcntl::S_IFSOCK] = "s"; |
22 |
|
23 |
=head2 getDiagnostics |
24 |
|
25 |
@PARAM1 String, path to a file |
26 |
@RETURNS HASHRef, file properties in clear text. |
27 |
undef, if no such file found |
28 |
@THROWS nothing, do not throw from here, especially not File-exceptions, |
29 |
because this subroutine is used in File-exception diagnostics and could cause a circular recursion when no file is found. |
30 |
|
31 |
=cut |
32 |
|
33 |
sub getDiagnostics { |
34 |
my ($path) = @_; |
35 |
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($path); |
36 |
return undef unless $dev; #No such file |
37 |
|
38 |
my %rv; |
39 |
$rv{user} = getpwuid($uid); |
40 |
$rv{group} = getgrgid($gid); |
41 |
$rv{permissions} = sprintf "%04o", Fcntl::S_IMODE($mode); |
42 |
$rv{filetype} = $filetypes[ Fcntl::S_IFMT($mode) ]; |
43 |
|
44 |
return \%rv; |
45 |
} |
46 |
|
47 |
our $diagnosticsStringFormat = '%s %s %s:%s'; |
48 |
sub getDiagnosticsString { |
49 |
my $stat = getDiagnostics(@_); |
50 |
return sprintf($diagnosticsStringFormat, $stat->{filetype}, $stat->{permissions}, $stat->{user}, $stat->{group}); |
51 |
} |
52 |
|
53 |
1; |