PDF.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PDF/ImageTemplate.pm ↑ Summary
1 #######################################################################
2 # $URL: svn+ssh://equilibrious@equilibrious.net/home/equilibrious/svnrepos/chrisdolan/Fuse-PDF/lib/Fuse/PDF.pm $
3 # $Date: 2008-06-06 22:47:54 -0500 (Fri, 06 Jun 2008) $
4 # $Author: equilibrious $
5 # $Revision: 767 $
6 ########################################################################
7  
8 package Fuse::PDF;
9  
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use Fuse qw(:xattr);
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use CAM::PDF;
18 use Fuse::PDF::ContentFS;
19 use Fuse::PDF::FS;
20  
21 our $VERSION = '0.09';
22  
23 {
24 # Hack fix for Fuse 0.09 which has the wrong constants for Mac. Get the
25 # right values directly from the .h file, if available
26 if (open my $fh, '<', '/usr/include/sys/xattr.h') {
27 my $content = do { local $INPUT_RECORD_SEPARATOR = undef; <$fh> };
28 close $fh or carp 'Failed to close xattr.h';
29 my %consts = $content =~ m/\#define \s+ (XATTR_(?:CREATE|REPLACE)) \s+ (0x\d+|\d+)/gxms; ## no critic(RegularExpressions::ProhibitEscapedMetacharacters) -- Perl::Critic v1.08 bug...
30 for my $name (keys %consts) {
31 my $value = $consts{$name} || next;
32 if ($value =~ m/0x(\d+)/xms) {
33 $value = hex $1;
34 }
35 no warnings 'redefine'; ## no critic(TestingAndDebugging::ProhibitNoWarnings)
36 no strict 'refs'; ## no critic(TestingAndDebugging::ProhibitNoStrict)
37 *{$name} = sub { return $value; };
38 }
39 }
40 }
41  
42  
43 sub new {
44 my ($pkg, $filename, $extra) = @_;
45 croak 'No PDF filename specified' if !defined $filename;
46 croak 'No PDF file found' if !-e $filename;
47 $extra ||= {};
48 my $pdf = CAM::PDF->new($filename, @{ $extra->{pdf_constructor} || [] });
49 return if !$pdf;
50 my $self = bless {
51 save_filename => $filename,
52 compact => 0,
53 fs_name => undef,
54 %{$extra},
55 pdf_mtime => -e $filename ? (stat $filename)[9] : $BASETIME, ## no critic(MagicNumber)
56 pdf => $pdf,
57 }, $pkg;
58 return $self;
59 }
60  
61 sub fs {
62 my ($self) = @_;
63  
64 my $fs_class = $self->{fs_name} && $self->{fs_name} eq 'pdf'
65 ? 'Fuse::PDF::ContentFS' : 'Fuse::PDF::FS';
66  
67 my $fs = $fs_class->new({
68 pdf_mtime => $self->{pdf_mtime},
69 pdf => $self->{pdf},
70 fs_name => $self->{fs_name},
71 }) or croak 'Failed to open the PDF or create a filesystem inside';
72  
73 if ($self->{revision}) {
74 my @revisions = $fs->all_revisions;
75 if ($self->{revision} =~ m/\D/xms && $self->{revision} <= @revisions) {
76 $fs = $revisions[@revisions - $self->{revision}];
77 } else {
78 croak 'Invalid filesystem revision: ' . $self->{revision};
79 }
80 }
81  
82 return $fs;
83 }
84  
85 sub mount { ## no critic(Subroutines::ProhibitExcessComplexity)
86 my ($self, $mountdir, $options) = @_;
87 $options ||= {};
88  
89 my $fs = $self->fs;
90 $fs->autosave_filename($self->{save_filename});
91 $fs->compact($self->{compact});
92 $fs->backup($self->{backup});
93  
94 # perform mount
95 if (!-d $mountdir) {
96 mkdir $mountdir;
97 if (!-d $mountdir) {
98 croak 'Failed to create mount directory: ' . $OS_ERROR;
99 }
100 }
101  
102 Fuse::main(
103 mountpoint => $mountdir,
104  
105 $fs->can('fs_getattr') ? (
106 getattr => sub {
107 my ($file) = @_;
108 return $fs->fs_getattr($file);
109 }
110 ) : (),
111 $fs->can('fs_readlink') ? (
112 readlink => sub {
113 my ($file) = @_;
114 return $fs->fs_readlink($file);
115 }
116 ) : (),
117 $fs->can('fs_getdir') ? (
118 getdir => sub {
119 my ($file) = @_;
120 return $fs->fs_getdir($file);
121 }
122 ) : (),
123 $fs->can('fs_mknod') ? (
124 mknod => sub {
125 my ($file, $modes, $dev) = @_;
126 return $fs->fs_mknod($file, $modes, $dev);
127 }
128 ) : (),
129 $fs->can('fs_mkdir') ? (
130 mkdir => sub {
131 my ($file, $perms) = @_;
132 return $fs->fs_mkdir($file, $perms);
133 }
134 ) : (),
135 $fs->can('fs_unlink') ? (
136 unlink => sub {
137 my ($file) = @_;
138 return $fs->fs_unlink($file);
139 }
140 ) : (),
141 $fs->can('fs_rmdir') ? (
142 rmdir => sub {
143 my ($file) = @_;
144 return $fs->fs_rmdir($file);
145 }
146 ) : (),
147 $fs->can('fs_symlink') ? (
148 symlink => sub {
149 my ($link, $file) = @_;
150 return $fs->fs_symlink($link, $file);
151 }
152 ) : (),
153 $fs->can('fs_rename') ? (
154 rename => sub {
155 my ($oldfile, $file) = @_;
156 return $fs->fs_rename($oldfile, $file);
157 }
158 ) : (),
159 $fs->can('fs_link') ? (
160 link => sub {
161 my ($srcfile, $file) = @_;
162 return $fs->fs_link($srcfile, $file);
163 }
164 ) : (),
165 $fs->can('fs_chmod') ? (
166 chmod => sub {
167 my ($file, $perms) = @_;
168 return $fs->fs_chmod($file, $perms);
169 }
170 ) : (),
171 $fs->can('fs_chown') ? (
172 chown => sub {
173 my ($file, $uid, $gid) = @_;
174 return $fs->fs_chown($file, $uid, $gid);
175 }
176 ) : (),
177 $fs->can('fs_truncate') ? (
178 truncate => sub {
179 my ($file, $length) = @_;
180 return $fs->fs_truncate($file, $length);
181 }
182 ) : (),
183 $fs->can('fs_utime') ? (
184 utime => sub {
185 my ($file, $atime, $utime) = @_;
186 return $fs->fs_utime($file, $atime, $utime);
187 }
188 ) : (),
189 $fs->can('fs_open') ? (
190 open => sub {
191 my ($file, $mode) = @_;
192 return $fs->fs_open($file, $mode);
193 }
194 ) : (),
195 $fs->can('fs_read') ? (
196 read => sub {
197 my ($file, $size, $offset) = @_;
198 return $fs->fs_read($file, $size, $offset);
199 }
200 ) : (),
201 $fs->can('fs_write') ? (
202 write => sub {
203 my ($file, $str, $offset) = @_;
204 return $fs->fs_write($file, $str, $offset);
205 }
206 ) : (),
207 $fs->can('fs_statfs') ? (
208 statfs => sub {
209 return $fs->fs_statfs();
210 }
211 ) : (),
212 $fs->can('fs_flush') ? (
213 flush => sub {
214 my ($file) = @_;
215 return $fs->fs_flush($file);
216 }
217 ) : (),
218 $fs->can('fs_release') ? (
219 release => sub {
220 my ($file, $mode) = @_;
221 return $fs->fs_release($file, $mode);
222 }
223 ) : (),
224 $fs->can('fs_fsync') ? (
225 fsync => sub {
226 my ($file, $flags) = @_;
227 return $fs->fs_fsync($file, $flags);
228 }
229 ) : (),
230 $fs->can('fs_setxattr') ? (
231 setxattr => sub {
232 my ($file, $key, $value, $flags) = @_;
233 my %flags = (
234 create => $flags & XATTR_CREATE(),
235 replace => $flags & XATTR_REPLACE(),
236 );
237 return $fs->fs_setxattr($file, $key, $value, \%flags);
238 }
239 ) : (),
240 $fs->can('fs_getxattr') ? (
241 getxattr => sub {
242 my ($file, $key) = @_;
243 return $fs->fs_getxattr($file, $key);
244 }
245 ) : (),
246 $fs->can('fs_listxattr') ? (
247 listxattr => sub {
248 my ($file, $key) = @_;
249 return $fs->fs_listxattr($file);
250 }
251 ) : (),
252 $fs->can('fs_removexattr') ? (
253 removexattr => sub {
254 my ($file, $key) = @_;
255 return $fs->fs_removexattr($file, $key);
256 }
257 ) : (),
258  
259 threaded => 0,
260 %{$options},
261 );
262 return;
263 }
264  
265 1;
266  
267 __END__
« PDF/ImageTemplate.pm ↑ Summary