| 1 | ####################################################################### | ||
| 2 | # $URL: svn+ssh://equilibrious@equilibrious.net/home/equilibrious/svnrepos/chrisdolan/Fuse-PDF/lib/Fuse/PDF/FS.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::FS; | ||
| 9 | |||
| 10 | use warnings; | ||
| 11 | use strict; | ||
| 12 | use 5.008; | ||
| 13 | |||
| 14 | use Carp qw(carp); | ||
| 15 | use Readonly; | ||
| 16 | use POSIX qw(:errno_h); | ||
| 17 | use Fcntl qw(:mode); | ||
| 18 | use English qw(-no_match_vars); | ||
| 19 | use CAM::PDF; | ||
| 20 | use CAM::PDF::Node; | ||
| 21 | use Fuse::PDF::ErrnoHacks; | ||
| 22 | |||
| 23 | our $VERSION = '0.09'; | ||
| 24 | |||
| 25 | # integer, increases when we break file format backward compatibility | ||
| 26 | Readonly::Scalar my $COMPATIBILITY_VERSION => 2; | ||
| 27 | |||
| 28 | # file format compatibility history: | ||
| 29 | # 1 = Fuse::PDF v0.01 | ||
| 30 | # 2 = Fuse::PDF v0.02-present | ||
| 31 | # add another layer to avoid adding filesystems right in the PDF root dict | ||
| 32 | # add fs timestamp | ||
| 33 | |||
| 34 | Readonly::Scalar my $PATHLEN => 255; | ||
| 35 | Readonly::Scalar my $BLOCKSIZE => 4096; | ||
| 36 | Readonly::Scalar my $ELOOP_LIMIT => 100; | ||
| 37 | Readonly::Scalar my $ROOT_FS_PERMS => oct 777; | ||
| 38 | Readonly::Scalar my $DEFAULT_SYMLINK_PERMS => oct 777; | ||
| 39 | |||
| 40 | Readonly::Scalar my $FREE_FILES => 1_000_000; | ||
| 41 | Readonly::Scalar my $MAX_BLOCKS => 1_000_000; | ||
| 42 | Readonly::Scalar my $FREE_BLOCKS => 500_000; | ||
| 43 | |||
| 44 | Readonly::Scalar my $FS_ROOT_KEY => 'FusePDF'; | ||
| 45 | |||
| 46 | # -------------------------------------------------- | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my ($pkg, $options) = @_; | ||
| 50 | return if ! $options; | ||
| 51 | return if ! $options->{pdf}; | ||
| 52 | |||
| 53 | my $self = bless { | ||
| 54 | # Order matters! | ||
| 55 | backup => 0, | ||
| 56 | compact => 1, | ||
| 57 | autosave_filename => undef, | ||
| 58 | fs_name => undef, | ||
| 59 | %{$options}, | ||
| 60 | dirty => 0, | ||
| 61 | backedup => {}, | ||
| 62 | }, $pkg; | ||
| 63 | |||
| 64 | if (!defined $self->{fs_name}) { | ||
| 65 | $self->{fs_name} = 'FusePDF_FS'; | ||
| 66 | } | ||
| 67 | |||
| 68 | # lookup/create fs object in PDF | ||
| 69 | my $root = $self->{pdf}->getRootDict(); | ||
| 70 | my ($o, $g) = ($root->{objnum}, $root->{gennum}); | ||
| 71 | |||
| 72 | $root->{$FS_ROOT_KEY} ||= CAM::PDF::Node->new('dictionary', {}, $o, $g); | ||
| 73 | my $fs_holder = $root->{$FS_ROOT_KEY}->{value}; | ||
| 74 | if ($fs_holder->{$self->{fs_name}}) { | ||
| 75 | $self->{fs} = $self->{pdf}->getValue($fs_holder->{$self->{fs_name}}); | ||
| 76 | } else { | ||
| 77 | my $fs = CAM::PDF::Node->new('object', CAM::PDF::Node->new('dictionary', { | ||
| 78 | nfiles => CAM::PDF::Node->new('number', 1), | ||
| 79 | maxinode => CAM::PDF::Node->new('number', 0), | ||
| 80 | root => $self->_newdir($root, $ROOT_FS_PERMS), | ||
| 81 | mtime => CAM::PDF::Node->new('number', time), | ||
| 82 | })); | ||
| 83 | # Don't bother marking the FS dirty unless we actually put something in it | ||
| 84 | my $objnum = $self->{pdf}->appendObject(undef, $fs, 0); # 0 means no-follow, so the newdir MUST be a single object | ||
| 85 | $fs_holder->{$self->{fs_name}} = CAM::PDF::Node->new('reference', $objnum, $o, $g); | ||
| 86 | $self->{fs} = $fs->{value}->{value}; | ||
| 87 | } | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub DESTROY { | ||
| 93 | my ($self) = @_; | ||
| 94 | if (defined $self->{autosave_filename}) { | ||
| 95 | $self->save($self->{autosave_filename}); | ||
| 96 | } | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub deletefs { | ||
| 101 | my ($self, $filename) = @_; | ||
| 102 | my $root = $self->{pdf}->getRootDict(); | ||
| 103 | if ($root->{$FS_ROOT_KEY}) { | ||
| 104 | delete $root->{$FS_ROOT_KEY}->{value}->{$self->{fs_name}}; | ||
| 105 | $self->{pdf}->cleanse(); | ||
| 106 | $self->{pdf}->cleanoutput($filename); | ||
| 107 | } | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub compact { ## no critic(ArgUnpacking) | ||
| 112 | my ($self, $boolean) = @_; | ||
| 113 | return $self->{compact} if @_ == 1; | ||
| 114 | $self->{compact} = $boolean ? 1 : undef; | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub backup { ## no critic(ArgUnpacking) | ||
| 119 | my ($self, $boolean) = @_; | ||
| 120 | return $self->{backup} if @_ == 1; | ||
| 121 | $self->{backup} = $boolean ? 1 : undef; | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub autosave_filename { ## no critic(ArgUnpacking) | ||
| 126 | my ($self, $filename) = @_; | ||
| 127 | return $self->{autosave_filename} if @_ == 1; | ||
| 128 | $self->{autosave_filename} = $filename; | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | # subclasses may wish to override this | ||
| 133 | sub _software_name { | ||
| 134 | my ($self) = @_; | ||
| 135 | return ref $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub save { | ||
| 139 | my ($self, $filename) = @_; | ||
| 140 | if ($self->{dirty}) { | ||
| 141 | $self->{pdf}->{changes}->{$self->{fs}->{root}->{objnum}} = 1; | ||
| 142 | # TODO: atomically? | ||
| 143 | $self->{fs}->{creator} = CAM::PDF::Node->new('string', $self->_software_name); | ||
| 144 | $self->{fs}->{version} = CAM::PDF::Node->new('string', $self->VERSION); | ||
| 145 | $self->{fs}->{compatibility} = CAM::PDF::Node->new('number', $COMPATIBILITY_VERSION); | ||
| 146 | if ($self->{compact}) { | ||
| 147 | $self->{pdf}->cleanse(); | ||
| 148 | $self->{pdf}->clean(); | ||
| 149 | } | ||
| 150 | if ($self->{backup} && -e $filename && !$self->{backedup}->{$filename}) { | ||
| 151 | my $backup_filename = $filename . '.bak'; | ||
| 152 | unlink $backup_filename; # ignore failure | ||
| 153 | rename $filename, $backup_filename or carp 'Failed to make a backup of the filesystem: ' . $OS_ERROR; | ||
| 154 | $self->{backedup}->{$filename} = 1; | ||
| 155 | } | ||
| 156 | $self->{pdf}->output($filename); | ||
| 157 | $self->{dirty} = 0; | ||
| 158 | } | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub previous_revision { | ||
| 163 | my ($self) = @_; | ||
| 164 | |||
| 165 | my $prev_pdf = $self->{pdf}->previousRevision(); | ||
| 166 | return if !$prev_pdf; | ||
| 167 | |||
| 168 | return __PACKAGE__->new({ | ||
| 169 | pdf => $prev_pdf, | ||
| 170 | fs_name => $self->{fs_name}, | ||
| 171 | }); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub all_revisions { | ||
| 175 | my ($self) = @_; | ||
| 176 | my @revs; | ||
| 177 | for (my $fs = $self; $fs; $fs = $fs->previous_revision) { ## no critic(ProhibitCStyleForLoops) | ||
| 178 | push @revs, $fs; | ||
| 179 | } | ||
| 180 | return @revs; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub statistics { | ||
| 184 | my ($self) = @_; | ||
| 185 | my %stats; | ||
| 186 | $stats{name} = $self->{fs_name}; | ||
| 187 | $stats{nfiles} = $self->{fs}->{nfiles}->{value}; | ||
| 188 | $stats{mtime} = $self->{fs}->{mtime}->{value}; | ||
| 189 | return \%stats; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub to_string { | ||
| 193 | my ($self) = @_; | ||
| 194 | my @stats = ($self->statistics); | ||
| 195 | my $fs = $self; | ||
| 196 | while ($fs = $fs->previous_revision) { | ||
| 197 | push @stats, $fs->statistics; | ||
| 198 | } | ||
| 199 | my @rows = ( | ||
| 200 | 'Name: ' . $stats[0]->{name}, | ||
| 201 | ); | ||
| 202 | for my $i (0 .. $#stats) { | ||
| 203 | my $s = $stats[$i]; | ||
| 204 | push @rows, 'Revision: ' . (@stats - $i); | ||
| 205 | push @rows, ' Modified: ' . localtime($s->{mtime}) . " ($s->{mtime})"; | ||
| 206 | push @rows, ' Files: ' . $s->{nfiles}; | ||
| 207 | } | ||
| 208 | |||
| 209 | return join "\n", @rows, q{}; | ||
| 210 | } | ||
| 211 | |||
| 212 | # -------------------------------------------------- | ||
| 213 | |||
| 214 | sub fs_getattr { | ||
| 215 | my ($self, $abspath) = @_; | ||
| 216 | my $f = $self->_file($abspath); | ||
| 217 | return -$f if !ref $f; | ||
| 218 | my $type = $f->{type}->{value}; | ||
| 219 | my $size = 'd' eq $type ? 0 : length $f->{content}->{value}; | ||
| 220 | my $blocks = 0 == $size ? 0 : (($size - 1) % $BLOCKSIZE) + 1; # round up | ||
| 221 | return | ||
| 222 | 0, # dev | ||
| 223 | $f->{inode}->{value}, | ||
| 224 | $f->{mode}->{value}, | ||
| 225 | $f->{nlink}->{value}, | ||
| 226 | $EFFECTIVE_USER_ID, # uid | ||
| 227 | 0+$EFFECTIVE_GROUP_ID, # gid | ||
| 228 | 0, #rdev | ||
| 229 | $size, | ||
| 230 | $f->{mtime}->{value}, # atime not preserved | ||
| 231 | $f->{mtime}->{value}, | ||
| 232 | $f->{ctime}->{value}, | ||
| 233 | $BLOCKSIZE, | ||
| 234 | $blocks; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub fs_readlink { | ||
| 238 | my ($self, $abspath) = @_; | ||
| 239 | my $f = $self->_file($abspath); | ||
| 240 | return -$f if !ref $f; | ||
| 241 | my $type = $f->{type}->{value}; | ||
| 242 | return -EINVAL() if 'l' ne $type; | ||
| 243 | return $f->{content}->{value}; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub fs_getdir { | ||
| 247 | my ($self, $abspath) = @_; | ||
| 248 | my $f = $self->_file($abspath); | ||
| 249 | return -$f if !ref $f; | ||
| 250 | return q{.}, q{..}, (keys %{$f->{content}->{value}}), 0; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub fs_mknod { | ||
| 254 | my ($self, $abspath, $perms, $dev) = @_; | ||
| 255 | my ($p, $name) = $self->_parent($abspath); | ||
| 256 | return -$p if !ref $p; | ||
| 257 | return -EEXIST() if q{.} eq $name; | ||
| 258 | return -EEXIST() if q{..} eq $name; | ||
| 259 | my $f = $p->{content}->{value}->{$name}; | ||
| 260 | return -EEXIST() if $f; | ||
| 261 | |||
| 262 | # don't support special files | ||
| 263 | my $is_special = !S_ISREG($perms) && !S_ISDIR($perms) && !S_ISLNK($perms); | ||
| 264 | return -EIO() if $is_special; | ||
| 265 | |||
| 266 | my $newfile = $self->_newfile($p, $perms); | ||
| 267 | my $mtime = $newfile->{value}->{mtime}->{value}; | ||
| 268 | $newfile->{value}->{inode}->{value} = ++$self->{fs}->{maxinode}->{value}; | ||
| 269 | $p->{content}->{value}->{$name} = $newfile; | ||
| 270 | #$p->{nlink}->{value}++; | ||
| 271 | $p->{mtime}->{value} = $mtime; | ||
| 272 | $self->{fs}->{nfiles}->{value}++; | ||
| 273 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 274 | $self->{dirty} = 1; | ||
| 275 | return 0; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub fs_mkdir { | ||
| 279 | my ($self, $abspath, $perm) = @_; | ||
| 280 | my ($p, $name) = $self->_parent($abspath); | ||
| 281 | return -$p if !ref $p; | ||
| 282 | return -EEXIST() if q{.} eq $name; | ||
| 283 | return -EEXIST() if q{..} eq $name; | ||
| 284 | my $f = $p->{content}->{value}->{$name}; | ||
| 285 | return -EEXIST() if $f; | ||
| 286 | my $newdir = $self->_newdir($p, $perm); | ||
| 287 | my $mtime = $newdir->{value}->{mtime}->{value}; | ||
| 288 | $newdir->{value}->{inode}->{value} = ++$self->{fs}->{maxinode}->{value}; | ||
| 289 | $p->{content}->{value}->{$name} = $newdir; | ||
| 290 | $p->{nlink}->{value}++; | ||
| 291 | $p->{mtime}->{value} = $mtime; | ||
| 292 | $self->{fs}->{nfiles}->{value}++; | ||
| 293 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 294 | $self->{dirty} = 1; | ||
| 295 | return 0; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub fs_unlink { | ||
| 299 | my ($self, $abspath) = @_; | ||
| 300 | my ($p, $name) = $self->_parent($abspath); | ||
| 301 | return -$p if !ref $p; | ||
| 302 | #use Data::Dumper; print STDERR "$name vs. ".Dumper($p); | ||
| 303 | my $f = $p->{content}->{value}->{$name}; | ||
| 304 | return -ENOENT() if !ref $f; | ||
| 305 | $f = $f->{value}; | ||
| 306 | my $type = $f->{type}->{value}; | ||
| 307 | return -ENOENT() if 'd' eq $type; # TODO: is this the right errno?? | ||
| 308 | |||
| 309 | # TODO: worry about open files? | ||
| 310 | |||
| 311 | delete $p->{content}->{value}->{$name}; | ||
| 312 | #$p->{nlink}->{value}--; | ||
| 313 | my $mtime = time; | ||
| 314 | $p->{mtime}->{value} = $mtime; | ||
| 315 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 316 | $self->{dirty} = 1; | ||
| 317 | return 0; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub fs_rmdir { | ||
| 321 | my ($self, $abspath) = @_; | ||
| 322 | my ($p, $name) = $self->_parent($abspath); | ||
| 323 | return -$p if !ref $p; | ||
| 324 | my $f = $p->{content}->{value}->{$name}; | ||
| 325 | return -ENOENT() if !ref $f; | ||
| 326 | $f = $f->{value}; | ||
| 327 | my $type = $f->{type}->{value}; | ||
| 328 | return -ENOTDIR() if 'd' ne $type; | ||
| 329 | return -ENOTEMPTY() if 0 != scalar keys %{ $f->{content}->{value} }; | ||
| 330 | delete $p->{content}->{value}->{$name}; | ||
| 331 | $p->{nlink}->{value}--; | ||
| 332 | my $mtime = time; | ||
| 333 | $p->{mtime}->{value} = $mtime; | ||
| 334 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 335 | $self->{dirty} = 1; | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub fs_symlink { | ||
| 340 | my ($self, $link, $abspath) = @_; | ||
| 341 | my ($p, $name) = $self->_parent($abspath); | ||
| 342 | return -$p if !ref $p; | ||
| 343 | return -EEXIST() if q{.} eq $name; | ||
| 344 | return -EEXIST() if q{..} eq $name; | ||
| 345 | my $f = $p->{content}->{value}->{$name}; | ||
| 346 | return -EEXIST() if $f; | ||
| 347 | $p->{content}->{value}->{$name} = $self->_newsymlink($p, $link); | ||
| 348 | #$p->{nlink}->{value}++; | ||
| 349 | my $mtime = time; | ||
| 350 | $p->{mtime}->{value} = $mtime; | ||
| 351 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 352 | $self->{fs}->{nfiles}->{value}++; | ||
| 353 | $self->{dirty} = 1; | ||
| 354 | return 0; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub fs_rename { | ||
| 358 | my ($self, $srcpath, $destpath) = @_; | ||
| 359 | my ($errno, $srcdirs, $srcpaths) = $self->_readpath($srcpath); | ||
| 360 | return -$errno if $errno; | ||
| 361 | |||
| 362 | my ($desterrno, $destdirs, $destpaths) = $self->_readpath($destpath, 1); | ||
| 363 | return -$desterrno if $desterrno; | ||
| 364 | |||
| 365 | my $src = $srcdirs->[-1]; | ||
| 366 | my $dest = $destdirs->[-1]; | ||
| 367 | |||
| 368 | my $root = $self->{fs}->{root}->{value}; | ||
| 369 | return -EACCESS if $root == $src; | ||
| 370 | |||
| 371 | if ($dest) { | ||
| 372 | return 0 if $dest == $src; # rename to self always works | ||
| 373 | return -EACCESS if $root == $dest; | ||
| 374 | my $srctype = $src->{type}->{value}; | ||
| 375 | my $desttype = $dest->{type}->{value}; | ||
| 376 | return -ENOTDIR() if 'd' eq $srctype && 'd' ne $desttype; | ||
| 377 | return -EISDIR() if 'd' ne $srctype && 'd' eq $desttype; | ||
| 378 | if ('d' eq $desttype && 0 != scalar keys %{$dest->{content}->{value}}) { | ||
| 379 | return -ENOTEMPTY(); | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | # Ensure dest is not inside src | ||
| 384 | if (@{$srcpaths} < @{$destpaths}) { | ||
| 385 | my $match = 1; | ||
| 386 | for my $i (0 .. $#{$srcpaths}) { | ||
| 387 | if ($srcpaths->[$i] ne $destpaths->[$i]) { | ||
| 388 | $match = 0; | ||
| 389 | last; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | return -EINVAL() if $match; | ||
| 393 | } | ||
| 394 | |||
| 395 | my $srcparent = $srcdirs->[-2]; ## no critic(MagicNumber) | ||
| 396 | my $destparent = $destdirs->[-2]; ## no critic(MagicNumber) | ||
| 397 | my $srcname = $srcpaths->[-1]; | ||
| 398 | my $destname = $destpaths->[-1]; | ||
| 399 | |||
| 400 | # supposed to set dest before removing src to avoid data loss, but meh... | ||
| 401 | $destparent->{content}->{value}->{$destname} = delete $srcparent->{content}->{value}->{$srcname}; | ||
| 402 | |||
| 403 | my $mtime = time; | ||
| 404 | $srcparent->{mtime}->{value} = $mtime; | ||
| 405 | $destparent->{mtime}->{value} = $mtime; # harmless if $srcparent == $destparent | ||
| 406 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 407 | $self->{dirty} = 1; | ||
| 408 | return 0; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub fs_link { | ||
| 412 | return -EIO(); | ||
| 413 | } | ||
| 414 | |||
| 415 | sub fs_chmod { | ||
| 416 | my ($self, $abspath, $perms) = @_; | ||
| 417 | my $f = $self->_file($abspath); | ||
| 418 | return -$f if !ref $f; | ||
| 419 | $f->{mode}->{value} = S_IFMT($f->{mode}->{value}) | S_IMODE($perms); | ||
| 420 | $self->{fs}->{mtime}->{value} = time; | ||
| 421 | $self->{dirty} = 1; | ||
| 422 | return 0; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub fs_chown { | ||
| 426 | my ($self, $abspath, $uid, $gid) = @_; | ||
| 427 | my $f = $self->_file($abspath); | ||
| 428 | return -$f if !ref $f; | ||
| 429 | #$f->{uid}->{value} = $uid; | ||
| 430 | #$f->{gid}->{value} = $gid; | ||
| 431 | #$self->{dirty} = 1; | ||
| 432 | return 0; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub fs_truncate { | ||
| 436 | my ($self, $abspath, $length) = @_; | ||
| 437 | my $f = $self->_file($abspath); | ||
| 438 | return -$f if !ref $f; | ||
| 439 | my $type = $f->{type}->{value}; | ||
| 440 | return -EISDIR() if 'd' eq $type; | ||
| 441 | if ($length <= 0) { | ||
| 442 | $f->{content}->{value} = q{}; | ||
| 443 | } else { | ||
| 444 | my $l = length $f->{content}->{value}; | ||
| 445 | if ($length < $l) { | ||
| 446 | $f->{content}->{value} = substr $f->{content}->{value}, 0, $length; | ||
| 447 | } elsif ($length > $l) { | ||
| 448 | $f->{content}->{value} .= "\0" x ($length - $l); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | my $mtime = time; | ||
| 452 | $f->{mtime}->{value} = $mtime; | ||
| 453 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 454 | $self->{dirty} = 1; | ||
| 455 | return 0; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub fs_utime { | ||
| 459 | my ($self, $abspath, $atime, $mtime) = @_; | ||
| 460 | my $f = $self->_file($abspath); | ||
| 461 | return -$f if !ref $f; | ||
| 462 | |||
| 463 | # Ignore atime | ||
| 464 | |||
| 465 | # Set utime, if changed | ||
| 466 | if ($f->{mtime}->{value} != $mtime) { | ||
| 467 | $f->{mtime}->{value} = $mtime; | ||
| 468 | $self->{fs}->{mtime}->{value} = time; | ||
| 469 | $self->{dirty} = 1; | ||
| 470 | } | ||
| 471 | return 0; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub fs_open { | ||
| 475 | my ($self, $abspath, $flags) = @_; | ||
| 476 | my $f = $self->_file($abspath); | ||
| 477 | return -$f if !ref $f; | ||
| 478 | # check flags? | ||
| 479 | return 0; | ||
| 480 | } | ||
| 481 | |||
| 482 | sub fs_read { | ||
| 483 | my ($self, $abspath, $size, $offset) = @_; | ||
| 484 | my $f = $self->_file($abspath); | ||
| 485 | return -$f if !ref $f; | ||
| 486 | return substr $f->{content}->{value}, $offset, $size; | ||
| 487 | } | ||
| 488 | |||
| 489 | sub fs_write { | ||
| 490 | my ($self, $abspath, $str, $offset) = @_; | ||
| 491 | my $f = $self->_file($abspath); | ||
| 492 | return -$f if !ref $f; | ||
| 493 | my $size = length $str; | ||
| 494 | substr($f->{content}->{value}, $offset, $size) = $str; ##no critic(ProhibitLvalueSubstr) | ||
| 495 | my $mtime = time; | ||
| 496 | $f->{mtime}->{value} = $mtime; | ||
| 497 | $self->{fs}->{mtime}->{value} = $mtime; | ||
| 498 | $self->{dirty} = 1; | ||
| 499 | return $size; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub fs_statfs { | ||
| 503 | my ($self) = @_; | ||
| 504 | return $PATHLEN, $self->{fs}->{nfiles}->{value}, $FREE_FILES, $MAX_BLOCKS, $FREE_BLOCKS, $BLOCKSIZE; | ||
| 505 | } | ||
| 506 | |||
| 507 | sub fs_flush { | ||
| 508 | my ($self, $abspath) = @_; | ||
| 509 | # TODO | ||
| 510 | return 0; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub fs_release { | ||
| 514 | my ($self, $abspath, $flags) = @_; | ||
| 515 | # TODO | ||
| 516 | return 0; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub fs_fsync { | ||
| 520 | my ($self, $abspath, $flags) = @_; | ||
| 521 | # TODO | ||
| 522 | return 0; | ||
| 523 | } | ||
| 524 | |||
| 525 | sub fs_setxattr { | ||
| 526 | my ($self, $abspath, $key, $value, $flags) = @_; | ||
| 527 | if (!$flags->{create} && !$flags->{replace}) { | ||
| 528 | return -EIO(); | ||
| 529 | } | ||
| 530 | my $f = $self->_file($abspath); | ||
| 531 | return -$f if !ref $f; | ||
| 532 | my ($o, $g) = ($f->{type}->{objnum}, $f->{type}->{gennum}); | ||
| 533 | my $xattr = $f->{xattr}; | ||
| 534 | if (!$xattr) { | ||
| 535 | $xattr = $f->{xattr} = CAM::PDF::Node->new('dictionary', {}, $o, $g); | ||
| 536 | } | ||
| 537 | if ($flags->{create}) { | ||
| 538 | return -EEXIST() if exists $xattr->{value}->{$key}; | ||
| 539 | } elsif ($flags->{replace}) { | ||
| 540 | return -ENOATTR() if !exists $xattr->{value}->{$key}; | ||
| 541 | } | ||
| 542 | $xattr->{value}->{$key} = CAM::PDF::Node->new('string', $value, $o, $g); | ||
| 543 | $self->{fs}->{mtime}->{value} = time; | ||
| 544 | $self->{dirty} = 1; | ||
| 545 | return 0; | ||
| 546 | } | ||
| 547 | |||
| 548 | sub fs_getxattr { | ||
| 549 | my ($self, $abspath, $key) = @_; | ||
| 550 | my $f = $self->_file($abspath); | ||
| 551 | return -$f if !ref $f; | ||
| 552 | my $xattr = $f->{xattr}; | ||
| 553 | return 0 if !$xattr; | ||
| 554 | return 0 if !exists $xattr->{value}->{$key}; | ||
| 555 | return $xattr->{value}->{$key}->{value}; | ||
| 556 | } | ||
| 557 | |||
| 558 | sub fs_listxattr { | ||
| 559 | my ($self, $abspath, $key) = @_; | ||
| 560 | my $f = $self->_file($abspath); | ||
| 561 | return -$f if !ref $f; | ||
| 562 | my $xattr = $f->{xattr}; | ||
| 563 | return ($xattr ? keys %{ $xattr->{value} } : ()), 0; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub fs_removexattr { | ||
| 567 | my ($self, $abspath, $key) = @_; | ||
| 568 | my $f = $self->_file($abspath); | ||
| 569 | return -$f if !ref $f; | ||
| 570 | my $xattr = $f->{xattr}; | ||
| 571 | return -ENOATTR() if !$xattr; | ||
| 572 | return -ENOATTR() if !exists $xattr->{value}->{$key}; | ||
| 573 | delete $xattr->{value}->{$key}; | ||
| 574 | $self->{fs}->{mtime}->{value} = time; | ||
| 575 | $self->{dirty} = 1; | ||
| 576 | return 0; | ||
| 577 | } | ||
| 578 | |||
| 579 | # -------------------------------------------------- | ||
| 580 | |||
| 581 | sub _parent { | ||
| 582 | my ($self, $path) = @_; | ||
| 583 | my ($errno, $dirs, $paths) = $self->_readpath($path, 1); | ||
| 584 | return $errno if $errno; | ||
| 585 | return $dirs->[-2], $paths->[-1], $dirs->[-1]; ## no critic(MagicNumber) | ||
| 586 | } | ||
| 587 | |||
| 588 | sub _file { | ||
| 589 | my ($self, $path) = @_; | ||
| 590 | my ($errno, $dirs, $paths) = $self->_readpath($path); | ||
| 591 | return $errno if $errno; | ||
| 592 | return $dirs->[-1]; | ||
| 593 | } | ||
| 594 | |||
| 595 | |||
| 596 | sub _readpath { | ||
| 597 | my ($self, $path, $parent, $nsymlinks) = @_; | ||
| 598 | |||
| 599 | $nsymlinks ||= 0; | ||
| 600 | |||
| 601 | my @dirs = ($self->{fs}->{root}->{value}); | ||
| 602 | my @path = split m{/}xms, $path; | ||
| 603 | my @realpath; | ||
| 604 | |||
| 605 | for (my $i = 0; $i < @path; ++$i) { ##no critic(ProhibitCStyleForLoops) | ||
| 606 | my $entry = $path[$i]; | ||
| 607 | next if q{} eq $entry; | ||
| 608 | |||
| 609 | my $type = $dirs[-1]->{type}->{value}; | ||
| 610 | return ENOTDIR() if 'd' ne $type; | ||
| 611 | next if q{.} eq $entry; | ||
| 612 | if (q{..} eq $entry) { | ||
| 613 | pop @dirs; | ||
| 614 | pop @realpath; | ||
| 615 | return EACCESS() if !@dirs; # tried to get parent of root | ||
| 616 | next; | ||
| 617 | } | ||
| 618 | push @realpath, $entry; | ||
| 619 | |||
| 620 | my $next = $dirs[-1]->{content}->{value}->{$entry}; | ||
| 621 | if (!$next) { | ||
| 622 | if ($parent && $i == $#path) { | ||
| 623 | push @dirs, undef; | ||
| 624 | return 0, \@dirs, \@realpath; | ||
| 625 | } | ||
| 626 | return ENOENT(); | ||
| 627 | } | ||
| 628 | my $f = $next->{value}; | ||
| 629 | if ('l' eq $f->{type}->{value}) { | ||
| 630 | if ($i != $#path) { | ||
| 631 | return ELOOP() if ++$nsymlinks >= $ELOOP_LIMIT; | ||
| 632 | my $linkpath = $f->{content}->{value}; | ||
| 633 | |||
| 634 | # cannot leave the filesystem | ||
| 635 | return EACCESS() if $linkpath =~ m{\A /}xms; | ||
| 636 | |||
| 637 | splice @path, $i, 1, split m{/}xms, $linkpath; | ||
| 638 | return $self->_readpath((join q{/}, @path), $parent, $nsymlinks); | ||
| 639 | } | ||
| 640 | } | ||
| 641 | push @dirs, $f; | ||
| 642 | } | ||
| 643 | |||
| 644 | return 0, \@dirs, \@realpath; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub _newfile { | ||
| 648 | my ($self, $parent, $perm) = @_; | ||
| 649 | my ($o, $g) = ($parent->{objnum}, $parent->{gennum}); | ||
| 650 | return $self->_newentry($o, $g, S_IFREG | $perm, | ||
| 651 | 'f', CAM::PDF::Node->new('string', q{}, $o, $g)); | ||
| 652 | } | ||
| 653 | |||
| 654 | sub _newsymlink { | ||
| 655 | my ($self, $parent, $src) = @_; | ||
| 656 | my ($o, $g) = ($parent->{objnum}, $parent->{gennum}); | ||
| 657 | return $self->_newentry($o, $g, S_IFLNK | $DEFAULT_SYMLINK_PERMS, | ||
| 658 | 'l', CAM::PDF::Node->new('string', $src, $o, $g)); | ||
| 659 | } | ||
| 660 | |||
| 661 | sub _newdir { | ||
| 662 | my ($self, $parent, $perm) = @_; | ||
| 663 | # MUST NOT create an new PDF objects | ||
| 664 | my ($o, $g) = ($parent->{objnum}, $parent->{gennum}); | ||
| 665 | my $dir = $self->_newentry($o, $g, S_IFDIR() | $perm, | ||
| 666 | 'd', CAM::PDF::Node->new('dictionary', {}, $o, $g)); | ||
| 667 | $dir->{value}->{nlink}->{value}++; | ||
| 668 | return $dir; | ||
| 669 | } | ||
| 670 | |||
| 671 | sub _newentry { ##no critic(ProhibitManyArgs) | ||
| 672 | my ($self, $o, $g, $perm, $type, $content) = @_; | ||
| 673 | # MUST NOT create an new PDF objects if type = 'd' | ||
| 674 | my $now = time; | ||
| 675 | return CAM::PDF::Node->new('dictionary', { | ||
| 676 | content => $content, | ||
| 677 | type => CAM::PDF::Node->new('string', $type, $o, $g), | ||
| 678 | inode => CAM::PDF::Node->new('number', 0, $o, $g), | ||
| 679 | mode => CAM::PDF::Node->new('number', $perm, $o, $g), | ||
| 680 | nlink => CAM::PDF::Node->new('number', 1, $o, $g), | ||
| 681 | mtime => CAM::PDF::Node->new('number', $now, $o, $g), | ||
| 682 | ctime => CAM::PDF::Node->new('number', $now, $o, $g), | ||
| 683 | }, $o, $g); | ||
| 684 | } | ||
| 685 | |||
| 686 | 1; | ||
| 687 | |||
| 688 | __END__ |