| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Plugin::File | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Plugin for encapsulating information about a system file. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Originally written by Michael Stevens <michael@etla.org> as the | ||
| 10 | # Directory plugin, then mutilated by Andy Wardley <abw@kfs.org> | ||
| 11 | # into separate File and Directory plugins, with some additional | ||
| 12 | # code for working with views, etc. | ||
| 13 | # | ||
| 14 | # COPYRIGHT | ||
| 15 | # Copyright 2000-2022 Michael Stevens, Andy Wardley. | ||
| 16 | # | ||
| 17 | # This module is free software; you can redistribute it and/or | ||
| 18 | # modify it under the same terms as Perl itself. | ||
| 19 | # | ||
| 20 | #============================================================================ | ||
| 21 | |||
| 22 | package Template::Plugin::File; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Cwd; | ||
| 27 | use File::Spec; | ||
| 28 | use File::Basename; | ||
| 29 | use base 'Template::Plugin'; | ||
| 30 | |||
| 31 | our $VERSION = '3.100'; | ||
| 32 | |||
| 33 | our @STAT_KEYS = qw( | ||
| 34 | dev ino mode nlink uid gid rdev size | ||
| 35 | atime mtime ctime blksize blocks | ||
| 36 | ); | ||
| 37 | |||
| 38 | |||
| 39 | #------------------------------------------------------------------------ | ||
| 40 | # new($context, $file, \%config) | ||
| 41 | # | ||
| 42 | # Create a new File object. Takes the pathname of the file as | ||
| 43 | # the argument following the context and an optional | ||
| 44 | # hash reference of configuration parameters. | ||
| 45 | #------------------------------------------------------------------------ | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; | ||
| 49 | my ($class, $context, $path) = @_; | ||
| 50 | my ($root, $home, @stat, $abs); | ||
| 51 | |||
| 52 | return $class->throw('no file specified') | ||
| 53 | unless defined $path and length $path; | ||
| 54 | |||
| 55 | # path, dir, name, root, home | ||
| 56 | |||
| 57 | if (File::Spec->file_name_is_absolute($path)) { | ||
| 58 | $root = ''; | ||
| 59 | } | ||
| 60 | elsif (($root = $config->{ root })) { | ||
| 61 | # strip any trailing '/' from root | ||
| 62 | $root =~ s[/$][]; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | $root = ''; | ||
| 66 | } | ||
| 67 | |||
| 68 | my ($name, $dir, $ext) = fileparse($path, '\.\w+'); | ||
| 69 | # fixup various items | ||
| 70 | $dir =~ s[/$][]; | ||
| 71 | $dir = '' if $dir eq '.'; | ||
| 72 | $name = $name . $ext; | ||
| 73 | $ext =~ s/^\.//g; | ||
| 74 | |||
| 75 | my @fields = File::Spec->splitdir($dir); | ||
| 76 | shift @fields if @fields && ! length $fields[0]; | ||
| 77 | $home = join('/', ('..') x @fields); | ||
| 78 | $abs = File::Spec->catfile($root ? $root : (), $path); | ||
| 79 | |||
| 80 | my $self = { | ||
| 81 | path => $path, | ||
| 82 | name => $name, | ||
| 83 | root => $root, | ||
| 84 | home => $home, | ||
| 85 | dir => $dir, | ||
| 86 | ext => $ext, | ||
| 87 | abs => $abs, | ||
| 88 | user => '', | ||
| 89 | group => '', | ||
| 90 | isdir => '', | ||
| 91 | stat => defined $config->{ stat } | ||
| 92 | ? $config->{ stat } | ||
| 93 | : ! $config->{ nostat }, | ||
| 94 | map { ($_ => '') } @STAT_KEYS, | ||
| 95 | }; | ||
| 96 | |||
| 97 | if ($self->{ stat }) { | ||
| 98 | (@stat = stat( $abs )) | ||
| 99 | || return $class->throw("$abs: $!"); | ||
| 100 | |||
| 101 | @$self{ @STAT_KEYS } = @stat; | ||
| 102 | |||
| 103 | unless ($config->{ noid }) { | ||
| 104 | $self->{ user } = eval { getpwuid( $self->{ uid }) || $self->{ uid } }; | ||
| 105 | $self->{ group } = eval { getgrgid( $self->{ gid }) || $self->{ gid } }; | ||
| 106 | } | ||
| 107 | $self->{ isdir } = -d $abs; | ||
| 108 | } | ||
| 109 | |||
| 110 | bless $self, $class; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | #------------------------------------------------------------------------- | ||
| 115 | # rel($file) | ||
| 116 | # | ||
| 117 | # Generate a relative filename for some other file relative to this one. | ||
| 118 | #------------------------------------------------------------------------ | ||
| 119 | |||
| 120 | sub rel { | ||
| 121 | my ($self, $path) = @_; | ||
| 122 | $path = $path->{ path } if ref $path eq ref $self; # assumes same root | ||
| 123 | return $path if $path =~ m[^/]; | ||
| 124 | return $path unless $self->{ home }; | ||
| 125 | return $self->{ home } . '/' . $path; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | #------------------------------------------------------------------------ | ||
| 130 | # present($view) | ||
| 131 | # | ||
| 132 | # Present self to a Template::View. | ||
| 133 | #------------------------------------------------------------------------ | ||
| 134 | |||
| 135 | sub present { | ||
| 136 | my ($self, $view) = @_; | ||
| 137 | $view->view_file($self); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub throw { | ||
| 142 | my ($self, $error) = @_; | ||
| 143 | die (Template::Exception->new('File', $error)); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ |